com.limegroup.gnutella.connection
Class MessageQueue

java.lang.Object
  extended bycom.limegroup.gnutella.connection.MessageQueue
Direct Known Subclasses:
PriorityMessageQueue, SimpleMessageQueue

public abstract class MessageQueue
extends java.lang.Object

A priority queue for messages. Used by ManagedConnection to implement the SACHRIFC flow-control algorithm. Subclasses override the add, removeNextInternal, and size template methods to implement different prioritization policies. NOT THREAD SAFE.

This class is designed for speed; hence the somewhat awkward use of resetCycle/extractMax instead of a simple iterator. Likewise, this class has a resetDropped() method instead of returning a (Message, int) pair in removeNext();


Field Summary
protected  int _dropped
          The number of messages dropped since the last call to resetDropped().
 
Constructor Summary
protected MessageQueue(int cycle, int timeout)
           
 
Method Summary
 void add(Message m)
          Adds m to this.
protected abstract  Message addInternal(Message m)
          Add m to this, returns any message that had to dropped to make room in a queue.
 boolean isEmpty()
          Returns true if this has any queued messages.
 Message removeNext()
          Removes and returns the next message to send from this during this cycle.
protected abstract  Message removeNextInternal()
          Same as removeNext, but ignores message age and cycle.
 void resetCycle()
          Resets the cycle counter used to control removeNext().
 int resetDropped()
          Returns the number of dropped messages since the last call to resetDropped().
abstract  int size()
          Returns the number of queued messages.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_dropped

protected int _dropped
The number of messages dropped since the last call to resetDropped().

Constructor Detail

MessageQueue

protected MessageQueue(int cycle,
                       int timeout)
                throws java.lang.IllegalArgumentException
Parameters:
cycle - the number of messages to return per cycle, i.e., between calls to resetCycle. This is used to tweak the ratios of various message types.
timeout - the max time to keep queued messages, in milliseconds. Set this to Integer.MAX_VALUE to avoid timeouts.
Method Detail

add

public void add(Message m)
Adds m to this. Message may be dropped in the process; find out how many by calling resetDropped().


addInternal

protected abstract Message addInternal(Message m)
Add m to this, returns any message that had to dropped to make room in a queue.


removeNext

public Message removeNext()
Removes and returns the next message to send from this during this cycle. Returns null if there are no more messages to send in this cycle. The returned message is guaranteed be younger than TIMEOUT milliseconds. Messages may be dropped in the process; find out how many by calling resetDropped(). (Subclasses should implement the removeNextInternal method and be sure to update the _dropped field if necessary.)

Returns:
the next message, or null if none

removeNextInternal

protected abstract Message removeNextInternal()
Same as removeNext, but ignores message age and cycle.

Returns:
the next message to send, or null if this is empty

resetCycle

public void resetCycle()
Resets the cycle counter used to control removeNext().


resetDropped

public final int resetDropped()
Returns the number of dropped messages since the last call to resetDropped().


size

public abstract int size()
Returns the number of queued messages.


isEmpty

public boolean isEmpty()
Returns true if this has any queued messages.