Search in sources :

Example 1 with EventStreamJsonMessage

use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage in project aws-iot-device-sdk-java-v2 by aws.

the class EventStreamRPCServiceModel method fromJson.

/**
 * Uses this service's specific model class
 * @param applicationModelType
 * @param payload
 * @return
 */
public EventStreamJsonMessage fromJson(final String applicationModelType, byte[] payload) {
    final Optional<Class<? extends EventStreamJsonMessage>> clazz = getApplicationModelClass(applicationModelType);
    if (!clazz.isPresent()) {
        throw new UnmappedDataException(applicationModelType);
    }
    final EventStreamJsonMessage msg = fromJson(clazz.get(), payload);
    msg.postFromJson();
    return msg;
}
Also used : EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage)

Example 2 with EventStreamJsonMessage

use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage 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 3 with EventStreamJsonMessage

use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage 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 4 with EventStreamJsonMessage

use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage in project aws-iot-device-sdk-java-v2 by aws.

the class IpcServerTests method testIpcServerAuthNUnset.

@Test
public void testIpcServerAuthNUnset() {
    final int port = randomPort();
    final Set<String> OPERATION_SET = new HashSet<>();
    OPERATION_SET.add("dummyOperationName");
    final EventStreamRPCServiceHandler handler = new EventStreamRPCServiceHandler() {

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

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

                @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;
        }
    };
    // missing 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) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with EventStreamJsonMessage

use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage in project aws-iot-device-sdk-java-v2 by aws.

the class IpcServerTests method testIpcServerAuthZUnset.

@Test
public void testIpcServerAuthZUnset() {
    final int port = randomPort();
    final Set<String> OPERATION_SET = new HashSet<>();
    OPERATION_SET.add("dummyOperationName");
    final EventStreamRPCServiceHandler handler = new EventStreamRPCServiceHandler() {

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

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

                @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());
    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) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

EventStreamJsonMessage (software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage)9 HashSet (java.util.HashSet)6 EventLoopGroup (software.amazon.awssdk.crt.io.EventLoopGroup)6 SocketOptions (software.amazon.awssdk.crt.io.SocketOptions)6 Test (org.junit.jupiter.api.Test)5 LinkedList (java.util.LinkedList)2 Optional (java.util.Optional)2 Gson (com.google.gson.Gson)1 GeneratedAbstractCreateLocalDeploymentOperationHandler (greengrass.GeneratedAbstractCreateLocalDeploymentOperationHandler)1 GeneratedAbstractSubscribeToTopicOperationHandler (greengrass.GeneratedAbstractSubscribeToTopicOperationHandler)1 GreengrassCoreIPCService (greengrass.GreengrassCoreIPCService)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 SocketAddress (java.net.SocketAddress)1 AbstractMap (java.util.AbstractMap)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Queue (java.util.Queue)1