-
public abstract class PacketQueue<T>An abstract queue of packets.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfacePacketQueue.PacketHandlerA simple interface to handle packets.
-
Field Summary
Fields Modifier and Type Field Description private static booleanenableStatisticsDefaultprivate ErrorHandlererrorHandler
-
Constructor Summary
Constructors Constructor Description PacketQueue()Initializes a new PacketQueue instance. PacketQueue(Boolean enableStatistics, String id, PacketQueue.PacketHandler<T> packetHandler)Initializes a new PacketQueue instance. PacketQueue(int capacity, boolean copy, Boolean enableStatistics, String id, PacketQueue.PacketHandler<T> packetHandler)Initializes a new PacketQueue instance. PacketQueue(int capacity, boolean copy, Boolean enableStatistics, String id, PacketQueue.PacketHandler<T> packetHandler, ExecutorService executor)Initializes a new PacketQueue instance.
-
Method Summary
Modifier and Type Method Description voidadd(Array<byte> buf, int off, int len)Adds a packet represented by a {@code byte[]}with a corresponding offset and length to this queue.voidadd(Array<byte> buf, int off, int len, Object context)Adds a packet represented by a {@code byte[]}with a corresponding offset and length, and a context object to this queue.voidadd(T pkt)Adds a specific packet ( {@code T}) instance to the queue.Tget()Removes and returns the packet ( {@code T}) at the head of this queue.Tpoll()Removes and returns the packet ( {@code T}) at the head of this queue, if the queue is non-empty.voidclose()Closes current PacketQueue instance. abstract Array<byte>getBuffer(T pkt)Extracts the underlying {@code byte[]}from a packet.abstract intgetOffset(T pkt)Extracts the offset of a packet. abstract intgetLength(T pkt)Extracts the length of a packet. abstract ObjectgetContext(T pkt)Extracts the context of a packet. JSONObjectgetDebugState()Gets a JSON representation of the parts of this object's state that are deemed useful for debugging. static voidsetEnableStatisticsDefault(boolean enable)Sets the default value for the {@code enableStatistics}constructor parameter.voidsetErrorHandler(ErrorHandler errorHandler)Sets the handler of errors (packets dropped or exceptions caught while processing). -
-
Constructor Detail
-
PacketQueue
PacketQueue()
Initializes a new PacketQueue instance.
-
PacketQueue
PacketQueue(Boolean enableStatistics, String id, PacketQueue.PacketHandler<T> packetHandler)
Initializes a new PacketQueue instance.- Parameters:
enableStatistics- whether detailed statistics should be gathered.This might affect performance.id- the ID of the packet queue, to be used for logging.packetHandler- An optional handler to be used by the queue forpackets read from it.
-
PacketQueue
PacketQueue(int capacity, boolean copy, Boolean enableStatistics, String id, PacketQueue.PacketHandler<T> packetHandler)
Initializes a new PacketQueue instance.- Parameters:
capacity- the capacity of the queue.copy- whether the queue is to store the instances it is given viathe various methods, or create a copy.enableStatistics- whether detailed statistics should be gathered.This might affect performance.id- the ID of the packet queue, to be used for logging.packetHandler- An optional handler to be used by the queue forpackets read from it.
-
PacketQueue
PacketQueue(int capacity, boolean copy, Boolean enableStatistics, String id, PacketQueue.PacketHandler<T> packetHandler, ExecutorService executor)
Initializes a new PacketQueue instance.- Parameters:
capacity- the capacity of the queue.copy- whether the queue is to store the instances it is given viathe various methods, or create a copy.enableStatistics- whether detailed statistics should be gathered.This might affect performance.id- the ID of the packet queue, to be used for logging.packetHandler- An optional handler to be used by the queue forpackets read from it.executor- An optional executor service to use to executepacketHandler for items added to queue.
-
-
Method Detail
-
add
void add(Array<byte> buf, int off, int len)
Adds a packet represented by a
{@code byte[]}with a corresponding offset and length to this queue.- Parameters:
buf- the to add.off- the offset into where data begins.len- the length of the data.
-
add
void add(Array<byte> buf, int off, int len, Object context)
Adds a packet represented by a
{@code byte[]}with a corresponding offset and length, and a context object to this queue.- Parameters:
buf- the to add.off- the offset into where data begins.len- the length of the data.context- an object which will be added to the queue as part of thepacket.
-
add
void add(T pkt)
Adds a specific packet (
{@code T}) instance to the queue.- Parameters:
pkt- the packet to add.
-
get
T get()
Removes and returns the packet (
{@code T}) at the head of this queue. Blocks until there is a packet in the queue. Returns{@code null}if the queue is closed or gets closed while waiting for a packet to be added.
-
poll
T poll()
Removes and returns the packet (
{@code T}) at the head of this queue, if the queue is non-empty. If the queue is closed or empty, returns null without blocking.
-
close
void close()
Closes current PacketQueue instance. No items will be added to queue when it's closed. Threads which were blocked in get will receive null. Asynchronous queue processing by asyncQueueHandler is stopped.
-
getBuffer
abstract Array<byte> getBuffer(T pkt)
Extracts the underlying
{@code byte[]}from a packet.- Parameters:
pkt- the packet to get the from.
-
getOffset
abstract int getOffset(T pkt)
Extracts the offset of a packet.
- Parameters:
pkt- the packet to get the offset of.
-
getLength
abstract int getLength(T pkt)
Extracts the length of a packet.
- Parameters:
pkt- the packet to get the length of.
-
getContext
abstract Object getContext(T pkt)
Extracts the context of a packet.
- Parameters:
pkt- the packet to get the context of.
-
getDebugState
JSONObject getDebugState()
Gets a JSON representation of the parts of this object's state that are deemed useful for debugging.
-
setEnableStatisticsDefault
static void setEnableStatisticsDefault(boolean enable)
Sets the default value for the
{@code enableStatistics}constructor parameter.- Parameters:
enable- the value to set.
-
setErrorHandler
void setErrorHandler(ErrorHandler errorHandler)
Sets the handler of errors (packets dropped or exceptions caught while processing).
- Parameters:
errorHandler- the handler to set.
-
-
-
-