peter royal wrote:


Yes, I didn't think about that possibility. But why not have something like the following:

  IoAcceptor acceptor = ...;
  IoHandlerFactory factory = ...;
  acceptor.accept(address, factory);

where IoHandlerFactory is an interface that has the responsibility to create IoHandlers. This factory would be used by the acceptor to create an IoHandler whenever a new session is accepted.

This way, both the client-side and server-side IoHandler could safely use state in instance variables. This seems more consistent to me.


I guess it just depends on what is the more common case. In my experience, I have no problems with everything flowing into a single IoHandler on the server side. So I would want an IoHandlerFactory that just returned the same instance each time. Whichever way it is implemented, I think it would be a valuable contribution to MINA.
-pete


If the handler is per session there's no need to pass in the session in all methods, is it? IMO it would be nicer to have a new interface:

interface SingleSessionIoHandler {
  void sessionOpened();
  void messageReceived(Object message);
  void sessionClosed();
  ...
}

implementation of this interface would typically store the session as an instance variable.

The factory interface would look like:

interface SingleSessionIoHandlerFactory {
  SingleSessionIoHandler create(IoSession session);
}

To achieve this in MINA you could implement a special IoHandler which delegates to a SingleSessionIoHandler which it gets from the session:

public class SingleSessionIoHandlerDelegate implements IoHandler {
  SingleSessionIoHandlerFactory factory;
  SingleSessionIoHandlerDelegate(SingleSessionIoHandlerFactory factory){
    this.factory = factory;
  }
  void sessionCreated(IoSession session) {
    SingleSessionIoHandler handler = factory.create(session);
    session.setAttribute("handler", handler);
  }
  void sessionOpened(IoSession session) {
    SingleSessionIoHandler handler = session.getAttribute("handler");
    handler.sessionOpened();
  }
  void messageReceived(IoSession session, Object message) {
    SingleSessionIoHandler handler = session.getAttribute("handler");
    handler.messageReceived(message);
  }
  ...
}

To create an acceptor you would simply do:

IoAcceptor acceptor = ...;
SingleSessionIoHandlerFactory  factory = ...;
acceptor.accept(address, new SingleSessionIoHandlerDelegate(factory));

My point is that there's no need to modify the IoAcceptor interface to support this kind of behaviour. I see no harm in including something like this in MINA. Simon, if you have a patch please add it to JIRA and we'll consider it.

/Nikals

Reply via email to