FileDocCategorySizeDatePackage
ExternalSeedPlugin.javaAPI DocAzureus 3.0.3.410723Fri Jun 08 12:08:42 BST 2007com.aelitis.azureus.plugins.extseed

ExternalSeedPlugin

public class ExternalSeedPlugin extends Object implements org.gudy.azureus2.plugins.download.DownloadManagerListener, org.gudy.azureus2.plugins.Plugin

Fields Summary
private static ExternalSeedReaderFactory[]
factories
private org.gudy.azureus2.plugins.PluginInterface
plugin_interface
private org.gudy.azureus2.plugins.download.DownloadManagerStats
dm_stats
private org.gudy.azureus2.plugins.logging.LoggerChannel
log
private Random
random
private Map
download_map
private Monitor
download_mon
Constructors Summary
Methods Summary
protected voidaddPeers(org.gudy.azureus2.plugins.download.Download download, java.util.List _peers)

		final List peers = new ArrayList();
		
		peers.addAll( _peers );
	
		if ( peers.size() > 0 ){
			
			boolean	add_listener = false;
			
			try{
				download_mon.enter();
				
				List	existing_peers = (List)download_map.get( download );
				
				if ( existing_peers == null ){
					
					add_listener	= true;
					
					existing_peers = new ArrayList();
					
					download_map.put( download, existing_peers );
				}
	
				Iterator	it = peers.iterator();
					
				while( it.hasNext()){
					
					ExternalSeedPeer	peer = (ExternalSeedPeer)it.next();
					
					boolean	skip = false;
					
					for (int j=0;j<existing_peers.size();j++){
						
						ExternalSeedPeer	existing_peer = (ExternalSeedPeer)existing_peers.get(j);
						
						if ( existing_peer.sameAs( peer )){
							
							skip	= true;
							
							break;
						}
					}

					if ( skip ){
						
						it.remove();
						
					}else{
						
						log( download.getName() + " found seed " + peer.getName());
						

						existing_peers.add( peer );
					}
				}
				
				
			}finally{
				
				download_mon.exit();
			}
			
			if ( add_listener ){
			
				download.addPeerListener(
					new DownloadPeerListener()
					{
						public void
						peerManagerAdded(
							Download		download,
							PeerManager		peer_manager )
						{
							List	existing_peers = getPeers();
	
							if ( existing_peers== null ){
								
								return;
							}
							
							for (int i=0;i<existing_peers.size();i++){
								
								ExternalSeedPeer	peer = (ExternalSeedPeer)existing_peers.get(i);
								
								peer.setManager( peer_manager );
							}
						}
						
						public void
						peerManagerRemoved(
							Download		download,
							PeerManager		peer_manager )
						{
							List	existing_peers = getPeers();
							
							if ( existing_peers== null ){
								
								return;
							}
							
							for (int i=0;i<existing_peers.size();i++){
								
								ExternalSeedPeer	peer = (ExternalSeedPeer)existing_peers.get(i);
								
								peer.setManager( null );
							}
						}
						
						protected List
						getPeers()
						{
							List	existing_peers = null;
							
							try{
								download_mon.enter();
								
								List	temp = (List)download_map.get( download );
								
								if ( temp != null ){
									
									existing_peers = new ArrayList( temp.size());
									
									existing_peers.addAll( temp );
								}
							}finally{
								
								download_mon.exit();
							}
							
							return( existing_peers );
						}
					});
			}else{
				
					// fix up newly added peers to current peer manager
				
				PeerManager	existing_pm = download.getPeerManager();
				
				if ( existing_pm != null ){
					
					for (int i=0;i<peers.size();i++){
						
						ExternalSeedPeer	peer = (ExternalSeedPeer)peers.get(i);

						if ( peer.getManager() == null ){
							
							peer.setManager( existing_pm );
						}
					}
				}
			}
		}
	
public voidaddSeed(org.gudy.azureus2.plugins.download.Download download, java.util.Map config)

		Torrent	torrent = download.getTorrent();
		
		if ( torrent == null ){
			
			return;
		}
		
		List	peers = new ArrayList();
		
		for (int i=0;i<factories.length;i++){
			
			ExternalSeedReader[]	x = factories[i].getSeedReaders( this, download, config );
			
			for (int j=0;j<x.length;j++){
				
				ExternalSeedReader	reader = x[j];
				
				ExternalSeedPeer	peer = new ExternalSeedPeer( this, download, reader );
				
				peers.add( peer );
			}
		}
		
		addPeers( download, peers );
	
public voiddownloadAdded(org.gudy.azureus2.plugins.download.Download download)

		Torrent	torrent = download.getTorrent();
		
		if ( torrent == null ){
			
			return;
		}
		
		List	peers = new ArrayList();
		
		for (int i=0;i<factories.length;i++){
			
			ExternalSeedReader[]	x = factories[i].getSeedReaders( this, download );
			
			for (int j=0;j<x.length;j++){
				
				ExternalSeedReader	reader = x[j];
				
				ExternalSeedPeer	peer = new ExternalSeedPeer( this, download, reader );
				
				peers.add( peer );
			}
		}
		
		addPeers( download, peers );
	
public voiddownloadRemoved(org.gudy.azureus2.plugins.download.Download download)

		try{
			download_mon.enter();
			
			download_map.remove( download );
			
		}finally{
			
			download_mon.exit();
		}	
	
public intgetGlobalDownloadRateBytesPerSec()

		return( dm_stats.getDataAndProtocolReceiveRate());
	
public org.gudy.azureus2.plugins.PluginInterfacegetPluginInterface()

		return( plugin_interface );
	
public voidinitialize(org.gudy.azureus2.plugins.PluginInterface _plugin_interface)

	
	 
	
			 
	
		plugin_interface	= _plugin_interface;
		
		dm_stats = plugin_interface.getDownloadManager().getStats();
		
		plugin_interface.getPluginProperties().setProperty( "plugin.version", 	"1.0" );
		plugin_interface.getPluginProperties().setProperty( "plugin.name", 		"External Seed" );
				
		log	= plugin_interface.getLogger().getTimeStampedChannel( "External Seeds" );
		
		final BasicPluginViewModel	view_model = 
			plugin_interface.getUIManager().createBasicPluginViewModel( "Plugin.extseed.name" );

		
		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" );
					}
				});		
		
		download_mon	= plugin_interface.getUtilities().getMonitor();
		
		Utilities utilities = plugin_interface.getUtilities();
		// XXX Would be better if we fired this off after (any) UI is complete,
		//     instead of a timer
		utilities.createTimer("ExternalPeerInitialize", Thread.MIN_PRIORITY).addEvent(
				utilities.getCurrentSystemTime() + 15000, new UTTimerEventPerformer() {
					public void perform(UTTimerEvent event) {
						plugin_interface.getDownloadManager().addListener(
								ExternalSeedPlugin.this);
					}
				});
		
		UTTimer timer = utilities.createTimer("ExternalPeerScheduler", true);
		timer.addPeriodicEvent(
				5000,
				new UTTimerEventPerformer()
				{
					public void
					perform(
						UTTimerEvent		event )
					{
						try{
							Iterator	it = download_map.values().iterator();
							
							while( it.hasNext()){
								
								List	peers = randomiseList((List)it.next());
								
								for (int i=0;i<peers.size();i++){
									
										// bail out early if the state changed for this peer
										// so one peer at a time gets a chance to activate
									
									if (((ExternalSeedPeer)peers.get(i)).checkConnection()){
										
										break;
									}
								}
							}
							
						}catch( Throwable e ){
							// we do this without holding the monitor as doing so causes potential
							// deadlock between download_mon and the connection's connection_mon
							
							// so ignore possible errors here that may be caused by concurrent
							// modification to the download_map ans associated lists. We are only
							// reading the data so errors will only be transient
						}
					}
				});
		
	
public voidlog(java.lang.String str)

		log.log( str );
	
public voidlog(java.lang.String str, java.lang.Throwable e)

		log.log( str, e );
	
protected java.util.ListrandomiseList(java.util.List l)

		if ( l.size() < 2 ){
			
			return(l);
		}
		
		List	new_list = new ArrayList();
		
		for (int i=0;i<l.size();i++){
			
			new_list.add( random.nextInt(new_list.size()+1), l.get(i));
		}
		
		return( new_list );
	
protected voidremovePeer(ExternalSeedPeer peer)

		Download	download = peer.getDownload();
		
		try{
			download_mon.enter();
		
			List	existing_peers = (List)download_map.get( download );

			if ( existing_peers != null ){
				
				if ( existing_peers.remove( peer )){
					
					log( download.getName() + " removed seed " + peer.getName());
				}
			}
		}finally{
			
			download_mon.exit();
		}