Search in sources :

Example 21 with Selector

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

the class SelectorTest method test_57456.

public void test_57456() throws Exception {
    Selector selector = Selector.open();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    try {
        // Connect.
        ssc.configureBlocking(false);
        ssc.socket().bind(null);
        SocketChannel sc = SocketChannel.open();
        sc.connect(ssc.socket().getLocalSocketAddress());
        sc.finishConnect();
        // Switch to non-blocking so we can use a Selector.
        sc.configureBlocking(false);
        // Have the 'server' write something.
        ssc.accept().write(ByteBuffer.allocate(128));
        // At this point, the client should be able to read or write immediately.
        // (It shouldn't be able to connect because it's already connected.)
        SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);
        assertEquals(1, selector.select());
        assertEquals(SelectionKey.OP_READ | SelectionKey.OP_WRITE, key.readyOps());
        assertEquals(0, selector.select());
    } finally {
        selector.close();
        ssc.close();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 22 with Selector

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

the class SelectorTest method testEINTR.

// http://b/6453247
// This test won't work on the host until/unless we start using libcorkscrew there.
// The runtime itself blocks SIGQUIT, so that doesn't cause poll(2) to EINTR directly.
// The EINTR is caused by the way libcorkscrew works.
public void testEINTR() throws Exception {
    Selector selector = Selector.open();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(2000);
                Libcore.os.kill(Libcore.os.getpid(), OsConstants.SIGQUIT);
            } catch (Exception ex) {
                fail();
            }
        }
    }).start();
    assertEquals(0, selector.select());
}
Also used : NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Example 23 with Selector

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

the class SocketChannelTest method test_56684.

public void test_56684() throws Exception {
    mockOs.enqueueFault("connect", ENETUNREACH);
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    Selector selector = Selector.open();
    SelectionKey selectionKey = sc.register(selector, SelectionKey.OP_CONNECT);
    try {
        sc.connect(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 }), 0));
        fail();
    } catch (ConnectException ex) {
    }
    try {
        sc.finishConnect();
    } catch (ClosedChannelException expected) {
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) ClosedChannelException(java.nio.channels.ClosedChannelException) InetSocketAddress(java.net.InetSocketAddress) Selector(java.nio.channels.Selector) ConnectException(java.net.ConnectException)

Example 24 with Selector

use of java.nio.channels.Selector in project java-in-action by xinghalo.

the class SelectorTest method main.

public static void main(String[] args) throws IOException {
    Selector selector = Selector.open();
    SocketChannel socketChannel = null;
    // selector时,channel需要时非阻塞的
    socketChannel.configureBlocking(false);
    SelectionKey selectionKey = socketChannel.register(selector, SelectionKey.OP_READ);
    // connect, accept, read, write
    // int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
    // 检测感兴趣的操作
    int interestSet = selectionKey.interestOps();
    boolean isInterestedInAccept = (interestSet & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT;
    boolean isInterestedInConnect = (interestSet & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT;
    boolean isInterestedInRead = (interestSet & SelectionKey.OP_READ) == SelectionKey.OP_READ;
    boolean isInterestedInWrite = (interestSet & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE;
    // 检测准备就绪的操作
    int readySet = selectionKey.readyOps();
    // 检测什么事件就绪
    selectionKey.isAcceptable();
    selectionKey.isConnectable();
    selectionKey.isReadable();
    selectionKey.isWritable();
    // 附加对象
    selectionKey.attach("a");
    String tag = (String) selectionKey.attachment();
    Set<SelectionKey> selectedKeys = selector.selectedKeys();
    Iterator keyIterator = selectedKeys.iterator();
    while (keyIterator.hasNext()) {
        SelectionKey key = (SelectionKey) keyIterator.next();
        if (key.isAcceptable()) {
        // 
        } else if (key.isConnectable()) {
        } else if (key.isReadable()) {
        } else if (key.isWritable()) {
        }
        keyIterator.remove();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) Iterator(java.util.Iterator) Selector(java.nio.channels.Selector)

Example 25 with Selector

use of java.nio.channels.Selector in project java-in-action by xinghalo.

the class SelectorTest2 method main.

public static void main(String[] args) throws IOException {
    Selector selector = Selector.open();
    SocketChannel channel = SocketChannel.open();
    channel.configureBlocking(false);
    // SelectionKey key = channel.register(selector, SelectionKey.OP_READ);
    while (true) {
        int readyChannels = selector.select();
        if (readyChannels == 0) {
            continue;
        }
        Set<SelectionKey> selectedKeys = selector.selectedKeys();
        Iterator keyIterator = selectedKeys.iterator();
        while (keyIterator.hasNext()) {
            SelectionKey key = (SelectionKey) keyIterator.next();
            if (key.isAcceptable()) {
            // 
            } else if (key.isConnectable()) {
            } else if (key.isReadable()) {
            } else if (key.isWritable()) {
            }
            keyIterator.remove();
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) Iterator(java.util.Iterator) 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