You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Stefan Bischof <st...@gmail.com> on 2007/02/01 14:28:34 UTC

newbiequestion: protocolcodec errorhandling

Hi,

I am working on programming an ICAP-server (a HTTP-like protocol). So 
far I just adapted the HTTP codec example. I created IcapRequestDecoder, 
IcapResponseEncoder and ServerHandler (just like the HTTP example).

Now I have a question regarding errors:

Scenario:
The client sends me a message which violates the protocol. As far as I 
understood, this problem should be detected in 
IcapRequestDecoder.decode. Right?

Ok. Now I have three options to handle such an error (I have to inform 
the client what the problem was - like HTTP '400 Bad Request'):
1. send a message immediately; return MessageDecoderResult.NOT_OK (not 
so good idea i think)
2. let ServerHandler.exceptionCaught send the error message to the 
client. In the second case I don't know how to find out what Exception 
was thrown or what message to return to the client. cause.getMessage() 
don't just returns the message of my Exception, but Exceptionname and 
Hexdump information.
3. Just return MessageDecoderResult.NOT_OK, get to 
ServerHandler.exceptionCaught and I have the same problem as before.

What is your recommended way, of informing the client of errors using 
protocol codecs?

Sorry, if I overlooked something, which should be obvious - as the 
subject says, I'm a MINA-newbie.

best regards
Stefan Bischof

Re: newbiequestion: protocolcodec errorhandling

Posted by Trustin Lee <tr...@gmail.com>.
On 4/5/07, Coding Horse <zh...@hotmail.com> wrote:
>
> My next question is how to clean up after(or before?) NOT_OK is returned from
> decodable() when protocol being violated.  I want to close the session.
> Should I clean up the buffer by myself?  Why I asked this question is
> because I got "ProtocolDecoderException: No appropriate message decoder".
> What is the correct way to handle this since I don't want to decode it(I
> know that protocol was violated already)?

DemuxingProtocolCodecFactory automatically throws a
ProtocolDecoderException ("no appropriate message decoder") when
there's no candidate left.  So you will be notified via
exceptionCaught handler method.  You can close the session there.

If you allocated some resource that needs explicit deallocation, then
you have to take care of it within decodable(), but I don't think most
decodable() implementations won't need such complex logic.  If there's
one, please let me know.

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

Re: newbiequestion: protocolcodec errorhandling

Posted by Coding Horse <zh...@hotmail.com>.
My next question is how to clean up after(or before?) NOT_OK is returned from
decodable() when protocol being violated.  I want to close the session. 
Should I clean up the buffer by myself?  Why I asked this question is
because I got "ProtocolDecoderException: No appropriate message decoder". 
What is the correct way to handle this since I don't want to decode it(I
know that protocol was violated already)?

thanks again!


Trustin Lee wrote:
> 
> On 4/5/07, Coding Horse <zh...@hotmail.com> wrote:
>>
>> Hi, Trustin,
>>
>> Could you please explain what will happen if NOT_OK is returned in
>> decodable()?  Is it the recommended way to check protocol header and if a
>> violation detected return NOT_OK in decodable()?  Thx.
> 
> Then it will be excluded from the candidate MessageDecoder list.
> That's all.  Decodable() is just a predicate, so we can simply return
> NOT_OK if protocol violation is detected.
> 
> You will have to program decodable() as simple as possible so it
> doesn't take long time to decide it can be decoded by itself or not.
> Any further detailed check should be in decode().
> 
> Trustin
> -- 
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP Key ID: 0x0255ECA6
> 
> 

-- 
View this message in context: http://www.nabble.com/newbiequestion%3A-protocolcodec-errorhandling-tf3154829.html#a9849090
Sent from the mina dev mailing list archive at Nabble.com.


Re: newbiequestion: protocolcodec errorhandling

Posted by Trustin Lee <tr...@gmail.com>.
On 4/5/07, Coding Horse <zh...@hotmail.com> wrote:
>
> Hi, Trustin,
>
> Could you please explain what will happen if NOT_OK is returned in
> decodable()?  Is it the recommended way to check protocol header and if a
> violation detected return NOT_OK in decodable()?  Thx.

Then it will be excluded from the candidate MessageDecoder list.
That's all.  Decodable() is just a predicate, so we can simply return
NOT_OK if protocol violation is detected.

You will have to program decodable() as simple as possible so it
doesn't take long time to decide it can be decoded by itself or not.
Any further detailed check should be in decode().

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

Re: newbiequestion: protocolcodec errorhandling

Posted by Coding Horse <zh...@hotmail.com>.
Hi, Trustin,

Could you please explain what will happen if NOT_OK is returned in
decodable()?  Is it the recommended way to check protocol header and if a
violation detected return NOT_OK in decodable()?  Thx.


Trustin Lee wrote:
> 
> On 2/3/07, Stefan Bischof <st...@gmail.com> wrote:
>>
>> Hi Trustin,
>>
>> Trustin Lee schrieb:
>> > Hi Stefan,
>> >
>> > On 2/1/07, Stefan Bischof <st...@gmail.com> wrote:
>> >
>> > Now I have a question regarding errors:
>> >>
>> >> Scenario:
>> >> The client sends me a message which violates the protocol. As far as I
>> >> understood, this problem should be detected in
>> >> IcapRequestDecoder.decode. Right?
>> >
>> >
>> > Yes, you can *detect* the problem, but you could also just do what you
>> > are
>> > supposed to do (i.e. parsing the message) and let the exception arise
>> > due to
>> > a unexpected value.  You don't need to perform every integrity check on
>> a
>> > received message as long as an exception can be thrown.
>> >
>> > Once an exception is throw from a ProtocolDecoder, MINA wraps the
>> > exception
>> > with ProtocolDecoderException, and forward it to your IoHandler's
>> > exceptionCaught() handler method.  You can check the type of the cause
>> > (exception) using 'instanceof' keyword:
>> >
>> > if (cause instanceof ProtocolDecoderException) {
>> >    Throwable causeOfCause = cause.getCause(); // what is thrown from
>> > ProtocolDecoder
>> >    session.write(new FourOhOhMessage());
>> > }
>> Thanks! I just tried and it naturally worked (after realizing some
>> strange eclipse behaviour, but thats another story). I just wasn't sure
>> about the 'MINA-way' of handling protocol decoding error issues.
>> > I've never thought about using MessageDecoderResult.NOT_OK, but using
>> > ProtocolDecoderException is better IMHO.  It sounds interesting though.
>> I wasn't sure what the NOT_OK is really for. But then I found a line in
>> the Javadoc for MessageDecoder.decode (the returns-section): "Returns:
>> [...] NOT_OK if you cannot decode current message due to protocol
>> specification violation.", and I thought, "hey, this is what I need". In
>> which case would you really need NOT_OK?
> 
> 
> NOT_OK is for decodable().  It's not usually for decode().  We need to fix
> the documentation.  Thanks for the info!
> 
> BTW: If you are interested, I could translate tutorials to german (when
>> they are ready).
> 
> 
> Very cool!
> 
> Trustin
> -- 
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP key fingerprints:
> * E167 E6AF E73A CBCE EE41  4A29 544D DE48 FE95 4E7E
> * B693 628E 6047 4F8F CFA4  455E 1C62 A7DC 0255 ECA6
> 
> 

-- 
View this message in context: http://www.nabble.com/newbiequestion%3A-protocolcodec-errorhandling-tf3154829.html#a9848301
Sent from the mina dev mailing list archive at Nabble.com.


Re: newbiequestion: protocolcodec errorhandling

Posted by Trustin Lee <tr...@gmail.com>.
On 2/3/07, Stefan Bischof <st...@gmail.com> wrote:
>
> Hi Trustin,
>
> Trustin Lee schrieb:
> > Hi Stefan,
> >
> > On 2/1/07, Stefan Bischof <st...@gmail.com> wrote:
> >
> > Now I have a question regarding errors:
> >>
> >> Scenario:
> >> The client sends me a message which violates the protocol. As far as I
> >> understood, this problem should be detected in
> >> IcapRequestDecoder.decode. Right?
> >
> >
> > Yes, you can *detect* the problem, but you could also just do what you
> > are
> > supposed to do (i.e. parsing the message) and let the exception arise
> > due to
> > a unexpected value.  You don't need to perform every integrity check on
> a
> > received message as long as an exception can be thrown.
> >
> > Once an exception is throw from a ProtocolDecoder, MINA wraps the
> > exception
> > with ProtocolDecoderException, and forward it to your IoHandler's
> > exceptionCaught() handler method.  You can check the type of the cause
> > (exception) using 'instanceof' keyword:
> >
> > if (cause instanceof ProtocolDecoderException) {
> >    Throwable causeOfCause = cause.getCause(); // what is thrown from
> > ProtocolDecoder
> >    session.write(new FourOhOhMessage());
> > }
> Thanks! I just tried and it naturally worked (after realizing some
> strange eclipse behaviour, but thats another story). I just wasn't sure
> about the 'MINA-way' of handling protocol decoding error issues.
> > I've never thought about using MessageDecoderResult.NOT_OK, but using
> > ProtocolDecoderException is better IMHO.  It sounds interesting though.
> I wasn't sure what the NOT_OK is really for. But then I found a line in
> the Javadoc for MessageDecoder.decode (the returns-section): "Returns:
> [...] NOT_OK if you cannot decode current message due to protocol
> specification violation.", and I thought, "hey, this is what I need". In
> which case would you really need NOT_OK?


NOT_OK is for decodable().  It's not usually for decode().  We need to fix
the documentation.  Thanks for the info!

BTW: If you are interested, I could translate tutorials to german (when
> they are ready).


Very cool!

Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41  4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4  455E 1C62 A7DC 0255 ECA6

Re: newbiequestion: protocolcodec errorhandling

Posted by Stefan Bischof <st...@gmail.com>.
Hi Trustin,

Trustin Lee schrieb:
> Hi Stefan,
>
> On 2/1/07, Stefan Bischof <st...@gmail.com> wrote:
>
> Now I have a question regarding errors:
>>
>> Scenario:
>> The client sends me a message which violates the protocol. As far as I
>> understood, this problem should be detected in
>> IcapRequestDecoder.decode. Right?
>
>
> Yes, you can *detect* the problem, but you could also just do what you 
> are
> supposed to do (i.e. parsing the message) and let the exception arise 
> due to
> a unexpected value.  You don't need to perform every integrity check on a
> received message as long as an exception can be thrown.
>
> Once an exception is throw from a ProtocolDecoder, MINA wraps the 
> exception
> with ProtocolDecoderException, and forward it to your IoHandler's
> exceptionCaught() handler method.  You can check the type of the cause
> (exception) using 'instanceof' keyword:
>
> if (cause instanceof ProtocolDecoderException) {
>    Throwable causeOfCause = cause.getCause(); // what is thrown from
> ProtocolDecoder
>    session.write(new FourOhOhMessage());
> }
Thanks! I just tried and it naturally worked (after realizing some 
strange eclipse behaviour, but thats another story). I just wasn't sure 
about the 'MINA-way' of handling protocol decoding error issues.
> I've never thought about using MessageDecoderResult.NOT_OK, but using
> ProtocolDecoderException is better IMHO.  It sounds interesting though.
I wasn't sure what the NOT_OK is really for. But then I found a line in 
the Javadoc for MessageDecoder.decode (the returns-section): "Returns: 
[...] NOT_OK if you cannot decode current message due to protocol 
specification violation.", and I thought, "hey, this is what I need". In 
which case would you really need NOT_OK?
> HTH,
> Trustin
Thanks for your help.

BTW: If you are interested, I could translate tutorials to german (when 
they are ready).

regards,
Stefan Bischof

Re: newbiequestion: protocolcodec errorhandling

Posted by Trustin Lee <tr...@gmail.com>.
Hi Stefan,

On 2/1/07, Stefan Bischof <st...@gmail.com> wrote:
>
> Hi,
>
> I am working on programming an ICAP-server (a HTTP-like protocol). So
> far I just adapted the HTTP codec example. I created IcapRequestDecoder,
> IcapResponseEncoder and ServerHandler (just like the HTTP example).


Sounds cool!

Now I have a question regarding errors:
>
> Scenario:
> The client sends me a message which violates the protocol. As far as I
> understood, this problem should be detected in
> IcapRequestDecoder.decode. Right?


Yes, you can *detect* the problem, but you could also just do what you are
supposed to do (i.e. parsing the message) and let the exception arise due to
a unexpected value.  You don't need to perform every integrity check on a
received message as long as an exception can be thrown.

Once an exception is throw from a ProtocolDecoder, MINA wraps the exception
with ProtocolDecoderException, and forward it to your IoHandler's
exceptionCaught() handler method.  You can check the type of the cause
(exception) using 'instanceof' keyword:

if (cause instanceof ProtocolDecoderException) {
    Throwable causeOfCause = cause.getCause(); // what is thrown from
ProtocolDecoder
    session.write(new FourOhOhMessage());
}

I've never thought about using MessageDecoderResult.NOT_OK, but using
ProtocolDecoderException is better IMHO.  It sounds interesting though.

HTH,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41  4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4  455E 1C62 A7DC 0255 ECA6