public abstract class AbstractPacketWorker
extends java.lang.Object
implements java.lang.Runnable
SocketIF
s, adds them
to respective queues and then reassembles packets based on the data in these
queues. Each socket maintains a queue of data, for there is a possibility
that the packets received are fragmented and cannot be reassembled during
processing. In this case, processing should be delegated until additional
data on that socket arrives.Modifier and Type | Field and Description |
---|---|
protected java.util.HashMap<SocketIF,java.nio.ByteBuffer> |
pendingData
Maps a SocketChannel to a list of ByteBuffer instances
|
protected java.util.ArrayDeque<SocketIF> |
pendingSockets
Sockets that need to be operated upon (i.e.
|
Constructor and Description |
---|
AbstractPacketWorker() |
Modifier and Type | Method and Description |
---|---|
void |
addData(SocketIF socket,
java.nio.ByteBuffer data,
int count)
Queue data received from a
SocketIF for processing and
reconstruction. |
void |
addListener(PacketListener listener)
Allows registration of multiple
PacketListener s to this
AbstractPacketWorker . |
protected void |
fireListeners(SocketIF socket,
PacketIF packet)
Once a
PacketIF has been completely reconstructed, registered
listeners are notified via this method. |
boolean |
isRunning()
Check whether the
AbstractPacketWorker is running. |
protected abstract void |
processData()
This is the main entry point of received data processing and reassembly.
|
void |
removeListener(PacketListener listener)
Allows de-registration of multiple
PacketListener s from this
AbstractPacketWorker . |
void |
run()
The run() method of the
AbstractPacketWorker . |
void |
setRunning(boolean running)
Set the running status of the
AbstractPacketWorker . |
protected final java.util.HashMap<SocketIF,java.nio.ByteBuffer> pendingData
protected final java.util.ArrayDeque<SocketIF> pendingSockets
public void addData(SocketIF socket, java.nio.ByteBuffer data, int count)
SocketIF
for processing and
reconstruction. A deep copy of the passed ByteBuffer
is made
locally. Following, a data queue for that SocketIF
is created if
it does not exist, and the current piece of data received is added to
that queue for later processing and reconstruction.socket
- The SocketIF data was received fromdata
- The ByteBuffer containing the data (bytes) receivedcount
- The number of bytes receivedprocessData()
protected abstract void processData()
public void run()
AbstractPacketWorker
. Here, sequential
processing of data that need to be reconstructed and processed should be
done in a FIFO fashion. The AbstractPacketWorker
is otherwise
waiting for incoming tasks via the
addData(SocketIF, ByteBuffer, int)
method.run
in interface java.lang.Runnable
public boolean isRunning()
AbstractPacketWorker
is running.public void setRunning(boolean running)
AbstractPacketWorker
. If the
running status of the worker is set to false, the AbstractPacketWorker is
interrupted (if waiting for a task) in order to cleanly shutdown.running
- Whether the PacketWorker should run or notpublic void addListener(PacketListener listener)
PacketListener
s to this
AbstractPacketWorker
.listener
- The listener to register to this PacketWorkerpublic void removeListener(PacketListener listener)
PacketListener
s from this
AbstractPacketWorker
.listener
- The listener to unregister from this PacketWorkerprotected void fireListeners(SocketIF socket, PacketIF packet)
PacketIF
has been completely reconstructed, registered
listeners are notified via this method. This method creates a local copy
of the already registered listeners when firing events, to avoid
potential concurrent modification exceptions.socket
- The SocketIF a complete PacketIF is reconstructedpacket
- The completely reconstructed AbstractPacketPacketListener.paketArrived(SocketIF, PacketIF)