Search in sources :

Example 1 with ProtonTransportOptions

use of io.vertx.proton.ProtonTransportOptions in project vertx-proton by vert-x3.

the class ProtonServerImpl method connectHandler.

@Override
public ProtonServerImpl connectHandler(Handler<ProtonConnection> handler) {
    this.handler = handler;
    server.connectHandler(netSocket -> {
        String hostname = null;
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
        // ignore
        }
        final ProtonConnectionImpl connection = new ProtonConnectionImpl(vertx, hostname);
        if (advertiseAnonymousRelayCapability) {
            connection.setOfferedCapabilities(new Symbol[] { ProtonConnectionImpl.ANONYMOUS_RELAY });
        }
        final ProtonSaslAuthenticator authenticator = authenticatorFactory.create();
        ProtonTransportOptions transportOptions = new ProtonTransportOptions();
        transportOptions.setHeartbeat(this.options.getHeartbeat());
        transportOptions.setMaxFrameSize(this.options.getMaxFrameSize());
        connection.bindServer(netSocket, new ProtonSaslAuthenticator() {

            @Override
            public void init(NetSocket socket, ProtonConnection protonConnection, Transport transport) {
                authenticator.init(socket, protonConnection, transport);
            }

            @Override
            public void process(Handler<Boolean> completionHandler) {
                final Context context = Vertx.currentContext();
                authenticator.process(complete -> {
                    final Context callbackContext = vertx.getOrCreateContext();
                    if (context != callbackContext) {
                        throw new IllegalStateException("Callback was not made on the original context");
                    }
                    if (complete) {
                        // The authenticator completed, now check success, do required post processing
                        if (succeeded()) {
                            handler.handle(connection);
                            connection.flush();
                        } else {
                            // auth failed, flush any pending data and disconnect client
                            connection.flush();
                            connection.disconnect();
                        }
                    }
                    completionHandler.handle(complete);
                });
            }

            @Override
            public boolean succeeded() {
                return authenticator.succeeded();
            }
        }, transportOptions);
    });
    return this;
}
Also used : NetSocket(io.vertx.core.net.NetSocket) Context(io.vertx.core.Context) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonServerOptions(io.vertx.proton.ProtonServerOptions) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) Context(io.vertx.core.Context) Future(io.vertx.core.Future) ProtonServer(io.vertx.proton.ProtonServer) ProtonTransportOptions(io.vertx.proton.ProtonTransportOptions) UnknownHostException(java.net.UnknownHostException) Transport(org.apache.qpid.proton.engine.Transport) InetAddress(java.net.InetAddress) NetServer(io.vertx.core.net.NetServer) Symbol(org.apache.qpid.proton.amqp.Symbol) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) UnknownHostException(java.net.UnknownHostException) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonTransportOptions(io.vertx.proton.ProtonTransportOptions) Transport(org.apache.qpid.proton.engine.Transport)

Example 2 with ProtonTransportOptions

use of io.vertx.proton.ProtonTransportOptions in project vertx-proton by vert-x3.

the class ProtonClientImpl method connectNetClient.

private void connectNetClient(NetClient netClient, String host, int port, String username, String password, ConnectCompletionHandler connectHandler, ProtonClientOptions options) {
    String serverName = options.getSniServerName() != null ? options.getSniServerName() : (options.getVirtualHost() != null ? options.getVirtualHost() : null);
    netClient.connect(port, host, serverName, res -> {
        if (res.succeeded()) {
            String virtualHost = options.getVirtualHost() != null ? options.getVirtualHost() : host;
            ProtonConnectionImpl conn = new ProtonConnectionImpl(vertx, virtualHost);
            conn.disconnectHandler(h -> {
                LOG.trace("Connection disconnected");
                if (!connectHandler.isComplete()) {
                    connectHandler.handle(Future.failedFuture(new VertxException("Disconnected")));
                }
            });
            ProtonSaslClientAuthenticatorImpl authenticator = new ProtonSaslClientAuthenticatorImpl(username, password, options.getEnabledSaslMechanisms(), connectHandler);
            ProtonTransportOptions transportOptions = new ProtonTransportOptions();
            transportOptions.setHeartbeat(options.getHeartbeat());
            transportOptions.setMaxFrameSize(options.getMaxFrameSize());
            conn.bindClient(netClient, res.result(), authenticator, transportOptions);
            // Need to flush here to get the SASL process going, or it will wait until calls on the connection are processed
            // later (e.g open()).
            conn.flush();
        } else {
            connectHandler.handle(Future.failedFuture(res.cause()));
        }
    });
}
Also used : VertxException(io.vertx.core.VertxException) ProtonTransportOptions(io.vertx.proton.ProtonTransportOptions)

Aggregations

ProtonTransportOptions (io.vertx.proton.ProtonTransportOptions)2 AsyncResult (io.vertx.core.AsyncResult)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 VertxException (io.vertx.core.VertxException)1 NetServer (io.vertx.core.net.NetServer)1 NetSocket (io.vertx.core.net.NetSocket)1 ProtonConnection (io.vertx.proton.ProtonConnection)1 ProtonServer (io.vertx.proton.ProtonServer)1 ProtonServerOptions (io.vertx.proton.ProtonServerOptions)1 ProtonSaslAuthenticator (io.vertx.proton.sasl.ProtonSaslAuthenticator)1 ProtonSaslAuthenticatorFactory (io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 Symbol (org.apache.qpid.proton.amqp.Symbol)1 Transport (org.apache.qpid.proton.engine.Transport)1