Package 

Class PacketQueue


  • 
    public abstract class PacketQueue<T>
    
                        

    An abstract queue of packets.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public interface PacketQueue.PacketHandler

      A simple interface to handle packets.

    • Method Summary

      Modifier and Type Method Description
      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.
      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.
      void add(T pkt) Adds a specific packet ({@code T}) instance to the queue.
      T get() Removes and returns the packet ({@code T}) at the head of this queue.
      T poll() Removes and returns the packet ({@code T}) at the head of this queue, if the queue is non-empty.
      void close() Closes current PacketQueue instance.
      abstract Array<byte> getBuffer(T pkt) Extracts the underlying {@code byte[]} from a packet.
      abstract int getOffset(T pkt) Extracts the offset of a packet.
      abstract int getLength(T pkt) Extracts the length of a packet.
      abstract Object getContext(T pkt) Extracts the context of a packet.
      JSONObject getDebugState() Gets a JSON representation of the parts of this object's state that are deemed useful for debugging.
      static void setEnableStatisticsDefault(boolean enable) Sets the default value for the {@code enableStatistics} constructor parameter.
      void setErrorHandler(ErrorHandler errorHandler) Sets the handler of errors (packets dropped or exceptions caught while processing).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.