Search in sources :

Example 61 with Selector

use of java.nio.channels.Selector in project cobar by alibaba.

the class NIOAcceptor method run.

@Override
public void run() {
    final Selector selector = this.selector;
    for (; ; ) {
        ++acceptCount;
        try {
            selector.select(1000L);
            Set<SelectionKey> keys = selector.selectedKeys();
            try {
                for (SelectionKey key : keys) {
                    if (key.isValid() && key.isAcceptable()) {
                        accept();
                    } else {
                        key.cancel();
                    }
                }
            } finally {
                keys.clear();
            }
        } catch (Throwable e) {
            LOGGER.warn(getName(), e);
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) Selector(java.nio.channels.Selector)

Example 62 with Selector

use of java.nio.channels.Selector in project cobar by alibaba.

the class NIOConnector method run.

@Override
public void run() {
    final Selector selector = this.selector;
    for (; ; ) {
        ++connectCount;
        try {
            selector.select(1000L);
            connect(selector);
            Set<SelectionKey> keys = selector.selectedKeys();
            try {
                for (SelectionKey key : keys) {
                    Object att = key.attachment();
                    if (att != null && key.isValid() && key.isConnectable()) {
                        finishConnect(key, att);
                    } else {
                        key.cancel();
                    }
                }
            } finally {
                keys.clear();
            }
        } catch (Throwable e) {
            LOGGER.warn(name, e);
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) Selector(java.nio.channels.Selector)

Example 63 with Selector

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

the class AbstractSelectableChannelTest method test_configureBlocking_Z_IllegalBlockingMode.

/**
     * @tests AbstractSelectableChannel#configureBlocking(boolean)
     */
public void test_configureBlocking_Z_IllegalBlockingMode() throws Exception {
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    SelectionKey acceptKey = sc.register(acceptSelector, SelectionKey.OP_READ, null);
    assertEquals(sc.keyFor(acceptSelector), acceptKey);
    SelectableChannel getChannel = sc.configureBlocking(false);
    assertEquals(getChannel, sc);
    try {
        sc.configureBlocking(true);
        fail("Should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) SelectableChannel(java.nio.channels.SelectableChannel) AbstractSelectableChannel(java.nio.channels.spi.AbstractSelectableChannel) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) Selector(java.nio.channels.Selector)

Example 64 with Selector

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

the class AbstractSelectableChannelTest method test_register_LSelectorILObject_IllegalArgument.

/**
     * @tests AbstractSelectableChannel#register(Selector, int, Object)
     */
public void test_register_LSelectorILObject_IllegalArgument() throws IOException {
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    assertTrue(acceptSelector.isOpen());
    MockSelectableChannel msc = new MockSelectableChannel(SelectorProvider.provider());
    msc.configureBlocking(false);
    // in nonblocking mode
    try {
        //different SelectionKey with validOps
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        msc.register(null, 0, null);
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    // in nonblocking mode, if selector closed
    acceptSelector.close();
    try {
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        msc.register(null, 0, null);
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        msc.register(acceptSelector, 0, null);
        fail("Should throw IllegalSelectorException");
    } catch (IllegalSelectorException e) {
    // expected
    }
    acceptSelector = SelectorProvider.provider().openSelector();
    // test in blocking mode
    msc.configureBlocking(true);
    try {
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        msc.register(null, 0, null);
        fail("Should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    acceptSelector.close();
    // in blocking mode, if selector closed
    try {
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        msc.register(null, 0, null);
        fail("Should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    // register with an object
    Object argObj = new Object();
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    try {
        sc.register(null, SelectionKey.OP_READ, argObj);
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    // if channel closed
    msc.close();
    try {
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw ClosedChannelException");
    } catch (ClosedChannelException e) {
    // expected
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) IllegalSelectorException(java.nio.channels.IllegalSelectorException) Selector(java.nio.channels.Selector)

Example 65 with Selector

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

the class AbstractSelectorTest method test_provider.

/**
     * @tests AbstractSelector#provider()
     */
public void test_provider() throws IOException {
    Selector mockSelector = new MockAbstractSelector(SelectorProvider.provider());
    assertTrue(mockSelector.isOpen());
    assertSame(SelectorProvider.provider(), mockSelector.provider());
    mockSelector = new MockAbstractSelector(null);
    assertNull(mockSelector.provider());
}
Also used : 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