On 4/12/07, Trustin Lee <[EMAIL PROTECTED]> wrote:

Hi Maarten,

On 4/12/07, Maarten Bosteels <[EMAIL PROTECTED]> wrote:
> Hi folks,
>
> Until now I had only implemented server apps with MINA (IoAcceptor), and
no
> client apps (IoConnector).
>
> I started working on a small tutorial about
ProtocolDecoder/ProtocolEncoder
> to complement Mark's Getting Started Guide,
> and while implementing the client side I was wondering if a ReadFuture
would
> be an interesting feauture.
>
> Suppose you have a request-response protocol and you want to implement
the
> client synchronously:
> connect, send a request and wait until the response comes in.
>
> What is currently the best way to go ?
>
> With a ReadFuture it could be done like this:
>
>     Object request = ...;
>     ConnectFuture connectFuture = connector.connect(new
> InetSocketAddress("localhost",5555));
>     connectFuture.join(100);
>     if (connectFuture.isConnected()) {
>       IoSession session = connectFuture.getSession();
>       session.write( request );
>       ReadFuture readFuture = session.read();
>       readFuture.join();
>       if (readFuture.messageRead()) {
>         Object response = readFuture.getResult();
>         doSomethingWith(response);
>       }
>       session.close().join();
>     }
>
> What do you think ?

It sounds very interesting.  Assuming we have ReadFuture, should
IoHandler.messageReceived() be notified?



Not sure about that.
How are people currently implementing a client that wants to wait for a
response ?

I did a very quick-and-dirty implementation:
* store a ReadFuture in the session
* IoHandler.messageReceived(Object message) calls readFuture.setResult
(message)

But it's of course way too simplistic. For instance, it should be possible
to do

session.write();
ReadFuture f1 = session.read();
ReadFuture f2 = session.read();
f1.join();
f2.join();

Storing a list of ReadFuture's in the IoSession is probably not a good idea
?
Unfortunately, I do not know mina internals well enough to do a proper
implementation.

Maarten

Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6

Reply via email to