Created
October 19, 2014 15:12
-
-
Save yrom/01788ab1039ab477c42e to your computer and use it in GitHub Desktop.
code snippet for java.nio.channels.Selector. http://tutorials.jenkov.com/java-nio/selectors.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the remote server | |
SocketAddress remote = new InetSocketAddress(Inet4Address.getByName(hostName), port); | |
// open a selector | |
Selector selector = Selector.open(); | |
// open a channel | |
SocketChannel channel = SocketChannel.open(); | |
// non-blocking | |
channel.configureBlocking(false); | |
// pending connect | |
channel.connect(remote); | |
// initiative register to the selector, and interest the connection-op | |
channel.register(selector, SelectionKey.OP_CONNECT); | |
ByteBuffer packBuffer = ByteBuffer.allocate(0xffff); | |
while (true) { | |
// blocking select "ready" channels | |
int readyChannels = selector.select(); | |
if (readyChannels == 0) { | |
System.out.println("wake up, but no interest ops..."); | |
continue; | |
} | |
Set<SelectionKey> selectedKeys = selector.selectedKeys(); | |
Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); | |
while (keyIterator.hasNext()) { | |
SelectionKey key = keyIterator.next(); | |
if (key.isAcceptable()) { | |
// a connection was accepted by a ServerSocketChannel. | |
// in this demo we will never be here | |
System.out.println("acceptable!"); | |
SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept(); | |
System.out.println("accept a channel:" + socketChannel); | |
} else if (key.isConnectable()) { | |
// a connection was established with a remote server. | |
System.out.println("connectable!"); | |
boolean connectionPending = ((SocketChannel) key.channel()).isConnectionPending(); | |
System.out.println("connectionPending:" + connectionPending); | |
if (connectionPending) { | |
// really connect to remote | |
boolean finishConnect = ((SocketChannel) key.channel()).finishConnect(); | |
System.out.println("finishConnect:" + finishConnect); | |
} | |
// we must write "hello world" to server first | |
// after connected | |
channel.register(selector, SelectionKey.OP_WRITE); | |
} else if (key.isReadable()) { | |
// a channel is ready for reading | |
System.out.println("readable!"); | |
if (init) { | |
packBuffer.clear(); | |
int read = ((SocketChannel) key.channel()).read(packBuffer); | |
System.out.println("read bytes:" + read); | |
handleData(packBuffer); | |
// is need to send heartbeat after read something ? | |
if (isNeedHeartBeat() || isNeedSendSomething()) | |
channel.register(selector, SelectionKey.OP_WRITE); | |
} else { | |
System.out.println("dosen't init"); | |
} | |
} else if (key.isWritable()) { | |
// a channel is ready for writing | |
System.out.println("writable"); | |
if (!init) { | |
// send "hello" to server | |
sayHello(key); | |
init = true; | |
} | |
if (isNeedHeartBeat()) | |
sendHeartBeat(key); | |
// or send others to server here | |
if (isNeedSendSomething()) | |
((SocketChannel) key.channel()).write(something); | |
// and we interest what server response immediately | |
channel.register(selector, SelectionKey.OP_READ); | |
} | |
// this channel is done processing | |
keyIterator.remove(); | |
// next time the channel becomes "ready" | |
// the selector will add it to the selected key set again. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment