Search in sources :

Example 11 with Selector

use of java.nio.channels.Selector in project jeromq by zeromq.

the class Poller method rebuildSelector.

private void rebuildSelector() {
    Selector newSelector;
    try {
        newSelector = Selector.open();
    } catch (IOException e) {
        throw new ZError.IOException(e);
    }
    try {
        selector.close();
    } catch (IOException e) {
    }
    selector = newSelector;
    for (PollSet pollSet : fdTable.values()) {
        pollSet.key = null;
    }
    retired.set(true);
}
Also used : IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Example 12 with Selector

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

the class UnixSelectorTest method testSelectUnConnectedChannel.

public void testSelectUnConnectedChannel() throws Exception {
    SocketChannel socketChannel2 = SocketChannel.open();
    socketChannel2.configureBlocking(false);
    Selector sel3 = Selector.open();
    SelectionKey mkey3 = socketChannel2.register(sel3, SelectionKey.OP_WRITE);
    // HUP is also treating as writable
    assertEquals(1, sel3.select(100));
    assertEquals(false, mkey3.isConnectable());
    // even the channel is not connected, the selector could be writable
    assertEquals(false, socketChannel2.isConnected());
    assertEquals(true, mkey3.isWritable());
    Selector sel4 = Selector.open();
    SelectionKey mkey4 = socketChannel2.register(sel4, SelectionKey.OP_CONNECT);
    assertEquals(1, sel4.select(100));
    assertEquals(false, mkey4.isWritable());
    assertEquals(true, mkey4.isConnectable());
    Selector sel5 = Selector.open();
    SelectionKey mkey5 = socketChannel2.register(sel5, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE);
    assertEquals(1, sel5.select(100));
    assertEquals(true, mkey5.isWritable());
    assertEquals(true, mkey5.isConnectable());
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SelectionKey(java.nio.channels.SelectionKey) Selector(java.nio.channels.Selector)

Example 13 with Selector

use of java.nio.channels.Selector 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 14 with Selector

use of java.nio.channels.Selector 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 15 with Selector

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

the class AbstractSelectorTest method test_register_LSelectorI.

/**
     * @tests AbstractSelector#register(Selector,int)
     */
public void test_register_LSelectorI() throws Exception {
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.configureBlocking(false);
    assertFalse(ssc.isRegistered());
    SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
    assertTrue(ssc.isRegistered());
    assertNotNull(acceptKey);
    assertTrue(acceptSelector.keys().contains(acceptKey));
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Aggregations

Selector (java.nio.channels.Selector)186 SelectionKey (java.nio.channels.SelectionKey)84 IOException (java.io.IOException)70 SocketChannel (java.nio.channels.SocketChannel)51 InetSocketAddress (java.net.InetSocketAddress)38 ByteBuffer (java.nio.ByteBuffer)28 ServerSocketChannel (java.nio.channels.ServerSocketChannel)27 Test (org.junit.Test)14 DatagramChannel (java.nio.channels.DatagramChannel)13 ClosedChannelException (java.nio.channels.ClosedChannelException)12 CancelledKeyException (java.nio.channels.CancelledKeyException)11 NioEndpoint (org.apache.tomcat.util.net.NioEndpoint)10 ClosedSelectorException (java.nio.channels.ClosedSelectorException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 EOFException (java.io.EOFException)7 ServerSocket (java.net.ServerSocket)7 SelectableChannel (java.nio.channels.SelectableChannel)7 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)5 Iterator (java.util.Iterator)5 MemberImpl (org.apache.catalina.tribes.membership.MemberImpl)5