FileDocCategorySizeDatePackage
PeerManager.javaAPI DocAzureus 3.0.3.425019Tue Jul 17 10:35:16 BST 2007com.aelitis.azureus.core.peermanager

PeerManager

public class PeerManager extends Object implements com.aelitis.azureus.core.stats.AzureusCoreStatsProvider

Fields Summary
private static final LogIDs
LOGID
private static final PeerManager
instance
private static final int
PENDING_TIMEOUT
private static final org.gudy.azureus2.core3.util.AEMonitor
timer_mon
private static Thread
timer_thread
private static Set
timer_targets
private final Map
registered_legacy_managers
private final Map
registered_links
private final ByteBuffer
legacy_handshake_header
private final org.gudy.azureus2.core3.util.AEMonitor
managers_mon
Constructors Summary
private PeerManager()

  

    
	  legacy_handshake_header = ByteBuffer.allocate( 20 );
	  legacy_handshake_header.put( (byte)BTHandshake.PROTOCOL.length() );
	  legacy_handshake_header.put( BTHandshake.PROTOCOL.getBytes() );
	  legacy_handshake_header.flip();
	    
	  Set	types = new HashSet();

	  types.add( AzureusCoreStats.ST_PEER_MANAGER_COUNT );
	  types.add( AzureusCoreStats.ST_PEER_MANAGER_PEER_COUNT );
	  types.add( AzureusCoreStats.ST_PEER_MANAGER_PEER_SNUBBED_COUNT );
	  types.add( AzureusCoreStats.ST_PEER_MANAGER_PEER_STALLED_DISK_COUNT );

	  AzureusCoreStats.registerProvider( types, this );
	  
	  init();
  
Methods Summary
public static com.aelitis.azureus.core.peermanager.PeerManagergetSingleton()
Get the singleton instance of the peer manager.

return
the peer manager

  return instance;  
protected voidinit()

     MessageManager.getSingleton().initialize();  //ensure it gets initialized
    
    NetworkManager.ByteMatcher matcher =
    	new NetworkManager.ByteMatcher() 
    {
		public int matchThisSizeOrBigger(){	return( 48 );}
    	public int maxSize() {  return 48;  }
    	public int minSize() { return 20; }

    	public Object
    	matches( 
    		TransportHelper		transport,
    		ByteBuffer 			to_compare, 
    		int 				port ) 
    	{ 
    		InetSocketAddress	address = transport.getAddress();
    		
    		int old_limit = to_compare.limit();
    		int old_position = to_compare.position();

    		to_compare.limit( old_position + 20 );

    		PeerManagerRegistrationImpl	routing_data = null;
    		
    		if( to_compare.equals( legacy_handshake_header ) ) {  //compare header 
    			to_compare.limit( old_position + 48 );
    			to_compare.position( old_position + 28 );

    			byte[]	hash = new byte[to_compare.remaining()];
    			
    			to_compare.get( hash );
    			
    			try{
    				managers_mon.enter();
    				  		
       				List	registrations = (List)registered_legacy_managers.get( new HashWrapper( hash ));
       				
       				if ( registrations != null ){
       					
       					routing_data = (PeerManagerRegistrationImpl)registrations.get(0);
       				}
    			}finally{
    				
    				managers_mon.exit();
    			}
    		}

    		//restore buffer structure
    		to_compare.limit( old_limit );
    		to_compare.position( old_position );

    		if ( routing_data != null ){
    			
    			if ( !routing_data.isActive()){
    			
    				if ( routing_data.isKnownSeed( address )){
    					
    					if (Logger.isEnabled()){
    						Logger.log(new LogEvent(LOGID, "Activation request from " + address + " denied as known seed" ));
    					}
    					
    					routing_data = null;
    					
    				}else{
    					
    					if ( !routing_data.getAdapter().activateRequest( address )){
 
    	  					if (Logger.isEnabled()){
        						Logger.log(new LogEvent(LOGID, "Activation request from " + address + " denied by rules" ));
        					}
     
    						routing_data = null;	
    					}
    				}
    			}
    		}
    		
    		return routing_data;
    	}
    	
    	public Object 
    	minMatches( 
    		TransportHelper		transport,
    		ByteBuffer 			to_compare, 
    		int 				port ) 
    	{ 
     		boolean matches = false;

    		int old_limit = to_compare.limit();
    		int old_position = to_compare.position();

    		to_compare.limit( old_position + 20 );

    		if( to_compare.equals( legacy_handshake_header ) ) { 
    			matches = true;
    		}

    			//restore buffer structure
    		
    		to_compare.limit( old_limit );
    		to_compare.position( old_position );

    		return matches?"":null;
    	}

    	public byte[][] 
    	getSharedSecrets()
    	{
    		return( null );	// registered manually above
    	}
    	
	   	public int 
		getSpecificPort()
		{
			return( -1 );
		}
    };
    
    // register for incoming connection routing
    NetworkManager.getSingleton().requestIncomingConnectionRouting(
        matcher,
        new NetworkManager.RoutingListener() 
        {
        	public void 
        	connectionRouted( 
        		NetworkConnection 	connection, 
        		Object 				routing_data ) 
        	{
        		PeerManagerRegistrationImpl	registration = (PeerManagerRegistrationImpl)routing_data;
        		
        		registration.route( connection, null );
        	}
        	
        	public boolean
      	  	autoCryptoFallback()
        	{
        		return( false );
        	}
        	},
        new MessageStreamFactory() {
          public MessageStreamEncoder createEncoder() {  return new BTMessageEncoder();  }
          public MessageStreamDecoder createDecoder() {  return new BTMessageDecoder();  }
        });
  
public PeerManagerRegistrationmanualMatchHash(java.net.InetSocketAddress address, byte[] hash)

			
  		PeerManagerRegistrationImpl	routing_data = null;
	  
  		try{
  			
  			managers_mon.enter();
				  		
  			List	registrations = (List)registered_legacy_managers.get( new HashWrapper( hash ));
   				
  			if ( registrations != null ){
   					
  				routing_data = (PeerManagerRegistrationImpl)registrations.get(0);
   			}
		}finally{
			
			managers_mon.exit();
		}
		
		if ( routing_data != null ){
			
			if ( !routing_data.isActive()){
			
				if ( routing_data.isKnownSeed( address )){
					
					if (Logger.isEnabled()){
						Logger.log(new LogEvent(LOGID, "Activation request from " + address + " denied as known seed" ));
					}
					
					routing_data = null;
					
				}else{
					
					if ( !routing_data.getAdapter().activateRequest( address )){

	  					if (Logger.isEnabled()){
    						Logger.log(new LogEvent(LOGID, "Activation request from " + address + " denied by rules" ));
    					}
 
						routing_data = null;	
					}
				}
			}
		}
		
		return routing_data;
  	
public PeerManagerRegistrationmanualMatchLink(java.net.InetSocketAddress address, java.lang.String link)

	
 		byte[]	hash;
  	
  		try{
  			managers_mon.enter();
				  		
  			PeerManagerRegistrationImpl	registration = (PeerManagerRegistrationImpl)registered_links.get( link );
  			
  			if ( registration == null ){
  				
  				return( null );
  			}
  			
  			hash = registration.getHash();
  			
  		}finally{
  			
  			managers_mon.exit();
  		}
  		
  		return( manualMatchHash( address, hash ));
  	
public voidmanualRoute(PeerManagerRegistration _registration, NetworkConnection _connection, PeerManagerRoutingListener _listener)

   		PeerManagerRegistrationImpl	registration = (PeerManagerRegistrationImpl)_registration;
		
		registration.route( _connection, _listener );
  	
protected static voidregisterForTimeouts(com.aelitis.azureus.core.peermanager.PeerManager$PeerManagerRegistrationImpl reg)

  
    
  
			 
  
	  try{
		  timer_mon.enter();
		  
		  timer_targets.add( reg );
		  
		  if ( timer_thread == null ){
			  
			  	timer_thread = 
				  new AEThread( "PeerManager:timeouts", true )
				  {
					  public void
					  runSupport()
					  {
						  int	idle_time	= 0;
						  
						  while( true ){
							  
							  try{
								  Thread.sleep( PENDING_TIMEOUT / 2 );
								  
							  }catch( Throwable e ){  
							  }
							  
							  try{
								  timer_mon.enter();

								  if ( timer_targets.size() == 0 ){
									  
									  idle_time += PENDING_TIMEOUT / 2;
									  
									  if ( idle_time >= 30*1000 ){
										  										  
										  timer_thread = null;
										  
										  break;
									  }
								  }else{
									  
									  idle_time = 0;
									  
									  Iterator	it = timer_targets.iterator();
									  
									  while( it.hasNext()){
										  
										  PeerManagerRegistrationImpl	registration = (PeerManagerRegistrationImpl)it.next();
										  
										  if ( !registration.timeoutCheck()){
											  
											  it.remove();
										  }
									  }
								  }
							  }finally{
								  
								  timer_mon.exit();
							  }
						  }
					  }
				  };
				  
				timer_thread.start();
		  }
	  }finally{
		  
		  timer_mon.exit();
	  }
  
public PeerManagerRegistrationregisterLegacyManager(org.gudy.azureus2.core3.util.HashWrapper hash, PeerManagerRegistrationAdapter adapter)

	  try{
		  managers_mon.enter();
		  		 
		  	// normally we only get a max of 1 of these. However, due to DownloadManager crazyness
		  	// we can get an extra one when adding a download that already exists...
		  
		  List	registrations = (List)registered_legacy_managers.get( hash );
			  
		  byte[][]	secrets = adapter.getSecrets();
		  
		  if ( registrations == null ){
			  
			  registrations = new ArrayList(1);
			  
			  registered_legacy_managers.put( hash, registrations );
		  		  
			  IncomingConnectionManager.getSingleton().addSharedSecrets( secrets );
		  }
		  
		  PeerManagerRegistration	registration = new PeerManagerRegistrationImpl( hash, adapter );
		    
		  registrations.add( registration );
		  
		  return( registration );
		  
	  }finally{
		  
		  managers_mon.exit();
	  }
  
public voidupdateStats(java.util.Set types, java.util.Map values)

	  if ( types.contains( AzureusCoreStats.ST_PEER_MANAGER_COUNT )){

		  values.put( AzureusCoreStats.ST_PEER_MANAGER_COUNT, new Long( registered_legacy_managers.size()));
	  }

	  if ( 	types.contains( AzureusCoreStats.ST_PEER_MANAGER_PEER_COUNT ) ||
			types.contains( AzureusCoreStats.ST_PEER_MANAGER_PEER_SNUBBED_COUNT ) ||
			types.contains( AzureusCoreStats.ST_PEER_MANAGER_PEER_STALLED_DISK_COUNT )){

		  long	total_peers 				= 0;
		  long	total_snubbed_peers			= 0;
		  long	total_stalled_pending_load	= 0;

		  try{

			  managers_mon.enter();

			  Iterator	it = registered_legacy_managers.values().iterator();

			  while( it.hasNext()){

				  List	registrations = (List)it.next();

				  Iterator	it2 = registrations.iterator();

				  while( it2.hasNext()){

					  PeerManagerRegistrationImpl reg = (PeerManagerRegistrationImpl)it2.next();

					  PEPeerControl control = reg.getActiveControl();

					  if ( control != null ){

						  total_peers 				+= control.getNbPeers();
						  total_snubbed_peers			+= control.getNbPeersSnubbed();
						  total_stalled_pending_load	+= control.getNbPeersStalledPendingLoad();
					  }
				  }
			  }
		  }finally{

			  managers_mon.exit();
		  }
		  if ( types.contains( AzureusCoreStats.ST_PEER_MANAGER_PEER_COUNT )){

			  values.put( AzureusCoreStats.ST_PEER_MANAGER_PEER_COUNT, new Long( total_peers ));
		  }
		  if ( types.contains( AzureusCoreStats.ST_PEER_MANAGER_PEER_SNUBBED_COUNT )){

			  values.put( AzureusCoreStats.ST_PEER_MANAGER_PEER_SNUBBED_COUNT, new Long( total_snubbed_peers ));
		  }
		  if ( types.contains( AzureusCoreStats.ST_PEER_MANAGER_PEER_STALLED_DISK_COUNT )){

			  values.put( AzureusCoreStats.ST_PEER_MANAGER_PEER_STALLED_DISK_COUNT, new Long( total_stalled_pending_load ));
		  }
	  }