Search in sources :

Example 1 with EventLoopGroup

use of software.amazon.awssdk.crt.io.EventLoopGroup 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 EventLoopGroup

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

the class ServerListenerTest method testBindErrorPropagates.

@Test
public void testBindErrorPropagates() throws ExecutionException, InterruptedException, TimeoutException {
    SocketOptions socketOptions = new SocketOptions();
    socketOptions.connectTimeoutMs = 3000;
    socketOptions.domain = SocketOptions.SocketDomain.IPv4;
    socketOptions.type = SocketOptions.SocketType.STREAM;
    EventLoopGroup elGroup = new EventLoopGroup(1);
    ServerBootstrap bootstrap = new ServerBootstrap(elGroup);
    ServerListener listener1 = new ServerListener("127.0.0.1", (short) 8039, socketOptions, null, bootstrap, new ServerListenerHandler() {

        public ServerConnectionHandler onNewConnection(ServerConnection serverConnection, int errorCode) {
            return null;
        }

        public void onConnectionShutdown(ServerConnection serverConnection, int errorCode) {
        }
    });
    assertNotNull(listener1);
    boolean exceptionThrown = false;
    try {
        ServerListener listener2 = new ServerListener("127.0.0.1", (short) 8039, socketOptions, null, bootstrap, new ServerListenerHandler() {

            public ServerConnectionHandler onNewConnection(ServerConnection serverConnection, int errorCode) {
                return null;
            }

            public void onConnectionShutdown(ServerConnection serverConnection, int errorCode) {
            }
        });
    } catch (CrtRuntimeException ex) {
        assertTrue(ex.getMessage().contains("AWS_IO_SOCKET_ADDRESS_IN_USE(1054), Socket address already in use."));
        exceptionThrown = true;
    }
    assertTrue(exceptionThrown);
    listener1.close();
    listener1.getShutdownCompleteFuture().get(1, TimeUnit.SECONDS);
    bootstrap.close();
    elGroup.close();
    elGroup.getShutdownCompleteFuture().get(1, TimeUnit.SECONDS);
    socketOptions.close();
}
Also used : EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) ServerBootstrap(software.amazon.awssdk.crt.io.ServerBootstrap) Test(org.junit.Test)

Example 3 with EventLoopGroup

use of software.amazon.awssdk.crt.io.EventLoopGroup 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 4 with EventLoopGroup

use of software.amazon.awssdk.crt.io.EventLoopGroup 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 5 with EventLoopGroup

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

the class EventLoopGroupTest method testCreateDestroy.

@Test
public void testCreateDestroy() {
    try (EventLoopGroup elg = new EventLoopGroup(1)) {
        assertNotNull(elg);
        assertTrue(!elg.isNull());
    } catch (CrtRuntimeException ex) {
        fail(ex.getMessage());
    }
}
Also used : EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) Test(org.junit.Test)

Aggregations

EventLoopGroup (software.amazon.awssdk.crt.io.EventLoopGroup)42 SocketOptions (software.amazon.awssdk.crt.io.SocketOptions)30 ClientBootstrap (software.amazon.awssdk.crt.io.ClientBootstrap)24 HostResolver (software.amazon.awssdk.crt.io.HostResolver)17 Test (org.junit.Test)14 Test (org.junit.jupiter.api.Test)11 ServerBootstrap (software.amazon.awssdk.crt.io.ServerBootstrap)11 List (java.util.List)8 ArrayList (java.util.ArrayList)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 ExecutionException (java.util.concurrent.ExecutionException)7 TimeoutException (java.util.concurrent.TimeoutException)6 Condition (java.util.concurrent.locks.Condition)6 Lock (java.util.concurrent.locks.Lock)6 ReentrantLock (java.util.concurrent.locks.ReentrantLock)6 Header (software.amazon.awssdk.crt.eventstream.Header)6 TlsContext (software.amazon.awssdk.crt.io.TlsContext)6 EventStreamRPCConnection (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection)6 EventStreamRPCConnectionConfig (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnectionConfig)6 EventStreamJsonMessage (software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage)6