use of software.amazon.awssdk.crt.io.SocketOptions in project aws-greengrass-nucleus by aws-greengrass.
the class IPCPubSubTest method GIVEN_PubSubEventStreamClient_WHEN_subscribe_to_wildcard_THEN_publishes_to_subtopic_only_once.
@Test
@SuppressWarnings({ "PMD.AvoidCatchingGenericException" })
void GIVEN_PubSubEventStreamClient_WHEN_subscribe_to_wildcard_THEN_publishes_to_subtopic_only_once() throws Exception {
LogConfig.getRootLogConfig().setLevel(Level.DEBUG);
String topicName = "/topic/1/+";
SubscribeToTopicRequest subscribeToTopicRequest = new SubscribeToTopicRequest();
subscribeToTopicRequest.setTopic(topicName);
CountDownLatch cdl = new CountDownLatch(1);
AtomicInteger atomicInteger = new AtomicInteger();
CountDownLatch subscriptionLatch = new CountDownLatch(1);
// Allowed resource /to*/#
String authToken = IPCTestUtils.getAuthTokeForService(kernel, "SubscribeAndPublishWildcard");
SocketOptions socketOptions = TestUtils.getSocketOptionsForIPC();
try (EventStreamRPCConnection clientConnection = IPCTestUtils.connectToGGCOverEventStreamIPC(socketOptions, authToken, kernel);
AutoCloseable l = TestUtils.createCloseableLogListener(m -> {
if (m.getMessage().contains("Subscribed to topic")) {
subscriptionLatch.countDown();
}
})) {
GreengrassCoreIPCClient greengrassCoreIPCClient = new GreengrassCoreIPCClient(clientConnection);
CompletableFuture<SubscribeToTopicResponse> fut = greengrassCoreIPCClient.subscribeToTopic(subscribeToTopicRequest, Optional.of(new StreamResponseHandler<SubscriptionResponseMessage>() {
@Override
public void onStreamEvent(SubscriptionResponseMessage message) {
assertNotNull(message.getBinaryMessage());
assertNull(message.getJsonMessage());
assertEquals("ABCDEFG", new String(message.getBinaryMessage().getMessage()));
atomicInteger.incrementAndGet();
cdl.countDown();
}
@Override
public boolean onStreamError(Throwable error) {
logger.atError().log("Received a stream error", error);
return false;
}
@Override
public void onStreamClosed() {
}
})).getResponse();
try {
fut.get(3, TimeUnit.SECONDS);
} catch (Exception e) {
logger.atError().setCause(e).log("Error when subscribing to component updates");
fail("Caught exception when subscribing to component updates");
}
assertTrue(subscriptionLatch.await(10, TimeUnit.SECONDS));
publishToTopicOverIpcAsBinaryMessage(greengrassCoreIPCClient, "/topic/1/2", "ABCDEFG");
assertTrue(cdl.await(20, TimeUnit.SECONDS));
}
}
use of software.amazon.awssdk.crt.io.SocketOptions 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);
}
use of software.amazon.awssdk.crt.io.SocketOptions in project aws-greengrass-nucleus by aws-greengrass.
the class TestUtils method getSocketOptionsForIPC.
public static SocketOptions getSocketOptionsForIPC() {
SocketOptions socketOptions = new SocketOptions();
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.LOCAL;
socketOptions.type = SocketOptions.SocketType.STREAM;
return socketOptions;
}
Aggregations