Hi,
I did read your mail completely (-:
I think it is very simple to migrate your code to grizzly.
Basicly for the server-client side you just need to setup a ProtocolChain with a ProtocolParserFilter
and another Filter to execute commands.
You probably can use the ProtocolParserFilter in mail
https://grizzly.dev.java.net/servlets/ReadMsg?list=users&msgNo=895
ProtocolParser implementation II from (Ken--_at_newsgroupstats.hk) and Alexey
as a guide.
I have attached a new version of SocketConnectionListener.java where I sketch out what I would
do. (In that I keep idle connections for 30 minutes. I' am not sure if that's what you wanted)
The code is really just how I would start.
No TimeoutHandling/No Exception Handling./No keepalive testing
Just tell me if this post helped....
Many Greetings
John
------------------------ Code ---------------
package org.sample.nio.connection.handler;
import java.io.IOException;
import java.nio.channels.ServerSocketChannel;
import org.sample.nio.QuoteManager;
import com.sun.grizzly.*;
import com.sun.grizzly.filter.ParserProtocolFilter;
/**
* This class listen for incoming connection
*
* @author Sebastien Dionne
*/
public class SocketConnectionListener implements Runnable {
protected String f_name;
protected int f_port;
protected QuoteManager f_quoteManager;
protected boolean f_shutdown;
protected ThreadGroup f_threadGroup;
protected ServerSocketChannel f_serverSocket;
/**
* @return the quoteManager
*/
public QuoteManager getQuoteManager() {
return f_quoteManager;
}
/**
* @param quoteManager the quoteManager
*/
public void setQuoteManager(QuoteManager quoteManager) {
this.f_quoteManager = quoteManager;
}
/**
* @param port port for the incomming connection
*/
public void setPort(int port) {
f_port = port;
}
/**
* @return the listening port
*/
public int getPort() {
return f_port;
}
private Controller controller;
/**
* Init
*/
public void init() {
System.out.println("listening for incomming TCP Connections on port : " + f_port);
try {
controller = new Controller();
DefaultPipeline defp = new DefaultPipeline();
defp.setMinThreads(20);
defp.setMaxThreads(100);
controller.setPipeline(defp);
TCPSelectorHandler tcpSelectorHandler = new TCPSelectorHandler();
DefaultSelectionKeyHandler keyHandler = new DefaultSelectionKeyHandler();
//keep connection for 30 minutes
keyHandler.setTimeout(30 * 1000 * 60);
tcpSelectorHandler.setSelectionKeyHandler(keyHandler);
tcpSelectorHandler.setPort(f_port);
controller.addSelectorHandler(tcpSelectorHandler);
} catch (Exception e) {
System.exit(-10);
}
QueryQuoteManagerFilter quoteManagerFilter = new QueryQuoteManagerFilter(f_quoteManager);
final ProtocolFilter parserProtocolFilter = new ParserProtocolFilter() {
public ProtocolParser newProtocolParser() {
return new YourProtocolParser();
}
};
final ProtocolChain protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(parserProtocolFilter);
protocolChain.addFilter(quoteManagerFilter);
((DefaultProtocolChain) protocolChain).setContinuousExecution(true);
ProtocolChainInstanceHandler pciHandler = new DefaultProtocolChainInstanceHandler() {
public ProtocolChain poll() {
return protocolChain;
}
public boolean offer(ProtocolChain protocolChain) {
return false;
}
};
controller.setProtocolChainInstanceHandler(pciHandler);
try {
controller.start();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @return the shutdown
*/
public boolean isShutdown() {
return f_shutdown;
}
/**
* shutdown the socket listener
*/
public void shutdown() {
}
/**
* processing
* <p/>
* - Open socket
* - a new Thread (ClientConnectionHandler)
* - start the new thread
*/
public void run() {
init();
}
public class QueryQuoteManagerFilter implements ProtocolFilter {
private QuoteManager manager;
public QueryQuoteManagerFilter(QuoteManager manager) {
this.manager = manager;
}
public boolean execute(Context context) throws IOException {
String query = (String) context.removeAttribute(ProtocolParser.MESSAGE);
//here should be the parsed query command string with
// which query should be able to create a command
// I have no time to rewrite ClientConnectionHandler
//Basicly you have in context a link to client connection
//with that and forexample TCPSelectorHandler your commands can send data to client
manager.processQuery(null, query);
return false;
}
public boolean postExecute(Context context) throws IOException {
return true;
}
}
public abstract class YourProtocolParser implements ProtocolParser {
// here you have to write your own parser
// I think parser in mail
//
https://grizzly.dev.java.net/servlets/ReadMsg?list=users&msgNo=895
// by Ken--_at_newsgroupstats.hk and Alexey would be a good starting point
}
}
--
Super-Aktion nur in der GMX Spieleflat: 10 Tage für 1 Euro.
Über 180 Spiele downloaden: http://flat.games.gmx.de