writing a client with grizzly...

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

writing a client with grizzly...

Erik Svensson-2
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


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

Oleksiy Stashok
Hello,

if you're registering callbackHandler with ConnectorHandler, it's
required to call finishConnect on onConnect event. For example:

/            public void onConnect(IOEvent<Context> ioEvent) {
                SelectionKey key = ioEvent.attachment().getSelectionKey();
                try{
                    tcpConnector.finishConnect(key);
                } catch (IOException ex){
                    // log
                }
                ioEvent.attachment().getSelectorHandler().register(key,
                        SelectionKey.OP_READ);
            }
/

Thanks.

WBR,
Alexey.

Erik Svensson wrote:

> 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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>  

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

Jeanfrancois Arcand-2
Hi,

Oleksiy Stashok wrote:

> Hello,
>
> if you're registering callbackHandler with ConnectorHandler, it's
> required to call finishConnect on onConnect event. For example:
>
> /            public void onConnect(IOEvent<Context> ioEvent) {
>                SelectionKey key = ioEvent.attachment().getSelectionKey();
>                try{
>                    tcpConnector.finishConnect(key);
>                } catch (IOException ex){
>                    // log
>                }
>                ioEvent.attachment().getSelectorHandler().register(key,
>                        SelectionKey.OP_READ);
>            }
> /
>

OK I will work on documenting this as this is the second email today
talking about this undocumented operation :-)

A+

-- Jeanfrancois


> Thanks.
>
> WBR,
> Alexey.
>
> Erik Svensson wrote:
>> 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
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>  
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

qbaze
In reply to this post by Oleksiy Stashok
Hola,

Oleksiy Stashok wrote:

> Hello,
>
> if you're registering callbackHandler with ConnectorHandler, it's
> required to call finishConnect on onConnect event. For example:
>
> /            public void onConnect(IOEvent<Context> ioEvent) {
>                SelectionKey key = ioEvent.attachment().getSelectionKey();
>                try{
>                    tcpConnector.finishConnect(key);
>                } catch (IOException ex){
>                    // log
>                }
>                ioEvent.attachment().getSelectorHandler().register(key,
>                        SelectionKey.OP_READ);
>            }
> /
>
> Thanks.
>
> WBR,
> Alexey.
Isn't it dangerous to just ignore the IOException? TCPConnector
finishConnect wraps socketChannel's finishConnect which (probably pretty
rarely but still) can throw exception leaving a socket in an unconnected
state. Even better, according to SocketChannel.finishConnect() 1.5 java doc:
"If a connection attempt fails, that is, if an invocation of this method
throws a checked exception, then the channel will be closed."

So we can end up with an unusable selectionKey (with underlaying channel
closed) being put in the Selector's set.

I haven't checked so it's just a theory :)

br,
pb

>
> Erik Svensson wrote:
>> 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
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>  
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

Erik Svensson-2
In reply to this post by Jeanfrancois Arcand-2
Howdy all,

On 1/22/08 5:10 PM, "Jeanfrancois Arcand" <[hidden email]>
wrote:

> Hi,
>
> Oleksiy Stashok wrote:
>> Hello,
>>
>> if you're registering callbackHandler with ConnectorHandler, it's
>> required to call finishConnect on onConnect event. For example:
>>
>> /            public void onConnect(IOEvent<Context> ioEvent) {
>>                SelectionKey key = ioEvent.attachment().getSelectionKey();
>>                try{
>>                    tcpConnector.finishConnect(key);
>>                } catch (IOException ex){
>>                    // log
>>                }
>>                ioEvent.attachment().getSelectorHandler().register(key,
>>                        SelectionKey.OP_READ);
>>            }
>> /
>>
>
> OK I will work on documenting this as this is the second email today
> talking about this undocumented operation :-)

With Oleksiys help my example worked.
I've modified TCPConnectorHandler:connect(SocketAddress remoteAddres,
SocketAddress localAddress) to check for a callbackHandler and if one isn't
installed it creates one (identical to the one created if controller ==
null). This makes TCPConnectorHandler a bit easier to use for the the most
common case.


cheers

Erik Svensson, SIX AB
 

> A+
>
> -- Jeanfrancois
>
>
>> Thanks.
>>
>> WBR,
>> Alexey.
>>
>> Erik Svensson wrote:
>>> 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
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>  
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

Erik Svensson-2
In reply to this post by qbaze
On 1/23/08 12:06 AM, "Piotr Bazan" <[hidden email]> wrote:

> Hola,
>
> Oleksiy Stashok wrote:
>> Hello,
>>
>> if you're registering callbackHandler with ConnectorHandler, it's
>> required to call finishConnect on onConnect event. For example:
>>
>> /            public void onConnect(IOEvent<Context> ioEvent) {
>>                SelectionKey key = ioEvent.attachment().getSelectionKey();
>>                try{
>>                    tcpConnector.finishConnect(key);
>>                } catch (IOException ex){
>>                    // log
>>                }
>>                ioEvent.attachment().getSelectorHandler().register(key,
>>                        SelectionKey.OP_READ);
>>            }
>> /
>>
>> Thanks.
>>
>> WBR,
>> Alexey.
> Isn't it dangerous to just ignore the IOException? TCPConnector
> finishConnect wraps socketChannel's finishConnect which (probably pretty
> rarely but still) can throw exception leaving a socket in an unconnected
> state. Even better, according to SocketChannel.finishConnect() 1.5 java doc:
> "If a connection attempt fails, that is, if an invocation of this method
> throws a checked exception, then the channel will be closed."
>
> So we can end up with an unusable selectionKey (with underlaying channel
> closed) being put in the Selector's set.
>
> I haven't checked so it's just a theory :)

Isn't checking TCPConnectorHandler:isConnected() enough?
If the connection attempt bombs it should return false.
Obviously you have to check it yourself but the functionality is there.

cheers

Erik Svensson, SIX AB

>
> br,
> pb
>>
>> Erik Svensson wrote:
>>> 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
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>  
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

Jeanfrancois Arcand-2
In reply to this post by qbaze
Salut,

Piotr Bazan wrote:

> Hola,
>
> Oleksiy Stashok wrote:
>> Hello,
>>
>> if you're registering callbackHandler with ConnectorHandler, it's
>> required to call finishConnect on onConnect event. For example:
>>
>> /            public void onConnect(IOEvent<Context> ioEvent) {
>>                SelectionKey key = ioEvent.attachment().getSelectionKey();
>>                try{
>>                    tcpConnector.finishConnect(key);
>>                } catch (IOException ex){
>>                    // log
>>                }
>>                ioEvent.attachment().getSelectorHandler().register(key,
>>                        SelectionKey.OP_READ);
>>            }
>> /
>>
>> Thanks.
>>
>> WBR,
>> Alexey.
> Isn't it dangerous to just ignore the IOException? TCPConnector
> finishConnect wraps socketChannel's finishConnect which (probably pretty
> rarely but still) can throw exception leaving a socket in an unconnected
> state. Even better, according to SocketChannel.finishConnect() 1.5 java
> doc:
> "If a connection attempt fails, that is, if an invocation of this method
> throws a checked exception, then the channel will be closed."
>
> So we can end up with an unusable selectionKey (with underlaying channel
> closed) being put in the Selector's set.
>
> I haven't checked so it's just a theory :)

You are probably right :-) We probably must cancel the SelectionKey and
the SocketChannel. Any patch in mind :-) I will take a look on my side :-)

Thanks!

-- Jeanfrancois


>
> br,
> pb
>>
>> Erik Svensson wrote:
>>> 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
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>  
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

Jeanfrancois Arcand-2
In reply to this post by Erik Svensson-2
Hi Erick

Erik Svensson wrote:

> Howdy all,
>
> On 1/22/08 5:10 PM, "Jeanfrancois Arcand" <[hidden email]>
> wrote:
>
>> Hi,
>>
>> Oleksiy Stashok wrote:
>>> Hello,
>>>
>>> if you're registering callbackHandler with ConnectorHandler, it's
>>> required to call finishConnect on onConnect event. For example:
>>>
>>> /            public void onConnect(IOEvent<Context> ioEvent) {
>>>                SelectionKey key = ioEvent.attachment().getSelectionKey();
>>>                try{
>>>                    tcpConnector.finishConnect(key);
>>>                } catch (IOException ex){
>>>                    // log
>>>                }
>>>                ioEvent.attachment().getSelectorHandler().register(key,
>>>                        SelectionKey.OP_READ);
>>>            }
>>> /
>>>
>> OK I will work on documenting this as this is the second email today
>> talking about this undocumented operation :-)
>
> With Oleksiys help my example worked.
> I've modified TCPConnectorHandler:connect(SocketAddress remoteAddres,
> SocketAddress localAddress) to check for a callbackHandler and if one isn't
> installed it creates one (identical to the one created if controller ==
> null). This makes TCPConnectorHandler a bit easier to use for the the most
> common case.

Agree. Can you send the diff -u :-) We gonna add it to the trunk.

Thanks!

-- Jeanfrancois


>
>
> cheers
>
> Erik Svensson, SIX AB
>  
>> A+
>>
>> -- Jeanfrancois
>>
>>
>>> Thanks.
>>>
>>> WBR,
>>> Alexey.
>>>
>>> Erik Svensson wrote:
>>>> 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
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [hidden email]
>>>> For additional commands, e-mail: [hidden email]
>>>>
>>>>  
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

Jeanfrancois Arcand-2
In reply to this post by Erik Svensson-2


Erik Svensson wrote:

> On 1/23/08 12:06 AM, "Piotr Bazan" <[hidden email]> wrote:
>
>> Hola,
>>
>> Oleksiy Stashok wrote:
>>> Hello,
>>>
>>> if you're registering callbackHandler with ConnectorHandler, it's
>>> required to call finishConnect on onConnect event. For example:
>>>
>>> /            public void onConnect(IOEvent<Context> ioEvent) {
>>>                SelectionKey key = ioEvent.attachment().getSelectionKey();
>>>                try{
>>>                    tcpConnector.finishConnect(key);
>>>                } catch (IOException ex){
>>>                    // log
>>>                }
>>>                ioEvent.attachment().getSelectorHandler().register(key,
>>>                        SelectionKey.OP_READ);
>>>            }
>>> /
>>>
>>> Thanks.
>>>
>>> WBR,
>>> Alexey.
>> Isn't it dangerous to just ignore the IOException? TCPConnector
>> finishConnect wraps socketChannel's finishConnect which (probably pretty
>> rarely but still) can throw exception leaving a socket in an unconnected
>> state. Even better, according to SocketChannel.finishConnect() 1.5 java doc:
>> "If a connection attempt fails, that is, if an invocation of this method
>> throws a checked exception, then the channel will be closed."
>>
>> So we can end up with an unusable selectionKey (with underlaying channel
>> closed) being put in the Selector's set.
>>
>> I haven't checked so it's just a theory :)
>
> Isn't checking TCPConnectorHandler:isConnected() enough?
> If the connection attempt bombs it should return false.
> Obviously you have to check it yourself but the functionality is there.

Right, but at the minimum we should log the exception I think. What do
you think?

Thanks!

-- Jeanfrancois

>
> cheers
>
> Erik Svensson, SIX AB
>
>> br,
>> pb
>>> Erik Svensson wrote:
>>>> 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
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [hidden email]
>>>> For additional commands, e-mail: [hidden email]
>>>>
>>>>  
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

Erik Svensson-2
On 1/23/08 5:12 PM, "Jeanfrancois Arcand" <[hidden email]>
wrote:

>
>
> Erik Svensson wrote:
>> On 1/23/08 12:06 AM, "Piotr Bazan" <[hidden email]> wrote:
>>
>>> Hola,
>>>
>>> Oleksiy Stashok wrote:
>>>> Hello,
>>>>
>>>> if you're registering callbackHandler with ConnectorHandler, it's
>>>> required to call finishConnect on onConnect event. For example:
>>>>
>>>> /            public void onConnect(IOEvent<Context> ioEvent) {
>>>>                SelectionKey key = ioEvent.attachment().getSelectionKey();
>>>>                try{
>>>>                    tcpConnector.finishConnect(key);
>>>>                } catch (IOException ex){
>>>>                    // log
>>>>                }
>>>>                ioEvent.attachment().getSelectorHandler().register(key,
>>>>                        SelectionKey.OP_READ);
>>>>            }
>>>> /
>>>>
>>>> Thanks.
>>>>
>>>> WBR,
>>>> Alexey.
>>> Isn't it dangerous to just ignore the IOException? TCPConnector
>>> finishConnect wraps socketChannel's finishConnect which (probably pretty
>>> rarely but still) can throw exception leaving a socket in an unconnected
>>> state. Even better, according to SocketChannel.finishConnect() 1.5 java doc:
>>> "If a connection attempt fails, that is, if an invocation of this method
>>> throws a checked exception, then the channel will be closed."
>>>
>>> So we can end up with an unusable selectionKey (with underlaying channel
>>> closed) being put in the Selector's set.
>>>
>>> I haven't checked so it's just a theory :)
>>
>> Isn't checking TCPConnectorHandler:isConnected() enough?
>> If the connection attempt bombs it should return false.
>> Obviously you have to check it yourself but the functionality is there.
>
> Right, but at the minimum we should log the exception I think. What do
> you think?

Logging is good. There is a problem with eating the exception since we can't
programmatically determine why we didn't get the connection or if we haven't
tried yet. But that might be nit-picking.

cheers
Erik Svensson, SIX AB



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: writing a client with grizzly...

Jeanfrancois Arcand-2
In reply to this post by Jeanfrancois Arcand-2
Thanks for the patch, Erick! Applied and available on the trunk!

-- Jeanfrancois

Jeanfrancois Arcand wrote:

> Hi Erick
>
> Erik Svensson wrote:
>> Howdy all,
>>
>> On 1/22/08 5:10 PM, "Jeanfrancois Arcand" <[hidden email]>
>> wrote:
>>
>>> Hi,
>>>
>>> Oleksiy Stashok wrote:
>>>> Hello,
>>>>
>>>> if you're registering callbackHandler with ConnectorHandler, it's
>>>> required to call finishConnect on onConnect event. For example:
>>>>
>>>> /            public void onConnect(IOEvent<Context> ioEvent) {
>>>>                SelectionKey key =
>>>> ioEvent.attachment().getSelectionKey();
>>>>                try{
>>>>                    tcpConnector.finishConnect(key);
>>>>                } catch (IOException ex){
>>>>                    // log
>>>>                }
>>>>                ioEvent.attachment().getSelectorHandler().register(key,
>>>>                        SelectionKey.OP_READ);
>>>>            }
>>>> /
>>>>
>>> OK I will work on documenting this as this is the second email today
>>> talking about this undocumented operation :-)
>>
>> With Oleksiys help my example worked.
>> I've modified TCPConnectorHandler:connect(SocketAddress remoteAddres,
>> SocketAddress localAddress) to check for a callbackHandler and if one
>> isn't
>> installed it creates one (identical to the one created if controller ==
>> null). This makes TCPConnectorHandler a bit easier to use for the the
>> most
>> common case.
>
> Agree. Can you send the diff -u :-) We gonna add it to the trunk.
>
> Thanks!
>
> -- Jeanfrancois
>
>
>>
>>
>> cheers
>>
>> Erik Svensson, SIX AB
>>  
>>> A+
>>>
>>> -- Jeanfrancois
>>>
>>>
>>>> Thanks.
>>>>
>>>> WBR,
>>>> Alexey.
>>>>
>>>> Erik Svensson wrote:
>>>>> 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
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: [hidden email]
>>>>> For additional commands, e-mail: [hidden email]
>>>>>
>>>>>  
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [hidden email]
>>>> For additional commands, e-mail: [hidden email]
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]