FileDocCategorySizeDatePackage
DHTSpeedTesterImpl.javaAPI DocAzureus 3.0.3.415222Wed Aug 08 09:41:18 BST 2007com.aelitis.azureus.core.dht.speed.impl

DHTSpeedTesterImpl

public class DHTSpeedTesterImpl extends Object implements com.aelitis.azureus.core.dht.speed.DHTSpeedTester

Fields Summary
private static final long
PING_TIMEOUT
private org.gudy.azureus2.plugins.PluginInterface
plugin_interface
private com.aelitis.azureus.core.dht.DHT
dht
private int
contact_num
private com.aelitis.azureus.core.util.bloom.BloomFilter
tried_bloom
private LinkedList
pending_contacts
private List
active_pings
private List
new_listeners
private com.aelitis.azureus.core.util.CopyOnWriteList
listeners
Constructors Summary
public DHTSpeedTesterImpl(com.aelitis.azureus.core.dht.DHT _dht)

	
	 
	
				 
	
		dht			= _dht;
		
		plugin_interface	= dht.getLogger().getPluginInterface();
		
		UTTimer	timer = plugin_interface.getUtilities().createTimer(
				"DHTSpeedTester:finder", true );
		
		timer.addPeriodicEvent( 
				5000,
				new UTTimerEventPerformer()
				{
					public void 
					perform(
						UTTimerEvent event) 
					{
						findContacts();	
					}
				});
		
		timer.addPeriodicEvent( 
				1000,
				new UTTimerEventPerformer()
				{
					int	tick_count;
					
					public void 
					perform(
						UTTimerEvent event) 
					{
						try{
							pingContacts( tick_count );
							
						}finally{
							
							tick_count++;
						}
					}
				});
	
Methods Summary
public voidaddListener(com.aelitis.azureus.core.dht.speed.DHTSpeedTesterListener listener)

		synchronized( new_listeners ){
			
			new_listeners.add( listener );
		}
	
protected voidfindContacts()

		DHTTransportContact[]	reachables = dht.getTransport().getReachableContacts();
		
		for (int i=0;i<reachables.length;i++){
			
			DHTTransportContact	contact = reachables[i];
			
			byte[]	address = contact.getAddress().getAddress().getAddress();
			
			if ( tried_bloom == null || tried_bloom.getEntryCount() > 500 ){
				
				tried_bloom = BloomFilterFactory.createAddOnly( 4096 );
			}
			
			if ( !tried_bloom.contains( address )){
				
				tried_bloom.add( address );
				
				synchronized( pending_contacts ){
					
					potentialPing	ping = 
						new potentialPing( 
								contact,
								DHTNetworkPositionManager.estimateRTT( contact.getNetworkPositions(), dht.getTransport().getLocalContact().getNetworkPositions()));
									
					pending_contacts.add( 0, ping );
					
					if ( pending_contacts.size() > 60 ){
						
						pending_contacts.removeLast();
					}
				}
			}
		}
	
public intgetContactNumber()

		return( contact_num );
	
protected voidinformResults(com.aelitis.azureus.core.dht.speed.DHTSpeedTesterContact[] contacts, int[] rtts)

		Iterator	it = listeners.iterator();
		
		while( it.hasNext()){
			
			try{
				((DHTSpeedTesterListener)it.next()).resultGroup( contacts, rtts );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
	
protected voidpingContacts(int tick_count)

		List	copy = null;
		
		synchronized( new_listeners ){
		
			if ( new_listeners.size() > 0 ){
				
				copy = new ArrayList( new_listeners );
				
				new_listeners.clear();
			}
		}
		
		if ( copy != null ){
			
			for (int i=0;i<copy.size();i++){
			
				DHTSpeedTesterListener	listener = (DHTSpeedTesterListener)copy.get(i);
				
				listeners.add( listener );
				
				for ( int j=0;j<active_pings.size();j++){
					
					activePing	ping = (activePing)active_pings.get(j);
					
					if ( ping.isInformedAlive()){
						
						try{
						
							listener.contactAdded( ping );
							
						}catch( Throwable e ){
							
							Debug.printStackTrace(e);
						}
					}
				}
			}
		}
		
		Iterator	pit = active_pings.iterator();
		
		pingInstanceSet	ping_set = new pingInstanceSet( true );
		
		while( pit.hasNext()){
			
			activePing ping = (activePing)pit.next();
			
			if ( ping.update( ping_set, tick_count )){
				
				if ( !ping.isInformedAlive()){
					
					ping.setInformedAlive();
					
					Iterator	it = listeners.iterator();
					
					while( it.hasNext()){
						
						try{
							((DHTSpeedTesterListener)it.next()).contactAdded( ping );
							
						}catch( Throwable e ){
							
							Debug.printStackTrace(e);
						}
					}
				}
			}
			
			if ( ping.isDead()){
				
				pit.remove();
				
				ping.informDead();
			}
		}
		
		ping_set.setFull();
		
			// we try and keep three active pings running so we can spot overall trends in ping time
			// each active ping is selected from the best rtt from the current 3 best three rtt estimates
		
		int	num_active = active_pings.size();
		
		if ( num_active < contact_num ){

			Set	pc = new TreeSet(
					new Comparator()
					{
						public int
						compare(
							Object	o1,
							Object	o2 )
						{
							potentialPing	p1 = (potentialPing)o1;
							potentialPing	p2 = (potentialPing)o2;
							
							return( p1.getRTT() - p2.getRTT());
						}				
					});
			
			synchronized( pending_contacts ){
				
				pc.addAll( pending_contacts );
			}
			
			Iterator	it = pc.iterator();
			
			if ( pc.size() >= 3){
				
					// find best candidates
				
				List	pps = new ArrayList();
				
				for (int i=0;i<3;i++){
					
					potentialPing	pp = (potentialPing)it.next();
					
					pps.add( pp );
					
					it.remove();
					
					synchronized( pending_contacts ){
						
						pending_contacts.remove( pp );
					}
				}
				
				active_pings.add( new activePing( pps ));
			}
		}else if ( num_active > contact_num ){
			
			for (int i=0;i<num_active-contact_num;i++){
				
				((activePing)active_pings.get(i)).destroy();
			}
		}
	
public voidremoveListener(com.aelitis.azureus.core.dht.speed.DHTSpeedTesterListener listener)

		listeners.remove( listener );
	
public voidsetContactNumber(int number)

		contact_num	= number;