FileDocCategorySizeDatePackage
AZMyInstanceImpl.javaAPI DocAzureus 3.0.3.49574Wed Jul 11 22:48:32 BST 2007com.aelitis.azureus.core.instancemanager.impl

AZMyInstanceImpl

public class AZMyInstanceImpl extends AZInstanceImpl

Fields Summary
public static final long
FORCE_READ_EXT_MIN
public static final long
UPNP_READ_MIN
private com.aelitis.azureus.core.AzureusCore
core
private AZInstanceManagerImpl
manager
private String
id
private InetAddress
internal_address
private int
tcp_port
private int
udp_port
private int
udp_non_data_port
private long
last_upnp_read
private InetAddress
dht_address
private long
dht_address_time
private long
last_force_read_ext
private InetAddress
last_external_address
Constructors Summary
protected AZMyInstanceImpl(com.aelitis.azureus.core.AzureusCore _core, AZInstanceManagerImpl _manager)

	
	
	
						
			 

	
		core	= _core;
		manager	= _manager;
		
		id	= COConfigurationManager.getStringParameter( "ID", "" );
		
		if ( id.length() == 0 ){
			
			id	= "" + SystemTime.getCurrentTime();
		}
		
		id = ByteFormatter.encodeString( new SHA1Simple().calculateHash( id.getBytes()));
		
		COConfigurationManager.addListener( 
			new COConfigurationListener()
			{
				public void
				configurationSaved()
				{
					readConfig( false );
				}
			});
		
		readConfig( true );
		
		core.addLifecycleListener(
			new AzureusCoreLifecycleAdapter()
			{
				public void
				started(
					AzureusCore		core )
				{
					core.removeLifecycleListener( this );
					
				    PluginInterface dht_pi = core.getPluginManager().getPluginInterfaceByClass( DHTPlugin.class );
		        	
				    DHTPlugin dht = null;
				    
				    if ( dht_pi != null ){
			    	
				    	dht = (DHTPlugin)dht_pi.getPlugin();
				    	
				    	if ( dht != null ){
				    		
				        	dht.addListener(
				        		new DHTPluginListener()
				        		{
				        			public void
				        			localAddressChanged(
				        				DHTPluginContact	local_contact )
				        			{
				        				dht_address 		= local_contact.getAddress().getAddress();
				        				dht_address_time	= SystemTime.getCurrentTime();
				        				
				        				manager.informChanged( AZMyInstanceImpl.this );
				        			}
				        		});
				    	}
				    }
				}
			});
	
Methods Summary
public java.net.InetAddressgetExternalAddress()

		return( readExternalAddress());
	
public java.lang.StringgetID()

		return( id );
	
public java.net.InetAddressgetInternalAddress()

		return( internal_address );
	
public intgetTCPListenPort()

		return( tcp_port );
	
public intgetUDPListenPort()

		return( udp_port );
	
public intgetUDPNonDataListenPort()

		return( udp_non_data_port );
	
protected voidreadConfig(boolean first_time)

		InetAddress	new_internal_address	= NetworkAdmin.getSingleton().getDefaultBindAddress();
		
		if ( new_internal_address == null ){
			
			try{
				new_internal_address = InetAddress.getByName( "0.0.0.0" );
				
			}catch( Throwable e ){			
			}
		}
				
		int	new_tcp_port 			= TCPNetworkManager.getSingleton().getTCPListeningPortNumber();
		int	new_udp_port 			= UDPNetworkManager.getSingleton().getUDPListeningPortNumber();
		int new_udp_non_data_port	= UDPNetworkManager.getSingleton().getUDPNonDataListeningPortNumber();
		
		boolean	same = true;
		
		if ( !first_time ){
			
			same = 	internal_address.equals( new_internal_address) &&
					tcp_port == new_tcp_port &&
					udp_port == new_udp_port &&
					udp_non_data_port == new_udp_non_data_port;
		}
		
		internal_address 	= new_internal_address;
		tcp_port			= new_tcp_port;
		udp_port			= new_udp_port;
		udp_non_data_port	= new_udp_non_data_port;
		
		if ( !same ){
			
			manager.informChanged( this );
		}
	
protected java.net.InetAddressreadExternalAddress()

		InetAddress	 external_address = null;

			// no point in kicking off any queries if we're closing
		
		if ( manager.isClosing()){
			
			external_address	= last_external_address;
			
			if ( external_address == null ){
				
				try{
					external_address = InetAddress.getByName("127.0.0.1");
					
				}catch( Throwable e ){
					
					Debug.printStackTrace(e);
				}
			}
			
			return( external_address );
		}
		
	    PluginInterface dht_pi = core.getPluginManager().getPluginInterfaceByClass( DHTPlugin.class );
        	
	    DHTPlugin dht = null;
	    
	    if ( dht_pi != null ){
    	
	    	dht = (DHTPlugin)dht_pi.getPlugin();
	    }
	    
	    	// if DHT has informed us of an address then we use this - most reliable up to date one
	    	// unless the version server cache time is more recent
	    
	    if ( dht_address != null && dht_address_time <= SystemTime.getCurrentTime()){
	    	
	    	long cache_time = VersionCheckClient.getSingleton().getCacheTime( false );
	    	 
	    	if ( cache_time <= dht_address_time ){
	    		
	    		external_address = dht_address;
	    	}
	    }

	    if ( 	external_address == null &&
	    		( dht == null || dht.getStatus() != DHTPlugin.STATUS_RUNNING )){
		
	    		// use cached version if available and the DHT isn't

			String	str_address = VersionCheckClient.getSingleton().getExternalIpAddress( true, false );
		
			if ( str_address != null ){
				
				try{
					
					external_address	= InetAddress.getByName( str_address );
					
				}catch( Throwable e ){
					
					Debug.printStackTrace(e);
				}
			}
		}
		
		if ( external_address == null && dht != null  ){
			
				// no cache, use DHT (this will hang during initialisation, hence the use of cached
				// version above
			
			try{
				external_address = dht.getLocalAddress().getAddress().getAddress();
	        	
			}catch( Throwable e ){
			}
		}
		
		long	now = SystemTime.getCurrentTime();
		
		if ( last_force_read_ext > now ){
			
			last_force_read_ext	= now;
		}
		
		boolean	ok_to_try_ext = now - last_force_read_ext > FORCE_READ_EXT_MIN;
		
	    	// try upnp - limit frequency unless external read is possible in which
			// case we try upnp first
			// currently we only use UPnP to validate our current external address, not
			// to deduce new ones (as for example there may be multiple upnp devices and
			// we don't know which one to believe
	   
		if ( external_address == null && last_external_address != null ){
			
			if ( last_upnp_read > now ){
				
				last_upnp_read = now;
			}
			
			if ( now - last_upnp_read > UPNP_READ_MIN || ok_to_try_ext ){
				
				last_upnp_read	= now;
				
				try{
				    PluginInterface upnp_pi = core.getPluginManager().getPluginInterfaceByClass( UPnPPlugin.class );
			        			    
				    if ( upnp_pi != null ){
			    	
				    	UPnPPlugin upnp = (UPnPPlugin)upnp_pi.getPlugin();
				    	
				    	String[]	addresses = upnp.getExternalIPAddresses();
				    	
				    	for (int i=0;i<addresses.length;i++){
				    		
				    		if ( addresses[i].equals( last_external_address.getHostAddress())){
				    			
				    			external_address = last_external_address; 
				    			
				    			break;
				    		}
				    	}
				    }
				}catch( Throwable e ){
				}
			}
		}
		
		if ( external_address == null ){
			
				// force read it
			
			if ( ok_to_try_ext ){
				
				last_force_read_ext	= now;
				
				external_address = core.getPluginManager().getDefaultPluginInterface().getUtilities().getPublicAddress();
			}
		}
		
			// no good address available		
		
		if ( external_address == null ){
				
			if ( last_external_address != null ){
				
				external_address = last_external_address;
				
			}else{
				try{
					external_address = InetAddress.getByName("127.0.0.1");
					
				}catch( Throwable e ){
					
					Debug.printStackTrace(e);
				}
			}
		}else{
			
			last_external_address	= external_address;
		}
		
		return( external_address );