Search in sources :

Example 46 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel 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)

Example 47 with ServerSocketChannel

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

the class HeronServerTest method testClose.

/**
   * Method: stop()
   */
@Test
public void testClose() throws Exception {
    runBase();
    heronServer.stop();
    Map<SocketChannel, SocketChannelHelper> activeConnections = heronServer.getActiveConnections();
    ServerSocketChannel acceptChannel = heronServer.getAcceptChannel();
    Assert.assertNotNull(acceptChannel);
    Assert.assertTrue(!acceptChannel.isOpen());
    Assert.assertNotNull(activeConnections);
    Assert.assertEquals(0, activeConnections.size());
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Test(org.junit.Test)

Example 48 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project voldemort by voldemort.

the class ServerTestUtilsTest method testFindFreePort.

// @Test
public void testFindFreePort() throws Exception {
    ServerSocketChannel serverSocketChannel;
    try {
        serverSocketChannel = ServerSocketChannel.open();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        assertTrue(false);
        return;
    }
    int port = ServerTestUtils.findFreePort();
    try {
        serverSocketChannel.socket().bind(new InetSocketAddress(port));
    } catch (IOException ioe) {
        ioe.printStackTrace();
        assertTrue(false);
        return;
    }
    assertTrue(true);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 49 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project eiger by wlloyd.

the class MessagingService method getServerSocket.

private List<ServerSocket> getServerSocket(InetAddress localEp) throws IOException, ConfigurationException {
    final List<ServerSocket> ss = new ArrayList<ServerSocket>();
    if (DatabaseDescriptor.getEncryptionOptions().internode_encryption != EncryptionOptions.InternodeEncryption.none) {
        ss.add(SSLFactory.getServerSocket(DatabaseDescriptor.getEncryptionOptions(), localEp, DatabaseDescriptor.getSSLStoragePort()));
        // setReuseAddress happens in the factory.
        logger_.info("Starting Encrypted Messaging Service on SSL port {}", DatabaseDescriptor.getSSLStoragePort());
    }
    ServerSocketChannel serverChannel = ServerSocketChannel.open();
    ServerSocket socket = serverChannel.socket();
    socket.setReuseAddress(true);
    InetSocketAddress address = new InetSocketAddress(localEp, DatabaseDescriptor.getStoragePort());
    try {
        socket.bind(address);
    } catch (BindException e) {
        if (e.getMessage().contains("in use"))
            throw new ConfigurationException(address + " is in use by another process.  Change listen_address:storage_port in cassandra.yaml to values that do not conflict with other services");
        else if (e.getMessage().contains("Cannot assign requested address"))
            throw new ConfigurationException("Unable to bind to address " + address + ". Set listen_address in cassandra.yaml to an interface you can bind to, e.g., your private IP address on EC2");
        else
            throw e;
    }
    logger_.info("Starting Messaging Service on port {}", DatabaseDescriptor.getStoragePort());
    ss.add(socket);
    return ss;
}
Also used : ConfigurationException(org.apache.cassandra.config.ConfigurationException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 50 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project zm-mailbox by Zimbra.

the class ZimbraSocketAcceptor method accept.

@Override
protected NioSession accept(IoProcessor<NioSession> processor, ServerSocketChannel handle) throws Exception {
    SelectionKey key = handle.keyFor(selector);
    if ((key == null) || (!key.isValid()) || (!key.isAcceptable())) {
        return null;
    }
    // accept the connection from the client
    SocketChannel ch = handle.accept();
    if (ch == null) {
        return null;
    }
    return new NioSocketSession(this, processor, ch);
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)122 SocketChannel (java.nio.channels.SocketChannel)65 InetSocketAddress (java.net.InetSocketAddress)55 IOException (java.io.IOException)43 ByteBuffer (java.nio.ByteBuffer)26 ServerSocket (java.net.ServerSocket)19 SelectionKey (java.nio.channels.SelectionKey)19 Selector (java.nio.channels.Selector)13 Socket (java.net.Socket)12 Test (org.junit.Test)11 ClosedChannelException (java.nio.channels.ClosedChannelException)10 InetAddress (java.net.InetAddress)9 ClosedSelectorException (java.nio.channels.ClosedSelectorException)8 SocketException (java.net.SocketException)6 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)5 URISyntaxException (java.net.URISyntaxException)4 CancelledKeyException (java.nio.channels.CancelledKeyException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4