Search in sources :

Example 16 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project cassandra by apache.

the class MessagingService method getServerSockets.

@SuppressWarnings("resource")
private List<ServerSocket> getServerSockets(InetAddress localEp) throws ConfigurationException {
    final List<ServerSocket> ss = new ArrayList<ServerSocket>(2);
    if (DatabaseDescriptor.getServerEncryptionOptions().internode_encryption != ServerEncryptionOptions.InternodeEncryption.none) {
        try {
            ss.add(SSLFactory.getServerSocket(DatabaseDescriptor.getServerEncryptionOptions(), localEp, DatabaseDescriptor.getSSLStoragePort()));
        } catch (IOException e) {
            throw new ConfigurationException("Unable to create ssl socket", e);
        }
        // setReuseAddress happens in the factory.
        logger.info("Starting Encrypted Messaging Service on SSL port {}", DatabaseDescriptor.getSSLStoragePort());
    }
    if (DatabaseDescriptor.getServerEncryptionOptions().internode_encryption != ServerEncryptionOptions.InternodeEncryption.all) {
        ServerSocketChannel serverChannel = null;
        try {
            serverChannel = ServerSocketChannel.open();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        ServerSocket socket = serverChannel.socket();
        try {
            socket.setReuseAddress(true);
        } catch (SocketException e) {
            FileUtils.closeQuietly(socket);
            throw new ConfigurationException("Insufficient permissions to setReuseAddress", e);
        }
        InetSocketAddress address = new InetSocketAddress(localEp, DatabaseDescriptor.getStoragePort());
        try {
            socket.bind(address, 500);
        } catch (BindException e) {
            FileUtils.closeQuietly(socket);
            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 new RuntimeException(e);
        } catch (IOException e) {
            FileUtils.closeQuietly(socket);
            throw new RuntimeException(e);
        }
        String nic = FBUtilities.getNetworkInterface(localEp);
        logger.info("Starting Messaging Service on {}:{}{}", localEp, DatabaseDescriptor.getStoragePort(), nic == null ? "" : String.format(" (%s)", nic));
        ss.add(socket);
    }
    return ss;
}
Also used : ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 17 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project hadoop by apache.

the class DataNode method startInfoServer.

/**
   * @see DFSUtil#getHttpPolicy(org.apache.hadoop.conf.Configuration)
   * for information related to the different configuration options and
   * Http Policy is decided.
   */
private void startInfoServer() throws IOException {
    // SecureDataNodeStarter will bind the privileged port to the channel if
    // the DN is started by JSVC, pass it along.
    ServerSocketChannel httpServerChannel = secureResources != null ? secureResources.getHttpServerChannel() : null;
    httpServer = new DatanodeHttpServer(getConf(), this, httpServerChannel);
    httpServer.start();
    if (httpServer.getHttpAddress() != null) {
        infoPort = httpServer.getHttpAddress().getPort();
    }
    if (httpServer.getHttpsAddress() != null) {
        infoSecurePort = httpServer.getHttpsAddress().getPort();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) DatanodeHttpServer(org.apache.hadoop.hdfs.server.datanode.web.DatanodeHttpServer)

Example 18 with ServerSocketChannel

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

the class SocketChannelTest method test_socketChannel_read_ByteBufferII_remoteClosed.

/**
     * @tests SocketChannel#read(ByteBuffer[], int, int) when remote server
     *        closed
     */
public void test_socketChannel_read_ByteBufferII_remoteClosed() throws Exception {
    // regression 1 for HARMONY-549
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    ssc.accept().close();
    ByteBuffer[] buf = { ByteBuffer.allocate(10) };
    assertEquals(-1, sc.read(buf, 0, 1));
    ssc.close();
    sc.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 19 with ServerSocketChannel

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

the class SocketChannelTest method test_write$LByteBuffer_invalid.

/**
     * @tests java.nio.channels.SocketChannel#write(ByteBuffer[])
     */
public void test_write$LByteBuffer_invalid() throws IOException {
    // Set-up
    ServerSocketChannel server = ServerSocketChannel.open();
    server.socket().bind(null);
    SocketChannel client = SocketChannel.open();
    client.connect(server.socket().getLocalSocketAddress());
    SocketChannel worker = server.accept();
    // Do some stuff
    try {
        client.write((ByteBuffer[]) null);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        client.write((ByteBuffer[]) null, 0, 0);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        client.write((ByteBuffer[]) null, 1, 0);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        client.write((ByteBuffer[]) null, 0, 1);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        client.write((ByteBuffer[]) null, 1, 1);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    ByteBuffer[] buffers = new ByteBuffer[1];
    buffers[0] = ByteBuffer.wrap("Hello ".getBytes("UTF-8"));
    try {
        client.write(buffers, -1, 1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        client.write(buffers, 0, -1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        client.write(buffers, 0, 2);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        client.write(buffers, 2, 0);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        client.write(null, 0, 0);
        fail();
    } catch (NullPointerException expected) {
    }
    // Tidy-up
    worker.close();
    client.close();
    server.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 20 with ServerSocketChannel

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

the class SocketChannelTest method test_socketChannel_write_ByteBuffer_posNotZero.

/**
     * @tests SocketChannel#write(ByteBuffer) if position is not zero
     */
public void test_socketChannel_write_ByteBuffer_posNotZero() throws Exception {
    // regression 5 for HARMONY-549
    final String testStr = "Hello World";
    ByteBuffer readBuf = ByteBuffer.allocate(11);
    ByteBuffer buf = ByteBuffer.wrap(testStr.getBytes());
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    buf.position(2);
    ssc.accept().write(buf);
    assertEquals(9, sc.read(readBuf));
    buf.flip();
    readBuf.flip();
    byte[] read = new byte[9];
    byte[] write = new byte[11];
    buf.get(write);
    readBuf.get(read);
    for (int i = 0; i < 9; i++) {
        assertEquals(read[i], write[i + 2]);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)274 SocketChannel (java.nio.channels.SocketChannel)156 InetSocketAddress (java.net.InetSocketAddress)128 IOException (java.io.IOException)96 ByteBuffer (java.nio.ByteBuffer)53 SelectionKey (java.nio.channels.SelectionKey)41 ServerSocket (java.net.ServerSocket)37 Test (org.junit.Test)27 Socket (java.net.Socket)24 ClosedChannelException (java.nio.channels.ClosedChannelException)22 Selector (java.nio.channels.Selector)22 ClosedSelectorException (java.nio.channels.ClosedSelectorException)15 InetAddress (java.net.InetAddress)14 CancelledKeyException (java.nio.channels.CancelledKeyException)11 EOFException (java.io.EOFException)8 SocketException (java.net.SocketException)8 BindException (java.net.BindException)7 SocketAddress (java.net.SocketAddress)7 Test (org.junit.jupiter.api.Test)7 OutgoingPacket (com.twitter.heron.common.network.OutgoingPacket)6