I use messageSent to close the IoSession after sending a client response. This is ideal for servers where the normal interaction is connect->request->response->close. It allows the session management logic to be cleanly separated from the request/response logic. It is also helpful when the decision to close the session or not depends on some advanced logic. This could be wrapped into an IoFutureListener instead if necessary.
I think the biggest reason for not removing the messageSent event is it takes away from the simplicity of MINA. One could argue that if you remove messageSent you should also remove sessionClosed, because you can just add an IoFutureListener when you close the session. With the filter chains that MINA supports placing logic "up" the chain is pretty easy. You could have a filter 4 levels "up" from where your logic is to send a message doing something with that sent message, or just counting it, etc. What about SSLFilter, doesn't it use messageSent? Instead, if you leave messageSent people have a choice. They can either use messageSent, or add an IoFutureListener as your suggesting. Rob ----- Original Message ---- From: Trustin Lee <[EMAIL PROTECTED]> To: [email protected] Sent: Thursday, July 26, 2007 10:54:54 AM Subject: Do we really need messageSent? (Was: Re: Small documentation on IoFilter) 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 <[EMAIL PROTECTED]> wrote: > On 7/26/07, Germán Borbolla Flores <[EMAIL PROTECTED]> 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 ____________________________________________________________________________________ Be a better Heartthrob. Get better relationship answers from someone who knows. Yahoo! Answers - Check it out. http://answers.yahoo.com/dir/?link=list&sid=396545433
