Search in sources :

Example 1 with RpcServer

use of software.amazon.awssdk.eventstreamrpc.RpcServer 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 2 with RpcServer

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

the class EchoTestServiceRunner method runService.

public void runService() {
    try (SocketOptions socketOptions = new SocketOptions()) {
        socketOptions.connectTimeoutMs = 3000;
        socketOptions.domain = domain;
        socketOptions.type = SocketOptions.SocketType.STREAM;
        final EchoTestRPCService service = new EchoTestRPCService();
        // wiring of operation handlers
        service.setEchoMessageHandler(EchoMessageHandler::new);
        service.setEchoStreamMessagesHandler(EchoStreamMessagesHandler::new);
        service.setCauseServiceErrorHandler(CauseServiceErrorHandler::new);
        service.setCauseStreamServiceToErrorHandler(CauseStreamServiceToError::new);
        service.setGetAllCustomersHandler(GetAllCustomersHandler::new);
        service.setGetAllProductsHandler(GetAllProductsHandler::new);
        service.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
        service.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
        rpcServer = new RpcServer(elGroup, socketOptions, null, hostname, port, service);
        rpcServer.runServer();
    }
}
Also used : EchoTestRPCService(software.amazon.awssdk.awstest.EchoTestRPCService) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) RpcServer(software.amazon.awssdk.eventstreamrpc.RpcServer)

Example 3 with RpcServer

use of software.amazon.awssdk.eventstreamrpc.RpcServer in project aws-greengrass-nucleus by aws-greengrass.

the class IPCEventStreamService method startup.

@SuppressWarnings({ "PMD.AvoidCatchingGenericException", "PMD.ExceptionAsFlowControl" })
@Override
public void startup() {
    Path rootPath = kernel.getNucleusPaths().rootPath();
    try {
        greengrassCoreIPCService.getAllOperations().forEach(operation -> greengrassCoreIPCService.setOperationHandler(operation, (context) -> new DefaultOperationHandler(GreengrassCoreIPCServiceModel.getInstance().getOperationModelContext(operation), context)));
        greengrassCoreIPCService.setAuthenticationHandler((List<Header> headers, byte[] bytes) -> ipcAuthenticationHandler(bytes));
        greengrassCoreIPCService.setAuthorizationHandler(this::ipcAuthorizationHandler);
        socketOptions = new SocketOptions();
        socketOptions.connectTimeoutMs = 3000;
        socketOptions.domain = SocketOptions.SocketDomain.LOCAL;
        socketOptions.type = SocketOptions.SocketType.STREAM;
        eventLoopGroup = new EventLoopGroup(1);
        Topic kernelUri = config.getRoot().lookup(SETENV_CONFIG_NAMESPACE, NUCLEUS_DOMAIN_SOCKET_FILEPATH);
        kernelUri.withValue(Platform.getInstance().prepareIpcFilepath(rootPath));
        Topic kernelRelativeUri = config.getRoot().lookup(SETENV_CONFIG_NAMESPACE, NUCLEUS_DOMAIN_SOCKET_FILEPATH_FOR_COMPONENT);
        kernelRelativeUri.withValue(Platform.getInstance().prepareIpcFilepathForComponent(rootPath));
        // For domain sockets:
        // 1. Port number is ignored. RpcServer does not accept a null value so we are using a default value.
        // 2. The hostname parameter expects the socket filepath
        rpcServer = new RpcServer(eventLoopGroup, socketOptions, null, Platform.getInstance().prepareIpcFilepathForRpcServer(rootPath), DEFAULT_PORT_NUMBER, greengrassCoreIPCService);
        rpcServer.runServer();
    } catch (RuntimeException e) {
        // Make sure to cleanup anything we created since we don't know where exactly we failed
        close();
        throw e;
    }
    logger.debug("Set IPC permissions");
    Platform.getInstance().setIpcFilePermissions(rootPath);
}
Also used : Path(java.nio.file.Path) UnauthenticatedException(com.aws.greengrass.ipc.exceptions.UnauthenticatedException) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) AuthenticationData(software.amazon.awssdk.eventstreamrpc.AuthenticationData) DefaultOperationHandler(com.aws.greengrass.ipc.common.DefaultOperationHandler) GreengrassCoreIPCService(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCService) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) Platform(com.aws.greengrass.util.platforms.Platform) Inject(javax.inject.Inject) SETENV_CONFIG_NAMESPACE(com.aws.greengrass.lifecyclemanager.GreengrassService.SETENV_CONFIG_NAMESPACE) LogManager(com.aws.greengrass.logging.impl.LogManager) Header(software.amazon.awssdk.crt.eventstream.Header) Authorization(software.amazon.awssdk.eventstreamrpc.Authorization) Path(java.nio.file.Path) Configuration(com.aws.greengrass.config.Configuration) RpcServer(software.amazon.awssdk.eventstreamrpc.RpcServer) GreengrassCoreIPCServiceModel(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCServiceModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) Topic(com.aws.greengrass.config.Topic) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) List(java.util.List) Utils(com.aws.greengrass.util.Utils) Closeable(java.io.Closeable) GreengrassEventStreamConnectMessage(software.amazon.awssdk.eventstreamrpc.GreengrassEventStreamConnectMessage) Logger(com.aws.greengrass.logging.api.Logger) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) DefaultOperationHandler(com.aws.greengrass.ipc.common.DefaultOperationHandler) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) RpcServer(software.amazon.awssdk.eventstreamrpc.RpcServer) List(java.util.List) Topic(com.aws.greengrass.config.Topic)

Aggregations

SocketOptions (software.amazon.awssdk.crt.io.SocketOptions)3 RpcServer (software.amazon.awssdk.eventstreamrpc.RpcServer)3 EventLoopGroup (software.amazon.awssdk.crt.io.EventLoopGroup)2 GreengrassEventStreamConnectMessage (software.amazon.awssdk.eventstreamrpc.GreengrassEventStreamConnectMessage)2 Configuration (com.aws.greengrass.config.Configuration)1 Topic (com.aws.greengrass.config.Topic)1 DefaultOperationHandler (com.aws.greengrass.ipc.common.DefaultOperationHandler)1 UnauthenticatedException (com.aws.greengrass.ipc.exceptions.UnauthenticatedException)1 SETENV_CONFIG_NAMESPACE (com.aws.greengrass.lifecyclemanager.GreengrassService.SETENV_CONFIG_NAMESPACE)1 Kernel (com.aws.greengrass.lifecyclemanager.Kernel)1 Logger (com.aws.greengrass.logging.api.Logger)1 LogManager (com.aws.greengrass.logging.impl.LogManager)1 Utils (com.aws.greengrass.util.Utils)1 Platform (com.aws.greengrass.util.platforms.Platform)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Gson (com.google.gson.Gson)1 GeneratedAbstractCreateLocalDeploymentOperationHandler (greengrass.GeneratedAbstractCreateLocalDeploymentOperationHandler)1 GeneratedAbstractSubscribeToTopicOperationHandler (greengrass.GeneratedAbstractSubscribeToTopicOperationHandler)1 GreengrassCoreIPCService (greengrass.GreengrassCoreIPCService)1