users@grizzly.java.net

RE: Re: [Q] Passing around connection context such as remoteDestinationAddress and LocalSourceAddress

From: Simon Trudeau <strudeau_at_bluetreewireless.com>
Date: Thu, 6 Mar 2008 11:07:11 -0500

For all the mailing list readers out there, here's my CallbackHandler onConnect method with Jean-François's recommendation:

public void onConnect(IOEvent<Context> ioEvent) {
                        SelectionKey k = ioEvent.attachment().getSelectionKey();
                        try {
                                connectorHandler.finishConnect(k);
                                remoteAddress = ((SocketChannel)k.channel()).socket().getInetAddress();
                                localAddress = ((SocketChannel)k.channel()).socket().getLocalAddress();
                                if(LOG.isTraceEnabled())
                                {
                                        LOG.trace("connection established from " + localAddress.toString() + " to " + remoteAddress.toString());
                                }
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                        ioEvent.attachment().getSelectorHandler().register(k, SelectionKey.OP_READ);
                }

Warning! Make sure you query for the remote and local address AFTER you invoke connectorHandler.finishConnect(k). Or else you will end up with and NPE exception.

Simon

-----Original Message-----
From: Jeanfrancois.Arcand_at_Sun.COM [mailto:Jeanfrancois.Arcand_at_Sun.COM]
Sent: March-06-08 10:39 AM
To: users_at_grizzly.dev.java.net
Subject: Re: [Q] Passing around connection context such as remoteDestinationAddress and LocalSourceAddress



Simon Trudeau wrote:
> How can I obtain a connection context inside a CallbackHandler?

((SocketChannel)SelectionKey.channel()).socket().getXXX

Thanks!

-- Jeanfrancois


>
> I would like to know, how can I obtain information about a connection?
> Most of the time, I will end up passing my connectorHandler around to
> read and to write, but I don't know how I can get information about the
> connection such as what's the local port I am connected from and what's
> the remote port and address I am connected to?
>
> Any ideas for me?
>
>
>
>
>
> Simon
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
For additional commands, e-mail: users-help_at_grizzly.dev.java.net