mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Trustin Lee" <trus...@gmail.com>
Subject Do we really need messageSent? (Was: Re: Small documentation on IoFilter)
Date Thu, 26 Jul 2007 14:54:54 GMT
Hi folks,

I found implementing message transformation in IoFilter becomes
extremely complicated because of messageSent event, like the following
Q&A.

We already have WriteFuture, so adding an IoFutureListener will simply
replace messageSent event handler.

Is there any real use case that messageSent event is useful?  If not,
shall we get rid of it from trunk?  I know it's a radical change, but
I believe it worths a lot in that it simplifies the architecture
without sacrificing features.

Trustin

On 7/26/07, Trustin Lee <trustin@gmail.com> wrote:
> On 7/26/07, Germán Borbolla Flores <gborbolla@insys-corp.com.mx> wrote:
> > Hi Trustin,
> >
> > I've just read the tutorial and I don't understand how to correctly
> > transform a Write request.
> >
> > I have the following filterWrite implementation:
> >
> >    public void filterWrite(NextFilter nextFilter, IoSession session,
> > WriteRequest writeRequest) throws Exception
> >    {
> >        Object message = writeRequest.getMessage();
> >        if (message instanceof ByteBuffer)
> >        {
> >            nextFilter.filterWrite(session, writeRequest);
> >        }
> >        else
> >        {
> >            if (message instanceof Byte)
> >            {
> >                ByteBuffer outBuffer = ByteBuffer.allocate(1);
> >                outBuffer.put((Byte)message);
> >                outBuffer.flip();
> >                nextFilter.filterWrite(session, new
> > WriteRequest(outBuffer, writeRequest.getFuture(),
> > writeRequest.getDestination()));
> >            }
> >            else if (message instanceof Integer)
> >            {
> >                ByteBuffer outBuffer = ByteBuffer.allocate(4);
> >                outBuffer.putInt((Integer)message);
> >                outBuffer.flip();
> >                nextFilter.filterWrite(session, new
> > WriteRequest(outBuffer, writeRequest.getFuture(),
> > writeRequest.getDestination()));
> >            }
> >            else if (message instanceof Long)
> >            {
> >                ByteBuffer outBuffer = ByteBuffer.allocate(8);
> >                outBuffer.putLong((Long)message);
> >                outBuffer.flip();
> >                nextFilter.filterWrite(session, new
> > WriteRequest(outBuffer, writeRequest.getFuture(),
> > writeRequest.getDestination()));
> >            }
> >            else if (message instanceof byte[])
> >            {
> >                byte[] byteArray = (byte[])message;
> >                ByteBuffer outBuffer = ByteBuffer.allocate(4 +
> > byteArray.length);
> >                outBuffer.putInt(byteArray.length);
> >                outBuffer.put(byteArray);
> >                outBuffer.flip();
> >                nextFilter.filterWrite(session, new
> > WriteRequest(outBuffer, writeRequest.getFuture(),
> > writeRequest.getDestination()));
> >            }
> >        }
> >    }
> >
> > Can you give me some pointers of how the messageSent should look like.
>
> You will have to create a new ByteBuffer subtype:
>
> private static class MyByteBuffer extends ByteBufferProxy {
>    private final Object value;
>    private ByteByteBuffer(Object value, ByteBuffer encodedValue) {
>        super(encodedValue);
>    }
>    public Object getValue() { return value; }
> }
>
> now in your messageSent implementation:
>
> public void messageSent(NextFilter nextFilter, IoSession session, Object msg) {
>    if (msg instanceof MyByteBuffer) {
>        nextFilter.messageSent(session, ((MyByteBuffer) msg).getValue());
>    } else {
>        nextFilter.messageSent(session, msg);
>    }
> }
>
> You also need to pass a new MyByteBuffer in filterWrite:
>
> nextFilter.filterWrite(session, new WriteRequest(new MyBuffer(message,
> outBuffer), ...);
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6

Mime
View raw message