You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Norman Maurer <nm...@byteaction.de> on 2007/01/22 12:44:28 UTC

Problems with writing a throttling IoFilter

Hi guys,

i started to testing MINA the last days for using in a SMTPServer. So 
far im very impressed. I wrote some IoFilters by extending 
IoFilterAdapter and all works fine.

Now i stuck on a new IoFilter.  I want to catch a message which is send 
from the Server and write on Char of the message per line to the client.

I tried this code but it always write the original response:

----------------------------------------------------
public class ThrottlingFilter extends IoFilterAdapter {
 
    @Override
    public void messageSent(NextFilter arg0, IoSession arg1, Object 
arg2) throws Exception {
 
        if (arg2 instanceof String) {
            String response = (String) arg2;
       
            for (int i = 0; i < response.length(); i++) {
                super.messageSent(arg0, arg1, response.charAt(i));
            }
        } else {
            super.messageSent(arg0, arg1, arg2);
        }
    }
}

-------------------------------------------------------

I checked that it reach the loop by putting some debug code in it.. So 
thats not the problem.

Any one can tell me what i did wrong ?

The other question is howto "sleep" for a given time before send 
response. Is Thread.sleep(long) ok in a IoFilter ?

Thx
Norman




Re: Problems with writing a throttling IoFilter

Posted by Niklas Therning <ni...@trillian.se>.
Norman Maurer wrote:
> Hi guys,
>
> i started to testing MINA the last days for using in a SMTPServer. So
> far im very impressed. I wrote some IoFilters by extending
> IoFilterAdapter and all works fine.
>
> Now i stuck on a new IoFilter.  I want to catch a message which is
> send from the Server and write on Char of the message per line to the
> client.
>
> I tried this code but it always write the original response:
>
> ----------------------------------------------------
> public class ThrottlingFilter extends IoFilterAdapter {
>
>    @Override
>    public void messageSent(NextFilter arg0, IoSession arg1, Object
> arg2) throws Exception {
>
>        if (arg2 instanceof String) {
>            String response = (String) arg2;
>                  for (int i = 0; i < response.length(); i++) {
>                super.messageSent(arg0, arg1, response.charAt(i));
>            }
>        } else {
>            super.messageSent(arg0, arg1, arg2);
>        }
>    }
> }
>
> -------------------------------------------------------
>
> I checked that it reach the loop by putting some debug code in it.. So
> thats not the problem.
>
> Any one can tell me what i did wrong ?
If I understand you correctly you want to intercept any String written
by the IoHandler to the MINA session and modify what actually gets
written. In that case you should probably override filterWrite() instead
of messageSent(). messageSent() gets called by MINA once a message has
been written successfully to the socket.
>
> The other question is howto "sleep" for a given time before send
> response. Is Thread.sleep(long) ok in a IoFilter ?
If you do sleep in an IoFilter you should have an ExecutorFilter in your
filter chain (MINA adds one by default) and your IoFilter should come
after the ExecutorFilter in the chain (chain.addLast(...) will take care
of that in most cases). The important thing is that you don't want to
call sleep() in the main IO thread since that will suspend all IO
activities.

Another option would be to use java.util.Timer.

HTH

-- 
Niklas Therning
www.spamdrain.net


Re: Problems with writing a throttling IoFilter

Posted by Norman Maurer <nm...@byteaction.de>.
Hi Niklas,

thank you for the help. Overriding filterWrite(..) did the job :-)

bye
Norman

Niklas Therning schrieb:
> Norman Maurer wrote:
>   
>> Hi guys,
>>
>> i started to testing MINA the last days for using in a SMTPServer. So
>> far im very impressed. I wrote some IoFilters by extending
>> IoFilterAdapter and all works fine.
>>
>> Now i stuck on a new IoFilter.  I want to catch a message which is
>> send from the Server and write on Char of the message per line to the
>> client.
>>
>> I tried this code but it always write the original response:
>>
>> ----------------------------------------------------
>> public class ThrottlingFilter extends IoFilterAdapter {
>>
>>    @Override
>>    public void messageSent(NextFilter arg0, IoSession arg1, Object
>> arg2) throws Exception {
>>
>>        if (arg2 instanceof String) {
>>            String response = (String) arg2;
>>                  for (int i = 0; i < response.length(); i++) {
>>                super.messageSent(arg0, arg1, response.charAt(i));
>>            }
>>        } else {
>>            super.messageSent(arg0, arg1, arg2);
>>        }
>>    }
>> }
>>
>> -------------------------------------------------------
>>
>> I checked that it reach the loop by putting some debug code in it.. So
>> thats not the problem.
>>
>> Any one can tell me what i did wrong ?
>>     
> If I understand you correctly you want to intercept any String written
> by the IoHandler to the MINA session and modify what actually gets
> written. In that case you should probably override filterWrite() instead
> of messageSent(). messageSent() gets called by MINA once a message has
> been written successfully to the socket.
>   
>> The other question is howto "sleep" for a given time before send
>> response. Is Thread.sleep(long) ok in a IoFilter ?
>>     
> If you do sleep in an IoFilter you should have an ExecutorFilter in your
> filter chain (MINA adds one by default) and your IoFilter should come
> after the ExecutorFilter in the chain (chain.addLast(...) will take care
> of that in most cases). The important thing is that you don't want to
> call sleep() in the main IO thread since that will suspend all IO
> activities.
>
> Another option would be to use java.util.Timer.
>
> HTH
>
>