Hello,
I am trying to do something that I don't even know is possible. Can I change
the codec "on the fly"?
Let me try and explain what I'm doing so you guys can follow my thoughts.
On my SessionHandler (extends IoHandlerAdapter) I add a Codec to the session as
it's created:
public void sessionCreated( IoSession session )
{
session.getFilterChain().clear();
session.getFilterChain().addFirst (
"idcodec",
new ProtocolCodecFilter(new
IDprotocolCodecFactory()));
}
OK, so it loads my IDprotocolCodecFactory which has one messageEncoder and one
messageDecoder:
public class IDprotocolCodecFactory extends DemuxingProtocolCodecFactory
{
public IDprotocolCodecFactory () {
super.register(IDmessageDecoder.class);
super.register(IDmessageEncoder.class);
}
}
This messageDecoder waits for 2 bytes, analyses them then returns
MessageDecoderResult.OK , and that fires the messageReceived() on my
SessionHanlder. So far, working as expected.
Then, I would like to change the Codec (e.g. the "identification stage" is
over, now the server expects another kind of messages). So, on the
messageReceived(), I try to change the codec:
public void messageReceived( IoSession session, Object message )
{
if (session.getFilterChain().contains("idcodec")){
session.getFilterChain().remove("idcodec");
session.getFilterChain().addFirst (
"messagecodec",
new ProtocolCodecFilter(new
MyMessageCodecFactory()));
}
else {
// after the "idcodec" was removed from the chain and "messagecodec" was
placed instead,
// messageReceived() will be triggered by "messagecodec" and will always
get to this 'else'.
// (or so I expect...)
}
}
The MyMessageCodecFactory class is similar the presented IDprotocolCodecFactory
(extends DemuxingProtocolCodecFactory )
Is it possible to change the codec like this? Because it's not working.
It's like I'm stuck with "idcodec" forever, it never changes to "messagecodec".
Any help and/or explanation is appreciated.
Fernando