FileDocCategorySizeDatePackage
UPnPPluginService.javaAPI DocAzureus 3.0.3.412114Tue Nov 28 23:12:14 GMT 2006com.aelitis.azureus.plugins.upnp

UPnPPluginService

public class UPnPPluginService extends Object
author
parg

Fields Summary
protected UPnPWANConnection
connection
protected org.gudy.azureus2.plugins.ui.config.BooleanParameter
alert_success
protected org.gudy.azureus2.plugins.ui.config.BooleanParameter
grab_ports
protected org.gudy.azureus2.plugins.ui.config.BooleanParameter
alert_other_port_param
protected org.gudy.azureus2.plugins.ui.config.BooleanParameter
release_mappings
protected List
service_mappings
protected org.gudy.azureus2.core3.util.AEMonitor
this_mon
Constructors Summary
protected UPnPPluginService(UPnPWANConnection _connection, UPnPWANConnectionPortMapping[] _ports, org.gudy.azureus2.plugins.ui.config.BooleanParameter _alert_success, org.gudy.azureus2.plugins.ui.config.BooleanParameter _grab_ports, org.gudy.azureus2.plugins.ui.config.BooleanParameter _alert_other_port_param, org.gudy.azureus2.plugins.ui.config.BooleanParameter _release_mappings)

	   
	
	
						
			
						
						
						
						
	
		connection				= _connection;
		alert_success			= _alert_success;
		grab_ports				= _grab_ports;
		alert_other_port_param	= _alert_other_port_param;
		release_mappings		= _release_mappings;
		
		for (int i=0;i<_ports.length;i++){

			service_mappings.add( new serviceMapping( _ports[i]));
		}
	
Methods Summary
protected voidcheckMapping(LoggerChannel log, UPnPMapping mapping)

		try{
			this_mon.enter();
		
			if ( mapping.isEnabled()){
				
					// check for change of port number and delete old value if so
				
				for (int i=0;i<service_mappings.size();i++){
					
					serviceMapping	sm = (serviceMapping)service_mappings.get(i);
					
					if ( sm.getMappings().contains( mapping )){
				
						if ( sm.getPort() != mapping.getPort()){
							
							removeMapping( log, mapping, sm, false );
						}
					}
				}
			
				serviceMapping	grab_in_progress	= null;
				
				String local_address = connection.getGenericService().getDevice().getRootDevice().getLocalAddress().getHostAddress();
				
				for (int i=0;i<service_mappings.size();i++){
					
					serviceMapping	sm = (serviceMapping)service_mappings.get(i);
					
					if ( 	sm.isTCP() 		== mapping.isTCP() &&
							sm.getPort() 	== mapping.getPort()){				
				
						if ( sm.getInternalHost().equals( local_address )){
							
								// make sure we tie this to the mapping in case it
								// was external to begin with
							
							sm.addMapping( mapping  );
							
							if ( !sm.getLogged(mapping)){
								
								sm.setLogged(mapping);
								
								log.log( "Mapping " + mapping.getString() + " already established" );
							}
							
							return;
							
						}else{
							
							if ( !grab_ports.getValue() ){
		
								if ( !sm.getLogged(mapping)){
									
									sm.setLogged(mapping);
								
									String	text = 
										MessageText.getString( 
											"upnp.alert.differenthost", 
											new String[]{ mapping.getString(), sm.getInternalHost()});
																
									if ( alert_other_port_param.getValue()){
									
										log.logAlertRepeatable( LoggerChannel.LT_WARNING, text );
									}else{
										
										log.log( text);
									}
								}
								
								return;
								
							}else{
								
									// we're going to grab it
								
								sm.addMapping( mapping  );
	
								grab_in_progress	= sm;
							}
						}
					}
				}
				
					// not found - try and establish it + add entry even if we fail so
					// that we don't retry later
							
				try{
					connection.addPortMapping( 
						mapping.isTCP(), mapping.getPort(), 
						getDescriptionForPort( mapping.isTCP(), mapping.getPort()));
								
					String	text;
					
					if ( grab_in_progress != null ){
						
						text = MessageText.getString( 
								"upnp.alert.mappinggrabbed", 
								new String[]{ mapping.getString(), grab_in_progress.getInternalHost()});
					}else{
						
						text = MessageText.getString( 
								"upnp.alert.mappingok", 
								new String[]{ mapping.getString()});
					}
					
					log.log( text );
					
					if ( alert_success.getValue()){
						
						log.logAlertRepeatable( LoggerChannel.LT_INFORMATION, text );
					}
					
				}catch( Throwable e ){
					
					String	text = 
						MessageText.getString( 
								"upnp.alert.mappingfailed", 
								new String[]{ mapping.getString()});
					
					log.log( text, e );
				
					if ( alert_other_port_param.getValue()){
					
						log.logAlertRepeatable( LoggerChannel.LT_ERROR, text );
					}
				}
				
				if ( grab_in_progress == null ){
					
					serviceMapping	new_mapping = new serviceMapping( mapping );
				
					service_mappings.add( new_mapping );
				}
				
			}else{
					// mapping is disabled
				
				removeMapping( log, mapping, false );
			}
		}finally{
			
			this_mon.exit();
		}
	
public java.lang.StringgetAddress()

		return( connection.getGenericService().getDevice().getRootDevice().getLocation().getHost());
	
protected java.lang.StringgetDescriptionForPort(boolean TCP, int port)

		return( "Azureus UPnP " + port + " " + (TCP?"TCP":"UDP"));
	
public java.lang.StringgetExternalAddress()

		try{
			return( connection.getExternalIPAddress());
			
		}catch( Throwable e ){
			
			return( null );
		}
	
public java.lang.StringgetInfo()

		return( connection.getGenericService().getDevice().getRootDevice().getInfo());
	
public java.lang.StringgetName()

		return( connection.getGenericService().getDevice().getRootDevice().getDevice().getFriendlyName());
	
protected java.lang.StringgetOldDescriptionForPort(int port)

			// Remove one day - port name was changed as some routers use name uniqueness
			// to manage ports, hence UDP and TCP with same name failed
		
		return( "Azureus UPnP " + port );
	
public intgetPort()

		URL	url = connection.getGenericService().getDevice().getRootDevice().getLocation();
		
		int	port = url.getPort();
		
		if ( port == -1 ){
			
			port = url.getDefaultPort();
		}
		
		return( port );
	
protected UPnPWANConnectiongetService()

		return( connection );
	
protected voidremoveMapping(LoggerChannel log, UPnPMapping mapping, boolean end_of_day)

		try{
			this_mon.enter();
					
			for (int i=0;i<service_mappings.size();i++){
				
				serviceMapping	sm = (serviceMapping)service_mappings.get(i);
				
				if ( 	sm.isTCP() == mapping.isTCP() &&
						sm.getPort() == mapping.getPort() &&
						sm.getMappings().contains( mapping )){
					
					removeMapping( log, mapping, sm, end_of_day );
	
					return;
				}
			}
		}finally{
			
			this_mon.exit();
		}
	
protected voidremoveMapping(LoggerChannel log, UPnPMapping upnp_mapping, com.aelitis.azureus.plugins.upnp.UPnPPluginService$serviceMapping service_mapping, boolean end_of_day)

		if ( service_mapping.isExternal()){
		
			log.log( "Mapping " + service_mapping.getString() + " not removed as not created by Azureus" );
			
		}else{
			int	persistent	=  UPnPMapping.PT_DEFAULT;
			
			List	mappings = service_mapping.getMappings();
			
			for (int i=0;i<mappings.size();i++){
				
				UPnPMapping	map = (UPnPMapping)mappings.get(i); 
					
				int	p = map.getPersistent(); 
				
				if ( p == UPnPMapping.PT_DEFAULT ){
				
						// default - leave as is
					
				}else if ( p == UPnPMapping.PT_TRANSIENT ){
					
						// transient overrides default
					
					if ( persistent == UPnPMapping.PT_DEFAULT ){
						
						persistent	= p;
					}
				}else{
					
						// persistent overrides all others
					
					persistent	= UPnPMapping.PT_PERSISTENT;
				}
			}
			
				// set effective persistency
			
			if ( persistent == UPnPMapping.PT_DEFAULT ){
				
				persistent = release_mappings.getValue()?UPnPMapping.PT_TRANSIENT:UPnPMapping.PT_PERSISTENT;
			}
			
				// only time we take note of whether or not to release the mapping is
				// at closedown
			
			if ( end_of_day && persistent == UPnPMapping.PT_PERSISTENT ){

				log.log( "Mapping " + service_mapping.getString() + " not removed as mapping is persistent" );
	
			}else{
				
					// get the name here for the deletion case so that the subsequent message makes 
					// sense (as the name is derived from the current mappings, so getting it after
					// deleting it results in a name of <external>)
				
				String	service_name = service_mapping.getString();
				
				service_mapping.removeMapping( upnp_mapping );
				
				if ( service_mapping.getMappings().size() == 0 ){
				
					try{
						connection.deletePortMapping( 
								service_mapping.isTCP(), service_mapping.getPort());
				
						log.log( "Mapping " + service_name + " removed" );
						
					}catch( Throwable e ){
						
						log.log( "Mapping " + service_name + " failed to delete", e );
					}
					
					service_mappings.remove(service_mapping);
					
				}else{
					
					log.log( "Mapping " + service_mapping.getString() + " not removed as interest remains" );
				}
			}
		}