FileDocCategorySizeDatePackage
OverallStatsImpl.javaAPI DocAzureus 3.0.3.46192Mon Jul 30 17:49:16 BST 2007org.gudy.azureus2.core3.stats.transfer.impl

OverallStatsImpl

public class OverallStatsImpl extends org.gudy.azureus2.core3.global.impl.GlobalManagerAdpater implements org.gudy.azureus2.core3.stats.transfer.OverallStats
author
Olivier

Fields Summary
private static final long
TEN_YEARS
private static final long
STATS_PERIOD
com.aelitis.azureus.core.AzureusCore
core
long
totalDownloaded
long
totalUploaded
long
totalUptime
long
lastDownloaded
long
lastUploaded
long
lastUptime
long
session_start_time
protected AEMonitor
this_mon
private int
tick_count
Constructors Summary
public OverallStatsImpl(com.aelitis.azureus.core.AzureusCore _core)

	core	= _core;
	
    Map 	stats = load();
    validateAndLoadValues(stats);

    core.addLifecycleListener(
    	new AzureusCoreLifecycleAdapter()
    	{
    		public void
    		componentCreated(
    			AzureusCore				core,
    			AzureusCoreComponent	component )
    		{
    			if ( component instanceof GlobalManager ){
    				
    				GlobalManager	gm = (GlobalManager)component;
    				
    				gm.addListener( OverallStatsImpl.this, false );
    				   
    			    SimpleTimer.addPeriodicEvent(
    			    	"OverallStats", 
    			    	STATS_PERIOD, 
    			    	new TimerEventPerformer()
    			    	{
    			    		public void 
    			    		perform(TimerEvent event) 
    			    		{
    			    			updateStats( false );
    			    		}
    			    	});
    			}
    		}
    	});

  
Methods Summary
public voiddestroyInitiated()

    updateStats( true );
  
public intgetAverageDownloadSpeed()

		if(totalUptime > 1) {
      return (int)(totalDownloaded / totalUptime);
    }
    return 0;
	
public intgetAverageUploadSpeed()

    if(totalUptime > 1) {
      return (int)(totalUploaded / totalUptime);
    }
    return 0;
	
public longgetDownloadedBytes()

		return totalDownloaded;
	
protected longgetLong(java.util.Map map, java.lang.String name)

	  if ( map == null ){
		  return( 0 );
	  }
	
	  Object	obj = map.get(name);
	
	  if (!(obj instanceof Long )){
		return(0);
	  }
	
	  return(((Long)obj).longValue());
  
public longgetSessionUpTime()

    return (SystemTime.getCurrentTime() - session_start_time) / 1000;
  
public longgetTotalUpTime()

		return totalUptime;
  
public longgetUploadedBytes()

		return totalUploaded;
	
private java.util.Mapload(java.lang.String filename)

  
    
    
  
    return( FileUtil.readResilientConfigFile( filename ));
  
private java.util.Mapload()

	  return( load("azureus.statistics"));
	
private voidsave(java.lang.String filename, java.util.Map map)

  	  
  	try{
  		this_mon.enter();
  	  		
  		FileUtil.writeResilientConfigFile( filename, map );
  		
  	}finally{
  		
  		this_mon.exit();
  	}
  
private voidsave(java.util.Map map)

	  save("azureus.statistics", map);
	
private voidupdateStats(boolean force)

  	try{
  		this_mon.enter();
  	
	    long current_time = SystemTime.getCurrentTime() / 1000;
	    
	    if ( current_time < lastUptime ) {  //time went backwards
	      lastUptime = current_time;
	      return;
	    }
	    
	    GlobalManagerStats stats = core.getGlobalManager().getStats();
	    
	    long	current_total_received 	= stats.getTotalDataBytesReceived() + stats.getTotalProtocolBytesReceived();
	    long	current_total_sent		= stats.getTotalDataBytesSent() + stats.getTotalProtocolBytesSent();
	    
	    totalDownloaded +=  current_total_received - lastDownloaded;
	    lastDownloaded = current_total_received;
	    
	    if( totalDownloaded < 0 )  totalDownloaded = 0;

	    totalUploaded +=  current_total_sent - lastUploaded;
	    lastUploaded = current_total_sent;

	    if( totalUploaded < 0 )  totalUploaded = 0;
	    
	    long delta = current_time - lastUptime;
	    
	    if( delta > 100 || delta < 0 ) { //make sure the time diff isn't borked
	      lastUptime = current_time;
	      return;
	    }
	    
	    if( totalUptime > TEN_YEARS ) {  //total uptime > 10years is an error, reset
	      totalUptime = 0;
	    }
	    
	    if( totalUptime < 0 )  totalUptime = 0;
	    
	    totalUptime += delta;
	    lastUptime = current_time;
	    
	    tick_count++;
    
	    HashMap	overallMap = new HashMap();
	    
	    overallMap.put("downloaded",new Long(totalDownloaded));
	    overallMap.put("uploaded",new Long(totalUploaded));
	    overallMap.put("uptime",new Long(totalUptime));

	    Map	map = new HashMap();
	    
	    map.put( "all", overallMap );
	    
	    save( map );
  	}finally{
  	
  		this_mon.exit();
  	}
  
private voidvalidateAndLoadValues(java.util.Map statisticsMap)

	  
    lastUptime = SystemTime.getCurrentTime() / 1000;

    Map overallMap = (Map) statisticsMap.get("all");

    totalDownloaded = getLong( overallMap, "downloaded" );
	totalUploaded = getLong( overallMap, "uploaded" );
	totalUptime = getLong( overallMap, "uptime" );