LimeWire Consolidated API

com.limegroup.gnutella.connection
Class AbstractMessageQueue

java.lang.Object
  extended by com.limegroup.gnutella.connection.AbstractMessageQueue
All Implemented Interfaces:
MessageQueue
Direct Known Subclasses:
PriorityMessageQueue, SimpleMessageQueue

public abstract class AbstractMessageQueue
extends Object
implements MessageQueue

A priority queue for messages. 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 AbstractMessageQueue(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.
 Object getDroppedStats()
          An inspectable object for dropped messages
 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

AbstractMessageQueue

protected AbstractMessageQueue(int cycle,
                               int timeout)
                        throws 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.
Throws:
IllegalArgumentException
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().

Specified by:
add in interface MessageQueue

getDroppedStats

public final Object getDroppedStats()
Description copied from interface: MessageQueue
An inspectable object for dropped messages

Specified by:
getDroppedStats in interface MessageQueue

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.)

Specified by:
removeNext in interface MessageQueue
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().

Specified by:
resetCycle in interface MessageQueue

resetDropped

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

Specified by:
resetDropped in interface MessageQueue

size

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

Specified by:
size in interface MessageQueue

isEmpty

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

Specified by:
isEmpty in interface MessageQueue

LimeWire Consolidated API

Copyright © 2009. All Rights Reserved.