You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Gustavo <gu...@yahoo.com.ar> on 2007/06/22 18:21:55 UTC

CumulativeProtocolDecoder with messages delimited by CRLF

Hi everyone! I'm new to Mina and I found it very interesting. I've some
questions related to CumulativeProtocolDecoder and messages delimited by
CRLF.

Here is the situation: I've got to connect to a socket where I receive text
messages from (I'm a client), this is how I configure my client:
        SocketConnectorConfig cfg = new SocketConnectorConfig();
        cfg.setConnectTimeout(30);
        cfg.getFilterChain().addLast(
                "CODEC", new ProtocolCodecFilter(new OWNCodecFactory()));
(1)
        int n = this.connProps.getConnectAttempts();
        SocketAddress addr = new InetSocketAddress(
                connProps.getHost(), 
                connProps.getPort());
        
(1) OWNCodecFactory is an extension of DemuxingProtocolCodecFactory, where I
add my encoders and decoders.

At this point there's no problem, I can connect to the socket and read
messages from it. The problems begins when the sockets sends more than one
text message in the same writting (messages are delimited by a CRLF), here
are some examples:

Message one: MESSAGE_TEXT<CR><LF>
Message two: MESSAGE_TEXT<CR><LF>MESSAGE_TEXT2<CR><LF>

I have to split the buffer into several messages and process them. At first
point I thought of an extension of CumulativeProtocolDecoder (because I need
the 'cumulative' functionality) that splits the ByteBuffer into several
chunks (one per message) and does exactly the same thing as
CumulativeProtocolDecoder, but with every chunk of the ByteBuffer received.
Once I have the message splitted and decoded I have to make them get to my
implementation of an IoHandlerAdapter to do some business logic. 
These are my questions:
a) Is this clear enough? ;) - apologies for my english
b) Is this a good aproach?
c) Is there a better and transparent way to do such a thing (first decoding
to split messages, and then process them)?
d) What if one of the decoders fails and throws an exception? I need to
process all messages independently

Thanks in advance,
Gustavo


-- 
View this message in context: http://www.nabble.com/CumulativeProtocolDecoder-with-messages-delimited-by-CRLF-tf3965652.html#a11255508
Sent from the mina dev mailing list archive at Nabble.com.


Re: CumulativeProtocolDecoder with messages delimited by CRLF

Posted by Germán Borbolla Flores <gb...@insys-corp.com.mx>.
Hi Gustavo,

Also take a look at the examples, especially the haiku one, I think it 
does exactly what you need.

Maarten Bosteels wrote:
> Hi Gustavo,
>
> On 6/22/07, Gustavo <gu...@yahoo.com.ar> wrote:
>>
>>
>> Hi everyone! I'm new to Mina and I found it very interesting. I've
> some
>> questions related to CumulativeProtocolDecoder and messages delimited
> by
>> CRLF.
>>
>> Here is the situation: I've got to connect to a socket where I receive
>> text
>> messages from (I'm a client), this is how I configure my client:
>>         SocketConnectorConfig cfg = new SocketConnectorConfig();
>>         cfg.setConnectTimeout(30);
>>         cfg.getFilterChain().addLast(
>>                 "CODEC", new ProtocolCodecFilter(new
> OWNCodecFactory()));
>> (1)
>>         int n = this.connProps.getConnectAttempts();
>>         SocketAddress addr = new InetSocketAddress(
>>                 connProps.getHost(),
>>                 connProps.getPort());
>>
>> (1) OWNCodecFactory is an extension of DemuxingProtocolCodecFactory,
> where
>> I
>> add my encoders and decoders.
>>
>> At this point there's no problem, I can connect to the socket and read
>> messages from it. The problems begins when the sockets sends more than
> one
>> text message in the same writting (messages are delimited by a CRLF),
> here
>> are some examples:
>>
>> Message one: MESSAGE_TEXT<CR><LF>
>> Message two: MESSAGE_TEXT<CR><LF>MESSAGE_TEXT2<CR><LF>
>>
>> I have to split the buffer into several messages and process them. At
>> first
>> point I thought of an extension of CumulativeProtocolDecoder (because
> I
>> need
>> the 'cumulative' functionality) that splits the ByteBuffer into
> several
>> chunks (one per message) and does exactly the same thing as
>> CumulativeProtocolDecoder, but with every chunk of the ByteBuffer
>> received.
>> Once I have the message splitted and decoded I have to make them get
> to my
>> implementation of an IoHandlerAdapter to do some business logic.
>> These are my questions:
>> a) Is this clear enough? ;) - apologies for my english
>> b) Is this a good aproach?
>> c) Is there a better and transparent way to do such a thing (first
>> decoding
>> to split messages, and then process them)?
>> d) What if one of the decoders fails and throws an exception? I need
> to
>> process all messages independently
>
>
> 1) Have you read
> http://mina.apache.org/tutorial-on-protocolcodecfilter.html
> 2) Have you seen the TextLineCodecFactory ? I think it already does what
> you
> want.
> If not, the implementation might be good ispiration :-)
>
> http://mina.apache.org/report/1.1/apidocs/org/apache/mina/filter/codec/t
> extline/TextLineCodecFactory.html
>
> Maarten
>
> Thanks in advance,
>> Gustavo
>>
>>
>> -- 
>> View this message in context:
>>
> http://www.nabble.com/CumulativeProtocolDecoder-with-messages-delimited-
> by-CRLF-tf3965652.html#a11255508
>> Sent from the mina dev mailing list archive at Nabble.com.
>>
>>
>

Re: CumulativeProtocolDecoder with messages delimited by CRLF

Posted by Maarten Bosteels <mb...@gmail.com>.
Hi Gustavo,

On 6/22/07, Gustavo <gu...@yahoo.com.ar> wrote:
>
>
> Hi everyone! I'm new to Mina and I found it very interesting. I've some
> questions related to CumulativeProtocolDecoder and messages delimited by
> CRLF.
>
> Here is the situation: I've got to connect to a socket where I receive
> text
> messages from (I'm a client), this is how I configure my client:
>         SocketConnectorConfig cfg = new SocketConnectorConfig();
>         cfg.setConnectTimeout(30);
>         cfg.getFilterChain().addLast(
>                 "CODEC", new ProtocolCodecFilter(new OWNCodecFactory()));
> (1)
>         int n = this.connProps.getConnectAttempts();
>         SocketAddress addr = new InetSocketAddress(
>                 connProps.getHost(),
>                 connProps.getPort());
>
> (1) OWNCodecFactory is an extension of DemuxingProtocolCodecFactory, where
> I
> add my encoders and decoders.
>
> At this point there's no problem, I can connect to the socket and read
> messages from it. The problems begins when the sockets sends more than one
> text message in the same writting (messages are delimited by a CRLF), here
> are some examples:
>
> Message one: MESSAGE_TEXT<CR><LF>
> Message two: MESSAGE_TEXT<CR><LF>MESSAGE_TEXT2<CR><LF>
>
> I have to split the buffer into several messages and process them. At
> first
> point I thought of an extension of CumulativeProtocolDecoder (because I
> need
> the 'cumulative' functionality) that splits the ByteBuffer into several
> chunks (one per message) and does exactly the same thing as
> CumulativeProtocolDecoder, but with every chunk of the ByteBuffer
> received.
> Once I have the message splitted and decoded I have to make them get to my
> implementation of an IoHandlerAdapter to do some business logic.
> These are my questions:
> a) Is this clear enough? ;) - apologies for my english
> b) Is this a good aproach?
> c) Is there a better and transparent way to do such a thing (first
> decoding
> to split messages, and then process them)?
> d) What if one of the decoders fails and throws an exception? I need to
> process all messages independently


1) Have you read http://mina.apache.org/tutorial-on-protocolcodecfilter.html
2) Have you seen the TextLineCodecFactory ? I think it already does what you
want.
If not, the implementation might be good ispiration :-)

http://mina.apache.org/report/1.1/apidocs/org/apache/mina/filter/codec/textline/TextLineCodecFactory.html

Maarten

Thanks in advance,
> Gustavo
>
>
> --
> View this message in context:
> http://www.nabble.com/CumulativeProtocolDecoder-with-messages-delimited-by-CRLF-tf3965652.html#a11255508
> Sent from the mina dev mailing list archive at Nabble.com.
>
>