private static void initConnection(Vertx vertx, ProtonConnection connection) { connection.openHandler(res -> { System.out.println("Client connection opened, container-id: " + connection.getRemoteContainer()); connection.open(); }); connection.closeHandler(c -> { System.out.println("Client closing connection, container-id: " + connection.getRemoteContainer()); connection.close(); connection.disconnect(); }); connection.disconnectHandler(c -> { System.out.println("Client socket disconnected, container-id: " + connection.getRemoteContainer()); connection.disconnect(); }); connection.sessionOpenHandler(session -> { session.closeHandler(x -> { session.close(); session.free(); }); session.open(); }); connection.senderOpenHandler(sender -> { initSender(vertx, connection, sender); }); connection.receiverOpenHandler(receiver -> { initReceiver(receiver); }); connection.open(); }
/** * Invoked when a client closes the connection with this server. * * @param con The connection to close. * @param res The client's close frame. */ private void handleRemoteConnectionClose(final ProtonConnection con, final AsyncResult<ProtonConnection> res) { if (res.succeeded()) { LOG.debug("client [container: {}] closed connection", con.getRemoteContainer()); } else { LOG.debug("client [container: {}] closed connection with error", con.getRemoteContainer(), res.cause()); } con.disconnectHandler(null); con.close(); con.disconnect(); }
private void onRemoteClose(final AsyncResult<ProtonConnection> remoteClose, final Handler<ProtonConnection> connectionLossHandler) { if (remoteClose.failed()) { log.info("remote server [{}:{}] closed connection with error condition: {}", connectionFactory.getHost(), connectionFactory.getPort(), remoteClose.cause().getMessage()); } else { log.info("remote server [{}:{}] closed connection", connectionFactory.getHost(), connectionFactory.getPort()); } connection.disconnectHandler(null); connection.close(); handleConnectionLoss(connectionLossHandler); }
/** * Closes the connection with the downstream container. * * @param stopFuture Always succeeds. */ @Override public final void stop(final Future<Void> stopFuture) { if (running) { if (downstreamConnection != null && !downstreamConnection.isDisconnected()) { final String container = downstreamConnection.getRemoteContainer(); logger.info("closing connection to downstream container [{}]", container); downstreamConnection.closeHandler(null).disconnectHandler(null).close(); } else { logger.debug("downstream connection already closed"); } running = false; } stopFuture.complete(); }
/** * Handles unexpected disconnection from downstream container. * * @param con the failed connection */ private void onDisconnectFromDownstreamContainer(final ProtonConnection con) { // all links to downstream host will now be stale and unusable logger.warn("lost connection to downstream container [{}], closing upstream receivers ...", con.getRemoteContainer()); for (UpstreamReceiver client : activeSenders.keySet()) { client.close(ErrorConditions.ERROR_NO_DOWNSTREAM_CONSUMER); } sendersPerConnection.clear(); activeSenders.clear(); con.disconnectHandler(null); con.disconnect(); final ProtonClientOptions clientOptions = createClientOptions(); if (clientOptions.getReconnectAttempts() != 0) { vertx.setTimer(300, reconnect -> { logger.info("attempting to re-connect to downstream container"); connectToDownstream(clientOptions); }); } }
log.info("closing connection to server [{}:{}]...", connectionFactory.getHost(), connectionFactory.getPort()); final ProtonConnection connectionToClose = connection; connectionToClose.disconnectHandler(null); // make sure we are not trying to re-connect connectionToClose.closeHandler(closedCon -> { if (closedCon.succeeded()) {
conn.disconnectHandler(result -> connectionDisconnected(conn)); conn.open(); } else {
/** * Verifies that the service invokes the <em>publishConnectionClosedEvent</em> * method when a client disconnects. */ @Test @SuppressWarnings({"rawtypes", "unchecked"}) public void testServerCallsPublishEventOnClientDisconnect() { // GIVEN a server to which a client is connected final Handler<ProtonConnection> publishConnectionClosedEvent = mock(Handler.class); final AmqpServiceBase<ServiceConfigProperties> server = createServer(null, publishConnectionClosedEvent); final ProtonConnection con = newConnection(Constants.PRINCIPAL_ANONYMOUS); server.onRemoteConnectionOpen(con); final ArgumentCaptor<Handler> closeHandlerCaptor = ArgumentCaptor.forClass(Handler.class); verify(con).disconnectHandler(closeHandlerCaptor.capture()); // WHEN the client disconnects from the service closeHandlerCaptor.getValue().handle(con); // THEN the publishConnectionClosedEvent method is invoked verify(publishConnectionClosedEvent).handle(any(ProtonConnection.class)); }
logger.debug("connection to container [{}] at [{}://{}:{}] open", downstreamConnection.getRemoteContainer(), clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort()); downstreamConnection.disconnectHandler(disconnectHandler); downstreamConnection.closeHandler(closeHandler); connectionResultHandler.handle(Future.succeededFuture(downstreamConnection)); }).disconnectHandler(disconnectedCon -> { logger.warn("can't open connection to container [{}] at [{}://{}:{}]: {}", downstreamConnection.getRemoteContainer(),
@Override protected void setRemoteConnectionOpenHandler(final ProtonConnection connection) { connection.sessionOpenHandler(remoteOpenSession -> handleSessionOpen(connection, remoteOpenSession)); connection.senderOpenHandler(remoteOpenSender -> handleSenderOpen(connection, remoteOpenSender)); connection.disconnectHandler(con -> { con.close(); con.disconnect(); }); connection.closeHandler(remoteClose -> { connection.close(); connection.disconnect(); }); connection.openHandler(remoteOpen -> { if (remoteOpen.failed()) { LOG.debug("ignoring peer's open frame containing error", remoteOpen.cause()); } else { processRemoteOpen(remoteOpen.result()); } }); }
logger.debug("connection to container [{}] at [{}://{}:{}] open", downstreamConnection.getRemoteContainer(), clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort()); downstreamConnection.disconnectHandler(disconnectHandler); downstreamConnection.closeHandler(closeHandler); connectionResultHandler.handle(Future.succeededFuture(downstreamConnection)); }).disconnectHandler(disconnectedCon -> { logger.warn("can't open connection to container [{}] at [{}://{}:{}]: {}", downstreamConnection.getRemoteContainer(),
private void onConnectRequest(final ProtonConnection con) { con.disconnectHandler(lostConnection -> { LOG.debug("lost connection to device [container: {}]", con.getRemoteContainer()); Optional.ofNullable(getConnectionLossHandler(con)).ifPresent(handler -> handler.handle(null));
private void setRemoteConnectionOpenHandler(final ProtonConnection connection) { connection.sessionOpenHandler(remoteOpenSession -> handleSessionOpen(connection, remoteOpenSession)); connection.receiverOpenHandler(remoteOpenReceiver -> handleReceiverOpen(connection, remoteOpenReceiver)); connection.senderOpenHandler(remoteOpenSender -> handleSenderOpen(connection, remoteOpenSender)); connection.disconnectHandler(this::handleRemoteDisconnect); connection.closeHandler(remoteClose -> handleRemoteConnectionClose(connection, remoteClose)); connection.openHandler(remoteOpen -> { LOG.info("client [container: {}, user: {}] connected", connection.getRemoteContainer(), getUserFromConnection(connection)); connection.open(); // attach an ID so that we can later inform downstream components when connection is closed connection.attachments().set(Constants.KEY_CONNECTION_ID, String.class, UUID.randomUUID().toString()); }); }
private void connectHandler(ProtonConnection connection) { connection.setContainer(containerId); connection.openHandler(conn -> { log.info("[{}]: Connection opened", containerId); }).closeHandler(conn -> { connection.close(); connection.disconnect(); log.info("[{}]: Connection closed", containerId); }).disconnectHandler(protonConnection -> { connection.disconnect(); log.debug("Disconnected"); }).open(); connection.sessionOpenHandler(ProtonSession::open); connection.senderOpenHandler(sender -> senderOpenHandler(connection, sender)); connection.receiverOpenHandler(receiver -> receiverOpenHandler(connection, receiver)); }
connection.close(); connection.disconnect(); }).disconnectHandler(protonConnection -> { LOG.info("Disconnecting"); connection.disconnect();
handleSenderOpen(connection, sender); }); connection.disconnectHandler(this::handleRemoteDisconnect); connection.closeHandler(remoteClose -> handleRemoteConnectionClose(connection, remoteClose)); connection.openHandler(remoteOpen -> {
handleSenderOpen(connection, sender); }); connection.disconnectHandler(this::handleRemoteDisconnect); connection.closeHandler(remoteClose -> handleRemoteConnectionClose(connection, remoteClose)); connection.openHandler(remoteOpen -> {
/** * Closes an expired client connection. * <p> * A connection is considered expired if the {@link HonoUser#isExpired()} method * of the user principal attached to the connection returns {@code true}. * * @param con The client connection. */ protected final void closeExpiredConnection(final ProtonConnection con) { if (!con.isDisconnected()) { final HonoUser clientPrincipal = Constants.getClientPrincipal(con); if (clientPrincipal != null) { LOG.debug("client's [{}] access token has expired, closing connection", clientPrincipal.getName()); con.disconnectHandler(null); con.closeHandler(null); con.setCondition(ProtonHelper.condition(AmqpError.UNAUTHORIZED_ACCESS, "access token expired")); con.close(); con.disconnect(); publishConnectionClosedEvent(con); } } }
/** * Closes an expired client connection. * <p> * A connection is considered expired if the {@link HonoUser#isExpired()} method * of the user principal attached to the connection returns {@code true}. * * @param con The client connection. */ protected final void closeExpiredConnection(final ProtonConnection con) { if (!con.isDisconnected()) { final HonoUser clientPrincipal = Constants.getClientPrincipal(con); if (clientPrincipal != null) { LOG.debug("client's [{}] access token has expired, closing connection", clientPrincipal.getName()); con.disconnectHandler(null); con.closeHandler(null); con.setCondition(ProtonHelper.condition(AmqpError.UNAUTHORIZED_ACCESS, "access token expired")); con.close(); con.disconnect(); publishConnectionClosedEvent(con); } } }
/** * Process a connection request accepted by the Proton server or * open the connection if it's working as client * * @param connection Proton connection accepted instance */ private void processConnection(ProtonConnection connection) { connection .openHandler(this::processOpenConnection) .closeHandler(this::processCloseConnection) .disconnectHandler(this::processDisconnection) .sessionOpenHandler(this::processOpenSession) .receiverOpenHandler(receiver -> { this.processOpenReceiver(connection, receiver); }) .senderOpenHandler(sender -> { this.processOpenSender(connection, sender); }); if (this.bridgeConfigProperties.getEndpointConfigProperties().getMode() == AmqpMode.CLIENT) { connection.open(); } }