public abstract class PacketQueue<T> extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
PacketQueue.PacketHandler<T>
A simple interface to handle packets.
|
Modifier and Type | Field and Description |
---|---|
private static int |
CACHE_CAPACITY
The capacity of the
byte[] cache, if it is enabled. |
private int |
capacity
The capacity of this
PacketQueue . |
private boolean |
closed
Whether this queue has been closed.
|
private boolean |
copy
Whether this
PacketQueue should store the byte[] or
T instances added to it via one of the add methods (if
false ), or create and store a copy (if true ). |
private static int |
DEFAULT_CAPACITY
The default capacity of a
PacketQueue . |
private PacketQueue.PacketHandler<T> |
handler
The
PacketQueue.PacketHandler optionally used to
handle packets read from this queue by thread . |
private String |
id
A string used to identify this
PacketQueue for logging purposes. |
private static Logger |
logger
The
Logger used by the PacketQueue class and its
instances for logging output. |
private int |
numDroppedPackets
The number of packets which were dropped from this
PacketQueue as
a result of a packet being added while the queue is at full capacity. |
private Queue<T> |
queue
The underlying
Queue which holds packets. |
private QueueStatistics |
queueStatistics
The
QueueStatistics instance optionally used to collect and print
detailed statistics about this queue. |
private Thread |
thread
|
Constructor and 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. |
Modifier and Type | Method and Description |
---|---|
void |
add(byte[] buf,
int off,
int len)
Adds a packet represented by a
byte[] with a corresponding
offset and length to this queue. |
void |
add(byte[] buf,
int off,
int len,
Object context)
Adds a packet represented by a
byte[] with a corresponding
offset and length, and a context object to this queue. |
void |
add(T pkt)
Adds a specific packet (
T ) instance to the queue. |
void |
close() |
protected abstract T |
createPacket(byte[] buf,
int off,
int len,
Object context)
Creates a new packet (
T instance) with the given byte[] ,
offset, length and context. |
private void |
doAdd(T pkt)
Adds a specific packet (
T ) instance to the queue. |
T |
get()
Removes and returns the packet (
T ) at the head of this queue. |
abstract byte[] |
getBuffer(T pkt)
Extracts the underlying
byte[] from a packet. |
private byte[] |
getByteArray(int len)
Get an unused
byte[] instance with length at least len . |
abstract Object |
getContext(T pkt)
Extracts the context of a packet.
|
abstract int |
getLength(T pkt)
Extracts the length of a packet.
|
abstract int |
getOffset(T pkt)
Extracts the offset of a packet.
|
static boolean |
logDroppedPacket(int numDroppedPackets)
Returns true if a warning should be logged after a queue has dropped
numDroppedPackets packets. |
T |
poll()
Removes and returns the packet (
T ) at the head of this queue, if
the queue is non-empty. |
private void |
runInReadingThread()
Perpetually reads packets from this
PacketQueue and uses
handler on each of them. |
private static final Logger logger
Logger
used by the PacketQueue
class and its
instances for logging output.private static final int DEFAULT_CAPACITY
PacketQueue
.private static final int CACHE_CAPACITY
byte[]
cache, if it is enabled.private final boolean copy
PacketQueue
should store the byte[]
or
T
instances added to it via one of the add
methods (if
false
), or create and store a copy (if true
).private final int capacity
PacketQueue
. If one of the add
methods is called while the queue holds this many packets, the first
packet in the queue will be dropped.private final QueueStatistics queueStatistics
QueueStatistics
instance optionally used to collect and print
detailed statistics about this queue.private final Thread thread
private final PacketQueue.PacketHandler<T> handler
PacketQueue.PacketHandler
optionally used to
handle packets read from this queue by thread
.private final String id
PacketQueue
for logging purposes.private boolean closed
private int numDroppedPackets
PacketQueue
as
a result of a packet being added while the queue is at full capacity.public PacketQueue()
PacketQueue
instance.public PacketQueue(boolean enableStatistics, String id, PacketQueue.PacketHandler<T> packetHandler)
PacketQueue
instance.enableStatistics
- whether detailed statistics should be calculated
and printed. WARNING: this will produce copious output (one line per
packet added or removed).id
- the ID of the packet queue, to be used for logging.packetHandler
- An optional handler to be used by the queue for
packets read from it. If a non-null value is passed the queue will
start its own thread, which will read packets from the queue and execute
handler.handlePacket
on them. If set to null, no thread will be
created, and the queue will provide access to the head element via
get()
and poll()
.public PacketQueue(int capacity, boolean copy, boolean enableStatistics, String id, PacketQueue.PacketHandler<T> packetHandler)
PacketQueue
instance.capacity
- the capacity of the queue.copy
- whether the queue is to store the instances it is given via
the various add
methods, or create a copy.enableStatistics
- whether detailed statistics should be calculated
and printed. WARNING: this will produce copious output (one line per
packet added or removed).id
- the ID of the packet queue, to be used for logging.packetHandler
- An optional handler to be used by the queue for
packets read from it. If a non-null value is passed the queue will
start its own thread, which will read packets from the queue and execute
handler.handlePacket
on them. If set to null, no thread will be
created, and the queue will provide access to the head element via
get()
and poll()
.public static boolean logDroppedPacket(int numDroppedPackets)
numDroppedPackets
packets.numDroppedPackets
- the number of dropped packets.true
if a warning should be logged.private void runInReadingThread()
PacketQueue
and uses
handler
on each of them.public void add(byte[] buf, int off, int len)
byte[]
with a corresponding
offset and length to this queue.buf
- the byte[]
to add.off
- the offset into byte[]
where data begins.len
- the length of the data.public void add(byte[] buf, int off, int len, Object context)
byte[]
with a corresponding
offset and length, and a context object to this queue.buf
- the byte[]
to add.off
- the offset into byte[]
where data begins.len
- the length of the data.context
- an object which will be added to the queue as part of the
packet.public void add(T pkt)
T
) instance to the queue.pkt
- the packet to add.private byte[] getByteArray(int len)
byte[]
instance with length at least len
.len
- the minimum length of the returned instance.byte[]
instance with length at least len
.private void doAdd(T pkt)
T
) instance to the queue.pkt
- the packet to add.public T get()
T
) at the head of this queue.
Blocks until there is a packet in the queue. Returns null
if
the queue is closed or gets closed while waiting for a packet to be added.public T poll()
T
) at the head of this queue, if
the queue is non-empty. If the queue is closed or empty, returns null
without blocking.public void close()
public abstract byte[] getBuffer(T pkt)
byte[]
from a packet.pkt
- the packet to get the byte[]
from.byte[]
of pkt
.public abstract int getOffset(T pkt)
pkt
- the packet to get the offset of.pkt
.public abstract int getLength(T pkt)
pkt
- the packet to get the length of.pkt
.public abstract Object getContext(T pkt)
pkt
- the packet to get the context of.pkt
.protected abstract T createPacket(byte[] buf, int off, int len, Object context)
T
instance) with the given byte[]
,
offset, length and context.buf
- the byte[]
of the new instance.off
- the offset of the new instance.len
- the length of the new instance.context
- the context of the new instance.T
instance).Copyright © 2018. All rights reserved.