Search in sources :

Example 96 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project undertow by undertow-io.

the class ServerWebSocketContainer method connectToServer.

@Override
public Session connectToServer(Class<?> aClass, URI uri) throws DeploymentException, IOException {
    if (closed) {
        throw new ClosedChannelException();
    }
    ConfiguredClientEndpoint config = getClientEndpoint(aClass, true);
    if (config == null) {
        throw JsrWebSocketMessages.MESSAGES.notAValidClientEndpointType(aClass);
    }
    try {
        AnnotatedEndpointFactory factory = config.getFactory();
        InstanceHandle<?> instance = config.getInstanceFactory().createInstance();
        XnioSsl ssl = null;
        for (WebsocketClientSslProvider provider : clientSslProviders) {
            ssl = provider.getSsl(xnioWorker, aClass, uri);
            if (ssl != null) {
                break;
            }
        }
        return connectToServerInternal(factory.createInstance(instance), ssl, config, uri);
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) AnnotatedEndpointFactory(io.undertow.websockets.jsr.annotated.AnnotatedEndpointFactory) XnioSsl(org.xnio.ssl.XnioSsl)

Example 97 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project undertow by undertow-io.

the class ServerWebSocketContainer method connectToServer.

public Session connectToServer(Class<?> aClass, WebSocketClient.ConnectionBuilder connectionBuilder) throws DeploymentException, IOException {
    if (closed) {
        throw new ClosedChannelException();
    }
    ConfiguredClientEndpoint config = getClientEndpoint(aClass, true);
    if (config == null) {
        throw JsrWebSocketMessages.MESSAGES.notAValidClientEndpointType(aClass);
    }
    try {
        AnnotatedEndpointFactory factory = config.getFactory();
        InstanceHandle<?> instance = config.getInstanceFactory().createInstance();
        return connectToServerInternal(factory.createInstance(instance), config, connectionBuilder);
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) AnnotatedEndpointFactory(io.undertow.websockets.jsr.annotated.AnnotatedEndpointFactory)

Example 98 with ClosedChannelException

use of java.nio.channels.ClosedChannelException 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());
    if (configured == null) {
        synchronized (clientEndpoints) {
            configured = clientEndpoints.get(endpointInstance.getClass());
            if (configured == null) {
                clientEndpoints.put(endpointInstance.getClass(), configured = new ConfiguredClientEndpoint());
            }
        }
    }
    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);
    endpointInstance.onOpen(undertowSession, cec);
    channel.resumeReceives();
    return undertowSession;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) 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) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) UpgradeFailedException(org.xnio.http.UpgradeFailedException) DeploymentException(javax.websocket.DeploymentException) ClientEndpointConfig(javax.websocket.ClientEndpointConfig)

Example 99 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project undertow by undertow-io.

the class ServerWebSocketContainer method connectToServer.

@Override
public Session connectToServer(final Endpoint endpointInstance, final ClientEndpointConfig config, final URI path) throws DeploymentException, IOException {
    if (closed) {
        throw new ClosedChannelException();
    }
    ClientEndpointConfig cec = config != null ? config : ClientEndpointConfig.Builder.create().build();
    XnioSsl ssl = null;
    for (WebsocketClientSslProvider provider : clientSslProviders) {
        ssl = provider.getSsl(xnioWorker, endpointInstance, cec, path);
        if (ssl != null) {
            break;
        }
    }
    //in theory we should not be able to connect until the deployment is complete, but the definition of when a deployment is complete is a bit nebulous.
    WebSocketClientNegotiation clientNegotiation = new ClientNegotiation(cec.getPreferredSubprotocols(), toExtensionList(cec.getExtensions()), cec);
    WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(xnioWorker, bufferPool, path).setSsl(ssl).setBindAddress(clientBindAddress).setClientNegotiation(clientNegotiation);
    return connectToServer(endpointInstance, config, connectionBuilder);
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) XnioSsl(org.xnio.ssl.XnioSsl) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) WebSocketClient(io.undertow.websockets.client.WebSocketClient) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation)

Example 100 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project heron by twitter.

the class ConnectTest method testStart.

/**
   * Test connection
   */
@Test
public void testStart() throws Exception {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
    SocketChannel socketChannel = null;
    try {
        runStreamManagerClient();
        socketChannel = serverSocketChannel.accept();
        configure(socketChannel);
        socketChannel.configureBlocking(false);
        close(serverSocketChannel);
        // Receive request
        IncomingPacket incomingPacket = new IncomingPacket();
        while (incomingPacket.readFromChannel(socketChannel) != 0) {
            // 1ms sleep to mitigate busy looping
            SysUtils.sleep(1);
        }
        // Send back response
        // Though we do not use typeName, we need to unpack it first,
        // since the order is required
        String typeName = incomingPacket.unpackString();
        REQID rid = incomingPacket.unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        for (int i = 0; i < Constants.RETRY_TIMES; i++) {
            InstanceControlMsg instanceControlMsg = inControlQueue.poll();
            if (instanceControlMsg != null) {
                nioLooper.exitLoop();
                threadsPool.shutdownNow();
                PhysicalPlanHelper physicalPlanHelper = instanceControlMsg.getNewPhysicalPlanHelper();
                Assert.assertEquals("test-bolt", physicalPlanHelper.getMyComponent());
                Assert.assertEquals(InetAddress.getLocalHost().getHostName(), physicalPlanHelper.getMyHostname());
                Assert.assertEquals(0, physicalPlanHelper.getMyInstanceIndex());
                Assert.assertEquals(1, physicalPlanHelper.getMyTaskId());
                break;
            } else {
                SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
            }
        }
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IncomingPacket(com.twitter.heron.common.network.IncomingPacket) InstanceControlMsg(com.twitter.heron.instance.InstanceControlMsg) PhysicalPlanHelper(com.twitter.heron.common.utils.misc.PhysicalPlanHelper) InetSocketAddress(java.net.InetSocketAddress) REQID(com.twitter.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(com.twitter.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Aggregations

ClosedChannelException (java.nio.channels.ClosedChannelException)211 ByteBuffer (java.nio.ByteBuffer)67 IOException (java.io.IOException)60 Test (org.junit.Test)23 InetSocketAddress (java.net.InetSocketAddress)19 SelectionKey (java.nio.channels.SelectionKey)18 SocketChannel (java.nio.channels.SocketChannel)15 ArrayList (java.util.ArrayList)13 NotYetConnectedException (java.nio.channels.NotYetConnectedException)11 InterruptedIOException (java.io.InterruptedIOException)10 CancelledKeyException (java.nio.channels.CancelledKeyException)10 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)9 File (java.io.File)9 ServerSocketChannel (java.nio.channels.ServerSocketChannel)9 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)8 FileChannel (java.nio.channels.FileChannel)8 ConnectException (java.net.ConnectException)7 FsVolumeReference (org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeReference)6 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)5 Command (com.cloud.agent.api.Command)5