Search in sources :

Example 76 with ServerSocketChannel

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

the class SecureDataNodeStarter method getSecureResources.

/**
   * Acquire privileged resources (i.e., the privileged ports) for the data
   * node. The privileged resources consist of the port of the RPC server and
   * the port of HTTP (not HTTPS) server.
   */
@VisibleForTesting
public static SecureResources getSecureResources(Configuration conf) throws Exception {
    HttpConfig.Policy policy = DFSUtil.getHttpPolicy(conf);
    boolean isSecure = UserGroupInformation.isSecurityEnabled();
    // Obtain secure port for data streaming to datanode
    InetSocketAddress streamingAddr = DataNode.getStreamingAddr(conf);
    int socketWriteTimeout = conf.getInt(DFSConfigKeys.DFS_DATANODE_SOCKET_WRITE_TIMEOUT_KEY, HdfsConstants.WRITE_TIMEOUT);
    int backlogLength = conf.getInt(CommonConfigurationKeysPublic.IPC_SERVER_LISTEN_QUEUE_SIZE_KEY, CommonConfigurationKeysPublic.IPC_SERVER_LISTEN_QUEUE_SIZE_DEFAULT);
    ServerSocket ss = (socketWriteTimeout > 0) ? ServerSocketChannel.open().socket() : new ServerSocket();
    ss.bind(streamingAddr, backlogLength);
    // Check that we got the port we need
    if (ss.getLocalPort() != streamingAddr.getPort()) {
        throw new RuntimeException("Unable to bind on specified streaming port in secure " + "context. Needed " + streamingAddr.getPort() + ", got " + ss.getLocalPort());
    }
    if (!SecurityUtil.isPrivilegedPort(ss.getLocalPort()) && isSecure) {
        throw new RuntimeException("Cannot start secure datanode with unprivileged RPC ports");
    }
    System.err.println("Opened streaming server at " + streamingAddr);
    // Bind a port for the web server. The code intends to bind HTTP server to
    // privileged port only, as the client can authenticate the server using
    // certificates if they are communicating through SSL.
    final ServerSocketChannel httpChannel;
    if (policy.isHttpEnabled()) {
        httpChannel = ServerSocketChannel.open();
        InetSocketAddress infoSocAddr = DataNode.getInfoAddr(conf);
        httpChannel.socket().bind(infoSocAddr);
        InetSocketAddress localAddr = (InetSocketAddress) httpChannel.socket().getLocalSocketAddress();
        if (localAddr.getPort() != infoSocAddr.getPort()) {
            throw new RuntimeException("Unable to bind on specified info port in secure " + "context. Needed " + streamingAddr.getPort() + ", got " + ss.getLocalPort());
        }
        System.err.println("Successfully obtained privileged resources (streaming port = " + ss + " ) (http listener port = " + localAddr.getPort() + ")");
        if (localAddr.getPort() > 1023 && isSecure) {
            throw new RuntimeException("Cannot start secure datanode with unprivileged HTTP ports");
        }
        System.err.println("Opened info server at " + infoSocAddr);
    } else {
        httpChannel = null;
    }
    return new SecureResources(ss, httpChannel);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpConfig(org.apache.hadoop.http.HttpConfig) ServerSocket(java.net.ServerSocket) ServerSocketChannel(java.nio.channels.ServerSocketChannel) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 77 with ServerSocketChannel

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

the class ScannerTest method test_Constructor_LReadableByteChannel.

public void test_Constructor_LReadableByteChannel() throws IOException {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(null);
    SocketChannel sc = SocketChannel.open();
    sc.connect(ssc.socket().getLocalSocketAddress());
    sc.configureBlocking(false);
    assertFalse(sc.isBlocking());
    ssc.accept().close();
    ssc.close();
    assertFalse(sc.isBlocking());
    Scanner s = new Scanner(sc);
    try {
        s.hasNextInt();
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
    sc.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Scanner(java.util.Scanner) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 78 with ServerSocketChannel

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

the class AbstractSelectorTest method test_register_LSelectorI_error.

/**
     * @tests AbstractSelector#register(Selector,int)
     */
public void test_register_LSelectorI_error() throws IOException {
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.configureBlocking(false);
    acceptSelector.close();
    assertFalse(acceptSelector.isOpen());
    try {
        ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    assertFalse(ssc.isRegistered());
    acceptSelector = Selector.open();
    ssc.configureBlocking(true);
    try {
        ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    assertFalse(ssc.isRegistered());
    ssc.configureBlocking(false);
    SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
    assertNotNull(acceptKey);
    assertTrue(acceptSelector.keys().contains(acceptKey));
    assertTrue(ssc.isRegistered());
}
Also used : SelectionKey(java.nio.channels.SelectionKey) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 79 with ServerSocketChannel

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

the class SocketChannelTest method test_write$LByteBuffer2.

/**
     * @tests java.nio.channels.SocketChannel#write(ByteBuffer[])
     */
public void test_write$LByteBuffer2() 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();
    // Test overlapping buffers
    byte[] data = "Hello world!".getBytes("UTF-8");
    ByteBuffer[] buffers = new ByteBuffer[3];
    buffers[0] = ByteBuffer.wrap(data, 0, 6);
    buffers[1] = ByteBuffer.wrap(data, 6, data.length - 6);
    buffers[2] = ByteBuffer.wrap(data);
    // Write them out, read what we wrote and check it
    client.write(buffers);
    client.close();
    ByteBuffer readBuffer = ByteBuffer.allocate(1024);
    while (EOF != worker.read(readBuffer)) {
    }
    ;
    readBuffer.flip();
    Buffer expected = ByteBuffer.allocate(1024).put(data).put(data).flip();
    assertEquals(expected, readBuffer);
    // Tidy-up
    worker.close();
    server.close();
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(java.nio.Buffer) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 80 with ServerSocketChannel

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

the class SocketChannelTest method test_socketChannel_write_close.

/**
     * @tests SocketChannel#write(ByteBuffer) after close
     */
public void test_socketChannel_write_close() throws Exception {
    // regression 4 for HARMONY-549
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    SocketChannel sock = ssc.accept();
    ByteBuffer buf = null;
    ssc.close();
    sc.close();
    try {
        sc.write(buf);
        fail("should throw NPE");
    } catch (NullPointerException e) {
    // expected
    }
    sock.close();
}
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)114 SocketChannel (java.nio.channels.SocketChannel)62 InetSocketAddress (java.net.InetSocketAddress)52 IOException (java.io.IOException)41 ByteBuffer (java.nio.ByteBuffer)26 SelectionKey (java.nio.channels.SelectionKey)19 ServerSocket (java.net.ServerSocket)18 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 CancelledKeyException (java.nio.channels.CancelledKeyException)5 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)5 URISyntaxException (java.net.URISyntaxException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4