You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Jesse Hou <hp...@gmail.com> on 2006/11/21 08:49:17 UTC

A question about Mina Design

Hello All,  When I read the source code of Mina, I found that in class
IoFilter there are tow methods "void filterWrite( IoSession session,
WriteRequest writeRequest );" and "void filterClose( IoSession session );"
when they are invoked they go through FilterChain from tail to head , but
other methods go through FilterChain from head to tail, what's the
difference ?  Is there any limitation ? Thank you very much.

Re: A question about Mina Design

Posted by Jesse Hou <hp...@gmail.com>.
      When I read the source code I just found the two different execution
orders, so I feel a little strange.
>From now on, I haven't the user case which must use the same execution
order.  Thanks for your answer.



>
> In case of write, the execution occurs in a reverse order (i.e. filter3,
> filter2, filter1).  Do you really want to override this default?  Could
> you
> tell me what's your use case?
>
> Trustin
> --
> what we call human nature is actually human habit
>
>

Re: A question about Mina Design

Posted by Trustin Lee <tr...@gmail.com>.
On 11/21/06, Jesse Hou <hp...@gmail.com> wrote:
>
> So if I add three filter and the three filter(filter1, filter2, filter3)
> must be execute from head to tail (first to last).  Both of client and
> server side will be add the three filters.  When the server receive the
> message from client , the method messageReceived will be fired and the
> message will travel through filter1 filter2 filter3,  maybe when I invoke
> session.write(message) in method messageReceived  I also want to the
> message
> travel through filter1 filter2 filter3,  then how to do realize it?  and I
> think the filter consuming by session.wirte (close) and messageReceive()
> is
> a little inconsistent. How do you think so ?


In case of write, the execution occurs in a reverse order (i.e. filter3,
filter2, filter1).  Do you really want to override this default?  Could you
tell me what's your use case?

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: A question about Mina Design

Posted by Jesse Hou <hp...@gmail.com>.
OK thank you,  if think of it like a stack , it's very consistent.

I haven't encounter the situation which having all events always
going head to tail, it's just a example. Thank you.


On 11/21/06, Niklas Therning <ni...@trillian.se> wrote:
>
> Jesse Hou wrote:
> > So if I add three filter and the three filter(filter1, filter2, filter3)
> > must be execute from head to tail (first to last).  Both of client and
> > server side will be add the three filters.  When the server receive the
> > message from client , the method messageReceived will be fired and the
> > message will travel through filter1 filter2 filter3,  maybe when I
> invoke
> > session.write(message) in method messageReceived  I also want to the
> > message
> > travel through filter1 filter2 filter3,  then how to do realize it?
> > and I
> > think the filter consuming by session.wirte (close) and
> > messageReceive() is
> > a little inconsistent. How do you think so ?
> When you invoke session.write() the message will travel though filter3
> filter2 filter1. Think of it like a stack (similar to the TCP/IP stack):
>
> IoHandler
>       |
> Filter3
>       |
> Filter2 (LoggingFilter)
>       |
> Filter1 (SSLFilter)
>       |
> Java NIO
>
> Events from NIO (messageReceived, sessionOpened, etc) travels up the
> stack while events from your IoHandler (write, close) travels down the
> stack. If Filter1 is an SSLFilter you wouldn't want the data you write
> using session.write() to be encrypted *before* it gets logged using
> Filter2 would you?
>
> It's not inconsistent at all acutally. :) Just think of it as events
> traveling upstream (from NIO to your handler) and downstream (from your
> handler to NIO).
>
> What is it that you would like to achieve by having all events always
> going head to tail?
>
> --
> Niklas Therning
> www.spamdrain.net
>
>

Re: A question about Mina Design

Posted by Niklas Therning <ni...@trillian.se>.
Jesse Hou wrote:
> So if I add three filter and the three filter(filter1, filter2, filter3)
> must be execute from head to tail (first to last).  Both of client and
> server side will be add the three filters.  When the server receive the
> message from client , the method messageReceived will be fired and the
> message will travel through filter1 filter2 filter3,  maybe when I invoke
> session.write(message) in method messageReceived  I also want to the
> message
> travel through filter1 filter2 filter3,  then how to do realize it? 
> and I
> think the filter consuming by session.wirte (close) and
> messageReceive() is
> a little inconsistent. How do you think so ?
When you invoke session.write() the message will travel though filter3
filter2 filter1. Think of it like a stack (similar to the TCP/IP stack):

IoHandler
      |
Filter3
      |
Filter2 (LoggingFilter)
      |
Filter1 (SSLFilter)
      |
Java NIO

Events from NIO (messageReceived, sessionOpened, etc) travels up the
stack while events from your IoHandler (write, close) travels down the
stack. If Filter1 is an SSLFilter you wouldn't want the data you write
using session.write() to be encrypted *before* it gets logged using
Filter2 would you?

It's not inconsistent at all acutally. :) Just think of it as events
traveling upstream (from NIO to your handler) and downstream (from your
handler to NIO).

What is it that you would like to achieve by having all events always
going head to tail?

-- 
Niklas Therning
www.spamdrain.net


Re: A question about Mina Design

Posted by Jesse Hou <hp...@gmail.com>.
So if I add three filter and the three filter(filter1, filter2, filter3)
must be execute from head to tail (first to last).  Both of client and
server side will be add the three filters.  When the server receive the
message from client , the method messageReceived will be fired and the
message will travel through filter1 filter2 filter3,  maybe when I invoke
session.write(message) in method messageReceived  I also want to the message
travel through filter1 filter2 filter3,  then how to do realize it?  and I
think the filter consuming by session.wirte (close) and messageReceive() is
a little inconsistent. How do you think so ?



> No, no limitation. The reason is that these events are generated by your
> code and consumed by MINA. They are typically generated from within your
> IoHandler by a call to session.write() or session.close() and will
> travel through the filter chain in the opposite direction (last filter
> in chain will see the event first). The other events are instead
> generated by MINA and will be consumed by your IoHandler.
>
> --
> Niklas Therning
> www.spamdrain.net
>
>

Re: A question about Mina Design

Posted by Niklas Therning <ni...@trillian.se>.
Jesse Hou wrote:
> Hello All,  When I read the source code of Mina, I found that in class
> IoFilter there are tow methods "void filterWrite( IoSession session,
> WriteRequest writeRequest );" and "void filterClose( IoSession session
> );"
> when they are invoked they go through FilterChain from tail to head , but
> other methods go through FilterChain from head to tail, what's the
> difference ?  Is there any limitation ? Thank you very much.
>
No, no limitation. The reason is that these events are generated by your
code and consumed by MINA. They are typically generated from within your
IoHandler by a call to session.write() or session.close() and will
travel through the filter chain in the opposite direction (last filter
in chain will see the event first). The other events are instead
generated by MINA and will be consumed by your IoHandler.

-- 
Niklas Therning
www.spamdrain.net