Java Code Examples for io.vertx.proton.ProtonConnection#closeHandler()

The following examples show how to use io.vertx.proton.ProtonConnection#closeHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleAuthenticationServer.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void setRemoteConnectionOpenHandler(final ProtonConnection connection) {
    connection.sessionOpenHandler(remoteOpenSession -> handleSessionOpen(connection, remoteOpenSession));
    connection.senderOpenHandler(remoteOpenSender -> handleSenderOpen(connection, remoteOpenSender));
    // no receiverOpenHandler set here
    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());
        }
    });
}
 
Example 2
Source File: AmqpAdapterTestBase.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
private Future<ProtonConnection> handleConnectAttempt(final ProtonConnection unopenedConnection) {

        final Promise<ProtonConnection> result = Promise.promise();

        unopenedConnection.openHandler(result);
        unopenedConnection.closeHandler(remoteClose -> {
            unopenedConnection.close();
        });
        unopenedConnection.open();

        return result.future()
                .map(con -> {
                    assertThat(unopenedConnection.getRemoteOfferedCapabilities()).contains(Constants.CAP_ANONYMOUS_RELAY);
                    this.context = Vertx.currentContext();
                    this.connection = unopenedConnection;
                    return con;
                })
                .recover(t -> {
                    return Optional.ofNullable(unopenedConnection.getRemoteCondition())
                            .map(condition -> Future.<ProtonConnection>failedFuture(StatusCodeMapper.from(condition)))
                            .orElse(Future.failedFuture(t));
                });
    }
 
Example 3
Source File: AmqpServiceBase.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 4
Source File: AmqpServiceBase.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Sets default handlers on a connection that has been opened
 * by a peer.
 * <p>
 * This method registers the following handlers
 * <ul>
 * <li>sessionOpenHandler - {@link #handleSessionOpen(ProtonConnection, ProtonSession)}</li>
 * <li>receiverOpenHandler - {@link #handleReceiverOpen(ProtonConnection, ProtonReceiver)}</li>
 * <li>senderOpenHandler - {@link #handleSenderOpen(ProtonConnection, ProtonSender)}</li>
 * <li>disconnectHandler - {@link #handleRemoteDisconnect(ProtonConnection)}</li>
 * <li>closeHandler - {@link #handleRemoteConnectionClose(ProtonConnection, AsyncResult)}</li>
 * <li>openHandler - {@link #processRemoteOpen(ProtonConnection)}</li>
 * </ul>
 * <p>
 * Subclasses should override this method in order to register service
 * specific handlers and/or to prevent registration of default handlers.
 *
 * @param connection The connection.
 */
protected void setRemoteConnectionOpenHandler(final ProtonConnection connection) {

    log.debug("received connection request from client");
    connection.sessionOpenHandler(session -> {
        HonoProtonHelper.setDefaultCloseHandler(session);
        handleSessionOpen(connection, session);
    });
    connection.receiverOpenHandler(receiver -> {
        HonoProtonHelper.setDefaultCloseHandler(receiver);
        handleReceiverOpen(connection, receiver);
    });
    connection.senderOpenHandler(sender -> {
        HonoProtonHelper.setDefaultCloseHandler(sender);
        handleSenderOpen(connection, sender);
    });
    connection.disconnectHandler(this::handleRemoteDisconnect);
    connection.closeHandler(remoteClose -> handleRemoteConnectionClose(connection, remoteClose));
    connection.openHandler(remoteOpen -> {
        if (remoteOpen.failed()) {
            log.debug("ignoring peer's open frame containing error", remoteOpen.cause());
        } else {
            processRemoteOpen(remoteOpen.result());
        }
    });
}
 
Example 5
Source File: ConnectionFactoryImpl.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
private void handleTimedOutOpenHandlerResult(final AsyncResult<ProtonConnection> openConnectionResult,
        final ProtonConnection downstreamConnection, final ProtonClientOptions clientOptions) {
    if (openConnectionResult.succeeded()) {
        logger.debug("ignoring received open frame from container [{}] at [{}://{}:{}, role: {}]: connection attempt already timed out",
                downstreamConnection.getRemoteContainer(),
                clientOptions.isSsl() ? "amqps" : "amqp",
                config.getHost(),
                config.getPort(),
                config.getServerRole());
        // close the connection again
        downstreamConnection.closeHandler(null);
        downstreamConnection.disconnectHandler(null);
        downstreamConnection.close();
    } else {
        final ErrorCondition error = downstreamConnection.getRemoteCondition();
        if (error == null) {
            logger.warn("ignoring failure to open connection to container [{}] at [{}://{}:{}, role: {}]: attempt already timed out",
                    downstreamConnection.getRemoteContainer(),
                    clientOptions.isSsl() ? "amqps" : "amqp",
                    config.getHost(),
                    config.getPort(),
                    config.getServerRole(),
                    openConnectionResult.cause());
        } else {
            logger.warn("ignoring failure to open connection to container [{}] at [{}://{}:{}, role: {}]: attempt already timed out; error: {} -{}",
                    downstreamConnection.getRemoteContainer(),
                    clientOptions.isSsl() ? "amqps" : "amqp",
                    config.getHost(),
                    config.getPort(),
                    config.getServerRole(),
                    error.getCondition(),
                    error.getDescription());
        }
    }
}
 
Example 6
Source File: VertxBasedAmqpProtocolAdapter.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Handles a remote peer's request to open a connection.
 *
 * @param con The connection to be opened.
 */
protected void onConnectRequest(final ProtonConnection con) {

    con.disconnectHandler(lostConnection -> {
        log.debug("lost connection to device [container: {}]", con.getRemoteContainer());
        onConnectionLoss(con);
        decrementConnectionCount(con);
    });
    con.closeHandler(remoteClose -> {
        handleRemoteConnectionClose(con, remoteClose);
        onConnectionLoss(con);
        decrementConnectionCount(con);
    });

    // when a begin frame is received
    con.sessionOpenHandler(session -> {
        HonoProtonHelper.setDefaultCloseHandler(session);
        handleSessionOpen(con, session);
    });
    // when the device wants to open a link for
    // uploading messages
    con.receiverOpenHandler(receiver -> {
        HonoProtonHelper.setDefaultCloseHandler(receiver);
        receiver.setMaxMessageSize(UnsignedLong.valueOf(getConfig().getMaxPayloadSize()));
        handleRemoteReceiverOpen(con, receiver);
    });
    // when the device wants to open a link for
    // receiving commands
    con.senderOpenHandler(sender -> {
        handleRemoteSenderOpenForCommands(con, sender);
    });
    con.openHandler(remoteOpen -> {
        final Device authenticatedDevice = getAuthenticatedDevice(con);
        if (authenticatedDevice == null) {
            metrics.incrementUnauthenticatedConnections();
        } else {
            metrics.incrementConnections(authenticatedDevice.getTenantId());
        }

        if (remoteOpen.failed()) {
            log.debug("ignoring device's open frame containing error", remoteOpen.cause());
        } else {
            processRemoteOpen(remoteOpen.result());
        }
    });
}