users@grizzly.java.net

Re: writing a client with grizzly...

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

On 1/23/08 12:06 AM, "Piotr Bazan" <bazi_at_world.pl> 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: 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
>