Search in sources :

Example 11 with SocketOptions

use of software.amazon.awssdk.crt.io.SocketOptions in project aws-iot-device-sdk-java-v2 by aws.

the class EventStreamRPCClientTests method testConnectionVersionMismatch.

@Test
public void testConnectionVersionMismatch() {
    final int port = randomPort();
    // below class is generated and just gets instantiated for what it is
    final TestIpcServiceHandler service = new TestIpcServiceHandler(false, request -> request, EventStreamJsonMessage.class, EventStreamJsonMessage.class, EventStreamJsonMessage.class, EventStreamJsonMessage.class);
    // handlers aren't relevant since no request will be made
    service.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
    service.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
    try (final EventLoopGroup elGroup = new EventLoopGroup(1);
        final HostResolver hostResolver = new HostResolver(elGroup, 64);
        final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, hostResolver);
        SocketOptions socketOptions = new SocketOptions()) {
        socketOptions.connectTimeoutMs = 3000;
        socketOptions.domain = SocketOptions.SocketDomain.IPv4;
        socketOptions.type = SocketOptions.SocketType.STREAM;
        try (final RpcServer ipcServer = new RpcServer(elGroup, socketOptions, null, "127.0.0.1", port, service)) {
            ipcServer.runServer();
            final EventStreamRPCConnectionConfig config = new EventStreamRPCConnectionConfig(clientBootstrap, elGroup, socketOptions, null, "127.0.0.1", port, () -> {
                final List<Header> headers = new LinkedList<>();
                headers.add(Header.createHeader("client-name", "accepted.foo"));
                return CompletableFuture.completedFuture(new MessageAmendInfo(headers, null));
            });
            try (final EventStreamRPCConnection connection = new EventStreamRPCConnection(config) {

                @Override
                protected String getVersionString() {
                    return "19.19.19";
                }
            }) {
                final CompletableFuture<Throwable> futureAccessDenied = new CompletableFuture<>();
                final CompletableFuture<Void> initialConnect = connection.connect(new EventStreamRPCConnection.LifecycleHandler() {

                    @Override
                    public void onConnect() {
                        futureAccessDenied.completeExceptionally(new AssertionFailedError("onConnect lifecycle handler method should not be called!"));
                    }

                    @Override
                    public void onDisconnect(int errorCode) {
                        futureAccessDenied.completeExceptionally(new AssertionFailedError("onDisconnect lifecycle handler method should not be called!"));
                    }

                    @Override
                    public boolean onError(Throwable t) {
                        futureAccessDenied.complete(t);
                        return true;
                    }
                });
                try {
                    initialConnect.get(5, TimeUnit.SECONDS);
                } catch (ExecutionException e) {
                    Assertions.assertEquals(AccessDeniedException.class, e.getCause().getClass());
                }
            }
        }
    } catch (InterruptedException | TimeoutException e) {
        Assertions.fail(e);
    }
    CrtResource.waitForNoResources();
}
Also used : AccessDeniedException(software.amazon.awssdk.eventstreamrpc.model.AccessDeniedException) HostResolver(software.amazon.awssdk.crt.io.HostResolver) CompletableFuture(java.util.concurrent.CompletableFuture) AssertionFailedError(org.opentest4j.AssertionFailedError) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) TestIpcServiceHandler(software.amazon.awssdk.eventstreamrpc.test.TestIpcServiceHandler) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) LinkedList(java.util.LinkedList) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) Header(software.amazon.awssdk.crt.eventstream.Header) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) Test(org.junit.jupiter.api.Test)

Example 12 with SocketOptions

use of software.amazon.awssdk.crt.io.SocketOptions in project aws-iot-device-sdk-java-v2 by aws.

the class EventStreamRPCClientTests method testClientClosedThrowsInitialFutureException.

@Test
public void testClientClosedThrowsInitialFutureException() {
    final int port = randomPort();
    // below class is generated and just gets instantiated for what it is
    final TestIpcServiceHandler service = new TestIpcServiceHandler(false, request -> request, EventStreamJsonMessage.class, EventStreamJsonMessage.class, EventStreamJsonMessage.class, EventStreamJsonMessage.class);
    // handlers aren't relevant since no request will be made
    service.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
    service.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
    try (final EventLoopGroup elGroup = new EventLoopGroup(1);
        final HostResolver hostResolver = new HostResolver(elGroup, 64);
        final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, hostResolver);
        SocketOptions socketOptions = new SocketOptions()) {
        socketOptions.connectTimeoutMs = 3000;
        socketOptions.domain = SocketOptions.SocketDomain.IPv4;
        socketOptions.type = SocketOptions.SocketType.STREAM;
        try (final RpcServer ipcServer = new RpcServer(elGroup, socketOptions, null, "127.0.0.1", port, service)) {
            ipcServer.runServer();
            final EventStreamRPCConnectionConfig config = new EventStreamRPCConnectionConfig(clientBootstrap, elGroup, socketOptions, null, "127.0.0.1", port, () -> {
                final List<Header> headers = new LinkedList<>();
                headers.add(Header.createHeader("client-name", "accepted.foo"));
                return CompletableFuture.completedFuture(new MessageAmendInfo(headers, null));
            });
            final EventStreamRPCConnection connection = new EventStreamRPCConnection(config);
            final CompletableFuture<Void> initialConnect = connection.connect(new EventStreamRPCConnection.LifecycleHandler() {

                @Override
                public void onConnect() {
                }

                @Override
                public void onDisconnect(int errorCode) {
                }

                @Override
                public boolean onError(Throwable t) {
                    return true;
                }
            });
            connection.disconnect();
            try {
                initialConnect.get(15, TimeUnit.SECONDS);
            } catch (ExecutionException e) {
                Assertions.assertEquals(EventStreamClosedException.class, e.getCause().getClass());
            }
        }
    } catch (InterruptedException | TimeoutException e) {
        Assertions.fail(e);
    }
    CrtResource.waitForNoResources();
}
Also used : TestIpcServiceHandler(software.amazon.awssdk.eventstreamrpc.test.TestIpcServiceHandler) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) HostResolver(software.amazon.awssdk.crt.io.HostResolver) LinkedList(java.util.LinkedList) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) Header(software.amazon.awssdk.crt.eventstream.Header) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 13 with SocketOptions

use of software.amazon.awssdk.crt.io.SocketOptions in project aws-iot-device-sdk-java-v2 by aws.

the class GreengrassV2ClientTest method before.

@BeforeEach
public void before() throws IOException {
    port = randomPort();
    try (final EventLoopGroup elGroup = new EventLoopGroup(1);
        SocketOptions socketOptions = new SocketOptions()) {
        socketOptions.connectTimeoutMs = 3000;
        socketOptions.domain = SocketOptions.SocketDomain.IPv4;
        socketOptions.type = SocketOptions.SocketType.STREAM;
        GreengrassCoreIPCService service = new GreengrassCoreIPCService();
        service.setCreateLocalDeploymentHandler((c) -> new GeneratedAbstractCreateLocalDeploymentOperationHandler(c) {

            @Override
            protected void onStreamClosed() {
            }

            @Override
            public CreateLocalDeploymentResponse handleRequest(CreateLocalDeploymentRequest request) {
                return new CreateLocalDeploymentResponse().withDeploymentId("deployment");
            }

            @Override
            public void handleStreamEvent(EventStreamJsonMessage streamRequestEvent) {
            }
        });
        service.setSubscribeToTopicHandler((c) -> new GeneratedAbstractSubscribeToTopicOperationHandler(c) {

            @Override
            protected void onStreamClosed() {
                subscriptionClosed.complete(null);
            }

            @Override
            public SubscribeToTopicResponse handleRequest(SubscribeToTopicRequest request) {
                new Thread(() -> {
                    sendStreamEvent(new SubscriptionResponseMessage().withBinaryMessage(new BinaryMessage().withMessage("message".getBytes(StandardCharsets.UTF_8))));
                }).start();
                return new SubscribeToTopicResponse().withTopicName(request.getTopic());
            }

            @Override
            public void handleStreamEvent(EventStreamJsonMessage streamRequestEvent) {
            }
        });
        service.setAuthenticationHandler((headers, bytes) -> {
            authenticationRequest = new Gson().fromJson(new String(bytes), GreengrassEventStreamConnectMessage.class);
            return () -> "connected";
        });
        service.setAuthorizationHandler(authenticationData -> Authorization.ACCEPT);
        ipcServer = new RpcServer(elGroup, socketOptions, null, "127.0.0.1", port, service);
        ipcServer.runServer();
        client = GreengrassCoreIPCClientV2.builder().withPort(port).withSocketPath("127.0.0.1").withSocketDomain(SocketOptions.SocketDomain.IPv4).withAuthToken("myAuthToken").build();
    }
}
Also used : SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) Gson(com.google.gson.Gson) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) BinaryMessage(software.amazon.awssdk.aws.greengrass.model.BinaryMessage) CreateLocalDeploymentRequest(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) CreateLocalDeploymentResponse(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) GeneratedAbstractCreateLocalDeploymentOperationHandler(greengrass.GeneratedAbstractCreateLocalDeploymentOperationHandler) RpcServer(software.amazon.awssdk.eventstreamrpc.RpcServer) GreengrassCoreIPCService(greengrass.GreengrassCoreIPCService) GeneratedAbstractSubscribeToTopicOperationHandler(greengrass.GeneratedAbstractSubscribeToTopicOperationHandler) SubscribeToTopicResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToTopicResponse) GreengrassEventStreamConnectMessage(software.amazon.awssdk.eventstreamrpc.GreengrassEventStreamConnectMessage) SubscribeToTopicRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToTopicRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with SocketOptions

use of software.amazon.awssdk.crt.io.SocketOptions in project aws-iot-device-sdk-java-v2 by aws.

the class IpcServerTests method testStartStopIpcServer.

@Test
public void testStartStopIpcServer() throws InterruptedException {
    final int port = randomPort();
    final EventStreamRPCServiceHandler handler = new EventStreamRPCServiceHandler() {

        @Override
        protected EventStreamRPCServiceModel getServiceModel() {
            return new EventStreamRPCServiceModel() {

                @Override
                public String getServiceName() {
                    return "TestService";
                }

                /**
                 * Retreives all operations on the service
                 *
                 * @return
                 */
                @Override
                public Collection<String> getAllOperations() {
                    return new HashSet<>();
                }

                @Override
                protected Optional<Class<? extends EventStreamJsonMessage>> getServiceClassType(String applicationModelType) {
                    return Optional.empty();
                }

                @Override
                public OperationModelContext getOperationModelContext(String operationName) {
                    return null;
                }
            };
        }

        @Override
        public Function<OperationContinuationHandlerContext, ? extends ServerConnectionContinuationHandler> getOperationHandler(String operationName) {
            return null;
        }

        @Override
        public boolean hasHandlerForOperation(String operation) {
            return true;
        }
    };
    handler.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
    handler.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
    try (final EventLoopGroup elGroup = new EventLoopGroup(1);
        SocketOptions socketOptions = new SocketOptions()) {
        socketOptions.connectTimeoutMs = 3000;
        socketOptions.domain = SocketOptions.SocketDomain.IPv4;
        socketOptions.type = SocketOptions.SocketType.STREAM;
        try (final IpcServer ipcServer = new IpcServer(elGroup, socketOptions, null, "127.0.0.1", port, handler)) {
            ipcServer.runServer();
            Socket clientSocket = new Socket();
            SocketAddress address = new InetSocketAddress("127.0.0.1", port);
            clientSocket.connect(address, 3000);
            // no real assertion to be made here as long as the above connection works...
            clientSocket.close();
            Thread.sleep(1_000);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    CrtResource.waitForNoResources();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) IOException(java.io.IOException) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 15 with SocketOptions

use of software.amazon.awssdk.crt.io.SocketOptions in project aws-iot-device-sdk-java-v2 by aws.

the class IpcServerTests method testIpcServerModelNotSet.

@Test
public void testIpcServerModelNotSet() {
    final int port = randomPort();
    final EventStreamRPCServiceHandler handler = new EventStreamRPCServiceHandler() {

        @Override
        protected EventStreamRPCServiceModel getServiceModel() {
            return null;
        }

        @Override
        public Function<OperationContinuationHandlerContext, ? extends ServerConnectionContinuationHandler> getOperationHandler(String operationName) {
            return null;
        }

        @Override
        public boolean hasHandlerForOperation(String operation) // what'll trigger the validation failure
        {
            return true;
        }
    };
    handler.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
    handler.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
    try (final EventLoopGroup elGroup = new EventLoopGroup(1);
        SocketOptions socketOptions = new SocketOptions()) {
        socketOptions.connectTimeoutMs = 3000;
        socketOptions.domain = SocketOptions.SocketDomain.IPv4;
        socketOptions.type = SocketOptions.SocketType.STREAM;
        try (final IpcServer ipcServer = new IpcServer(elGroup, socketOptions, null, "127.0.0.1", port, handler)) {
            Assertions.assertThrows(InvalidServiceConfigurationException.class, () -> {
                ipcServer.runServer();
            });
        }
    }
    CrtResource.waitForNoResources();
}
Also used : EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) Test(org.junit.jupiter.api.Test)

Aggregations

SocketOptions (software.amazon.awssdk.crt.io.SocketOptions)43 EventLoopGroup (software.amazon.awssdk.crt.io.EventLoopGroup)30 Test (org.junit.jupiter.api.Test)18 ClientBootstrap (software.amazon.awssdk.crt.io.ClientBootstrap)18 ExecutionException (java.util.concurrent.ExecutionException)13 Test (org.junit.Test)11 HostResolver (software.amazon.awssdk.crt.io.HostResolver)11 EventStreamRPCConnection (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection)11 ServerBootstrap (software.amazon.awssdk.crt.io.ServerBootstrap)10 List (java.util.List)9 IOException (java.io.IOException)8 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)8 ArrayList (java.util.ArrayList)7 CompletableFuture (java.util.concurrent.CompletableFuture)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 HashSet (java.util.HashSet)5