Search in sources :

Example 91 with SelectionKey

use of java.nio.channels.SelectionKey in project Aeron by real-logic.

the class DataTransportPoller method registerForRead.

public SelectionKey registerForRead(final ReceiveChannelEndpoint transport) {
    SelectionKey key = null;
    try {
        transports = ArrayUtil.add(transports, transport);
        key = transport.receiveDatagramChannel().register(selector, SelectionKey.OP_READ, transport);
    } catch (final ClosedChannelException ex) {
        LangUtil.rethrowUnchecked(ex);
    }
    return key;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 92 with SelectionKey

use of java.nio.channels.SelectionKey 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 93 with SelectionKey

use of java.nio.channels.SelectionKey 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 94 with SelectionKey

use of java.nio.channels.SelectionKey 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 95 with SelectionKey

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

SelectionKey (java.nio.channels.SelectionKey)202 IOException (java.io.IOException)93 SocketChannel (java.nio.channels.SocketChannel)59 Selector (java.nio.channels.Selector)44 ServerSocketChannel (java.nio.channels.ServerSocketChannel)41 ClosedChannelException (java.nio.channels.ClosedChannelException)30 InetSocketAddress (java.net.InetSocketAddress)27 CancelledKeyException (java.nio.channels.CancelledKeyException)25 ByteBuffer (java.nio.ByteBuffer)24 ClosedSelectorException (java.nio.channels.ClosedSelectorException)17 SelectableChannel (java.nio.channels.SelectableChannel)13 Test (org.junit.Test)13 Iterator (java.util.Iterator)9 Socket (java.net.Socket)7 ArrayList (java.util.ArrayList)7 SocketTimeoutException (java.net.SocketTimeoutException)6 AbstractSelectionKey (java.nio.channels.spi.AbstractSelectionKey)6 EOFException (java.io.EOFException)5 DatagramChannel (java.nio.channels.DatagramChannel)5 HashSet (java.util.HashSet)5