Class PacketQueue
- java.lang.Object
-
- com.gurock.smartinspect.packets.PacketQueue
-
public class PacketQueue extends Object
Manages a memory size limited queue of packets.This class is responsible for managing a size limited queue of packets. This functionality is needed by the Protocol#isValidOption method. The maximum total memory size of the queue can be set with the setBacklog method. New packets can be added with the push method. Packets which are no longer needed can be retrieved and removed from the queue with the pop method.
Note: This class is not guaranteed to be thread safe.
-
-
Constructor Summary
Constructors Constructor Description PacketQueue()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all packets from the queue.longgetBacklog()Returns the total maximum memory size of this queue in bytes.intgetCount()Returns the current amount of packets in this queue.Packetpop()Returns a packet and removes it from the queue.voidpush(Packet packet)Adds a new packet to the queue.voidsetBacklog(long backlog)Sets the total maximum memory size of this queue in bytes.
-
-
-
Method Detail
-
push
public void push(Packet packet)
Adds a new packet to the queue.This method adds the supplied packet to the queue. The size of the queue is incremented by the size of the supplied packet (plus some internal management overhead). If the total occupied memory size of this queue exceeds the backlog limit after adding the new packet, then already added packets will be removed from this queue until the backlog size limit is reached again.
- Parameters:
packet- The packet to add
-
pop
public Packet pop()
Returns a packet and removes it from the queue. If the queue is not empty, this method removes the oldest packet from the queue (also known as FIFO) and returns it. The total size of the queue is decremented by the size of the returned packet (plus some internal management overhead).- Returns:
- The removed packet or null if the queue does not contain any packets
-
clear
public void clear()
Removes all packets from the queue.Removing all packets of the queue is done by calling the pop method for each packet in the current queue.
-
getBacklog
public long getBacklog()
Returns the total maximum memory size of this queue in bytes. Please see the setBacklog method for more information about the backlog property.- Returns:
- The total maximum memory size of this queue in bytes
-
setBacklog
public void setBacklog(long backlog)
Sets the total maximum memory size of this queue in bytes.Each time a new packet is added with the push method, it will be verified that the total occupied memory size of the queue still falls below the supplied backlog limit. To satisfy this constraint, old packets are removed from the queue when necessary.
- Parameters:
backlog- The new backlog size
-
getCount
public int getCount()
Returns the current amount of packets in this queue.For each added packet this counter is incremented by one and for each removed packet (either with the pop method or automatically while resizing the queue) this counter is decremented by one. If the queue is empty, this method returns 0.
- Returns:
- The current amount of packets in this queue
-
-