FileDocCategorySizeDatePackage
PeerIdentityManager.javaAPI DocAzureus 3.0.3.47571Sun Oct 22 21:21:28 BST 2006org.gudy.azureus2.core3.peer.util

PeerIdentityManager

public class PeerIdentityManager extends Object
Maintains peer identity information.

Fields Summary
private static final boolean
MUTLI_CONTROLLERS
private static final org.gudy.azureus2.core3.util.AEMonitor
class_mon
private static final Map
dataMap
private static int
totalIDs
Constructors Summary
Methods Summary
public static booleanaddIdentity(PeerIdentityDataID data_id, byte[] peer_id, int local_port, java.lang.String ip)
Add a new peer identity to the manager.

param
data_id unique id for the data item associated with this connection
param
peer_id unique id for this peer connection
param
ip remote peer's ip address

     PeerIdentity peerID = new PeerIdentity( peer_id, local_port );
    
    try{
      class_mon.enter();
        
      Map peerMap = (Map)dataMap.get( data_id );
      if( peerMap == null ) {
        peerMap = new HashMap();
        dataMap.put( data_id, peerMap );
      }
           
      String old = (String)peerMap.put( peerID, ip );
      if( old == null ) {
        totalIDs++;
        
        return( true );
      }else{    	
    	return( false );
      }
    }finally{
      class_mon.exit();
    }
  
public static booleancontainsIPAddress(PeerIdentityDataID data_id, java.lang.String ip)
Check if the given IP address is already present in the manager's peer identity list for the given data item (i.e. check if there is already a peer with that IP address).

param
data_id id for the data item associated with this connection
param
ip IP address to check for
return
true if the IP is found, false if not found

    
    try{
    	class_mon.enter();
   	  
      Map peerMap = (Map)dataMap.get( data_id );
      if( peerMap != null ) {
        if( peerMap.containsValue( ip ) ) {
          return true;
        }
      }
    }finally{
    	class_mon.exit();
    }
    
    return false;
  
public static booleancontainsIdentity(PeerIdentityDataID data_id, byte[] peer_id, int local_port)
Check if the manager already has the given peer identity.

param
data_id id for the data item associated with this connection
param
peer_id id for this peer connection
return
true if the peer identity is found, false if not found

    PeerIdentity peerID = new PeerIdentity( peer_id, local_port );
    
    try{
    	class_mon.enter();
  
      Map peerMap = (Map)dataMap.get( data_id );
      if( peerMap != null ) {
        if( peerMap.containsKey( peerID ) ) {
          return true;
        }
      }
    }finally{
    	class_mon.exit();
    }
    
    return false;
  
public static PeerIdentityDataIDcreateDataID(byte[] data)

 
  /*
  static{
	  new AEThread("mon",true)
	  {
		  public void
		  runSupport()
		  {
			  monitor();
		  }
	  }.start();
  }
    
  static void
  monitor()
  {
	  while( true ){
		  
		  try{
		  
			  class_mon.enter();
			  
			  System.out.println( "tot = " + getTotalIdentityCount());
			  
			  Iterator it = dataMap.entrySet().iterator();
			  
			  while( it.hasNext()){
				  
				  Map.Entry	entry = (Map.Entry)it.next();
				  
				  PeerIdentityDataID id = (PeerIdentityDataID)entry.getKey();
				  
				  Map	vals = (Map)entry.getValue();
				  
				  System.out.println( "  id " + ByteFormatter.encodeString( id.getDataID())+ " -> " + vals.size());
			  }
		  }finally{
			  
			  class_mon.exit();
		  }
		  try{
			  Thread.sleep(10000);
		  }catch( Throwable e ){
			  
		  }
	  }
  }
  */
  
    
  
  			 
  
  	PeerIdentityDataID	data_id = new PeerIdentityDataID( data );
  	
  	Map peerMap;
  	
    try{
        class_mon.enter();
      
        peerMap = (Map)dataMap.get( data_id );
        
        if( peerMap == null ){
        	
          peerMap = new HashMap();
          
          dataMap.put( data_id, peerMap );
        }
    }finally{
    	
    	class_mon.exit();
    }
	
	data_id.setPeerMap( peerMap );
	
	return( data_id );
  
public static intgetIdentityCount(PeerIdentityDataID data_id)
Get the total number of peer identities managed for the given data item.

param
data_id data item to count over
return
total number of peers for this data item

  	return( data_id.getPeerMap().size());
  
public static intgetTotalIdentityCount()
Get the total number of peer identities managed.

return
total number of peers over all data items

    return totalIDs;
  
public static voidremoveIdentity(PeerIdentityDataID data_id, byte[] peer_id, int local_port)
Remove a peer identity from the manager.

param
data_id id for the data item associated with this connection
param
peer_id id for this peer connection

     
    try{
    	class_mon.enter();
      
      Map peerMap = (Map)dataMap.get( data_id );
      if( peerMap != null ) {
        PeerIdentity peerID = new PeerIdentity( peer_id, local_port );
               
        String old = (String)peerMap.remove( peerID );
        if( old != null ) {
          totalIDs--;
        }else{
      	  Debug.out( "id not present: id=" + peerID.getString());
   	
        }
      }
    }finally{
    	class_mon.exit();
    }