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 *
* - 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--@newsgroupstats.hk and Alexey would be a good starting point } }