use Selector to multiplex Pipe.SourceChannels?
Hi,
i am a little confused. I 'd like an object to have some references to multiple Pipe.SourceChannels and periodically check for incomig data. MultiPortEcho.java seems to do what i want, but by using SocketChannels.
Do i have to do something similar? Create a Selector, get the keys and check for OP_ACCEPT and OP_READ?
MultiPortEcho.java
ibm.com/developerWorks
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
publicclass MultiPortEcho
{
privateint ports[];
private ByteBuffer echoBuffer = ByteBuffer.allocate( 1024 );
public MultiPortEcho(int ports[] )throws IOException{
this.ports = ports;
go();
}
privatevoid go()throws IOException{
// Create a new selector
Selector selector = Selector.open();
// Open a listener on each port, and register each one
// with the selector
for (int i=0; i<ports.length; ++i){
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false );
ServerSocket ss = ssc.socket();
InetSocketAddress address =new InetSocketAddress( ports[i] );
ss.bind( address );
SelectionKey key = ssc.register( selector, SelectionKey.OP_ACCEPT );
System.out.println("Going to listen on "+ports[i] );
}
while (true){
int num = selector.select();
Set selectedKeys = selector.selectedKeys();
Iterator it = selectedKeys.iterator();
while (it.hasNext()){
SelectionKey key = (SelectionKey)it.next();
if ((key.readyOps() & SelectionKey.OP_ACCEPT)
== SelectionKey.OP_ACCEPT){
// Accept the new connection
ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
SocketChannel sc = ssc.accept();
sc.configureBlocking(false );
// Add the new connection to the selector
SelectionKey newKey = sc.register( selector, SelectionKey.OP_READ );
it.remove();
System.out.println("Got connection from "+sc );
}elseif ((key.readyOps() & SelectionKey.OP_READ)
== SelectionKey.OP_READ){
// Read the data
SocketChannel sc = (SocketChannel)key.channel();
// Echo data
int bytesEchoed = 0;
while (true){
echoBuffer.clear();
int r = sc.read( echoBuffer );
if (r><=0){
break;
}
echoBuffer.flip();
sc.write( echoBuffer );
bytesEchoed += r;
}
System.out.println("Echoed "+bytesEchoed+" from "+sc );
it.remove();
}
}
//System.out.println( "going to clear" );
//selectedKeys.clear();
//System.out.println( "cleared" );
}
}
staticpublicvoid main( String args[] )throws Exception{
if (args.length<=0){
System.err.println("Usage: java MultiPortEcho port [port port ...]" );
System.exit( 1 );
}
int ports[] =newint[args.length];
for (int i=0; i<args.length; ++i){
ports[i] = Integer.parseInt( args[i] );
}
new MultiPortEcho( ports );
}
}
Message was edited by:
uig>

