Chowdhury, Shubhankar [WHQKA] wrote:
Thanks Emanuel but actually I the message lengths vary in each message - that's part of the message itself. If you see the message format "14-2-Data_length" - so starting at 14th byte and I will have an short value giving me the data length.
This is cover but my explaination : as soon as you are able to decode the length, you know how many bytes to get to have a complete portion of your full message. The only problem is that the length, stored as four bytes (if its an int, for instance), has to be decoded itself. But at least, you know that you expect 4 bytes to decode an int.
Once I receive the whole message then only I can apply the byte off-sets to extract the message body and header etc.
This is not the right way to decode your message. Here, you assume you already have received the full message. Just consider that your message can arrive byte by byte, you just have to make your decoder happy with that. Once you cand deal with such a flow of separated bytes, every other cases are just simple.


but with in.available() I would have to know the message length before hand 
isn't it? Am I missing something?
yep. You are missing the fact that you can decode what you have already received.
Thanks,
Shubhankar
847-700-4452
-----Original Message-----
From: Emmanuel Lecharny [mailto:[email protected]] On Behalf Of Emmanuel 
Lecharny
Sent: Friday, June 19, 2009 5:46 PM
To: [email protected]
Subject: Re: Handling variable length request/response in MINA

Chowdhury, Shubhankar [WHQKA] wrote:
o o the whole format got garbled - let me try in plain text
----------------------------------------------------------
----------------------------------------------------------

This might be a very dumb post but please help.
I have already used MINA for a few projects mainly using the
CumulativeProtocolDecoder and RequestResponseFilter for the client
applications and using TextlineCodec for a few server instances. But my
next attempt is a bit complex where the data that comes is a structured
data of variable length basically of the following format-

Offset-LengthInByte-FieldName
------------------------------
0-8-application_id
8-2-Version
10-2-Type
12-2-message_id_number
14-2-Data_length
16-4-Reserved
20-data_length-Data

The application would be a server listening to a port (TCP) and also
would return back some response based on the type field. So the client
would initiate by sending a "login-request" - in response we have to
send "accept" or "reject", then the data would be flowing, then log-out,
keep-alive etc and all of them would follow the above structure.

How do I handle this using MINA? Is there anyway I can use the
CumulativeProtocolDecoder?
Actually, it will easy. As you know the length of your message, it's just a matter of storing what has already been received in the session, until you got all the message.

The decoder must be able to stop in the middle of an int, for instance, which is not that complicated, assuming that an int is a fixed size element. You just have to check that you have 4 bytes available in the buffer to decode the int, otherwise, you just store the buffer in the session, and wait for the next bytes to come.

Sounds complicated, but it's not.

The real issue is if you have very long messages : in this case, you may want to stream to a file the incoming data, to avoid a OOM.

When you have received everything, then you can push the resulting eleme,t to your handler (if of course you built a data structure on the fly).

Hope it helps.



--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org


Reply via email to