You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Zohar Amir <zo...@gmail.com> on 2006/04/01 21:05:14 UTC

still looking for help with StreamIoHandler

Hello,
I'm trying to use MINA, but I have some problems. I've posted here asking
for help, but I still haven't resolved the issues. The solution to my
problem may be better usage of MINA, but I simply don't know. Frankly - I'm
stuck with my project.
I'll try to sum up my problem again:
I have mobile clients connecting to my server via TCP. Clienmts may send
more than a single message at a time. Being mobile clients, messages may be
slow in arriving.
All this lead me to try and use the StreamIoHandler. When trying to do so
I'm facing a problem with clients that send a single message and then close
the connection - this leads to the invocation of the sessionClosed method
before the new handling thread gets a chance to read the available data from
the incoming stream.
Can anyone please help me with this?
Thanks,
Zohar.

Re: still looking for help with StreamIoHandler

Posted by Zohar Amir <zo...@gmail.com>.
I'm at home now, so I'll have to wait and see if it really works for me, but
it's very promising - looks like what I was looking for.
Big thanks,
Zohar.

On 4/1/06, peter royal <pr...@apache.org> wrote:
>
> On Apr 1, 2006, at 4:05 PM, Zohar Amir wrote:
> > let's say that the client sent 3 messages together (single TCP
> > send). In
> > that case my CumulativeProtocolDecoder starts decoding and can
> > actually
> > produce 3 message objects, but once decode returns true, my
> > IoHandler's
> > messageReceived is invoked with only one of them.  Is there a way
> > to produce
> > the 3 objects and make MINA invoke messageReceived repeatedly with
> > all 3?
>
> a CumulativeProtocolDecoder should only decode one message at a time.
> Decode one message, return true. If there is still data available, it
> will be called again, as many times as necessary as long as there is
> data and you return true. And then you will have discrete
> messageReceived invocations with each message that you have decoded.
> -pete
>
>
> --
> proyal@apache.org - http://fotap.org/~osi
>
>
>
>
>

Re: still looking for help with StreamIoHandler

Posted by peter royal <pr...@apache.org>.
On Apr 1, 2006, at 4:05 PM, Zohar Amir wrote:
> let's say that the client sent 3 messages together (single TCP  
> send). In
> that case my CumulativeProtocolDecoder starts decoding and can  
> actually
> produce 3 message objects, but once decode returns true, my  
> IoHandler's
> messageReceived is invoked with only one of them.  Is there a way  
> to produce
> the 3 objects and make MINA invoke messageReceived repeatedly with  
> all 3?

a CumulativeProtocolDecoder should only decode one message at a time.  
Decode one message, return true. If there is still data available, it  
will be called again, as many times as necessary as long as there is  
data and you return true. And then you will have discrete  
messageReceived invocations with each message that you have decoded.
-pete


-- 
proyal@apache.org - http://fotap.org/~osi



Re: still looking for help with StreamIoHandler

Posted by Zohar Amir <zo...@gmail.com>.
let's say that the client sent 3 messages together (single TCP send). In
that case my CumulativeProtocolDecoder starts decoding and can actually
produce 3 message objects, but once decode returns true, my IoHandler's
messageReceived is invoked with only one of them.  Is there a way to produce
the 3 objects and make MINA invoke messageReceived repeatedly with all 3?

On 4/1/06, peter royal <pr...@apache.org> wrote:
>
> On Apr 1, 2006, at 3:49 PM, Zohar Amir wrote:
> > Yes, it makes sense, but what about the other issue - multiple
> > messages sent
> > together? Can I decode a few messages and let MINA invoke my
> > IoHandler's
> > messageReceived method with all of them somehow?
>
> IoHandler.messageReceived will be called once for each message that
> you decode. Is that what you want?
> -pete
>
> --
> proyal@apache.org - http://fotap.org/~osi
>
>
>
>
>

Re: still looking for help with StreamIoHandler

Posted by peter royal <pr...@apache.org>.
On Apr 1, 2006, at 3:49 PM, Zohar Amir wrote:
> Yes, it makes sense, but what about the other issue - multiple  
> messages sent
> together? Can I decode a few messages and let MINA invoke my  
> IoHandler's
> messageReceived method with all of them somehow?

IoHandler.messageReceived will be called once for each message that  
you decode. Is that what you want?
-pete

-- 
proyal@apache.org - http://fotap.org/~osi



Re: still looking for help with StreamIoHandler

Posted by Zohar Amir <zo...@gmail.com>.
Yes, it makes sense, but what about the other issue - multiple messages sent
together? Can I decode a few messages and let MINA invoke my IoHandler's
messageReceived method with all of them somehow?

On 4/1/06, peter royal <pr...@apache.org> wrote:
>
> On Apr 1, 2006, at 2:39 PM, Zohar Amir wrote:
> > 1. What's so bad about StreamIoHandler?
>
> its best used for integration with "legacy" code that is stream-
> based. When working in MINA, you'll get the best performance by not
> dropping back to blocking streams.
>
> > 2. I've searched in
> > mina-0.9.2\examples\src\main\java\org\apache\mina\examples for
> > ProtocolCodecFilter but didn't find anything - can you point at the
> > right
> > example?
>
> the 'sumup' example.. http://svn.apache.org/viewcvs.cgi/directory/
> trunks/mina/examples/src/main/java/org/apache/mina/examples/sumup/
>
> > 3. Just from looking at the javadoc, it seems like messageReceived is
> > invoked with a message - how is it created? Can this message be
> > created with
> > data from several packets arriving (cellular networks are sometimes
> > slow)?
> > What if several protocol messages are sent together?
> > Thanks again,
>
> You provide the ProtocolCodecFilter with a ProtocolEncoder and a
> ProtocolDecoder. Encoder is simple, it receives an application-level
> message (some sort of Object) and should turn it into a ByteBuffer.
>
> For the Decoder, I highly recommend extending
> CumulativeProtocolDecoder. It will help your packet problem. You
> subclass 'doDecode', and if you don't have a full packet in the
> ByteBuffer, just return false, and nothing further will happen. The
> next received packet will be appended, and it will be called again,
> Just decode a single message, return true, and if there is more data,
> your decoder will be called again.
>
> Then, in your IoHandler / filters after the ProtocolCodecFilter, the
> object in messageReceived will be the object that you decoded, rather
> than a raw ByteBuffer.
>
> Make sense?
>
> -pete
>
>
>
> --
> proyal@apache.org - http://fotap.org/~osi
>
>
>
>
>

Re: still looking for help with StreamIoHandler

Posted by peter royal <pr...@apache.org>.
On Apr 1, 2006, at 2:39 PM, Zohar Amir wrote:
> 1. What's so bad about StreamIoHandler?

its best used for integration with "legacy" code that is stream- 
based. When working in MINA, you'll get the best performance by not  
dropping back to blocking streams.

> 2. I've searched in
> mina-0.9.2\examples\src\main\java\org\apache\mina\examples for
> ProtocolCodecFilter but didn't find anything - can you point at the  
> right
> example?

the 'sumup' example.. http://svn.apache.org/viewcvs.cgi/directory/ 
trunks/mina/examples/src/main/java/org/apache/mina/examples/sumup/

> 3. Just from looking at the javadoc, it seems like messageReceived is
> invoked with a message - how is it created? Can this message be  
> created with
> data from several packets arriving (cellular networks are sometimes  
> slow)?
> What if several protocol messages are sent together?
> Thanks again,

You provide the ProtocolCodecFilter with a ProtocolEncoder and a  
ProtocolDecoder. Encoder is simple, it receives an application-level  
message (some sort of Object) and should turn it into a ByteBuffer.

For the Decoder, I highly recommend extending  
CumulativeProtocolDecoder. It will help your packet problem. You  
subclass 'doDecode', and if you don't have a full packet in the  
ByteBuffer, just return false, and nothing further will happen. The  
next received packet will be appended, and it will be called again,  
Just decode a single message, return true, and if there is more data,  
your decoder will be called again.

Then, in your IoHandler / filters after the ProtocolCodecFilter, the  
object in messageReceived will be the object that you decoded, rather  
than a raw ByteBuffer.

Make sense?

-pete



-- 
proyal@apache.org - http://fotap.org/~osi



Re: still looking for help with StreamIoHandler

Posted by Zohar Amir <zo...@gmail.com>.
Thanks for your reply.
1. What's so bad about StreamIoHandler?
2. I've searched in
mina-0.9.2\examples\src\main\java\org\apache\mina\examples for
ProtocolCodecFilter but didn't find anything - can you point at the right
example?
3. Just from looking at the javadoc, it seems like messageReceived is
invoked with a message - how is it created? Can this message be created with
data from several packets arriving (cellular networks are sometimes slow)?
What if several protocol messages are sent together?
Thanks again,
Zohar.

On 4/1/06, peter royal <pr...@apache.org> wrote:
>
> On Apr 1, 2006, at 2:05 PM, Zohar Amir wrote:
> > I'll try to sum up my problem again:
> > I have mobile clients connecting to my server via TCP. Clienmts may
> > send
> > more than a single message at a time. Being mobile clients,
> > messages may be
> > slow in arriving.
> > All this lead me to try and use the StreamIoHandler. When trying to
> > do so
> > I'm facing a problem with clients that send a single message and
> > then close
> > the connection - this leads to the invocation of the sessionClosed
> > method
> > before the new handling thread gets a chance to read the available
> > data from
> > the incoming stream.
> > Can anyone please help me with this?
>
> I highly recommend *not* using the StreamIoHandler. Use the
> ProtocolCodecFilter to implement your protocol. There are some
> examples in the mina distribution that illustrate its use.
> -pete
>
> --
> proyal@apache.org - http://fotap.org/~osi
>
>
>
>
>

Re: still looking for help with StreamIoHandler

Posted by peter royal <pr...@apache.org>.
On Apr 1, 2006, at 2:05 PM, Zohar Amir wrote:
> I'll try to sum up my problem again:
> I have mobile clients connecting to my server via TCP. Clienmts may  
> send
> more than a single message at a time. Being mobile clients,  
> messages may be
> slow in arriving.
> All this lead me to try and use the StreamIoHandler. When trying to  
> do so
> I'm facing a problem with clients that send a single message and  
> then close
> the connection - this leads to the invocation of the sessionClosed  
> method
> before the new handling thread gets a chance to read the available  
> data from
> the incoming stream.
> Can anyone please help me with this?

I highly recommend *not* using the StreamIoHandler. Use the  
ProtocolCodecFilter to implement your protocol. There are some  
examples in the mina distribution that illustrate its use.
-pete

-- 
proyal@apache.org - http://fotap.org/~osi