FileDocCategorySizeDatePackage
UploadSessionPicker.javaAPI DocAzureus 3.0.3.46009Tue Jul 25 22:56:50 BST 2006com.aelitis.azureus.core.peermanager.uploadslots

UploadSessionPicker

public class UploadSessionPicker extends Object

Fields Summary
private final LinkedList
next_optimistics
private final AEMonitor
next_optimistics_mon
private final LinkedList
helpers
private final DownloadingRanker
down_ranker
private final SeedingRanker
seed_ranker
Constructors Summary
protected UploadSessionPicker()



	  
		/*nothing*/
	
Methods Summary
protected voidderegisterHelper(UploadHelper helper)

		try {  next_optimistics_mon.enter();
			helpers.remove( helper );
		
			boolean rem = next_optimistics.removeAll( Collections.singleton( helper ) );		
			if( !rem ) Debug.out( "!rem" );
		}
		finally {  next_optimistics_mon.exit();  }
	
protected intgetHelperCount()

		try {  next_optimistics_mon.enter();
			return next_optimistics.size();
		}
		finally {  next_optimistics_mon.exit();  }
	
private java.util.ArrayListglobalGetAllDownloadPeers()

		try {  next_optimistics_mon.enter();
			ArrayList all = new ArrayList();
			
			for( Iterator it = helpers.iterator(); it.hasNext(); ) {
				UploadHelper helper = (UploadHelper)it.next();
				
				if( !helper.isSeeding() )  {  //filter out seeding
					all.addAll( helper.getAllPeers() );
				}
			}
			
			return all;
		}
		finally {  next_optimistics_mon.exit();  }
	
private voidinsertHelper(UploadHelper helper)

		int pos = RandomUtils.nextInt( next_optimistics.size() + 1 );  //pick a random location
		next_optimistics.add( pos, helper );  //store into location
	
protected java.util.LinkedListpickBestDownloadSessions(int max_sessions)

		//TODO factor download priority into best calculation?
		
		ArrayList all_peers = globalGetAllDownloadPeers();
		
		if( all_peers.isEmpty() )  return new LinkedList();
		
		ArrayList best = down_ranker.rankPeers( max_sessions, all_peers );
		
		if( best.size() != max_sessions ) {
			Debug.outNoStack( "best.size()[" +best.size()+ "] != max_sessions[" +max_sessions+ "]" );
		}
		
		if( best.isEmpty() ) {
			return new LinkedList();
		}
		
		
		LinkedList best_sessions = new LinkedList();
		
		for( Iterator it = best.iterator(); it.hasNext(); ) {
			PEPeerTransport peer = (PEPeerTransport)it.next();
			UploadSession session = new UploadSession( peer, UploadSession.TYPE_DOWNLOAD );
			best_sessions.add( session );
		}
		
		return best_sessions;
	
protected UploadSessionpickNextOptimisticSession()

		
		try {  next_optimistics_mon.enter();		
		
			HashSet failed_helpers = null;
		
			int loops_allowed = next_optimistics.size();
			
			while( loops_allowed > 0 ) {
				
				UploadHelper helper = (UploadHelper)next_optimistics.removeFirst();  //take from front
	
				next_optimistics.addLast( helper );   //add back at end
				
				if( failed_helpers == null || !failed_helpers.contains( helper ) ) {   //pre-emptive check to see if we've already tried this helper
					
					PEPeerTransport peer;
					
					if( helper.isSeeding() ) {
						peer = seed_ranker.getNextOptimisticPeer( helper.getAllPeers() );
					}
					else {
						peer = down_ranker.getNextOptimisticPeer( helper.getAllPeers() );						
					}
					
					if( peer == null )  {  //no peers from this helper to unchoke
						
						if( failed_helpers == null )  failed_helpers = new HashSet();   //lazy alloc
						
						failed_helpers.add( helper );  //remember this helper in case we get it again in another loop round
					}
					else {   //found an optimistic peer!
						
						return new UploadSession( peer, helper.isSeeding() ? UploadSession.TYPE_SEED : UploadSession.TYPE_DOWNLOAD );						
					}
				}
				
				loops_allowed--;
			}
			
			return null;  //no optimistic peer found
			
		}
		finally {  next_optimistics_mon.exit();  }
	
protected voidregisterHelper(UploadHelper helper)

		try {  next_optimistics_mon.enter();
			helpers.add( helper );
		
			int priority = helper.getPriority();
		
			//the higher the priority, the more optimistic unchoke chances they get
			for( int i=0; i < priority; i++ ) {
				insertHelper( helper );
			}			
		}
		finally {  next_optimistics_mon.exit();  }
	
protected voidupdateHelper(UploadHelper helper)

		try {  next_optimistics_mon.enter();
			int priority = helper.getPriority();  //new priority
		
			int count = 0;
		
			for( Iterator it = next_optimistics.iterator(); it.hasNext(); ) {
				UploadHelper h = (UploadHelper)it.next();
				if( h == helper ) {
					count++;
				
					if( count > priority ) {  //new priority is lower
						it.remove();  //trim
					}
				}
			}
		
			if( count < priority ) {  //new priority is higher
				for( int i=count; i < priority; i++ ) {
					insertHelper( helper );  //add
				}
			}
		}
		finally {  next_optimistics_mon.exit();  }