public abstract class TimedEndDecoder extends Object implements ProtocolDecoder
ProtocolDecoder
which detects a frame end
based on the absence of new data (timeout).
Some protocol use a timeout to detect a frame end. e.g. Modbus defines a
end-of-frame
as a timeout of x milliseconds in which no data is sent.
This decoder helps to implement such protocols.
In order to use it you will need to derive your class from this one and
implement timeout(IoSession, ProtocolDecoderOutput)
which will be
called in the case of the timeout. The timeout detect will be started by
calling tick(IoSession, ProtocolDecoderOutput)
. It will remember the
output and pass it later on to the
timeout(IoSession, ProtocolDecoderOutput)
call. So when you read
data in your
ProtocolDecoder#decode(IoSession, org.apache.mina.common.ByteBuffer, ProtocolDecoderOutput)
you simply call tick(IoSession, ProtocolDecoderOutput)
and the
timeout is started or pushed back if it was already started.
If you need to clear the timeout use clear(IoSession)
Modifier and Type | Field and Description |
---|---|
protected IoFilter.NextFilter |
nextFilter |
Constructor and Description |
---|
TimedEndDecoder(ScheduledExecutorService scheduler,
long timeout,
TimeUnit timeUnit)
Create a new instance
|
Modifier and Type | Method and Description |
---|---|
void |
clear(IoSession session)
Clear the end-of-frame detection for this session
|
void |
dispose(IoSession session)
Releases all resources related with this decoder.
|
void |
finishDecode(IoSession session,
ProtocolDecoderOutput out)
Invoked when the specified session is closed.
|
void |
setNextFilter(IoFilter.NextFilter nextFilter) |
protected void |
tick() |
void |
tick(IoSession session,
ProtocolDecoderOutput out)
Start or tick the end-of-frame detection for this session
|
abstract void |
timeout(IoSession session,
ProtocolDecoderOutput out)
Handle the timeout situation
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
decode
protected IoFilter.NextFilter nextFilter
public TimedEndDecoder(ScheduledExecutorService scheduler, long timeout, TimeUnit timeUnit)
scheduler
- the scheduler to use for cyclic jobstimeout
- the timeout in milliseconds for the end-of-frame detectionprotected void tick()
public void dispose(IoSession session) throws Exception
ProtocolDecoder
dispose
in interface ProtocolDecoder
Exception
- if failed to dispose all resourcespublic void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception
ProtocolDecoder
ProtocolDecoder.decode(IoSession, IoBuffer, ProtocolDecoderOutput)
method didn't process completely.finishDecode
in interface ProtocolDecoder
Exception
- if the read data violated protocol specificationpublic abstract void timeout(IoSession session, ProtocolDecoderOutput out) throws Exception
session
- the session for which the timeout occurredout
- the outputException
public void tick(IoSession session, ProtocolDecoderOutput out)
session
- the sessionout
- the protocol decoder output to use for the finished framepublic void clear(IoSession session)
session
- the sessionpublic void setNextFilter(IoFilter.NextFilter nextFilter)