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

PeerMessageLimiter

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
message_counts
Constructors Summary
public PeerMessageLimiter()

  
  
    
    /*nothing*/
  
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.

param
message_id message to count
param
max_counts max counts allowed within the given time limit
param
time_limit_ms time in ms that the count limiting applies
return
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)it.next()).longValue();
        
        if( time < cutoff ) {  //this count is older than the limit allows
          it.remove();  //drop it
        }
        else {  //still within limit
          break;
        }
      }
      
      if( data.counts.size() > data.max_counts ) {   //too many counts within the time limit
        return false;   //return error
      }
    }
    
    return true;