users@grizzly.java.net

Re: writing a client with grizzly...

From: Erik Svensson <erik.svensson_at_six.se>
Date: Wed, 23 Jan 2008 08:28:29 +0100

Howdy all,

On 1/22/08 5:10 PM, "Jeanfrancois Arcand" <Jeanfrancois.Arcand_at_Sun.COM>
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: users-unsubscribe_at_grizzly.dev.java.net
>>> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
>> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>