users@grizzly.java.net

Re: unable to create a grizzly client using ProtocolChain

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Mon, 05 Jan 2009 14:27:26 +0100

Sebastien, are you sure, that QuoteQueryProtocolFilter is never called
on client side? Can you pls. double check this?
If no - pls. send me client and server side code - I'll check it.

Thank you.

WBR,
Alexey.

On Dec 23, 2008, at 14:56 , Survivant 00 wrote:

> 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
>