FileDocCategorySizeDatePackage
StatsWriterPeriodicImpl.javaAPI DocAzureus 3.0.3.45308Tue Jan 16 08:14:22 GMT 2007org.gudy.azureus2.core3.stats.impl

StatsWriterPeriodicImpl

public class StatsWriterPeriodicImpl extends Object implements StatsWriterPeriodic, COConfigurationListener
author
parg

Fields Summary
private static final LogIDs
LOGID
private static StatsWriterPeriodicImpl
singleton
private static org.gudy.azureus2.core3.util.AEMonitor
class_mon
private static int
start_count
private static Thread
current_thread
private long
last_write_time
private com.aelitis.azureus.core.AzureusCore
core
private boolean
config_enabled
private int
config_period
private String
config_dir
private String
config_file
Constructors Summary
protected StatsWriterPeriodicImpl(com.aelitis.azureus.core.AzureusCore _core)

		core	= _core;
		
		COConfigurationManager.addListener( this );	
	
Methods Summary
public voidconfigurationSaved()

			// only pick up configuration changes when saved
			
		readConfigValues();
		
		writeStats();
	
public static StatsWriterPeriodiccreate(com.aelitis.azureus.core.AzureusCore _core)

	
	  
	
				 
	
		try{
			class_mon.enter();
		
			if ( singleton == null ){
				
				singleton = new StatsWriterPeriodicImpl(_core);
			}
			
			return( singleton );
			
		}finally{
			
			class_mon.exit();
		}
	
protected voidreadConfigValues()

		config_enabled 	= COConfigurationManager.getBooleanParameter( "Stats Enable" );
		
		config_period	= COConfigurationManager.getIntParameter( "Stats Period" );
		
		config_dir		= COConfigurationManager.getStringParameter( "Stats Dir" );
		
		config_file		= COConfigurationManager.getStringParameter( "Stats File" );
	
public voidstart()

		try{
			class_mon.enter();
			
			start_count++;
			
			if ( start_count == 1 ){
							
				current_thread = 
					new AEThread("StatsWriter"){
						public void
						runSupport()
						{
							update();
						}
					};
					
				current_thread.setDaemon( true );
				
				current_thread.start();
			}
		}finally{
			
			class_mon.exit();
		}
	
public voidstop()

		try{
			class_mon.enter();
			
			start_count--;
			
			if ( start_count == 0 ){
				
				current_thread = null;
			}
		}finally{
			
			class_mon.exit();
		}
	
protected voidupdate()

		readConfigValues();
		
		while( true ){
									
			try{
				class_mon.enter();
				
				if ( Thread.currentThread() != current_thread ){
					
					break;
				}
				
				writeStats();	
				
			}catch( Throwable e ){
				
				Debug.printStackTrace(e );
				
			}finally{
				
				class_mon.exit();
			}
			
			try{
				int	period;
				
				if ( !config_enabled ){
					
					period = DEFAULT_SLEEP_PERIOD;
								
				}else{
				
				 	period = config_period*1000;
				}
				
				if ( period > DEFAULT_SLEEP_PERIOD ){
					
					period = DEFAULT_SLEEP_PERIOD;
				}
				
				Thread.sleep( period );
				
			}catch( InterruptedException e ){
				
				Debug.printStackTrace( e );
			}
		}
	
protected voidwriteStats()

							
		if ( !config_enabled ){
			
			return;
		}

		int	period = config_period;
		
		long	now = SystemTime.getCurrentTime() /1000;
        
        if( now < last_write_time ) {  //time went backwards
          last_write_time   = now;
        }
		
			// if we have a 1 second period then now-last-write_time will often be 0 (due to the
			// rounding of SystemTime) and the stats won't be written - hence the check against
			// (period-1). Its only
		
		if ( now - last_write_time < ( period - 1 ) ){
			
			return;
		}
		
		last_write_time	= now;
		
		try{
			String	dir = config_dir;

			dir = dir.trim();
			
			if ( dir.length() == 0 ){
				
				dir = File.separator;			
			}
			
			String	file_name = dir;
			
			if ( !file_name.endsWith( File.separator )){
				
				file_name = file_name + File.separator;
			}
			
			String	file = config_file;

			if ( file.trim().length() == 0 ){
				
				file = DEFAULT_STATS_FILE_NAME;
			}
			
			file_name += file;
		
			if (Logger.isEnabled())
				Logger.log(new LogEvent(LOGID, "Stats Logged to '" + file_name + "'"));				
			
			new StatsWriterImpl( core ).write( file_name );
			
		}catch( Throwable e ){
			Logger.log(new LogEvent(LOGID, "Stats Logging fails", e));
		}