org.exolab.core.messenger
Class PacketQueue

java.lang.Object
  extended byorg.exolab.core.messenger.PacketQueue

public class PacketQueue
extends java.lang.Object

This class implements a queue of packets.

Packets are added at the end of the queue, and removed from the front. The queue can have a size limit set - when reached, all calls to add a new packet will block.

Version:
$Revision: 1.4 $
Author:
Tim Anderson
See Also:
Packet

Constructor Summary
PacketQueue()
          Creates a new empty queue, of unlimited size
PacketQueue(int limit)
          Creates a new empty queue, of limited size.
 
Method Summary
 void add(org.exolab.core.messenger.Packet packet)
          Add a new packet to the end of the queue.
 void close()
          Close the queue.
 boolean isClosed()
          Returns true if the queue is closed
 boolean isEmpty()
          Returns true if this queue is empty
 org.exolab.core.messenger.Packet peek()
          Return the packet at the front of the queue, without removing it.
 org.exolab.core.messenger.Packet remove()
          Remove a packet from the top of the queue.
 boolean waitForPacket(long timeout)
          Wait at most timeout milliseconds until a packet is available
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PacketQueue

public PacketQueue()
Creates a new empty queue, of unlimited size


PacketQueue

public PacketQueue(int limit)
Creates a new empty queue, of limited size. Once the no. of packets in the queue reaches the limit, calls to add new packets block until space is available.

Parameters:
limit - the maximum number of packets that the queue can contain at any one time.
Method Detail

add

public void add(org.exolab.core.messenger.Packet packet)
Add a new packet to the end of the queue. If the queue has a size limit in place, and the queue has reached that limit, this call will block, until space is available.

Parameters:
packet - the packet to add
Throws:
java.lang.IllegalArgumentException - if packet is null

remove

public org.exolab.core.messenger.Packet remove()
Remove a packet from the top of the queue. This call blocks until a packet is available.

Returns:
the next available packet, or null, if the queue has been closed

peek

public org.exolab.core.messenger.Packet peek()
Return the packet at the front of the queue, without removing it. If no packet is available, this returns null.

Returns:
the packet at the front of the queue, or null if no packet is currently queued

waitForPacket

public boolean waitForPacket(long timeout)
Wait at most timeout milliseconds until a packet is available

Parameters:
timeout - the maximum time to wait (in milliseconds) for a packet to become available. A timeout of zero never expires and the call blocks indefinitely, or until the queue is closed
Returns:
true if a packet becomes available in the specified time

isEmpty

public boolean isEmpty()
Returns true if this queue is empty

Returns:
true if this queue is empty

close

public void close()
Close the queue. If it is blocked then unblock it and release it, so it can close correctly


isClosed

public boolean isClosed()
Returns true if the queue is closed



Copyright © 1999-2005 The Exolab Group. All Rights Reserved.