You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Paul Furbacher <pf...@mac.com> on 2007/08/20 21:39:10 UTC

Proxy server example -- introduce a random delay from client to proxied server?

I'm trying to modify the proxy server example to introduce a random delay to
messages coming from the client before they are sent to the server.  The
reason for doing this is that I have to simulate scrambling the order of
messages from client to server. 

At the moment, my idea is to wrap the incoming message (byte buffer) in an
object which also has a single-shot timer task, and then putting those
wrapper objects into a collection.  As an individual message times out in
the delay collection, it becomes "deliverable".  It seems as though I need
to create a "message needs sending" event, but I'm not sure how that could
be done.  

I'm new to the mina framework, and would appreciate a few pointers -- not
necessarily any code, just pointers as to what and how to think about this
within the context of the framework.

Thanks.

--

Paul Furbacher

 
-- 
View this message in context: http://www.nabble.com/Proxy-server-example----introduce-a-random-delay-from-client-to-proxied-server--tf4301103s16868.html#a12242579
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: Proxy server example -- introduce a random delay from client to proxied server?

Posted by Trustin Lee <tr...@gmail.com>.
On 8/21/07, Maarten Bosteels <mb...@gmail.com> wrote:
> On 8/21/07, Paul Furbacher <pf...@mac.com> wrote:
> >
> >
> > Maarten,
> >
> > Thanks for the suggestion.  At first read, it seemed so obvious, so
> > simple.
> >
> > But besides a lot of debug error statements like the following
> >
> >   [...] ServerToProxyIoHandler - [localhost/127.0.0.1:9001]
> > java.lang.IllegalStateException: Timer already cancelled.
> >
> > and
> >
> >   [...] ClientToProxyIoHandler - [/127.0.0.1:60974]
> > java.lang.IllegalStateException: Timer already cancelled. ...
>
>
> seems  like you  added the  TimerTask to both ClientToProxy and
> ServerToProxy.
> I only added  it to ClientToProxy and didn't get these errors.
> But it is always possible that the proxied server closes the connection
> before you could forward the request.
>
> the messages arrive in the same order they were dispatched by the client.
>
>
> That's odd, maybe you need higher variation in the delay ?
> Do you have control over the server ? I mean, can you ensure that it does
> not close the connections too early ?
>
> The desired effect of having the randomized delay is to scramble the message
> > order, something like the following:
> >
> > Client send order   1 2 3 4 5 . . .
> >
> > Client receive order   3 1 4 5 2 . . .
> >
> > Perhaps an IoHandler is not the right place to do this? Would an IoFilter
> > be
> > more appropriate?
>
>
> I guess you only want to randomize the messages to test the server ?
> Wouldn't it be simpler then to write a test-case that sends a hard-coded
> list of messages (randomized by hand) ?

Giving delay just randomly can cause mixed order of messages.  For
example, giving 500ms delay to the msg #1 and 1ms to #2 can switch the
order of messages.  The following code will work better:

   private volatile long lastScheduledTime = System.currentTimeMillis();
   private final Random random = new Random();
   private final Timer timer = new Timer();

   public void messageReceived(final IoSession session, final Object
message) throws Exception {
       final int c = counter.incrementAndGet();
       ByteBuffer rb = (ByteBuffer) message;
       final ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
       rb.mark();
       wb.put(rb);
       wb.flip();
       rb.reset();
       long scheduledTime = lastScheduledTime + random.nextInt(500);
       final IoSession proxySession = (IoSession) session.getAttachment();
       timer.schedule(
               new TimerTask() {
                   public void run() {
                       proxySession.write(wb);
                   }
               }, new Date(scheduledTime));;
       lastScheduledTime = scheduledTime;
   }

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

Re: Proxy server example -- introduce a random delay from client to proxied server?

Posted by Maarten Bosteels <mb...@gmail.com>.
On 8/21/07, Paul Furbacher <pf...@mac.com> wrote:
>
>
> Maarten,
>
> Thanks for the suggestion.  At first read, it seemed so obvious, so
> simple.
>
> But besides a lot of debug error statements like the following
>
>   [...] ServerToProxyIoHandler - [localhost/127.0.0.1:9001]
> java.lang.IllegalStateException: Timer already cancelled.
>
> and
>
>   [...] ClientToProxyIoHandler - [/127.0.0.1:60974]
> java.lang.IllegalStateException: Timer already cancelled. ...


seems  like you  added the  TimerTask to both ClientToProxy and
ServerToProxy.
I only added  it to ClientToProxy and didn't get these errors.
But it is always possible that the proxied server closes the connection
before you could forward the request.

the messages arrive in the same order they were dispatched by the client.


That's odd, maybe you need higher variation in the delay ?
Do you have control over the server ? I mean, can you ensure that it does
not close the connections too early ?

The desired effect of having the randomized delay is to scramble the message
> order, something like the following:
>
> Client send order   1 2 3 4 5 . . .
>
> Client receive order   3 1 4 5 2 . . .
>
> Perhaps an IoHandler is not the right place to do this? Would an IoFilter
> be
> more appropriate?


I guess you only want to randomize the messages to test the server ?
Wouldn't it be simpler then to write a test-case that sends a hard-coded
list of messages (randomized by hand) ?

Maarten

--
>
> Paul Furbacher
>
>
>
> Maarten Bosteels-4 wrote:
> >
> > Hello Paul,
> >
> > How about adding this to ClientToProxyIoHandler ?
> >
> >     private Random random = new Random();
> >     private Timer timer = new Timer();
> >
> >     public void messageReceived(final IoSession session, final Object
> > message) throws Exception {
> >         final int c = counter.incrementAndGet();
> >         ByteBuffer rb = (ByteBuffer) message;
> >         final ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
> >         rb.mark();
> >         wb.put(rb);
> >         wb.flip();
> >         rb.reset();
> >         long delay = random.nextInt(500);
> >         final IoSession proxySession = (IoSession)
> > session.getAttachment();
> >         timer.schedule(
> >                 new TimerTask() {
> >                     public void run() {
> >                         proxySession.write(wb);
> >                     }
> >                 }, delay);
> >     }
> >
> > Maarten
> >
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/Proxy-server-example----introduce-a-random-delay-from-client-to-proxied-server--tf4301103s16868.html#a12242579
> >> Sent from the Apache MINA Support Forum mailing list archive at
> >> Nabble.com
> >
>
> --
> View this message in context:
> http://www.nabble.com/Proxy-server-example----introduce-a-random-delay-from-client-to-proxied-server--tf4301103s16868.html#a12248916
> Sent from the Apache MINA Support Forum mailing list archive at Nabble.com
> .
>
>

Re: Proxy server example -- introduce a random delay from client to proxied server?

Posted by Paul Furbacher <pf...@mac.com>.
Maarten,

Thanks for the suggestion.  At first read, it seemed so obvious, so simple.

But besides a lot of debug error statements like the following

  [...] ServerToProxyIoHandler - [localhost/127.0.0.1:9001]
java.lang.IllegalStateException: Timer already cancelled.

and 

  [...] ClientToProxyIoHandler - [/127.0.0.1:60974]
java.lang.IllegalStateException: Timer already cancelled. ...


the messages arrive in the same order they were dispatched by the client.  

The desired effect of having the randomized delay is to scramble the message
order, something like the following:

Client send order   1 2 3 4 5 . . .

Client receive order   3 1 4 5 2 . . .

Perhaps an IoHandler is not the right place to do this? Would an IoFilter be
more appropriate?  

-- 

Paul Furbacher



Maarten Bosteels-4 wrote:
> 
> Hello Paul,
> 
> How about adding this to ClientToProxyIoHandler ?
> 
>     private Random random = new Random();
>     private Timer timer = new Timer();
> 
>     public void messageReceived(final IoSession session, final Object
> message) throws Exception {
>         final int c = counter.incrementAndGet();
>         ByteBuffer rb = (ByteBuffer) message;
>         final ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
>         rb.mark();
>         wb.put(rb);
>         wb.flip();
>         rb.reset();
>         long delay = random.nextInt(500);
>         final IoSession proxySession = (IoSession)
> session.getAttachment();
>         timer.schedule(
>                 new TimerTask() {
>                     public void run() {
>                         proxySession.write(wb);
>                     }
>                 }, delay);
>     }
> 
> Maarten
> 
>> --
>> View this message in context:
>> http://www.nabble.com/Proxy-server-example----introduce-a-random-delay-from-client-to-proxied-server--tf4301103s16868.html#a12242579
>> Sent from the Apache MINA Support Forum mailing list archive at
>> Nabble.com
> 

-- 
View this message in context: http://www.nabble.com/Proxy-server-example----introduce-a-random-delay-from-client-to-proxied-server--tf4301103s16868.html#a12248916
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: Proxy server example -- introduce a random delay from client to proxied server?

Posted by Maarten Bosteels <mb...@gmail.com>.
Hello Paul,

How about adding this to ClientToProxyIoHandler ?

    private Random random = new Random();
    private Timer timer = new Timer();

    public void messageReceived(final IoSession session, final Object
message) throws Exception {
        final int c = counter.incrementAndGet();
        ByteBuffer rb = (ByteBuffer) message;
        final ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
        rb.mark();
        wb.put(rb);
        wb.flip();
        rb.reset();
        long delay = random.nextInt(500);
        final IoSession proxySession = (IoSession) session.getAttachment();
        timer.schedule(
                new TimerTask() {
                    public void run() {
                        proxySession.write(wb);
                    }
                }, delay);
    }

Maarten



On 8/20/07, Paul Furbacher <pf...@mac.com> wrote:
>
>
> I'm trying to modify the proxy server example to introduce a random delay
> to
> messages coming from the client before they are sent to the server.  The
> reason for doing this is that I have to simulate scrambling the order of
> messages from client to server.
>
> At the moment, my idea is to wrap the incoming message (byte buffer) in an
> object which also has a single-shot timer task, and then putting those
> wrapper objects into a collection.  As an individual message times out in
> the delay collection, it becomes "deliverable".  It seems as though I need
> to create a "message needs sending" event, but I'm not sure how that could
> be done.
>
> I'm new to the mina framework, and would appreciate a few pointers -- not
> necessarily any code, just pointers as to what and how to think about this
> within the context of the framework.
>
> Thanks.
>
> --
>
> Paul Furbacher
>
>
> --
> View this message in context:
> http://www.nabble.com/Proxy-server-example----introduce-a-random-delay-from-client-to-proxied-server--tf4301103s16868.html#a12242579
> Sent from the Apache MINA Support Forum mailing list archive at Nabble.com
> .
>
>