use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class HonoClientImpl method connect.
private void connect(final ProtonClientOptions options, final Handler<AsyncResult<HonoClient>> connectionHandler, final Handler<ProtonConnection> disconnectHandler) {
context.runOnContext(connect -> {
if (isConnectedInternal()) {
LOG.debug("already connected to server [{}:{}]", connectionFactory.getHost(), connectionFactory.getPort());
connectionHandler.handle(Future.succeededFuture(this));
} else if (connecting.compareAndSet(false, true)) {
if (options == null) {
// by default, try to re-connect forever
clientOptions = new ProtonClientOptions().setConnectTimeout(200).setReconnectAttempts(-1).setReconnectInterval(Constants.DEFAULT_RECONNECT_INTERVAL_MILLIS);
} else {
clientOptions = options;
}
connectionFactory.connect(clientOptions, remoteClose -> onRemoteClose(remoteClose, disconnectHandler), failedConnection -> onRemoteDisconnect(failedConnection, disconnectHandler), conAttempt -> {
connecting.compareAndSet(true, false);
if (conAttempt.failed()) {
if (conAttempt.cause() instanceof SecurityException) {
// SASL handshake has failed
connectionHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "failed to authenticate with server")));
} else {
reconnect(conAttempt.cause(), connectionHandler, disconnectHandler);
}
} else {
// make sure we try to re-connect as often as we tried to connect initially
reconnectAttempts = new AtomicInteger(0);
final ProtonConnection newConnection = conAttempt.result();
if (shuttingDown.get()) {
// if client was shut down in the meantime, we need to immediately
// close again the newly created connection
newConnection.closeHandler(null);
newConnection.disconnectHandler(null);
newConnection.close();
connectionHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_CONFLICT, "client is already shut down")));
} else {
setConnection(newConnection);
connectionHandler.handle(Future.succeededFuture(this));
}
}
});
} else {
LOG.debug("already trying to connect to server ...");
connectionHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_CONFLICT, "already connecting to server")));
}
});
}
use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class HonoClientImpl method handleConnectionLoss.
private void handleConnectionLoss(final Handler<ProtonConnection> connectionLossHandler) {
if (isConnectedInternal()) {
connection.disconnect();
}
final ProtonConnection failedConnection = this.connection;
setConnection(null);
activeSenders.clear();
activeRequestResponseClients.clear();
failAllCreationRequests();
if (connectionLossHandler != null) {
connectionLossHandler.handle(failedConnection);
} else {
reconnect(attempt -> {
}, null);
}
}
use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class AbstractHonoClientTest method testCreateReceiverFails.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void testCreateReceiverFails(final Supplier<ErrorCondition> errorSupplier, final Predicate<Throwable> failureAssertion) {
final Record attachments = new RecordImpl();
final ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.getRemoteCondition()).thenReturn(errorSupplier.get());
when(receiver.attachments()).thenReturn(attachments);
final ProtonConnection con = mock(ProtonConnection.class);
when(con.createReceiver(anyString())).thenReturn(receiver);
final Future<ProtonReceiver> result = AbstractHonoClient.createReceiver(context, props, con, "source", ProtonQoS.AT_LEAST_ONCE, (delivery, msg) -> {
}, null);
final ArgumentCaptor<Handler> openHandler = ArgumentCaptor.forClass(Handler.class);
verify(receiver).openHandler(openHandler.capture());
openHandler.getValue().handle(Future.failedFuture(new IllegalStateException()));
assertTrue(result.failed());
assertTrue(failureAssertion.test(result.cause()));
}
use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class EventConsumerImplTest method testHandlerCallsCloseHook.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void testHandlerCallsCloseHook(final TestContext ctx, final BiConsumer<ProtonReceiver, ArgumentCaptor<Handler>> handlerCaptor) {
// GIVEN an open event consumer
final Async consumerCreation = ctx.async();
final BiConsumer<ProtonDelivery, Message> eventConsumer = mock(BiConsumer.class);
final RecordImpl attachments = new RecordImpl();
final Source source = mock(Source.class);
when(source.getAddress()).thenReturn("source/address");
final ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.isOpen()).thenReturn(Boolean.TRUE);
when(receiver.getSource()).thenReturn(source);
when(receiver.attachments()).thenReturn(attachments);
when(receiver.open()).then(answer -> {
attachments.set(EventConsumerImpl.KEY_LINK_ESTABLISHED, Boolean.class, Boolean.TRUE);
consumerCreation.complete();
return receiver;
});
final ProtonConnection con = mock(ProtonConnection.class);
when(con.createReceiver(anyString())).thenReturn(receiver);
final Handler<String> closeHook = mock(Handler.class);
final ArgumentCaptor<Handler> captor = ArgumentCaptor.forClass(Handler.class);
EventConsumerImpl.create(vertx.getOrCreateContext(), new ClientConfigProperties(), con, "source/address", eventConsumer, ok -> {
}, closeHook);
consumerCreation.await();
handlerCaptor.accept(receiver, captor);
// WHEN the receiver link is closed
captor.getValue().handle(Future.succeededFuture(receiver));
// THEN the close hook is called
verify(closeHook).handle(any());
// and the receiver link is closed
verify(receiver).close();
}
use of io.vertx.proton.ProtonConnection in project hono by eclipse.
the class HonoMessagingTest method newConnection.
private static ProtonConnection newConnection(final HonoUser user) {
final Record attachments = new RecordImpl();
attachments.set(Constants.KEY_CONNECTION_ID, String.class, CON_ID);
attachments.set(Constants.KEY_CLIENT_PRINCIPAL, HonoUser.class, user);
final ProtonConnection con = mock(ProtonConnection.class);
when(con.attachments()).thenReturn(attachments);
when(con.getRemoteContainer()).thenReturn("test-client");
return con;
}
Aggregations