FileDocCategorySizeDatePackage
RunEverythingPlugin.javaAPI DocAzureus 3.0.3.48741Thu Jul 27 23:48:52 BST 2006com.aelitis.azureus.plugins.startstoprules.always

RunEverythingPlugin

public class RunEverythingPlugin extends Object implements org.gudy.azureus2.plugins.download.DownloadManagerListener, org.gudy.azureus2.plugins.download.DownloadListener, org.gudy.azureus2.plugins.download.DownloadTrackerListener, org.gudy.azureus2.plugins.Plugin

Fields Summary
private org.gudy.azureus2.plugins.PluginInterface
plugin_interface
private org.gudy.azureus2.plugins.logging.LoggerChannel
logger
private Map
downloads
private org.gudy.azureus2.plugins.utils.Monitor
downloads_mon
private org.gudy.azureus2.plugins.utils.Semaphore
work_sem
private volatile boolean
closing
Constructors Summary
Methods Summary
public voidannounceResult(org.gudy.azureus2.plugins.download.DownloadAnnounceResult result)

		checkRules();
	
protected voidcheckRules()

		work_sem.release();
	
public voiddownloadAdded(org.gudy.azureus2.plugins.download.Download download)

		log( "added: " + download.getName() + ", state = " + Download.ST_NAMES[ download.getState()]);
		
		downloadData	dd = new downloadData( download );
		
		try{
			downloads_mon.enter();

			downloads.put( download, dd );
	
		}finally{
			
			downloads_mon.exit();
		}
		
		download.addListener( this );
		
		checkRules();
	
public voiddownloadRemoved(org.gudy.azureus2.plugins.download.Download download)

		try{
			downloads_mon.enter();
			
			downloads.remove( download );
			
		}finally{
			
			downloads_mon.exit();
		}
		
		download.removeListener( this );
	
		checkRules();
	
public voidinitialize(org.gudy.azureus2.plugins.PluginInterface _pi)

		plugin_interface	= _pi;
		
		logger				= plugin_interface.getLogger().getChannel( "RunEverythingSeedingRules" ); 
				
		plugin_interface.addListener(
			new PluginListener()
			{
				public void
				initializationComplete()
				{
				}
				
				public void
				closedownInitiated()
				{
					closing	= true;
				}
				
				public void
				closedownComplete()
				{
				}
			});
			
		
		downloads = new HashMap(); 
		
		downloads_mon = plugin_interface.getUtilities().getMonitor();
		
		work_sem	= plugin_interface.getUtilities().getSemaphore();
		
		plugin_interface.getDownloadManager().addListener( this );
		
		plugin_interface.getUtilities().createTimer("DownloadRules", true ).addPeriodicEvent(
			10000,
			new UTTimerEventPerformer()
			{
				public void 
				perform(
					UTTimerEvent event) 
				{
					checkRules();
				}
			});
		
		plugin_interface.getUtilities().createThread(
			"DownloadRules",
			new Runnable()
			{
				public void
				run()
				{
					processLoop();
				}
			});
	
public static voidload(org.gudy.azureus2.plugins.PluginInterface _plugin_interface)

	    PluginManagerDefaults defaults = PluginManager.getDefaults();

	    defaults.setDefaultPluginEnabled( PluginManagerDefaults.PID_START_STOP_RULES, false );
	
protected voidlog(java.lang.String str)

		logger.log( str );
	
public voidpositionChanged(org.gudy.azureus2.plugins.download.Download download, int oldPosition, int newPosition)

		checkRules();
	
protected voidprocessLoop()

		while( !closing ){
			
			work_sem.reserve();
			
			while( work_sem.reserveIfAvailable()){
			}
			
			try{
				processSupport();
				
				Thread.sleep( 250 );
				
			}catch( Throwable e ){
				
				e.printStackTrace();
			}
		}
	
protected voidprocessSupport()

		if ( closing ){
			
			return;
		}
		
		try{
			downloads_mon.enter();

			List	dls = new ArrayList( downloads.values());
			
				// remove any ignored ones
			
			Iterator	it = dls.iterator();
			
			while (it.hasNext()){
				
				downloadData	dd = (downloadData)it.next();
				
				if ( dd.ignore()){
					
					it.remove();
				}
			}		
									
				// execute an "initialize" on any waiting ones
				
			it = dls.iterator();
			
			while (it.hasNext()){
				
				downloadData	dd = (downloadData)it.next();
				
				if ( dd.getState() == Download.ST_WAITING ){
					
					it.remove();
					
					try{
						log( "initialising " + dd.getName());
						
						dd.getDownload().initialize();
						
					}catch( DownloadException e ){
						
						e.printStackTrace();
					}
				}
			}
			
				// execute a "start" on any READY ones
			
			it = dls.iterator();
			
			while (it.hasNext()){
				
				downloadData	dd = (downloadData)it.next();
				
				if ( dd.getState() == Download.ST_READY ){
					
					it.remove();
					
					try{
						log( "starting " + dd.getName());
						
						dd.getDownload().start();
						
					}catch( DownloadException e ){
						
						e.printStackTrace();
					}
				}
			}
					
				// start downloads
			
			it = dls.iterator();

			while (it.hasNext()){
				
				downloadData	dd  = (downloadData)it.next();

				if ( dd.getState() == Download.ST_QUEUED && !dd.isComplete()){
					
					try{					
						it.remove();
						
						log( "restarting download " + dd.getName());
						
						dd.getDownload().restart();
												
					}catch( DownloadException e ){
						
						e.printStackTrace();
					}
				}
			}
					
				// start seeds
			
			it = dls.iterator();
			
			while ( it.hasNext()){
			
				downloadData	dd  = (downloadData)it.next();

				if ( dd.isComplete() && dd.getState() == Download.ST_QUEUED ){
					
					try{					
						it.remove();
						
						log( "restarting seed " + dd.getName());
						
						dd.getDownload().restart();
												
					}catch( DownloadException e ){
						
						e.printStackTrace();
					}
				}
			}		
		}finally{
			
			downloads_mon.exit();
		}
	
public voidscrapeResult(org.gudy.azureus2.plugins.download.DownloadScrapeResult result)

		checkRules();
	
public voidstateChanged(org.gudy.azureus2.plugins.download.Download download, int old_state, int new_state)

		log( "Rules: state change for " + download.getName() + ": " + Download.ST_NAMES[old_state] + "->" + Download.ST_NAMES[new_state]);
		
		checkRules();