users@grizzly.java.net

unable to create a grizzly client using ProtocolChain

From: Survivant 00 <survivant00_at_gmail.com>
Date: Tue, 23 Dec 2008 08:56:48 -0500

I'm trying to create a client using ProtocolChain but the client never
received messages from the server.

and another thing : when I'm using controller.start(); the program block
there, and if I'm using new Thread(controler).start(); it's fine.. (still
not receiving messages from the server)

here my code


package grizzly.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import grizzly.client.filter.QuoteQueryManagerFilter;
import grizzly.client.filter.QuoteQueryProtocolFilter;
import com.sun.grizzly.BaseSelectionKeyHandler;
import com.sun.grizzly.CallbackHandler;
import com.sun.grizzly.Controller;
import com.sun.grizzly.DefaultProtocolChain;
import com.sun.grizzly.ProtocolChain;
import com.sun.grizzly.ProtocolChainInstanceHandler;
import com.sun.grizzly.TCPConnectorHandler;
import com.sun.grizzly.TCPSelectorHandler;
import com.sun.grizzly.util.ByteBufferFactory;

public class SmallGrizzlyClient {

    private static final Logger s_logger =
LoggerFactory.getLogger(SmallGrizzlyClient.class);

    private TCPConnectorHandler connector_handler;
    private Controller controller;
    private TCPSelectorHandler tcp_selector_handler;

    private ByteBuffer buf = ByteBufferFactory.allocateView(1000, false);
    private ByteBuffer response = ByteBufferFactory.allocateView(1000,
false);

    public void init() {

        controller = new Controller();

        tcp_selector_handler = new TCPSelectorHandler(true);
        tcp_selector_handler.setSelectionKeyHandler(new
BaseSelectionKeyHandler());
        controller.addSelectorHandler(tcp_selector_handler);

        QuoteQueryProtocolFilter protocolParser = new
QuoteQueryProtocolFilter();
        QuoteQueryManagerFilter quoteManagerFilter = new
QuoteQueryManagerFilter();


        ProtocolChainInstanceHandler pciHandler = new
ProtocolChainInstanceHandler() {

           final ProtocolChain protocolChain = new DefaultProtocolChain();

           public boolean offer(ProtocolChain protocolChain) {
               return false;

           }

           public ProtocolChain poll() {

               return protocolChain;
           }
       };

       controller.setProtocolChainInstanceHandler(pciHandler);

       ProtocolChain protocolChain = pciHandler.poll();
       protocolChain.addFilter(protocolParser);
       protocolChain.addFilter(quoteManagerFilter);

       //new Thread(controller).start(); // continue fine
       try {
        controller.start(); // BLOCK HERE
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        connector_handler = (TCPConnectorHandler)
controller.acquireConnectorHandler(Controller.Protocol.TCP);
    }

    public void connect(String host, int port){
        try {
            connector_handler.connect(new InetSocketAddress(host, port),
(CallbackHandler)null);

        } catch (Exception e) {
            s_logger.error("Exception in execute..." + e);
        }
    }

    public void send(String quote) throws Exception {
        byte[] msg = quote.getBytes();

        buf = ByteBufferFactory.allocateView(msg.length, false);

        buf.put(msg);
        buf.flip();
        connector_handler.write(buf, true);
        buf.clear();

    }

    public void close(){
        try {
            if(connector_handler!=null){
                connector_handler.close();
            }
            if(controller!=null){
                controller.stop();
            }
        } catch (IOException e) {
            s_logger.error("IOException", e);
        }
    }

    public static void main(String[] args) {
        SmallGrizzlyClient client = new SmallGrizzlyClient();

        String host = "localhost";
        int port = 7803;

        try {
            client.init();
            Thread.sleep(300);
            client.connect(host, port);

            Thread.sleep(300);
            client.send("hello[eoq]");

            Thread.sleep(5000);

            client.close();

            Thread.sleep(300);
        } catch(Exception e){
            s_logger.error("main", e);
        } finally {
            try {client.close();}catch(Exception e){}
        }

    }

}


PS I know that the message sent by the client is receive on the server and
in the server log I see a message sent too. I have a debug log in all my
method in the QuoteQueryProtocolFilter and QuoteQueryProtocolManager