FileDocCategorySizeDatePackage
TRHostTorrentHostImpl.javaAPI DocAzureus 3.0.3.414538Wed Sep 12 23:18:06 BST 2007org.gudy.azureus2.core3.tracker.host.impl

TRHostTorrentHostImpl

public class TRHostTorrentHostImpl extends Object implements TRHostTorrent
author
parg

Fields Summary
private TRHostImpl
host
private TRTrackerServer
server
private TRTrackerServerTorrent
server_torrent
private TOTorrent
torrent
private long
date_added
private int
port
private List
listeners_cow
private List
removal_listeners
private int
status
private boolean
persistent
private boolean
passive
private long
sos_uploaded
private long
sos_downloaded
private long
sos_bytes_in
private long
sos_bytes_out
private long
sos_announce
private long
sos_scrape
private long
sos_complete
private long
last_uploaded
private long
last_downloaded
private long
last_bytes_in
private long
last_bytes_out
private long
last_announce
private long
last_scrape
private Average
average_uploaded
private Average
average_downloaded
private Average
average_bytes_in
private Average
average_bytes_out
private Average
average_announce
private Average
average_scrape
private boolean
disable_reply_caching
private HashMap
data
protected AEMonitor
this_mon
Constructors Summary
protected TRHostTorrentHostImpl(TRHostImpl _host, TRTrackerServer _server, TOTorrent _torrent, int _port, long _date_added)


	
	
				
			
				
						
					 
	
		host		= _host;
		server		= _server;
		torrent		= _torrent;
		port		= _port;
		date_added	= _date_added;
	
Methods Summary
public voidaddListener(TRHostTorrentListener l)

		try{
			this_mon.enter();
	
			List	new_listeners = new ArrayList( listeners_cow );
			
			new_listeners.add(l);
			
			listeners_cow	= new_listeners;
			
		}finally{
			
			this_mon.exit();
		}
		
		host.torrentListenerRegistered();
	
public voidaddRemovalListener(TRHostTorrentWillBeRemovedListener l)

		try{
			this_mon.enter();
		
			removal_listeners.add(l);
			
		}finally{
			
			this_mon.exit();
		}
	
public booleancanBeRemoved()

		ArrayList	listeners_copy;
		
		try{
			this_mon.enter();
		
			listeners_copy = new ArrayList( removal_listeners );
		
		}finally{
			
			this_mon.exit();
		}
		
		for (int i=0;i<listeners_copy.size();i++){
			
			((TRHostTorrentWillBeRemovedListener)listeners_copy.get(i)).torrentWillBeRemoved( this );
		}
		
		return( true );
	
public voiddisableReplyCaching()

		TRTrackerServerTorrent	st = server_torrent;
		
		disable_reply_caching	= true;
		
		if ( st != null ){
		
			st.disableCaching();
		}
	
public longgetAnnounceCount()

		TRTrackerServerTorrentStats	stats = getStats();
	
		if ( stats != null ){
		
			return( sos_announce + stats.getAnnounceCount());
		}
		
		return( sos_announce );
	
public longgetAverageAnnounceCount()

		return( average_announce.getAverage());
	
public longgetAverageBytesIn()

		return( average_bytes_in.getAverage());
	
public longgetAverageBytesOut()

		return( average_bytes_out.getAverage() );
	
public longgetAverageDownloaded()

		return( average_downloaded.getAverage() );
	
public longgetAverageScrapeCount()

		return( average_scrape.getAverage());
	
public longgetAverageUploaded()

		return( average_uploaded.getAverage() );
	
public intgetBadNATCount()

		TRTrackerServerTorrentStats	stats = getStats();
		
		if ( stats != null ){
			
			return( stats.getBadNATPeerCount());
		}
			
		return( 0 );
	
public longgetCompletedCount()

		TRTrackerServerTorrentStats	stats = getStats();
		
		if ( stats != null ){
			
			return( sos_complete + stats.getCompletedCount());
		}
		
		return( sos_complete );
	
public java.lang.ObjectgetData(java.lang.String key)
To retreive arbitrary objects against this object.

  	if (data == null) return null;
    return data.get(key);
  
public longgetDateAdded()

		return( date_added );
	
public intgetLeecherCount()

		TRTrackerServerTorrentStats	stats = getStats();
	
		if ( stats != null ){
		
			return( stats.getLeecherCount());
		}
		
		return( 0 );
	
public TRHostPeer[]getPeers()

		try{
		
			TRTrackerServerPeer[]	peers = server.getPeers( torrent.getHash());
		
			if ( peers != null ){
			
				TRHostPeer[]	res = new TRHostPeer[peers.length];
				
				for (int i=0;i<peers.length;i++){
					
					res[i] = new TRHostPeerHostImpl(peers[i]);
				}
				
				return( res );
			}
		}catch( TOTorrentException e ){
			
			Debug.printStackTrace( e );
		}
		
		return( new TRHostPeer[0] );
	
public intgetPort()

		return( port );
	
public longgetScrapeCount()

		TRTrackerServerTorrentStats	stats = getStats();
		
		if ( stats != null ){
			
			return( sos_scrape + stats.getScrapeCount());
		}
		
		return( sos_scrape );
	
public intgetSeedCount()

		TRTrackerServerTorrentStats	stats = getStats();
	
		if ( stats != null ){
		
			return( stats.getSeedCount());
		}
		
		return( 0 );
	
protected TRTrackerServergetServer()

		return( server );
	
protected TRTrackerServerTorrentStatsgetStats()

		TRTrackerServerTorrent	st = server_torrent;
		
		if ( st != null ){
			
			return( st.getStats());
		}
		
		return( null );
	
public intgetStatus()

		return( status );
	
public TOTorrentgetTorrent()

		return( torrent );
	
public longgetTotalBytesIn()

		TRTrackerServerTorrentStats	stats = getStats();
		
		if ( stats != null ){
			
			return( sos_bytes_in + stats.getBytesIn());
		}
		
		return( sos_bytes_in );	
	
public longgetTotalBytesOut()

		TRTrackerServerTorrentStats	stats = getStats();
		
		if ( stats != null ){
			
			return( sos_bytes_out + stats.getBytesOut());
		}
		
		return( sos_bytes_out );	
	
public longgetTotalDownloaded()

		TRTrackerServerTorrentStats	stats = getStats();
		
		if ( stats != null ){
			
			return( sos_downloaded + stats.getDownloaded());
		}
		
		return( sos_downloaded );	
	
public longgetTotalLeft()

		TRTrackerServerTorrentStats	stats = getStats();
		
		if ( stats != null ){
			
			return( stats.getAmountLeft());
		}
		
		return( 0 );	
	
public longgetTotalUploaded()

		TRTrackerServerTorrentStats	stats = getStats();
		
		if ( stats != null ){
			
			return( sos_uploaded + stats.getUploaded());
		}
		
		return( sos_uploaded );
	
public booleanisPassive()

		return( passive );
	
public booleanisPersistent()

		return( persistent );
	
protected voidpostProcess(TRHostTorrentRequest req)

		List	listeners_ref = listeners_cow;
	
		for (int i=0;i<listeners_ref.size();i++){
		
			try{
				((TRHostTorrentListener)listeners_ref.get(i)).postProcess(req);
				
			}catch( TRHostException e ){
				
				throw( e );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
	
protected voidpreProcess(TRHostTorrentRequest req)

		List	listeners_ref = listeners_cow;
	
		for (int i=0;i<listeners_ref.size();i++){
		
			try{
				((TRHostTorrentListener)listeners_ref.get(i)).preProcess(req);
				
			}catch( TRHostException e ){
				
				throw( e );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
	
public voidremove()

		
		canBeRemoved();
		
		stop();
		
		host.remove( this );
	
public voidremoveListener(TRHostTorrentListener l)

		try{
			this_mon.enter();
		
			List	new_listeners = new ArrayList( listeners_cow );
			
			new_listeners.remove(l);
			
			listeners_cow	= new_listeners;
			
		}finally{
			
			this_mon.exit();
		}
	
public voidremoveRemovalListener(TRHostTorrentWillBeRemovedListener l)

		try{
			this_mon.enter();
		
			removal_listeners.remove(l);
		}finally{
			
			this_mon.exit();
		}
	
public voidsetData(java.lang.String key, java.lang.Object value)
To store arbitrary objects against this object.

  	try{
  		this_mon.enter();
  	
	  	if (data == null) {
	  	  data = new HashMap();
	  	}
	    if (value == null) {
	      if (data.containsKey(key))
	        data.remove(key);
	    } else {
	      data.put(key, value);
	    }
  	}finally{
  		
  		this_mon.exit();
  	}
  
public voidsetPassive(boolean b)

		passive	= b;
	
protected voidsetPersistent(boolean _persistent)

		persistent	= _persistent;
	
protected voidsetStartOfDayValues(long _date_added, long completed, long announces, long scrapes, long uploaded, long downloaded, long bytes_in, long bytes_out)

		date_added			= _date_added;
		sos_complete		= completed;
		sos_announce		= announces;
		sos_scrape			= scrapes;
		sos_uploaded		= uploaded;
		sos_downloaded		= downloaded;
		sos_bytes_in		= bytes_in;
		sos_bytes_out		= bytes_out;
	
protected voidsetTorrent(TOTorrent _torrent)

		torrent = _torrent;
	
public voidstart()

			// there's a potential deadlock situation if we call the server while holding
			// the torrent lock, as the server then calls back to the host and we get
			// a torrent -> host monitor chain. We already have a host->torrent chain.
			// easiest solution is to delegate call to the host, which will grab the host
			// monitor and then call back out to startSupport. Hence the chain is in the
			// right direction
		
		host.startTorrent( this );
	
protected voidstartSupport()

		try{
			this_mon.enter();
			
			// System.out.println( "TRHostTorrentHostImpl::start");
			
			status = TS_STARTED;
					
			server_torrent = server.permit( "", torrent.getHash(), true);
		
			if ( disable_reply_caching ){
				
				server_torrent.disableCaching();
			}
			
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
			
		}finally{
			
			this_mon.exit();
		}
				
		host.hostTorrentStateChange( this );
	
public voidstop()

		host.stopTorrent( this );
	
protected voidstopSupport()

		try{
			this_mon.enter();
			
			// System.out.println( "TRHostTorrentHostImpl::stop");
			
			status = TS_STOPPED;
				
			server.deny( torrent.getHash(), true);
		
			TRTrackerServerTorrent		st				= server_torrent;
			
			TRTrackerServerTorrentStats	torrent_stats 	= st==null?null:st.getStats();	
				
			if ( torrent_stats != null ){
				
				sos_uploaded	= sos_uploaded 		+ torrent_stats.getUploaded();
				sos_downloaded	= sos_downloaded 	+ torrent_stats.getDownloaded();
				sos_bytes_in	= sos_bytes_in 		+ torrent_stats.getBytesIn();
				sos_bytes_out	= sos_bytes_out 	+ torrent_stats.getBytesOut();
				sos_announce	= sos_announce		+ torrent_stats.getAnnounceCount();
				sos_scrape		= sos_scrape		+ torrent_stats.getScrapeCount();
				sos_complete	= sos_complete		+ torrent_stats.getCompletedCount();
				
				torrent_stats	= null;
			}
			
			last_uploaded		= 0;
			last_downloaded		= 0;
			last_bytes_in		= 0;
			last_bytes_out		= 0;
			last_announce		= 0;
			last_scrape			= 0;
			
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
			
		}finally{
			
			this_mon.exit();
		}
		
		host.hostTorrentStateChange( this );
	
protected voidupdateStats()

		TRTrackerServerTorrentStats	stats = getStats();
	
		if ( stats != null ){
		
			long	current_uploaded 	= stats.getUploaded();
								
			long ul_diff = current_uploaded - last_uploaded;
				
			if ( ul_diff < 0 ){
	
				ul_diff = 0;
			}
			
			average_uploaded.addValue(ul_diff);
			
			last_uploaded = current_uploaded;
			
				// downloaded 
			
			long	current_downloaded 	= stats.getDownloaded();
			
			long dl_diff = current_downloaded - last_downloaded;
				
			if ( dl_diff < 0 ){
				
				dl_diff = 0;
			}
			
			average_downloaded.addValue(dl_diff);
			
			last_downloaded = current_downloaded;
			
				// bytes in 
			
			long	current_bytes_in 	= stats.getBytesIn();
			
			long bi_diff = current_bytes_in - last_bytes_in;
			
			if ( bi_diff < 0 ){
								
				bi_diff = 0;
			}
			
			average_bytes_in.addValue(bi_diff);
			
			last_bytes_in = current_bytes_in;

				// bytes out 
			
			long	current_bytes_out 	= stats.getBytesOut();
			
			long bo_diff = current_bytes_out - last_bytes_out;
			
			if ( bo_diff < 0 ){
								
				bo_diff = 0;
			}
			
			average_bytes_out.addValue(bo_diff);
			
			last_bytes_out = current_bytes_out;
		
				// announce
			
			long	current_announce 	= stats.getAnnounceCount();
			
			long an_diff = current_announce - last_announce;
			
			if ( an_diff < 0 ){
								
				an_diff = 0;
			}
			
			average_announce.addValue(an_diff);
			
			last_announce = current_announce;
			
				// scrape 
			
			long	current_scrape 	= stats.getScrapeCount();
			
			long sc_diff = current_scrape - last_scrape;
			
			if ( sc_diff < 0 ){
								
				sc_diff = 0;
			}
			
			average_scrape.addValue(sc_diff);
			
			last_scrape = current_scrape;
		}