List of usage examples for java.nio.channels SelectionKey selector
public abstract Selector selector();
From source file:net.ymate.platform.serv.nio.support.NioEventGroup.java
public NioEventProcessor processor(SelectionKey key) { for (NioEventProcessor _processor : __processors) { if (key.selector() == _processor.__selector) { return _processor; }//w w w. jav a2 s . c om } return null; }
From source file:org.apache.catalina.cluster.tcp.TcpReplicationThread.java
/** * The actual code which drains the channel associated with * the given key. This method assumes the key has been * modified prior to invocation to turn off selection * interest in OP_READ. When this method completes it * re-enables OP_READ and calls wakeup() on the selector * so the selector will resume watching this channel. *///from ww w.java 2 s .c o m private void drainChannel(SelectionKey key) throws Exception { boolean packetReceived = false; SocketChannel channel = (SocketChannel) key.channel(); int count; buffer.clear(); // make buffer empty ObjectReader reader = (ObjectReader) key.attachment(); // loop while data available, channel is non-blocking while ((count = channel.read(buffer)) > 0) { buffer.flip(); // make buffer readable int pkgcnt = reader.append(buffer.array(), 0, count); buffer.clear(); // make buffer empty } //check to see if any data is available int pkgcnt = reader.execute(); while (pkgcnt > 0) { if (synchronous) { sendAck(key, channel); } //end if pkgcnt--; } if (count < 0) { // close channel on EOF, invalidates the key channel.close(); return; } // resume interest in OP_READ, OP_WRITE key.interestOps(key.interestOps() | SelectionKey.OP_READ); // cycle the selector so this key is active again key.selector().wakeup(); }