Search in sources :

Example 66 with Selector

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

the class AbstractSelectorTest method test_isOpen.

/**
     * @tests AbstractSelector#isOpen()
     */
public void test_isOpen() throws Exception {
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    assertTrue(acceptSelector.isOpen());
    acceptSelector.close();
    assertFalse(acceptSelector.isOpen());
}
Also used : Selector(java.nio.channels.Selector)

Example 67 with Selector

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

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

the class SelectorTest method test_nonBlockingConnect.

public void test_nonBlockingConnect() throws IOException {
    SocketChannel channel = null;
    try {
        channel = SocketChannel.open();
        channel.configureBlocking(false);
        Selector selector = Selector.open();
        channel.register(selector, SelectionKey.OP_CONNECT);
        channel.connect(localAddress);
        channel.finishConnect();
        selector.select();
        assertEquals(0, selector.selectedKeys().size());
    } finally {
        channel.close();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) Selector(java.nio.channels.Selector)

Example 69 with Selector

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

the class UnixSelectorTest method testSelectorAcceptAndRead.

public void testSelectorAcceptAndRead() throws Exception {
    Selector sel0 = Selector.open();
    Selector sel1 = Selector.open();
    Server server = new Server();
    SelectionKey mkey0 = server.serverChannel.register(sel0, SelectionKey.OP_ACCEPT);
    server.serverChannel.register(sel1, SelectionKey.OP_ACCEPT);
    // HUP is treating as acceptable
    assertEquals(1, sel0.select(100));
    assertEquals(true, sel0.selectedKeys().contains(mkey0));
    server.initialize();
    // after bind can not accept
    assertEquals(0, sel1.select(100));
    server.accept();
    Thread.sleep(1000);
    SocketChannel socketChannel = SocketChannel.open();
    socketChannel.configureBlocking(false);
    Selector sel2 = Selector.open();
    socketChannel.register(sel2, SelectionKey.OP_WRITE);
    boolean isConnected = socketChannel.connect(server.socket.getLocalSocketAddress());
    if (!isConnected) {
        socketChannel.finishConnect();
    }
    assertEquals(true, socketChannel.isConnected());
    server.close();
    Thread.sleep(3000);
    assertEquals(true, socketChannel.isConnected());
    assertEquals(1, sel2.select(100));
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 70 with Selector

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

the class NioEventLoop method rebuildSelector0.

private void rebuildSelector0() {
    final Selector oldSelector = selector;
    final Selector newSelector;
    if (oldSelector == null) {
        return;
    }
    try {
        newSelector = openSelector();
    } catch (Exception e) {
        logger.warn("Failed to create a new Selector.", e);
        return;
    }
    // Register all channels to the new Selector.
    int nChannels = 0;
    for (SelectionKey key : oldSelector.keys()) {
        Object a = key.attachment();
        try {
            if (!key.isValid() || key.channel().keyFor(newSelector) != null) {
                continue;
            }
            int interestOps = key.interestOps();
            key.cancel();
            SelectionKey newKey = key.channel().register(newSelector, interestOps, a);
            if (a instanceof AbstractNioChannel) {
                // Update SelectionKey
                ((AbstractNioChannel) a).selectionKey = newKey;
            }
            nChannels++;
        } catch (Exception e) {
            logger.warn("Failed to re-register a Channel to the new Selector.", e);
            if (a instanceof AbstractNioChannel) {
                AbstractNioChannel ch = (AbstractNioChannel) a;
                ch.unsafe().close(ch.unsafe().voidPromise());
            } else {
                @SuppressWarnings("unchecked") NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
                invokeChannelUnregistered(task, key, e);
            }
        }
    }
    selector = newSelector;
    try {
        // time to close the old selector as everything else is registered to the new one
        oldSelector.close();
    } catch (Throwable t) {
        if (logger.isWarnEnabled()) {
            logger.warn("Failed to close the old Selector.", t);
        }
    }
    logger.info("Migrated " + nChannels + " channel(s) to the new Selector.");
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SelectableChannel(java.nio.channels.SelectableChannel) CancelledKeyException(java.nio.channels.CancelledKeyException) EventLoopException(io.netty.channel.EventLoopException) IOException(java.io.IOException) ChannelException(io.netty.channel.ChannelException) Selector(java.nio.channels.Selector)

Aggregations

Selector (java.nio.channels.Selector)85 SelectionKey (java.nio.channels.SelectionKey)43 IOException (java.io.IOException)26 SocketChannel (java.nio.channels.SocketChannel)24 InetSocketAddress (java.net.InetSocketAddress)17 ServerSocketChannel (java.nio.channels.ServerSocketChannel)17 ByteBuffer (java.nio.ByteBuffer)10 ClosedChannelException (java.nio.channels.ClosedChannelException)8 DatagramChannel (java.nio.channels.DatagramChannel)8 CancelledKeyException (java.nio.channels.CancelledKeyException)6 ClosedSelectorException (java.nio.channels.ClosedSelectorException)4 SelectableChannel (java.nio.channels.SelectableChannel)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Test (org.junit.Test)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)3 BigDecimal (java.math.BigDecimal)2 ConnectException (java.net.ConnectException)2 DatagramPacket (java.net.DatagramPacket)2 DatagramSocket (java.net.DatagramSocket)2 InetAddress (java.net.InetAddress)2