|
Page Edited :
MINA :
ProtocolCodec discussion
ProtocolCodec discussion has been edited by Emmanuel Lécharny (Nov 11, 2008). Change summary: Added some doc about the events IntroductionThis filter is one of the most important one. In many case, writing a MINA based application without having such a filter added is not really meaningful. Events handledHere is a list of all the handled events in this filter :
DescriptionConstructorIn order to be able to encode or decode a message, we need to pass the filter a factory, which will be used to create those two parts of the codec :
The factory is pretty simple. It offers two methods : public interface ProtocolCodecFactory { /** * Returns a new (or reusable) instance of [EMAIL PROTECTED] ProtocolEncoder} which * encodes message objects into binary or protocol-specific data. */ ProtocolEncoder getEncoder(IoSession session) throws Exception;
/**
* Returns a new (or reusable) instance of [EMAIL PROTECTED] ProtocolDecoder} which
* decodes binary or protocol-specific data into message objects.
*/
ProtocolDecoder getDecoder(IoSession session) throws Exception;
}
It's also possible to pass the encoder and decoder directly, as a factory will be created internally to encapsulate those two methods. SessionCreated eventThe current handling for this event is pretty simple : it stores the Encoder and Decoder instances in the session attributes. public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception { // Creates the decoder and stores it into the newly created session session.setAttribute(DECODER, factory.getDecoder(session)); // Creates the encoder and stores it into the newly created session session.setAttribute(ENCODER, factory.getEncoder(session)); // Call the next filter nextFilter.sessionCreated(session); }
SessionClosed eventThis event is received when the session is closed. We have to remove the Encoder and Decoder instances from the session's attributes, and to dispose those elements. As we may have remaining messages to decode, we have to process them first. We have different cases, as of MINA 2.0.0-M3 :
OnPreAddThis event is received when we try to insert this filter during a session. What it does is very simple : - Checks that the filter is not already present in the chain. If so, generates an exception - Initialize the encoder and decoder. Nothing much to tell about this simple handler OnPostRemoveThis event is received when this filter is removed from the chain. We simply :
Nothing much to tell about this simple handler MessageReceived event |
Unsubscribe or edit your notifications preferences
