Search in sources :

Example 1 with IoFuture

use of org.xnio.IoFuture in project undertow by undertow-io.

the class ServerWebSocketContainer method connectToServer.

public Session connectToServer(final Endpoint endpointInstance, final ClientEndpointConfig config, WebSocketClient.ConnectionBuilder connectionBuilder) throws DeploymentException, IOException {
    if (closed) {
        throw new ClosedChannelException();
    }
    ClientEndpointConfig cec = config != null ? config : ClientEndpointConfig.Builder.create().build();
    WebSocketClientNegotiation clientNegotiation = connectionBuilder.getClientNegotiation();
    IoFuture<WebSocketChannel> session = connectionBuilder.connect();
    Number timeout = (Number) cec.getUserProperties().get(TIMEOUT);
    if (session.await(timeout == null ? DEFAULT_WEB_SOCKET_TIMEOUT_SECONDS : timeout.intValue(), TimeUnit.SECONDS) == IoFuture.Status.WAITING) {
        // add a notifier to close the channel if the connection actually completes
        session.cancel();
        session.addNotifier(new IoFuture.HandlingNotifier<WebSocketChannel, Object>() {

            @Override
            public void handleDone(WebSocketChannel data, Object attachment) {
                IoUtils.safeClose(data);
            }
        }, null);
        throw JsrWebSocketMessages.MESSAGES.connectionTimedOut();
    }
    WebSocketChannel channel;
    try {
        channel = session.get();
    } catch (UpgradeFailedException e) {
        throw new DeploymentException(e.getMessage(), e);
    }
    EndpointSessionHandler sessionHandler = new EndpointSessionHandler(this);
    final List<Extension> extensions = new ArrayList<>();
    final Map<String, Extension> extMap = new HashMap<>();
    for (Extension ext : cec.getExtensions()) {
        extMap.put(ext.getName(), ext);
    }
    for (WebSocketExtension e : clientNegotiation.getSelectedExtensions()) {
        Extension ext = extMap.get(e.getName());
        if (ext == null) {
            throw JsrWebSocketMessages.MESSAGES.extensionWasNotPresentInClientHandshake(e.getName(), clientNegotiation.getSupportedExtensions());
        }
        extensions.add(ExtensionImpl.create(e));
    }
    ConfiguredClientEndpoint configured = clientEndpoints.get(endpointInstance.getClass());
    Endpoint instance = endpointInstance;
    if (configured == null) {
        synchronized (clientEndpoints) {
            // make sure to create an instance of AnnotatedEndpoint if we have the annotation
            configured = getClientEndpoint(endpointInstance.getClass(), false);
            if (configured == null) {
                // if we don't, add an endpoint anyway to the list of clientEndpoints
                clientEndpoints.put(endpointInstance.getClass(), configured = new ConfiguredClientEndpoint());
            } else {
                // use the  factory in configured to reach the endpoint
                instance = configured.getFactory().createInstance(new ImmediateInstanceHandle<>(endpointInstance));
            }
        }
    }
    EncodingFactory encodingFactory = EncodingFactory.createFactory(classIntrospecter, cec.getDecoders(), cec.getEncoders());
    UndertowSession undertowSession = new UndertowSession(channel, connectionBuilder.getUri(), Collections.<String, String>emptyMap(), Collections.<String, List<String>>emptyMap(), sessionHandler, null, new ImmediateInstanceHandle<>(endpointInstance), cec, connectionBuilder.getUri().getQuery(), encodingFactory.createEncoding(cec), configured, clientNegotiation.getSelectedSubProtocol(), extensions, connectionBuilder);
    instance.onOpen(undertowSession, cec);
    channel.resumeReceives();
    return undertowSession;
}
Also used : HashMap(java.util.HashMap) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) ArrayList(java.util.ArrayList) IoFuture(org.xnio.IoFuture) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint) UpgradeFailedException(org.xnio.http.UpgradeFailedException) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClosedChannelException(java.nio.channels.ClosedChannelException) WebSocketExtension(io.undertow.websockets.WebSocketExtension) WebSocketExtension(io.undertow.websockets.WebSocketExtension) Extension(javax.websocket.Extension) ImmediateInstanceHandle(io.undertow.servlet.util.ImmediateInstanceHandle) DeploymentException(javax.websocket.DeploymentException)

Example 2 with IoFuture

use of org.xnio.IoFuture in project undertow by undertow-io.

the class ServerWebSocketContainer method connectToServerInternal.

private Session connectToServerInternal(final Endpoint endpointInstance, final ConfiguredClientEndpoint cec, WebSocketClient.ConnectionBuilder connectionBuilder) throws DeploymentException, IOException {
    IoFuture<WebSocketChannel> session = connectionBuilder.connect();
    Number timeout = (Number) cec.getConfig().getUserProperties().get(TIMEOUT);
    IoFuture.Status result = session.await(timeout == null ? DEFAULT_WEB_SOCKET_TIMEOUT_SECONDS : timeout.intValue(), TimeUnit.SECONDS);
    if (result == IoFuture.Status.WAITING) {
        // add a notifier to close the channel if the connection actually completes
        session.cancel();
        session.addNotifier(new IoFuture.HandlingNotifier<WebSocketChannel, Object>() {

            @Override
            public void handleDone(WebSocketChannel data, Object attachment) {
                IoUtils.safeClose(data);
            }
        }, null);
        throw JsrWebSocketMessages.MESSAGES.connectionTimedOut();
    }
    WebSocketChannel channel;
    try {
        channel = session.get();
    } catch (UpgradeFailedException e) {
        throw new DeploymentException(e.getMessage(), e);
    }
    EndpointSessionHandler sessionHandler = new EndpointSessionHandler(this);
    final List<Extension> extensions = new ArrayList<>();
    final Map<String, Extension> extMap = new HashMap<>();
    for (Extension ext : cec.getConfig().getExtensions()) {
        extMap.put(ext.getName(), ext);
    }
    String subProtocol = null;
    if (connectionBuilder.getClientNegotiation() != null) {
        for (WebSocketExtension e : connectionBuilder.getClientNegotiation().getSelectedExtensions()) {
            Extension ext = extMap.get(e.getName());
            if (ext == null) {
                throw JsrWebSocketMessages.MESSAGES.extensionWasNotPresentInClientHandshake(e.getName(), connectionBuilder.getClientNegotiation().getSupportedExtensions());
            }
            extensions.add(ExtensionImpl.create(e));
        }
        subProtocol = connectionBuilder.getClientNegotiation().getSelectedSubProtocol();
    }
    UndertowSession undertowSession = new UndertowSession(channel, connectionBuilder.getUri(), Collections.<String, String>emptyMap(), Collections.<String, List<String>>emptyMap(), sessionHandler, null, new ImmediateInstanceHandle<>(endpointInstance), cec.getConfig(), connectionBuilder.getUri().getQuery(), cec.getEncodingFactory().createEncoding(cec.getConfig()), cec, subProtocol, extensions, connectionBuilder);
    endpointInstance.onOpen(undertowSession, cec.getConfig());
    channel.resumeReceives();
    return undertowSession;
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) HashMap(java.util.HashMap) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) ArrayList(java.util.ArrayList) IoFuture(org.xnio.IoFuture) WebSocketExtension(io.undertow.websockets.WebSocketExtension) Extension(javax.websocket.Extension) UpgradeFailedException(org.xnio.http.UpgradeFailedException) DeploymentException(javax.websocket.DeploymentException)

Example 3 with IoFuture

use of org.xnio.IoFuture in project undertow by undertow-io.

the class ConnectHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (exchange.getRequestMethod().equals(Methods.CONNECT)) {
        if (!allowed.resolve(exchange)) {
            // not sure if this is the best response
            exchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
            return;
        }
        String[] parts = exchange.getRequestPath().split(":");
        if (parts.length != 2) {
            // not sure if this is the best response
            exchange.setStatusCode(StatusCodes.BAD_REQUEST);
            return;
        }
        final String host = parts[0];
        final Integer port = Integer.parseInt(parts[1]);
        exchange.dispatch(SameThreadExecutor.INSTANCE, new Runnable() {

            @Override
            public void run() {
                exchange.getConnection().getIoThread().openStreamConnection(new InetSocketAddress(host, port), new ChannelListener<StreamConnection>() {

                    @Override
                    public void handleEvent(final StreamConnection clientChannel) {
                        exchange.acceptConnectRequest(new HttpUpgradeListener() {

                            @Override
                            public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
                                final ClosingExceptionHandler handler = new ClosingExceptionHandler(streamConnection, clientChannel);
                                Transfer.initiateTransfer(clientChannel.getSourceChannel(), streamConnection.getSinkChannel(), ChannelListeners.closingChannelListener(), ChannelListeners.writeShutdownChannelListener(ChannelListeners.<StreamSinkChannel>flushingChannelListener(ChannelListeners.closingChannelListener(), ChannelListeners.closingChannelExceptionHandler()), ChannelListeners.closingChannelExceptionHandler()), handler, handler, exchange.getConnection().getByteBufferPool());
                                Transfer.initiateTransfer(streamConnection.getSourceChannel(), clientChannel.getSinkChannel(), ChannelListeners.closingChannelListener(), ChannelListeners.writeShutdownChannelListener(ChannelListeners.<StreamSinkChannel>flushingChannelListener(ChannelListeners.closingChannelListener(), ChannelListeners.closingChannelExceptionHandler()), ChannelListeners.closingChannelExceptionHandler()), handler, handler, exchange.getConnection().getByteBufferPool());
                            }
                        });
                        exchange.setStatusCode(200);
                        exchange.endExchange();
                    }
                }, OptionMap.create(Options.TCP_NODELAY, true)).addNotifier(new IoFuture.Notifier<StreamConnection, Object>() {

                    @Override
                    public void notify(IoFuture<? extends StreamConnection> ioFuture, Object attachment) {
                        if (ioFuture.getStatus() == IoFuture.Status.FAILED) {
                            exchange.setStatusCode(503);
                            exchange.endExchange();
                        }
                    }
                }, null);
            }
        });
    } else {
        next.handleRequest(exchange);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) IoFuture(org.xnio.IoFuture) StreamConnection(org.xnio.StreamConnection) HttpServerExchange(io.undertow.server.HttpServerExchange) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener)

Example 4 with IoFuture

use of org.xnio.IoFuture in project undertow by undertow-io.

the class UndertowXnioSsl method connectSsl.

@SuppressWarnings("deprecation")
public IoFuture<ConnectedSslStreamChannel> connectSsl(final XnioWorker worker, final InetSocketAddress bindAddress, final InetSocketAddress destination, final ChannelListener<? super ConnectedSslStreamChannel> openListener, final ChannelListener<? super BoundChannel> bindListener, final OptionMap optionMap) {
    final FutureResult<ConnectedSslStreamChannel> futureResult = new FutureResult<>(IoUtils.directExecutor());
    final IoFuture<SslConnection> futureSslConnection = openSslConnection(worker, bindAddress, destination, new ChannelListener<SslConnection>() {

        public void handleEvent(final SslConnection sslConnection) {
            final ConnectedSslStreamChannel assembledChannel = new AssembledConnectedSslStreamChannel(sslConnection, sslConnection.getSourceChannel(), sslConnection.getSinkChannel());
            if (!futureResult.setResult(assembledChannel)) {
                safeClose(assembledChannel);
            } else {
                ChannelListeners.invokeChannelListener(assembledChannel, openListener);
            }
        }
    }, bindListener, optionMap).addNotifier(new IoFuture.HandlingNotifier<SslConnection, FutureResult<ConnectedSslStreamChannel>>() {

        public void handleCancelled(final FutureResult<ConnectedSslStreamChannel> result) {
            result.setCancelled();
        }

        public void handleFailed(final IOException exception, final FutureResult<ConnectedSslStreamChannel> result) {
            result.setException(exception);
        }
    }, futureResult);
    futureResult.getIoFuture().addNotifier(new IoFuture.HandlingNotifier<ConnectedStreamChannel, IoFuture<SslConnection>>() {

        public void handleCancelled(final IoFuture<SslConnection> result) {
            result.cancel();
        }
    }, futureSslConnection);
    futureResult.addCancelHandler(futureSslConnection);
    return futureResult.getIoFuture();
}
Also used : SslConnection(org.xnio.ssl.SslConnection) ConnectedStreamChannel(org.xnio.channels.ConnectedStreamChannel) ChannelListener(org.xnio.ChannelListener) FutureResult(org.xnio.FutureResult) IoFuture(org.xnio.IoFuture) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel) IOException(java.io.IOException) ConnectedSslStreamChannel(org.xnio.channels.ConnectedSslStreamChannel) AssembledConnectedSslStreamChannel(org.xnio.channels.AssembledConnectedSslStreamChannel)

Aggregations

IoFuture (org.xnio.IoFuture)4 WebSocketExtension (io.undertow.websockets.WebSocketExtension)2 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DeploymentException (javax.websocket.DeploymentException)2 Extension (javax.websocket.Extension)2 UpgradeFailedException (org.xnio.http.UpgradeFailedException)2 HttpServerExchange (io.undertow.server.HttpServerExchange)1 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)1 ImmediateInstanceHandle (io.undertow.servlet.util.ImmediateInstanceHandle)1 WebSocketClientNegotiation (io.undertow.websockets.client.WebSocketClientNegotiation)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ClientEndpoint (javax.websocket.ClientEndpoint)1 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)1 Endpoint (javax.websocket.Endpoint)1 ServerEndpoint (javax.websocket.server.ServerEndpoint)1 ChannelListener (org.xnio.ChannelListener)1