Le 11/4/12 4:37 PM, Marko Asplund a écrit :
Hi,
I'm using Mina 2.0 for implementing a network server application.
The server accepts client connections and communicates with them using
a custom TCP-based protocol.
I'd like to implement a separate control channel for the server
through which messages could be injected to be delivered to selected,
normal client session(s).
I've done some experimenting and it seems to be possible to get a list
of active IoSessions from an acceptor using
IoAcceptor.getManagedSessions().
You can then select the correct client session(s) and write packets to
the client using IoSession.write(myPacket).
Would this be a valid approach for implementing a control channel?
Yes.
Are there any caveats I should be aware of? In particular are the
above mentioned APIs thread-safe?
It depends on what you do in your encoder and decoder : if they are
stateless, then you shoudl'nt have any problem, as the message will be
enqueued in a thread safe list before being sent. Keep in mind that the
processing of a write is done by a single thread, up to this queue.
Now, again, if you have any filter using any shared data, then it's up
to you to protect the access to those data in a multi threaded environment.
What about other IoSession methods, e.g. getAttribute() and setAttribute()?
Those methods are thread-safe, assuming you don't use a fancy data
structure instead of the default one (ConcurrentHashMap). Now, that does
not mean the content is thread safe. If you use an attribute which is
shared, then be sure that it's protected against concurrent
modifications...
--
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com