PeerMessageLimiter.javaAPI DocAzureus Feb 09 19:42:50 GMT 2006com.aelitis.azureus.core.peermanager.utils


public class PeerMessageLimiter extends Object
Handles incoming peer message counting/timing/stats in order to catch and block abusive peers.

Fields Summary
private final HashMap
Constructors Summary
public PeerMessageLimiter()

Methods Summary
public booleancountIncomingMessage(java.lang.String message_id, int max_counts, int time_limit_ms)
Add the reception of the given message to time-limited count.

message_id message to count
max_counts max counts allowed within the given time limit
time_limit_ms time in ms that the count limiting applies
true if the added count is within acceptable time limits, false if there have been too many counts

    CountData data = (CountData)message_counts.get( message_id );
    if( data == null ) {  //new message
      data = new CountData( max_counts, time_limit_ms );
      message_counts.put( message_id, data );
    long now = SystemTime.getCurrentTime();
    data.counts.addLast( new Long( now ) );
    if( data.counts.size() > data.max_counts ) {  //we've potentially reached our count limit
      long cutoff = now - data.time_limit;
      //prune out any expired counts
      for( Iterator it = data.counts.iterator(); it.hasNext(); ) {
        long time = ((Long);
        if( time < cutoff ) {  //this count is older than the limit allows
          it.remove();  //drop it
        else {  //still within limit
      if( data.counts.size() > data.max_counts ) {   //too many counts within the time limit
        return false;   //return error
    return true;