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();
}
}
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());
}
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) {
}
}
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();
}
}
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();
}
}
}
Aggregations