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();
}
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();
}
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();
}
}
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();
}
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();
}
Aggregations