FileDocCategorySizeDatePackage
UploadSlotManager.javaAPI DocAzureus 3.0.3.48433Sun Jul 30 14:43:14 BST 2006com.aelitis.azureus.core.peermanager.uploadslots

UploadSlotManager

public class UploadSlotManager extends Object

Fields Summary
private static final int
EXPIRE_NORMAL
private static final int
EXPIRE_OPTIMISTIC
private static final int
EXPIRE_SEED
public static boolean
AUTO_SLOT_ENABLE
private long
last_process_time
private static final UploadSlotManager
instance
private final UploadSessionPicker
picker
private final UploadSlot[]
slots
private long
current_round
int
count
Constructors Summary
private UploadSlotManager()

	
	
	
	  
		if( AUTO_SLOT_ENABLE ) {		
			System.out.println( "UPLOAD_SLOT_MANAGER SCHEDULAR STARTED" );		
		
			PeerControlSchedulerFactory.getSingleton().register( new PeerControlInstance() {
				public void schedule() {
					long now = SystemTime.getCurrentTime();
				
					if( now - last_process_time >= 10000 ) {   //10sec process loop				
						process();					
						last_process_time = now;
					}
					else if( last_process_time > now ) {
						Debug.out( "OOPS, time went backwards!" );
						last_process_time = now;
					}				
				}
			});
		}
		else {
			//System.out.println( "AUTO UPLOAD SLOT *DISABLED*" );
		}
	
Methods Summary
public voidderegisterHelper(UploadHelper helper)

		if( AUTO_SLOT_ENABLE ) {
			picker.deregisterHelper( helper );
		}
	
private UploadSessiongetNextBestSession(java.util.LinkedList best)

	
	      
		count++;
		System.out.print( "getNextBestSession [" +count+"] best.size=" +best.size()+ "  " );
		
		if( !best.isEmpty() ) {
			UploadSession session = (UploadSession)best.removeFirst();   //get next
			
			if( !isAlreadySlotted( session ) ) {   //found an unslotted session
				
				System.out.println( "OK found session [" +session.getStatsTrace()+ "]" );
				
				return session;
			}			
			
			System.out.println( "FAIL already-slotted session [" +session.getStatsTrace()+ "]" );
			
			return getNextBestSession( best );			//oops, already been slotted, try again
		}
		
		return null;
	
public static com.aelitis.azureus.core.peermanager.uploadslots.UploadSlotManagergetSingleton()

	
	      return instance;  
private booleanisAlreadySlotted(UploadSession session)

		for( int i=0; i < slots.length; i++ ) {
			UploadSession s = slots[i].getSession();
			if( s != null && s.isSameSession( session ) )  return true;			
		}
		
		return false;
	
private UploadSessionpickOptSession()

		
		
		int max = picker.getHelperCount();  //max number of sessions the picker will return before it loops back 'round
		
		for( int i=0; i < max; i++ ) {   //make sure we don't loop
			
			UploadSession session = picker.pickNextOptimisticSession();
			
			if( session != null && !isAlreadySlotted( session ) ) {
				return session;  //found!
			}	
		}
		
		return null;  //no optimistic sessions
	
private voidprintSlotStats()

		
		System.out.println( "\nUPLOAD SLOTS [" +current_round+ "x]:" );
		
		for( int i=0; i < slots.length; i++ ) {
			UploadSlot slot = slots[i];
			
			System.out.print( "[" +i+ "]: " );
			
			String slot_type = slot.getSlotType() == UploadSlot.TYPE_NORMAL ? "NORM" : "OPTI";
			
			long rem = slot.getExpireRound() - current_round;
			String remaining = rem < 0 ? "" : " [" +rem+ "]rr";		
			
			String ses_trace = slot.getSession() == null ? "EMPTY" : slot.getSession().getStatsTrace();
			
			System.out.println( slot_type + remaining+ " : " +ses_trace );			
		}		
	
private voidprocess()

		
		if( !AUTO_SLOT_ENABLE )  return;
		
		current_round++;
		
		ArrayList to_stop = new ArrayList();
		
		//get a list of the best sessions, peers who are uploading to us in download mode
		LinkedList best_sessions = picker.pickBestDownloadSessions( slots.length );
		
		int best_size = best_sessions.size();
		
		
		//go through all currently expired slots and pick sessions for next round
		for( int i=0; i < slots.length; i++ ) {
			UploadSlot slot = slots[i];
			
			if( slot.getExpireRound() <= current_round ) {  //expired			
				UploadSession session = slot.getSession();
				
				if( session != null ) {
					to_stop.add( session );  //make sure it gets stopped
					slot.setSession( null );  //clear slot					
				}
				
				if( slot.getSlotType() == UploadSlot.TYPE_OPTIMISTIC ) {		//optimistic
					//pick new session for optimistic upload
					session = pickOptSession();
					
					if( session == null ) {
						continue;
					}
					
					if( session.getSessionType() == UploadSession.TYPE_SEED ) {  //place first seed session in a normal slot
						best_sessions.addFirst( session );  //put at front of good list to ensure it gets picked						
						//pick a new optimistic session, whatever type
						session = pickOptSession();						
						if( session == null )  continue;
					}
					
					slot.setSession( session );  //place the new session in the slot
					slot.setExpireRound( current_round + EXPIRE_OPTIMISTIC );  //set the new expire time
				}
				else {   //normal					
					session = getNextBestSession( best_sessions );  //get the next "best" session
					
					if( session == null && best_size == slots.length ) {
						Debug.out( "session == null && best_size == slots.length" );
					}
					
					
					if( session == null ) {  //no download mode peers, must be only seeding; or all best are already slotted						
						session = pickOptSession();   //just pick the next optimistic
						if( session == null )  continue;   //no optimistic either
					}
					
					slot.setSession( session );  //place the session in the slot
					slot.setExpireRound( current_round + ( session.getSessionType() == UploadSession.TYPE_SEED ? EXPIRE_SEED : EXPIRE_NORMAL ) );  //set the new expire time
				}
				
			}
		}
				
		//start and stop sessions for the round
	
		//filter out sessions allowed to continue another round, so we don't stop-start them
		for( Iterator it = to_stop.iterator(); it.hasNext(); ) {
			UploadSession stop_s = (UploadSession)it.next();
			
			for( int i=0; i < slots.length; i++ ) {
				if( stop_s.isSameSession( slots[i].getSession() ) ) {  //need to do this because two session objects can represent the same peer
					it.remove();
					break;
				}
			}		
		}
		
		//stop discontinued sessions
		for( Iterator it = to_stop.iterator(); it.hasNext(); ) {  
			UploadSession session = (UploadSession)it.next();
			session.stop();
		}

		//ensure sessions are started
		for( int i=0; i < slots.length; i++ ) {
			UploadSession s = slots[i].getSession();
			if( s != null )  s.start();
		}
		
		printSlotStats();		
	
public voidregisterHelper(UploadHelper helper)

		if( AUTO_SLOT_ENABLE ) {
			picker.registerHelper( helper );
		}
	
public voidupdateHelper(UploadHelper helper)
Notify of helper state change (i.e. priority changed)

param
helper

		if( AUTO_SLOT_ENABLE ) {
			picker.updateHelper( helper );
		}