Search in sources :

Example 76 with SocketChannel

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

the class AbstractSelectableChannelTest method test_keyfor_LSelector.

/**
     * @tests AbstractSelectableChannel#keyFor(Selector)
     */
public void test_keyfor_LSelector() throws Exception {
    SocketChannel sc = SocketChannel.open();
    Object argObj = new Object();
    sc.configureBlocking(false);
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    Selector acceptSelectorOther = SelectorProvider.provider().openSelector();
    SelectionKey acceptKey = sc.register(acceptSelector, SelectionKey.OP_READ, argObj);
    assertEquals(sc.keyFor(acceptSelector), acceptKey);
    SelectionKey acceptKeyObjNull = sc.register(acceptSelector, SelectionKey.OP_READ, null);
    assertSame(sc.keyFor(acceptSelector), acceptKeyObjNull);
    assertSame(acceptKeyObjNull, acceptKey);
    SelectionKey acceptKeyOther = sc.register(acceptSelectorOther, SelectionKey.OP_READ, null);
    assertSame(sc.keyFor(acceptSelectorOther), acceptKeyOther);
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) Selector(java.nio.channels.Selector)

Example 77 with SocketChannel

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

the class AbstractSelectableChannelTest method test_register_LSelectorILObject.

/**
     * @tests AbstractSelectableChannel#register(Selector, int, Object)
     */
public void test_register_LSelectorILObject() throws IOException {
    assertFalse(testChannel.isRegistered());
    Selector acceptSelector1 = SelectorProvider.provider().openSelector();
    Selector acceptSelector2 = new MockAbstractSelector(SelectorProvider.provider());
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    SelectionKey acceptKey = sc.register(acceptSelector1, SelectionKey.OP_READ, null);
    assertNotNull(acceptKey);
    assertTrue(acceptKey.isValid());
    assertSame(sc, acceptKey.channel());
    //test that sc.register invokes Selector.register()
    acceptKey = sc.register(acceptSelector2, SelectionKey.OP_READ, null);
    assertNull(acceptKey);
    // Regression test to ensure acceptance of a selector with empty
    // interest set.
    SocketChannel channel = SocketChannel.open();
    channel.configureBlocking(false);
    Selector selector = Selector.open();
    channel.register(selector, 0);
    selector.close();
    channel.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) Selector(java.nio.channels.Selector)

Example 78 with SocketChannel

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

the class OldSocketTest method test_getOutputStream_shutdownOutput.

public void test_getOutputStream_shutdownOutput() throws Exception {
    // regression test for Harmony-873
    ServerSocket ss = new ServerSocket(0);
    Socket s = new Socket("127.0.0.1", ss.getLocalPort());
    ss.accept();
    s.shutdownOutput();
    try {
        s.getOutputStream();
        fail("should throw SocketException");
    } catch (IOException e) {
    // expected
    } finally {
        s.close();
    }
    SocketChannel channel = SocketChannel.open(new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort()));
    channel.configureBlocking(false);
    ss.accept();
    Socket socket = channel.socket();
    OutputStream out = null;
    try {
        out = socket.getOutputStream();
        out.write(1);
        fail("IllegalBlockingModeException was not thrown.");
    } catch (IllegalBlockingModeException ibme) {
    //expected
    } finally {
        if (out != null)
            out.close();
        socket.close();
        channel.close();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 79 with SocketChannel

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

the class SocketTest method checkSocketLocalAndRemoteAddresses.

public void checkSocketLocalAndRemoteAddresses(boolean setOptions) throws Exception {
    InetAddress host = InetAddress.getLocalHost();
    // Open a local server port.
    ServerSocketChannel ssc = ServerSocketChannel.open();
    InetSocketAddress listenAddr = new InetSocketAddress(host, 0);
    ssc.socket().bind(listenAddr, 0);
    ServerSocket ss = ssc.socket();
    // Open a socket to the local port.
    SocketChannel out = SocketChannel.open();
    out.configureBlocking(false);
    if (setOptions) {
        out.socket().setTcpNoDelay(false);
    }
    InetSocketAddress addr = new InetSocketAddress(host, ssc.socket().getLocalPort());
    out.connect(addr);
    while (!out.finishConnect()) {
        Thread.sleep(1);
    }
    SocketChannel in = ssc.accept();
    if (setOptions) {
        in.socket().setTcpNoDelay(false);
    }
    InetSocketAddress outRemoteAddress = (InetSocketAddress) out.socket().getRemoteSocketAddress();
    InetSocketAddress outLocalAddress = (InetSocketAddress) out.socket().getLocalSocketAddress();
    InetSocketAddress inLocalAddress = (InetSocketAddress) in.socket().getLocalSocketAddress();
    InetSocketAddress inRemoteAddress = (InetSocketAddress) in.socket().getRemoteSocketAddress();
    System.err.println("inLocalAddress: " + inLocalAddress);
    System.err.println("inRemoteAddress: " + inRemoteAddress);
    System.err.println("outLocalAddress: " + outLocalAddress);
    System.err.println("outRemoteAddress: " + outRemoteAddress);
    assertEquals(outRemoteAddress.getPort(), ss.getLocalPort());
    assertEquals(inLocalAddress.getPort(), ss.getLocalPort());
    assertEquals(inRemoteAddress.getPort(), outLocalAddress.getPort());
    assertEquals(inLocalAddress.getAddress(), ss.getInetAddress());
    assertEquals(inRemoteAddress.getAddress(), ss.getInetAddress());
    assertEquals(outLocalAddress.getAddress(), ss.getInetAddress());
    assertEquals(outRemoteAddress.getAddress(), ss.getInetAddress());
    in.close();
    out.close();
    ssc.close();
    assertNull(in.socket().getRemoteSocketAddress());
    assertNull(out.socket().getRemoteSocketAddress());
    assertEquals(in.socket().getLocalSocketAddress(), ss.getLocalSocketAddress());
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 80 with SocketChannel

use of java.nio.channels.SocketChannel in project blade by biezhi.

the class SelectorManager method chooseSelector.

private ManagedSelector chooseSelector(SelectableChannel channel) {
    // Ideally we would like to have all connections from the same client end
    // up on the same selector (to try to avoid smearing the data from a single
    // client over all cores), but because of proxies, the remote address may not
    // really be the client - so we have to hedge our bets to ensure that all
    // channels don't end up on the one selector for a proxy.
    ManagedSelector candidate1 = null;
    if (channel != null) {
        try {
            if (channel instanceof SocketChannel) {
                SocketAddress remote = ((SocketChannel) channel).getRemoteAddress();
                if (remote instanceof InetSocketAddress) {
                    byte[] addr = ((InetSocketAddress) remote).getAddress().getAddress();
                    if (addr != null) {
                        int s = addr[addr.length - 1] & 0xFF;
                        candidate1 = _selectors[s % getSelectorCount()];
                    }
                }
            }
        } catch (IOException x) {
            LOG.ignore(x);
        }
    }
    // The ++ increment here is not atomic, but it does not matter,
    // so long as the value changes sometimes, then connections will
    // be distributed over the available selectors.
    long s = _selectorIndex++;
    int index = (int) (s % getSelectorCount());
    ManagedSelector candidate2 = _selectors[index];
    if (candidate1 == null || candidate1.size() >= candidate2.size() * 2)
        return candidate2;
    return candidate1;
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

SocketChannel (java.nio.channels.SocketChannel)662 IOException (java.io.IOException)298 ServerSocketChannel (java.nio.channels.ServerSocketChannel)285 InetSocketAddress (java.net.InetSocketAddress)202 ByteBuffer (java.nio.ByteBuffer)163 SelectionKey (java.nio.channels.SelectionKey)105 Socket (java.net.Socket)87 Test (org.junit.Test)81 ClosedChannelException (java.nio.channels.ClosedChannelException)50 Selector (java.nio.channels.Selector)42 SocketAddress (java.net.SocketAddress)35 ServerSocket (java.net.ServerSocket)31 ClosedSelectorException (java.nio.channels.ClosedSelectorException)28 CancelledKeyException (java.nio.channels.CancelledKeyException)27 ConnectException (java.net.ConnectException)26 ArrayList (java.util.ArrayList)26 SocketTimeoutException (java.net.SocketTimeoutException)23 SelectableChannel (java.nio.channels.SelectableChannel)21 SocketException (java.net.SocketException)20 HashMap (java.util.HashMap)20