users@grizzly.java.net

writing a client with grizzly...

From: Erik Svensson <erik.svensson_at_six.se>
Date: Tue, 22 Jan 2008 16:12:00 +0100

Hwody all!

We're newcomers to grizzly, looking to use grizzly as a comm backbone for a
near-realtime information flow system for the financial industry.
I've created a simple server without a problem and now I'm looking at
writing a client using grizzly and here I run into some snags.
I've been looking at
http://blogs.sun.com/oleksiys/resource/ssl-client/GrizzlySSLNIOClient.java
as an exampel but I can't get it to work.
The code:

    <snip>
  private TCPConnectorHandler connector_handler;
  private Controller controller;
  private TCPSelectorHandler tcp_selector_handler;

  private ByteBuffer buf = ByteBuffer.allocate(100);

    controller = new Controller();
    tcp_selector_handler = new TCPSelectorHandler(true); // true to get
client
    controller.addSelectorHandler(tcp_selector_handler);

    controller.addStateListener(new ControllerStateListenerAdapter() {

      public void onReady() {
        System.out.println("Ready!");
      }

      public void onStarted() {
        System.out.println("Controller started!");
      }

    });

    new Thread(controller).start();
    synchronized(this) {
      try {
          wait(30000);
      } catch(Exception e) {
        System.out.println("Timeout in wait"+e.getMessage());
      }
    }

    connector_handler =(TCPConnectorHandler)
controller.acquireConnectorHandler(Controller.Protocol.TCP);
    connector_handler.setController(controller);

    System.out.println("Thread is started.? "+(controller.isStarted() ?
"Yes" : "No"));
    try {
      byte[] filler = new byte[92];

      for (int i = 0; i< filler.length; i++) {
        filler[i] = 2;
      }

     
      connector_handler.connect(new InetSocketAddress(host,port), new
CallbackHandler() {
        public void onConnect(IOEvent e) {
          System.out.println("Callbackhandler: OnConnect...");
        }

        public void onRead(IOEvent e) {
          System.out.println("Callbackhandler: OnRead...");
        }

        public void onWrite(IOEvent e) {
          System.out.println("Callbackhandler: OnWrite...");
        }

      });
      
      int ctr = 0;
      while (ctr < repeats) {
        buf.putLong(System.nanoTime());
        buf.put(filler);
        buf.flip();
        connector_handler.write(buf,false);
        buf.clear();
        connector_handler.read(buf,true);
        buf.flip();
        elapsed += System.nanoTime() - buf.getLong();
        buf.clear();
        ctr++;
      }
      System.out.println(""+repeats+" run at a total of "+elapsed+"
nanoseconds. Per packet it comes down to "+elapsed/repeats+
              " nanoseconds per roundtrip.");
    } catch(Exception e) {
      System.out.println("Exception in execute..."+e);
      e.printStackTrace(System.out);
    }
  }

WHen I try to run it I get an java.nio.channels.NotYetConnectedException.
I've checked that I can connect to the host. What am I doing wrong?

cheers

Erik Svensson, SIX AB