Created
May 24, 2011 07:26
-
-
Save legnaleurc/988259 to your computer and use it in GitHub Desktop.
NIO practice
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
package org.sandbox; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.ClosedChannelException; | |
import java.nio.channels.ClosedSelectorException; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.Selector; | |
import java.nio.channels.SocketChannel; | |
import java.util.Iterator; | |
import java.util.Set; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class Reactor implements Runnable { | |
private Selector selector; | |
private SocketChannel socket; | |
private ExecutorService pool; | |
public Reactor( SocketChannel socket ) { | |
this.pool = Executors.newCachedThreadPool(); | |
this.socket = socket; | |
try { | |
this.selector = Selector.open(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
SelectionKey key = null; | |
try { | |
key = socket.register( this.selector, SelectionKey.OP_READ ); | |
} catch (ClosedChannelException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
key.attach( new Runnable() { | |
@Override | |
public void run() { | |
byte [] buffer = new byte[1024]; | |
ByteBuffer bb = ByteBuffer.wrap( buffer ); | |
try { | |
Reactor.this.socket.read( bb ); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
try { | |
Reactor.this.socket.close(); | |
} catch( IOException e1 ) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
try { | |
Reactor.this.selector.close(); | |
} catch( IOException e1 ) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
return; | |
} | |
String line = new String( buffer ); | |
System.out.printf( "> %s\n", line ); | |
} | |
} ); | |
this.pool.execute( new Runnable() { | |
@Override | |
public void run() { | |
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ); | |
try { | |
String line = reader.readLine(); | |
ByteBuffer bb = ByteBuffer.wrap( line.getBytes() ); | |
if( bb == null ) { | |
return; | |
} | |
Reactor.this.socket.write( bb ); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} ); | |
} | |
@Override | |
public void run() { | |
while( true ) { | |
try { | |
this.selector.select(); | |
Set< SelectionKey > keys = this.selector.selectedKeys(); | |
for( Iterator< SelectionKey > it = keys.iterator(); it.hasNext(); ) { | |
SelectionKey key = it.next(); | |
Runnable action = (Runnable)key.attachment(); | |
action.run(); | |
} | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
break; | |
} catch (ClosedSelectorException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment