Class DemuxingProtocolDecoder
java.lang.Object
org.apache.mina.filter.codec.ProtocolDecoderAdapter
org.apache.mina.filter.codec.CumulativeProtocolDecoder
org.apache.mina.filter.codec.demux.DemuxingProtocolDecoder
- All Implemented Interfaces:
ProtocolDecoder
A composite Internal mechanism of
ProtocolDecoder that demultiplexes incoming IoBuffer
decoding requests into an appropriate MessageDecoder.
Internal mechanism of MessageDecoder selection
-
DemuxingProtocolDecoderiterates the list of candidateMessageDecoders and callsMessageDecoder.decodable(IoSession, IoBuffer). Initially, all registeredMessageDecoders are candidates. -
If
MessageDecoderResult.NOT_OKis returned, it is removed from the candidate list. -
If
MessageDecoderResult.NEED_DATAis returned, it is retained in the candidate list, and itsMessageDecoder.decodable(IoSession, IoBuffer)will be invoked again when more data is received. -
If
MessageDecoderResult.OKis returned,DemuxingProtocolDecoderfound the rightMessageDecoder. -
If there's no candidate left, an exception is raised. Otherwise,
DemuxingProtocolDecoderwill keep iterating the candidate list.
IoBuffer
in MessageDecoder.decodable(IoSession, IoBuffer) will be reverted back to its
original value.
Once a MessageDecoder is selected, DemuxingProtocolDecoder calls
MessageDecoder.decode(IoSession, IoBuffer, ProtocolDecoderOutput) continuously
reading its return value:
-
MessageDecoderResult.NOT_OK- protocol violation;ProtocolDecoderExceptionis raised automatically. -
MessageDecoderResult.NEED_DATA- needs more data to read the whole message;MessageDecoder.decode(IoSession, IoBuffer, ProtocolDecoderOutput)will be invoked again when more data is received. -
MessageDecoderResult.OK- successfully decoded a message; the candidate list will be reset and the selection process will start over.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classprivate static classprivate class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate MessageDecoderFactory[]private static final Class<?>[]private static final AttributeKey -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMessageDecoder(Class<? extends MessageDecoder> decoderClass) Adds a new message decoder classvoidaddMessageDecoder(MessageDecoder decoder) Adds a new message decoder instancevoidaddMessageDecoder(MessageDecoderFactory factory) Adds a new message decoder factoryvoidReleases the cumulative buffer used by the specifiedsession.protected booleandoDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) Implement this method to consume the specified cumulative buffer and decode its content into message(s).voidfinishDecode(IoSession session, ProtocolDecoderOutput out) Override this method to deal with the closed connection.private DemuxingProtocolDecoder.StateMethods inherited from class CumulativeProtocolDecoder
decode, setTransportMetadataFragmentation
-
Field Details
-
STATE
-
decoderFactories
-
EMPTY_PARAMS
-
-
Constructor Details
-
DemuxingProtocolDecoder
public DemuxingProtocolDecoder()
-
-
Method Details
-
addMessageDecoder
Adds a new message decoder class- Parameters:
decoderClass- The decoder class
-
addMessageDecoder
Adds a new message decoder instance- Parameters:
decoder- The decoder instance
-
addMessageDecoder
Adds a new message decoder factory- Parameters:
factory- The decoder factory
-
doDecode
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception Implement this method to consume the specified cumulative buffer and decode its content into message(s).- Specified by:
doDecodein classCumulativeProtocolDecoder- Parameters:
session- The current Sessionin- the cumulative bufferout- TheProtocolDecoderOutputthat will receive the decoded message- Returns:
trueif and only if there's more to decode in the buffer and you want to havedoDecodemethod invoked again. Returnfalseif remaining data is not enough to decode, then this method will be invoked again when more data is cumulated.- Throws:
Exception- if cannot decodein.
-
finishDecode
Override this method to deal with the closed connection. The default implementation does nothing.- Specified by:
finishDecodein interfaceProtocolDecoder- Overrides:
finishDecodein classProtocolDecoderAdapter- Parameters:
session- The current Sessionout- TheProtocolDecoderOutputthat contains the decoded message- Throws:
Exception- if the read data violated protocol specification
-
dispose
Releases the cumulative buffer used by the specifiedsession. Please don't forget to callsuper.dispose( session )when you override this method.- Specified by:
disposein interfaceProtocolDecoder- Overrides:
disposein classCumulativeProtocolDecoder- Parameters:
session- The current Session- Throws:
Exception- if failed to dispose all resources
-
getState
- Throws:
Exception
-