Search in sources :

Example 1 with HostResolver

use of software.amazon.awssdk.crt.io.HostResolver in project aws-greengrass-nucleus by aws-greengrass.

the class IPCEventStreamServiceTest method testClientConnection.

@Test
@SuppressWarnings("PMD.CloseResource")
void testClientConnection() throws Exception {
    CountDownLatch connectionLatch = new CountDownLatch(1);
    EventStreamRPCConnection connection = null;
    try (EventLoopGroup elg = new EventLoopGroup(1);
        ClientBootstrap clientBootstrap = new ClientBootstrap(elg, new HostResolver(elg));
        SocketOptions socketOptions = TestUtils.getSocketOptionsForIPC()) {
        String ipcServerSocketPath = Platform.getInstance().prepareIpcFilepathForComponent(mockRootPath);
        final EventStreamRPCConnectionConfig config = new EventStreamRPCConnectionConfig(clientBootstrap, elg, socketOptions, null, ipcServerSocketPath, DEFAULT_PORT_NUMBER, GreengrassConnectMessageSupplier.connectMessageSupplier("authToken"));
        connection = new EventStreamRPCConnection(config);
        final boolean[] disconnected = { false };
        final int[] disconnectedCode = { -1 };
        // this is a bit cumbersome but does not prevent a convenience wrapper from exposing a sync
        // connect() or a connect() that returns a CompletableFuture that errors
        // this could be wrapped by utility methods to provide a more
        connection.connect(new EventStreamRPCConnection.LifecycleHandler() {

            @Override
            public void onConnect() {
                connectionLatch.countDown();
            }

            @Override
            public void onDisconnect(int errorCode) {
                disconnected[0] = true;
                disconnectedCode[0] = errorCode;
            }

            // This on error is for any errors that is connection level, including problems during connect()
            @Override
            public boolean onError(Throwable t) {
                // hints at handler to disconnect due to this error
                return true;
            }
        });
        assertTrue(connectionLatch.await(2, TimeUnit.SECONDS));
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) EventStreamRPCConnection(software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) HostResolver(software.amazon.awssdk.crt.io.HostResolver) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) EventStreamRPCConnectionConfig(software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnectionConfig) Test(org.junit.jupiter.api.Test)

Example 2 with HostResolver

use of software.amazon.awssdk.crt.io.HostResolver in project aws-crt-java by awslabs.

the class ProxyTest method buildDirectMqttConnection.

private MqttClientConnection buildDirectMqttConnection(ProxyTestType testType, ProxyAuthType authType) {
    try (EventLoopGroup eventLoopGroup = new EventLoopGroup(1);
        HostResolver resolver = new HostResolver(eventLoopGroup);
        ClientBootstrap bootstrap = new ClientBootstrap(eventLoopGroup, resolver);
        TlsContext tlsContext = createX509TlsContext(null);
        MqttClient mqttClient = new MqttClient(bootstrap, tlsContext);
        MqttConnectionConfig connectionConfig = new MqttConnectionConfig();
        TlsContext proxyTlsContext = createProxyTlsContext(testType)) {
        HttpProxyOptions proxyOptions = buildProxyOptions(testType, authType, proxyTlsContext);
        String clientId = PROXY_TEST_CLIENTID + (UUID.randomUUID()).toString();
        connectionConfig.setMqttClient(mqttClient);
        connectionConfig.setEndpoint(MQTT_ENDPOINT);
        connectionConfig.setHttpProxyOptions(proxyOptions);
        connectionConfig.setCleanSession(true);
        connectionConfig.setClientId(clientId);
        connectionConfig.setPort(MQTT_DIRECT_PORT);
        connectionConfig.setProtocolOperationTimeoutMs(60000);
        return new MqttClientConnection(connectionConfig);
    }
}
Also used : MqttClient(software.amazon.awssdk.crt.mqtt.MqttClient) HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) MqttConnectionConfig(software.amazon.awssdk.crt.mqtt.MqttConnectionConfig) ClientTlsContext(software.amazon.awssdk.crt.io.ClientTlsContext) TlsContext(software.amazon.awssdk.crt.io.TlsContext) HostResolver(software.amazon.awssdk.crt.io.HostResolver)

Example 3 with HostResolver

use of software.amazon.awssdk.crt.io.HostResolver in project aws-crt-java by awslabs.

the class ProxyTest method buildProxiedX509CredentialsProvider.

private CredentialsProvider buildProxiedX509CredentialsProvider(ProxyTestType testType, ProxyAuthType authType) {
    try (EventLoopGroup eventLoopGroup = new EventLoopGroup(1);
        HostResolver resolver = new HostResolver(eventLoopGroup);
        ClientBootstrap bootstrap = new ClientBootstrap(eventLoopGroup, resolver);
        TlsContext tlsContext = createX509TlsContext(null);
        TlsContext proxyTlsContext = createProxyTlsContext(testType)) {
        HttpProxyOptions proxyOptions = buildProxyOptions(testType, authType, proxyTlsContext);
        X509CredentialsProvider.X509CredentialsProviderBuilder builder = new X509CredentialsProvider.X509CredentialsProviderBuilder();
        builder.withClientBootstrap(bootstrap).withEndpoint(X509_CREDENTIALS_ENDPOINT).withProxyOptions(proxyOptions).withRoleAlias(X509_CREDENTIALS_ROLE_ALIAS).withThingName(X509_CREDENTIALS_THING_NAME).withTlsContext(tlsContext);
        return builder.build();
    }
}
Also used : HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) ClientTlsContext(software.amazon.awssdk.crt.io.ClientTlsContext) TlsContext(software.amazon.awssdk.crt.io.TlsContext) HostResolver(software.amazon.awssdk.crt.io.HostResolver) X509CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.X509CredentialsProvider)

Example 4 with HostResolver

use of software.amazon.awssdk.crt.io.HostResolver in project aws-crt-java by awslabs.

the class HttpClientConnectionManagerTest method createConnectionManager.

private HttpClientConnectionManager createConnectionManager(URI uri, int numThreads, int numConnections) {
    try (EventLoopGroup eventLoopGroup = new EventLoopGroup(1);
        HostResolver resolver = new HostResolver(eventLoopGroup);
        ClientBootstrap bootstrap = new ClientBootstrap(eventLoopGroup, resolver);
        SocketOptions sockOpts = new SocketOptions();
        TlsContext tlsContext = createHttpClientTlsContext()) {
        HttpClientConnectionManagerOptions options = new HttpClientConnectionManagerOptions();
        options.withClientBootstrap(bootstrap).withSocketOptions(sockOpts).withTlsContext(tlsContext).withUri(uri).withMaxConnections(numConnections);
        return HttpClientConnectionManager.create(options);
    }
}
Also used : EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) HttpClientConnectionManagerOptions(software.amazon.awssdk.crt.http.HttpClientConnectionManagerOptions) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) TlsContext(software.amazon.awssdk.crt.io.TlsContext) HostResolver(software.amazon.awssdk.crt.io.HostResolver)

Example 5 with HostResolver

use of software.amazon.awssdk.crt.io.HostResolver in project aws-crt-java by awslabs.

the class ClientBootstrapTest method testCreateDestroy.

@Test
public void testCreateDestroy() throws ExecutionException, InterruptedException {
    EventLoopGroup elg = new EventLoopGroup(1);
    HostResolver hostResolver = new HostResolver(elg);
    ClientBootstrap bootstrap = new ClientBootstrap(elg, hostResolver);
    assertNotNull(bootstrap);
    bootstrap.close();
    bootstrap.getShutdownCompleteFuture().get();
    hostResolver.close();
    elg.close();
}
Also used : EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) HostResolver(software.amazon.awssdk.crt.io.HostResolver) Test(org.junit.Test)

Aggregations

EventLoopGroup (software.amazon.awssdk.crt.io.EventLoopGroup)17 HostResolver (software.amazon.awssdk.crt.io.HostResolver)17 ClientBootstrap (software.amazon.awssdk.crt.io.ClientBootstrap)16 SocketOptions (software.amazon.awssdk.crt.io.SocketOptions)11 TlsContext (software.amazon.awssdk.crt.io.TlsContext)6 LinkedList (java.util.LinkedList)5 ExecutionException (java.util.concurrent.ExecutionException)5 Test (org.junit.jupiter.api.Test)5 Header (software.amazon.awssdk.crt.eventstream.Header)5 TestIpcServiceHandler (software.amazon.awssdk.eventstreamrpc.test.TestIpcServiceHandler)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 TimeoutException (java.util.concurrent.TimeoutException)4 HttpProxyOptions (software.amazon.awssdk.crt.http.HttpProxyOptions)3 ClientTlsContext (software.amazon.awssdk.crt.io.ClientTlsContext)3 EventStreamRPCConnection (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection)3 EventStreamRPCConnectionConfig (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnectionConfig)3 EchoTestRPC (software.amazon.awssdk.awstest.EchoTestRPC)2 EchoTestRPCClient (software.amazon.awssdk.awstest.EchoTestRPCClient)2 HttpClientConnectionManagerOptions (software.amazon.awssdk.crt.http.HttpClientConnectionManagerOptions)2 TlsCipherPreference (software.amazon.awssdk.crt.io.TlsCipherPreference)2