users@grizzly.java.net

[Q] How-to client Protocol

From: Simon Trudeau <strudeau_at_bluetreewireless.com>
Date: Fri, 29 Feb 2008 13:54:53 -0500

I'm looking for help building a TCP client with a specific protocol :

 

If server is password protected:

 

1. Client connects to server
2. Server sends password prompt to client
3. Client sends server's password
4. Server sends acknowledgement
5. Client sends request
6. Server sends reply
7. Repeat step 5 and 6

 

If server is not password protected:

 

1. Client connects to server
2. Client sends request
3. Server sends reply
4. Repeat step 2 and 3

 

How should I go about implementing this protocol? In once case, the
client, upon establishing a connection, can send a request right away in
the other case, it must wait for the server to negotiate the password.

 

What's the right way to handle protocol specific data exchange on the
client side?

 

My first guess would be to create a custom CallbackHandler to trigger
the password negociation upon connecting to the server like so...

 

connector_handler.connect(new InetSocketAddress(host,port),
new CallbackHandler<Context>()
{
public void onConnect(IOEvent<Context> e)
{
SelectionKey k = e.attachment().getSelectionKey();
System.out.println("Callbackhandler: OnConnect...");
try
{
        connector_handler.finishConnect(k);
 
        //Negociate password password exchange here
} catch(Exception ex)
{
        System.out.println("exception in
CallbackHandler:"+ex.getMessage());
}
e.attachment().getSelectorHandler().register(k,SelectionKey.OP_READ);
}
 
public void onRead(IOEvent<Context> e){}
public void onWrite(IOEvent<Context> e){}
});
 
It might work... but it definitely doesn't look very nice. Is there some
sort of state machine facility to deal with this kind of situation? I
guess there must be since Grizzly is implemented with protocols such as
SIP.
 
There is also something called a ProtocolFilter, I guess it could be
better suited than using the CallbackHandler...eventhough I need to
define a behaviour OnConnection...
 
Using a ProtocolFilter, how would I go about making it stateful and
maintaining state information between invocation of the ProtocolFilter?
I guess it has something to do with the Context object... But how do you
store user defined states in this object? Do I have to overload it?
 
 
Well, I guess I have a lot of interrogations! :.) Are there any test
cases in the Grizzly code base that could help me? Are there any
tutorials, code samples, on the topic?
 
 
Thanks for your help,

 

 

Simon