public class DatagramServerHandler extends AbstractDatagramHandler
DatagramSession
object that will be associated with the
datagram handle created by the IDatagramHandlerFactory
passed in the
constructor.
Limitations:: As the session objects created by this handler do not implement their own I/O functionalities calling to methods suspending read/write operations will also suspend all other sessions currently handled by this handler.
Modifier and Type | Field and Description |
---|---|
protected Map<SocketAddress,DatagramSession> |
sessions
Sessions currently handled by this handler.
|
protected Map<SocketAddress,ITimerTask> |
timers
A map holding timers used internally by the class.
|
Constructor and Description |
---|
DatagramServerHandler(IDatagramHandlerFactory handlerFactory)
Constructs a datagram server handler with the
DefaultSessionConfig and the
DefaultSessionStructureFactory . |
DatagramServerHandler(IDatagramHandlerFactory handlerFactory,
ISessionConfig config)
Constructs a datagram server handler with the
DefaultSessionStructureFactory . |
DatagramServerHandler(IDatagramHandlerFactory handlerFactory,
ISessionConfig config,
ISessionStructureFactory factory)
Constructs a datagram server handler.
|
Modifier and Type | Method and Description |
---|---|
protected IEngine |
createEngine(SocketAddress remoteAddress,
ISessionConfig config)
Determines if newly created session should be an engine-driver session.
|
void |
event(DataEvent event,
long length)
Called to notify about an I/O operation.
|
void |
event(SessionEvent event)
Called to notify about a change of the associated session state.
|
void |
event(SocketAddress remoteAddress,
DataEvent event,
long length)
Called to notify about an I/O operation related to a specified remote host.
|
void |
exception(Throwable t)
Called to notify about an exception caught during processing of I/O or
protocol related operations.
|
ISessionConfig |
getConfig()
Returns the configuration object that will be used to configure the
behavior of the associated session.
|
ISessionStructureFactory |
getFactory()
Returns the factory object that will be used to configure the internal
structure of the associated session.
|
protected DatagramSession[] |
getSessions()
Returns sessions currently handled by this handler.
|
boolean |
incident(SessionIncident incident,
Throwable t)
Called to notify about an incident that occurred during processing of I/O
or protocol related operations.
|
void |
read(byte[] data)
Called when new bytes were read from the input buffer.
|
void |
read(ByteBuffer data)
Called when new bytes were read from the input buffer.
|
void |
read(Object msg)
Called when a new message was read and decoded from the input buffer.
|
void |
read(SocketAddress remoteAddress,
byte[] datagram)
Called when a new datagram was received from a remote end that is identified
by the given remote address.
|
void |
read(SocketAddress remoteAddress,
ByteBuffer datagram)
Called when a new datagram was received from a remote end that is identified
by the given remote address.
|
void |
read(SocketAddress remoteAddress,
Object datagram)
Called when a new message was received and decoded from a remote end
that is identified by the given remote address.
|
void |
timer(Object event)
Called to notify about an expiration of the timer identified by the
specified event object.
|
getSession, setSession
getName, timer
protected Map<SocketAddress,DatagramSession> sessions
Thread-safe considerations: It is not required by the SNF4J
framework that the Map implementation is thread safe, however if a class
extending this class requires thread-safe access to the map then it
should be replaced by a thread-safe implementation. In addition, to
provide full thread-safe implementation of this handler the method
getSessions()
should be overridden as well as it is not
thread-safe by default.
protected Map<SocketAddress,ITimerTask> timers
Thread-safe considerations: It is not required by the SNF4J framework that the map implementation is thread safe.
public DatagramServerHandler(IDatagramHandlerFactory handlerFactory)
DefaultSessionConfig
and the
DefaultSessionStructureFactory
.handlerFactory
- the factory used to create datagram handlers the will be
associated with newly created sessions for remote hostspublic DatagramServerHandler(IDatagramHandlerFactory handlerFactory, ISessionConfig config)
DefaultSessionStructureFactory
.handlerFactory
- the factory used to create datagram handlers the will be
associated with newly created sessions for remote hostsconfig
- the configuration for a session associated with this handler
or null
to use the default configurationpublic DatagramServerHandler(IDatagramHandlerFactory handlerFactory, ISessionConfig config, ISessionStructureFactory factory)
handlerFactory
- the factory used to create datagram handlers the will be
associated with newly created sessions for remote hostsconfig
- the configuration for a session associated with this handler
or null
to use the default configurationfactory
- the factory used to configure the internal structure of a
session associated with this handler or null
to use the default structure factorypublic void read(byte[] data)
AbstractHandler
read
methods are
met.
The passed array can be safely stored or modified by this method as it will not be used by the caller.
By default it simply passes the data
value to the
IHandler.read(Object)
method.
read
in interface IHandler
read
in interface IDatagramReader
read
in class AbstractHandler
data
- bytes that was read from the input buffer.public void read(ByteBuffer data)
AbstractHandler
ByteBuffer
objects or when the associated
session is configured to optimize data copying and uses an allocator
supporting the releasing of no longer used buffers
The passed byte buffer can be safely stored or modified by this method as it will not be used by the caller. However, if the associated session is configured to optimize data copying and uses an allocator supporting the releasing of no longer used buffers it may be required to release it in this method unless the original buffer has been already released by one of the associated decoders.
By default it simply passes the data
value to the
IHandler.read(Object)
method.
read
in interface IHandler
read
in interface IDatagramReader
read
in class AbstractHandler
data
- bytes that was read from the input buffer.public void read(Object msg)
IHandler
byte[]
and ByteBuffer
.msg
- the message that was read and decoded from the input buffer.public void read(SocketAddress remoteAddress, byte[] datagram)
AbstractDatagramHandler
This method is called when none of the conditions for calling other
read
methods with specified remote address are met.
The passed array can be safely stored or modified by this method as it will not be used by the caller.
By default it simply passes the remoteAddress
and data
values
to the IDatagramHandler.read(SocketAddress,Object)
method.
read
in interface IDatagramHandler
read
in interface IDatagramReader
read
in class AbstractDatagramHandler
remoteAddress
- address of the remote end.datagram
- the datagram received from the remote end.public void read(SocketAddress remoteAddress, ByteBuffer datagram)
AbstractDatagramHandler
This method is called when the associated session is configured with a codec
pipeline in which the last decoder produces ByteBuffer
objects or
when the associated session is configured to optimize data copying and uses
an allocator supporting the releasing of no longer used buffers.
The passed buffer can be safely stored or modified by this method as it will not be used by the caller. However, if the associated session is configured to optimize data copying and uses an allocator supporting the releasing of no longer used buffers it may be required to release it in this method unless the original buffer has been already released by one of the associated decoders.
By default it simply passes the remoteAddress
and data
values
to the IDatagramHandler.read(SocketAddress,Object)
method.
read
in interface IDatagramHandler
read
in interface IDatagramReader
read
in class AbstractDatagramHandler
remoteAddress
- address of the remote end.datagram
- the datagram received from the remote end.public void read(SocketAddress remoteAddress, Object datagram)
IDatagramHandler
byte[]
and ByteBuffer
.
The method is only called for sessions created with a disconnected datagram channel.
remoteAddress
- address of the remote end.datagram
- the message received from the remote end.public void event(SessionEvent event)
IHandler
event
in interface IHandler
event
in class AbstractHandler
event
- an event related with the change of the session stateSessionEvent
public void event(DataEvent event, long length)
IHandler
event
in interface IHandler
event
in class AbstractHandler
event
- an event related with the type of I/O operationlength
- the number of bytes related with the I/O operationDataEvent
public void event(SocketAddress remoteAddress, DataEvent event, long length)
IDatagramHandler
event
in interface IDatagramHandler
event
in class AbstractDatagramHandler
remoteAddress
- the address of the remote hostevent
- an event related with the type of I/O operationlength
- the number of bytes related with the I/O operationDataEvent
protected DatagramSession[] getSessions()
sessions
object.
Thread-safe considerations: By default the access to the
sessions
object done by this method is not thread safe.
public void exception(Throwable t)
IHandler
exception
in interface IHandler
exception
in class AbstractHandler
t
- the exception caughtpublic boolean incident(SessionIncident incident, Throwable t)
IHandler
incident
in interface IHandler
incident
in class AbstractHandler
incident
- an incident that occurredt
- an exception that is related with the incident or
null
true
to indicate that the incident was handled and
the default action should not be executed by the SNF4J framework
underneath.public ISessionStructureFactory getFactory()
IHandler
getFactory
in interface IHandler
getFactory
in class AbstractHandler
ISessionStructureFactory
public ISessionConfig getConfig()
IHandler
getConfig
in interface IHandler
getConfig
in class AbstractHandler
ISessionConfig
protected IEngine createEngine(SocketAddress remoteAddress, ISessionConfig config) throws Exception
null
.remoteAddress
- address of the remote peer for which the session is
about to be createdconfig
- the configuration for the session being creatednull
if the session being
created should not be engine-drivenException
- if the engine could not be createdpublic void timer(Object event)
IHandler
timer
in interface IHandler
timer
in class AbstractHandler
event
- the event object that identifies the timerCopyright © 2017–2022 SNF4J.ORG. All rights reserved.