You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Trustin Lee <tr...@gmail.com> on 2007/04/12 13:36:38 UTC

ReadFuture (Was: Re: New feature in 2.0: Request-Response Filter)

Hi Maarten,

On 4/12/07, Maarten Bosteels <mb...@gmail.com> 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?

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

Re: ReadFuture (Was: Re: New feature in 2.0: Request-Response Filter)

Posted by Trustin Lee <tr...@gmail.com>.
On 4/12/07, Maarten Bosteels <mb...@gmail.com> wrote:

<snip/>

> > 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
> ?

Hmm... I feel the internal implementation might become very complex.
It's just a feeling, so we need to try to implement it.

BTW what would be the advantage of returning future over just
providing awaitMessage() like the following?

    Object m1 = session.awaitMessage();
    Object m2 = session.awaitMessage();

Actually I implemented similar stuff in RequestResponseFilter:

    Request req = ...;
    session.write(req);
    Response res = req.awaitResponse();

It might also be better to provide an IoHandler implementation that
provides a blocking message queue, like we did with StreamIoHandler.

WDYT?

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

Re: ReadFuture (Was: Re: New feature in 2.0: Request-Response Filter)

Posted by Maarten Bosteels <mb...@gmail.com>.
On 4/12/07, Trustin Lee <tr...@gmail.com> wrote:
>
> Hi Maarten,
>
> On 4/12/07, Maarten Bosteels <mb...@gmail.com> 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
>