FileDocCategorySizeDatePackage
LocalTrackerPlugin.javaAPI DocAzureus 3.0.3.415566Fri Sep 07 08:44:10 BST 2007com.aelitis.azureus.plugins.tracker.local

LocalTrackerPlugin

public class LocalTrackerPlugin extends Object implements org.gudy.azureus2.plugins.download.DownloadManagerListener, org.gudy.azureus2.plugins.download.DownloadListener, com.aelitis.azureus.core.instancemanager.AZInstanceManagerListener, Plugin

Fields Summary
private static final String
PLUGIN_NAME
private static final String
PLUGIN_CONFIGSECTION_ID
private static final long
ANNOUNCE_PERIOD
private static final long
RE_ANNOUNCE_PERIOD
private PluginInterface
plugin_interface
private com.aelitis.azureus.core.instancemanager.AZInstanceManager
instance_manager
private boolean
active
private org.gudy.azureus2.plugins.torrent.TorrentAttribute
ta_networks
private Map
downloads
private Map
track_times
private String
last_autoadd
private String
last_subnets
private org.gudy.azureus2.plugins.ui.config.BooleanParameter
enabled
private long
plugin_start_time
private long
current_time
private org.gudy.azureus2.plugins.logging.LoggerChannel
log
private Monitor
mon
Constructors Summary
Methods Summary
protected voidcheckActivation()

		try{
			mon.enter();
		
			if ( active ){
				
				return;
			}
			
			active	= true;
			
			plugin_interface.getUtilities().createThread(
				"Tracker",
				new Runnable()
				{
					public void
					run()
					{
						track();
					}
				});
			
		}finally{
			
			mon.exit();
		}
	
public voiddownloadAdded(org.gudy.azureus2.plugins.download.Download download)

		try{
			mon.enter();
		
			Torrent	torrent = download.getTorrent();
			
			if ( torrent == null ){
				
				return;
			}
			
			if ( torrent.isPrivate()){
				
				log.log( "Not tracking " + download.getName() + ": torrent is private" );

				return;
			}
		
			String[]	networks = download.getListAttribute( ta_networks );
			
			boolean	public_net = false;
			
			for (int i=0;i<networks.length;i++){
				
				if ( networks[i].equalsIgnoreCase( "Public" )){
						
					public_net	= true;
					
					break;
				}
			}
			
			if ( !public_net ){
				
				log.log( "Not tracking " + download.getName() + ": torrent has no public network" );

				return;
			}

			if ( enabled.getValue()){
				log.log( "Tracking " + download.getName());
			}

			downloads.put( download, new Long(0));
			
			download.addListener( this );
			
		}finally{
			
			mon.exit();
		}
	
public voiddownloadRemoved(org.gudy.azureus2.plugins.download.Download download)

		try{
			mon.enter();
		
			downloads.remove( download );
			
			download.removeListener( this );
			
		}finally{
			
			mon.exit();
		}
	
protected voidforceTrack(org.gudy.azureus2.plugins.download.Download download)

		try{
			mon.enter();

			downloads.put( download, new Long(0));
			
			String	dl_key = plugin_interface.getUtilities().getFormatters().encodeBytesToString(download.getTorrent().getHash());

			Iterator	it = track_times.values().iterator();
			
			while( it.hasNext()){
				
				((Map)it.next()).remove( dl_key );
			}
		}finally{
			
			mon.exit();
		}
		
		track( download );
	
protected voidhandleTrackResult(com.aelitis.azureus.core.instancemanager.AZInstanceTracked tracked_inst)

		AZInstance	inst	= tracked_inst.getInstance();
		
		Download	download = tracked_inst.getDownload();
				
		boolean	is_seed = tracked_inst.isSeed();
		
		long	now		= plugin_interface.getUtilities().getCurrentSystemTime();
		
		boolean	skip 	= false;
		
			// this code is here to deal with multiple interface machines that receive the result multiple times
		
		try{
			mon.enter();
			
			Map	map = (Map)track_times.get( inst.getID() );
			
			if ( map == null ){
				
				map	= new HashMap();
				
				track_times.put( inst.getID(), map );
			}
			
			String	dl_key = plugin_interface.getUtilities().getFormatters().encodeBytesToString(download.getTorrent().getHash());
			
			Long	last_track = (Long)map.get( dl_key );
			
			if ( last_track != null ){
				
				long	lt = last_track.longValue();
				
				if ( now - lt < 30*1000 ){
					
					skip	= true;
				}
			}
			
			map.put( dl_key, new Long(now));
			
		}finally{
			
			mon.exit();
		}
		
		if ( skip ){
		
			return;
		}
		
		log.log( "Tracked: " + inst.getString() + ": " + download.getName() + ", seed = " + is_seed );

		if ( download.isComplete() && is_seed ){
			
			return;
		}
		
		PeerManager	peer_manager = download.getPeerManager();
		
		if ( peer_manager != null ){	
			
			String	peer_ip			= inst.getInternalAddress().getHostAddress();
			int		peer_tcp_port	= inst.getTCPListenPort();
			int		peer_udp_port	= inst.getUDPListenPort();
			
			log.log( "    " + download.getName() + ": Injecting peer " + peer_ip + ":" + peer_tcp_port + "/" + peer_udp_port);
			
			peer_manager.addPeer( peer_ip, peer_tcp_port, peer_udp_port, false );
		}
	
public voidinitialize(PluginInterface _plugin_interface)

	
	 
	
		 	 
	
		plugin_interface	= _plugin_interface;
				
		plugin_interface.getPluginProperties().setProperty( "plugin.version", 	"1.0" );
		plugin_interface.getPluginProperties().setProperty( "plugin.name", 		PLUGIN_NAME );

		ta_networks 	= plugin_interface.getTorrentManager().getAttribute( TorrentAttribute.TA_NETWORKS );

		mon	= plugin_interface.getUtilities().getMonitor();
		
		log = plugin_interface.getLogger().getTimeStampedChannel(PLUGIN_NAME);
		
		UIManager	ui_manager = plugin_interface.getUIManager();
		
		BasicPluginConfigModel	config = ui_manager.createBasicPluginConfigModel( ConfigSection.SECTION_PLUGINS, PLUGIN_CONFIGSECTION_ID );

		config.addLabelParameter2( "Plugin.localtracker.info" );
		
		enabled = config.addBooleanParameter2( "Plugin.localtracker.enable", "Plugin.localtracker.enable", true );

		config.addLabelParameter2( "Plugin.localtracker.networks.info" );
		
		final StringParameter subnets = config.addStringParameter2( "Plugin.localtracker.networks", "Plugin.localtracker.networks", "" );

		final BooleanParameter include_wellknown = config.addBooleanParameter2( "Plugin.localtracker.wellknownlocals", "Plugin.localtracker.wellknownlocals", true );
		
		config.addLabelParameter2( "Plugin.localtracker.autoadd.info" );
		
		final StringParameter autoadd = config.addStringParameter2( "Plugin.localtracker.autoadd", "Plugin.localtracker.autoadd", "" );
		
		/*
		 * actually these parameters affect LAN detection as a whole, not just the local tracker,
		 * so leave them enabled...
		 * 
		enabled.addEnabledOnSelection( lp1 );
		enabled.addEnabledOnSelection( subnets );
		enabled.addEnabledOnSelection( lp2 );
		enabled.addEnabledOnSelection( autoadd );
		*/
		
		final BasicPluginViewModel	view_model = 
			plugin_interface.getUIManager().createBasicPluginViewModel( "Plugin.localtracker.name" );

		view_model.setConfigSectionID(PLUGIN_CONFIGSECTION_ID);
		view_model.getActivity().setVisible( false );
		view_model.getProgress().setVisible( false );
		
		log.addListener(
				new LoggerChannelListener()
				{
					public void
					messageLogged(
						int		type,
						String	content )
					{
						view_model.getLogArea().appendText( content + "\n" );
					}
					
					public void
					messageLogged(
						String		str,
						Throwable	error )
					{
						if ( str.length() > 0 ){
							view_model.getLogArea().appendText( str + "\n" );
						}
						
						StringWriter sw = new StringWriter();
						
						PrintWriter	pw = new PrintWriter( sw );
						
						error.printStackTrace( pw );
						
						pw.flush();
						
						view_model.getLogArea().appendText( sw.toString() + "\n" );
					}
				});
		
		plugin_start_time = plugin_interface.getUtilities().getCurrentSystemTime();

		instance_manager	= AzureusCoreFactory.getSingleton().getInstanceManager();
		
		instance_manager.addListener( this );
		
		plugin_interface.getPluginconfig().addListener(
				new PluginConfigListener()
				{
					public void
					configSaved()
					{
						processSubNets( subnets.getValue(),include_wellknown.getValue() );
						processAutoAdd( autoadd.getValue());
					}
				});
			
		processSubNets(subnets.getValue(), include_wellknown.getValue());
		processAutoAdd(autoadd.getValue());

		// XXX Would be better if we fired this off after (any) UI is complete,
		//     instead of a timer
		
		Utilities utilities = plugin_interface.getUtilities();
		utilities.createTimer("azlocalplugin:init", Thread.MIN_PRIORITY).addEvent(
				utilities.getCurrentSystemTime() + 15000, new UTTimerEventPerformer() {
					public void perform(UTTimerEvent event) {

						// take this off the main thread to reduce initialisation delay if we
						// have a lot of torrents

						plugin_interface.getDownloadManager().addListener(
								LocalTrackerPlugin.this);
					}
				});
	
public voidinstanceChanged(com.aelitis.azureus.core.instancemanager.AZInstance instance)

		if ( !enabled.getValue()){
			
			return;
		}
		
		log.log( "Changed: " + instance.getString());
	
public voidinstanceFound(com.aelitis.azureus.core.instancemanager.AZInstance instance)

		if ( !enabled.getValue()){
		
			return;
		}
		
		log.log( "Found: " + instance.getString());
		
		try{
			mon.enter();
			
			track_times.put( instance.getID(), new HashMap());
			
		}finally{
			
			mon.exit();
		}
		
		checkActivation();
	
public voidinstanceLost(com.aelitis.azureus.core.instancemanager.AZInstance instance)

		try{
			mon.enter();
			
			track_times.remove( instance.getID());
			
		}finally{
			
			mon.exit();
		}
		
		if ( !enabled.getValue()){
			
			return;
		}
		
		log.log( "Lost: " + instance.getString());
	
public voidinstanceTracked(com.aelitis.azureus.core.instancemanager.AZInstanceTracked instance)

		if ( !enabled.getValue()){
			
			return;
		}
		
		handleTrackResult( instance );
	
public voidpositionChanged(org.gudy.azureus2.plugins.download.Download download, int oldPosition, int newPosition)

	
protected voidprocessAutoAdd(java.lang.String autoadd)

		if ( autoadd.equals( last_autoadd )){
			
			return;
		}
		
		last_autoadd = autoadd;
		
		StringTokenizer	tok = new StringTokenizer( autoadd, ";");
					
		while( tok.hasMoreTokens()){
			
			String	peer = tok.nextToken();
				
			try{
				
				InetAddress p = InetAddress.getByName( peer.trim());
				
				if ( instance_manager.addInstance( p )){
				
					log.log( "Added peer '" + peer + "'" );
				}
			}catch( Throwable e ){
				
				log.log( "Failed to decode peer '" + peer + "'", e );
			}
		}
	
protected voidprocessSubNets(java.lang.String subnets, boolean include_well_known)

		if ( include_well_known != instance_manager.getIncludeWellKnownLANs()){
		
			instance_manager.setIncludeWellKnownLANs( include_well_known );
			
			log.log( "Include well known local networks set to " + include_well_known );
		}
		
		if ( subnets.equals( last_subnets )){
			
			return;
		}
		
		last_subnets = subnets;
		
		StringTokenizer	tok = new StringTokenizer( subnets, ";");
				
		while( tok.hasMoreTokens()){
			
			String	net = tok.nextToken().trim();
				
			try{
				
				if ( instance_manager.addLANSubnet( net )){
				
					log.log( "Added network '" + net + "'" );
				}
								
			}catch( Throwable e ){
				
				log.log( "Failed to add network '" + net + "'", e );
			}
		}
	
public voidstateChanged(org.gudy.azureus2.plugins.download.Download download, int old_state, int new_state)

		if ( 	new_state == Download.ST_DOWNLOADING ||
				new_state == Download.ST_SEEDING ){
			
			forceTrack( download );
		}
	
protected voidtrack()

		long	now = plugin_interface.getUtilities().getCurrentSystemTime();

		if ( now - plugin_start_time < 60*1000 ){
			
			try{
					// initial small delay to let things stabilise
				
				Thread.sleep( 15*1000 ); 
				
			}catch( Throwable e ){
			}
		}
				
		
		plugin_interface.getUtilities().createTimer( "LanPeerFinder:Tracker", true ).addPeriodicEvent(
				30*1000,
				new UTTimerEventPerformer() {
					
					public void perform( UTTimerEvent	event ) {
						
						current_time = plugin_interface.getUtilities().getCurrentSystemTime();

						try{
							
							List	todo = new ArrayList();
							
							try{
								mon.enter();
								
								Iterator	it = downloads.entrySet().iterator();
								
								while( it.hasNext()){
									
									Map.Entry	entry = (Map.Entry)it.next();
									
									Download	dl 		= (Download)entry.getKey();
									long		when	= ((Long)entry.getValue()).longValue();
									
									if ( when > current_time || current_time - when > ANNOUNCE_PERIOD ){
										
										todo.add( dl );
									}
								}

							}finally{
								
								mon.exit();
							}
							
							for (int i=0;i<todo.size();i++){
							
								track((Download)todo.get(i));
							}
							
						}catch( Throwable e ){
							
							log.log(e);
						}
						
					}
		
				});
		
	
protected voidtrack(org.gudy.azureus2.plugins.download.Download download)

		long	now = plugin_interface.getUtilities().getCurrentSystemTime();

		boolean	ok = false;
		
		try{
			mon.enter();
			
			Long		l_last_track	= (Long)downloads.get( download );
			
			if ( l_last_track == null ){
				
				return;
			}
			
			long	last_track = l_last_track.longValue();
			
			if ( last_track > now || now - last_track > RE_ANNOUNCE_PERIOD ){
					
				ok	= true;
	
				downloads.put( download, new Long( now ));
			}
			
		}finally{
			
			mon.exit();
		}
		
		if ( ok ){
			
			trackSupport( download );
		}
	
protected voidtrackSupport(org.gudy.azureus2.plugins.download.Download download)

		if ( !enabled.getValue()){
			
			return;
		}
		
		int	state = download.getState();
		
		if ( state == Download.ST_ERROR || state == Download.ST_STOPPED ){
			
			return;
		}
		
		AZInstanceTracked[]	peers = instance_manager.track( download );
		
		for (int i=0;i<peers.length;i++){
			
			handleTrackResult( peers[i] );
		}