Hi,
Chad Gallemore wrote:
> Hi,
>
>
>
> We are looking to use Grizzly to start up listening on a port and accept
> TCP packets that are sent from a logger (java.util.SocketHandler). I am
> having trouble finding out how exactly we accomplish this. I’ve used
> Grizzly as an http listener, which obviously doesn’t work in this
> situation. Below is what I have now, and I am wanting to refactor that
> out to utilize Grizzly to accomplish the same task:
>
>
>
> Public static void main (String [] args) {
>
> ServerSocket ss = new ServerSocket(8888);
>
>
>
> while (true) {
>
> final Socket socket = ss.accept();
>
> //Spin off a new thread to handle the rest of the processing.
>
> Runnable r = new Runnable() {
>
> public void run() {
>
> try {
>
> processData(socket);
>
> } catch (Exception e) {
>
> e.printStackTrace(); //To change body of catch
> statement use File | Settings | File Templates.
>
> }
>
> }
>
> };
>
> new Thread(r).start();
>
> }
>
> }
>
>
>
> Instead I was hoping to do something like the following, with my adapter
> able to handle all of my processing,
Just do:
Controller con = new Controller();
con.setProtocolChainInstanceHandler(new
DefaultProtocolChainInstanceHandler(){
public ProtocolChain poll() {
ProtocolChain protocolChain = protocolChains.poll();
if (protocolChain == null){
protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(new ReadFilter());
protocolChain.addFilter(new MyProtocolFilter());
<<<<<<<< Write your logic inside MyProtocolFilter <<<<<<<<<<<<<<<<<<<<<
}
return protocolChain;
}
});
con.addSelectorHandler(new TCPSelectorHandler());
That should so do what you want.
Thanks
-- Jeanfrancois
I later realized this won’t work
> because it only works over HTTP. Is there a way
>
> That I can achieve the same thing over TCP:
>
>
>
> Public static void main (String [] args) {
>
> selectorThread.setPort(8888);
>
> selectorThread.setAdapter(new NmrObserverProcessorAdapter(handler));
>
> selectorThread.initEndpoint();
>
>
>
> Runnable run = new Runnable() {
>
> public void run() {
>
> try {
>
> System.out.println("starting selector thread endpoint");
>
> selectorThread.startEndpoint();
>
> } catch (Throwable t) {
>
> System.out.println(t);
>
> log.severe("Error starting endpoint for server " + t);
>
> }
>
> }
>
> };
>
>
>
> Thread t = new Thread(run);
>
> t.setDaemon(true);
>
> t.start();
>
> }
>
>
>
> Thanks for the help.
>
>
>
> **Chad**** Gallemore**
>
> 407 Pennsylvania Ave, Suite 201, Joplin, MO 64801
>
> (417) 626-6510 (w)
>
> (909) 528-9405 (c)
>
> (417) 625-1810 (f)
>
> http://gallemore.blogspot.com/ <http://cgallemore@blogspot.com/>
>
>
>
>
>