Sorry about that I feel Grizzly is a little bit complicated to me.
In the past, I use the following code with core jdk nio api:
private HashMap<TcpClient> clientList = new HashMap();
public void run() {
while (bRun) {
try {
int keys = selector.select();
if (keys > 0) {
Set keys = selector.selectedKeys();
Iterator iterator = keys.iterator();
while (iterator.hasNext()) {
SelectionKey objSelectionKey =
(SelectionKey)iterator.next();
iterator.remove();
if (objSelectionKey.isValid() &&
objSelectionKey.isAcceptable()) {
ServerSocketChannel objReadyChannel =
(ServerSocketChannel)objSelectionKey.channel();
SocketChannel socketChannel =
objReadyChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector,
SelectionKey.OP_READ);
TcpClient client = new TcpClient(socketChannel);
clientList.put(client.getID(), client);
}
// handle read
if ()
// handle write
if ()
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void sendMessageToClient(String clientId, ByteBuffer message) throws
IOException {
clientList.get(clientId).getSocketChannel().write(message);
}
public void boardcast(ByteBuffer message) throws IOException{
Enumeration en = clientList.enumeration();
// boardcast
}
I start to play Grizzly now and I have no idea how to build code with
similar purpose as Grizzly seems to encapsulate SocketChannel. I start with
Echo Server example:
public void startSIPServerDemo() throws Exception {
Controller controller = new Controller();
TCPSelectorHandler tcpSelector = new TCPSelectorHandler();
InetAddress address = InetAddress.getByName("0.0.0.0");
tcpSelector.setInet(address);
tcpSelector.setPort(8080);
controller.addSelectorHandler(tcpSelector);
Pipeline mySharedPipeline = new DefaultPipeline();
mySharedPipeline.setMaxThreads(5);
controller.setPipeline(mySharedPipeline);
ProtocolChainInstanceHandler pciHandler = new
ProtocolChainInstanceHandler() {
final private ProtocolChain protocolChain = new
DefaultProtocolChain();
@Override
public ProtocolChain poll() {
return protocolChain;
}
@Override
public boolean offer(ProtocolChain instance) {
return true;
}
};
controller.setProtocolChainInstanceHandler(pciHandler);
ProtocolChain protocolChain = pciHandler.poll();
protocolChain.addFilter(new MyParserProtocolFilter());
protocolChain.addFilter(new MyProcessorFilter());
controller.start();
}
Any cure?
--
View this message in context: http://www.nabble.com/Best-Practice-to-broadcast---send-private-messages-to-client%28s%29-tp19025702p20091022.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.