FileDocCategorySizeDatePackage
UnchokerUtil.javaAPI DocAzureus 3.0.3.47441Thu Jan 25 14:16:02 GMT 2007com.aelitis.azureus.core.peermanager.unchoker

UnchokerUtil

public class UnchokerUtil extends Object
Utility collection for unchokers.

Fields Summary
Constructors Summary
Methods Summary
public static org.gudy.azureus2.core3.peer.impl.PEPeerTransportgetNextOptimisticPeer(java.util.ArrayList all_peers, boolean factor_reciprocated, boolean allow_snubbed)
Choose the next peer, optimistically, that should be unchoked.

param
all_peers list of peer to choose from
param
factor_reciprocated if true, factor in how much (if any) this peer has reciprocated when choosing
param
allow_snubbed allow the picking of snubbed-state peers as last resort
return
the next peer to optimistically unchoke, or null if there are no peers available

	  
	  ArrayList	peers = getNextOptimisticPeers( all_peers, factor_reciprocated, allow_snubbed, 1 );
	  
	  if ( peers != null ){
		  
		  return((PEPeerTransport)peers.get(0));
	  }
	  
	  return( null );
  
public static java.util.ArrayListgetNextOptimisticPeers(java.util.ArrayList all_peers, boolean factor_reciprocated, boolean allow_snubbed, int num_needed)

    //find all potential optimistic peers
    ArrayList optimistics = new ArrayList();
    for( int i=0; i < all_peers.size(); i++ ) {
    	PEPeerTransport peer = (PEPeerTransport)all_peers.get( i );
      
      if( isUnchokable( peer, false ) && peer.isChokedByMe() ) {
        optimistics.add( peer );
      }
    }
    
    if( optimistics.isEmpty() && allow_snubbed ) {  //try again, allowing snubbed peers as last resort
      for( int i=0; i < all_peers.size(); i++ ) {
      	PEPeerTransport peer = (PEPeerTransport)all_peers.get( i );
        
        if( isUnchokable( peer, true ) && peer.isChokedByMe() ) {
          optimistics.add( peer );
        }
      }
    }

    if( optimistics.isEmpty() )  return null;  //no unchokable peers avail
    
    //factor in peer reciprocation ratio when picking optimistic peers
    
    ArrayList	result = new ArrayList(optimistics.size());
    
    if ( factor_reciprocated ){
    	
      ArrayList ratioed_peers = new ArrayList( optimistics.size() );
      long[] ratios = new long[ optimistics.size() ];
      Arrays.fill( ratios, Long.MIN_VALUE );
        
      //order by upload ratio
      for( int i=0; i < optimistics.size(); i++ ) {
      	PEPeerTransport peer = (PEPeerTransport)optimistics.get( i );

        //score of >0 means we've uploaded more, <0 means we've downloaded more
        long score = peer.getStats().getTotalDataBytesSent() - peer.getStats().getTotalDataBytesReceived();

        UnchokerUtil.updateLargestValueFirstSort( score, ratios, peer, ratioed_peers, 0 );  //higher value = worse score
      }
      
	  for (int i=0;i<num_needed && ratioed_peers.size() > 0;i++ ){

		  double factor = 1F / ( 0.8 + 0.2 * Math.pow( RandomUtils.nextFloat(), -1 ) );  //map to sorted list using a logistic curve 
      
		  int pos = (int)(factor * ratioed_peers.size());

		  result.add(ratioed_peers.remove( pos ));
	  }
    }else{

	    for (int i=0;i<num_needed && optimistics.size() > 0;i++ ){
	    	
		    int rand_pos = new Random().nextInt( optimistics.size() );
		    		    
		    result.add( optimistics.remove( rand_pos ));
	    }
    }
    
    return( result );
    
    //TODO:
    //in downloading mode, we would be better off optimistically unchoking just peers we are interested in ourselves,
    //as they could potentially reciprocate. however, new peers have no pieces to share, and are not interesting to
    //us, and would never be unchoked, and thus would never get any data.
    //we could use a deterministic method for new peers to get their very first piece from us
  
public static booleanisUnchokable(org.gudy.azureus2.core3.peer.impl.PEPeerTransport peer, boolean allow_snubbed)
Test whether or not the given peer is allowed to be unchoked.

param
peer to test
param
allow_snubbed if true, ignore snubbed state
return
true if peer is allowed to be unchoked, false if not

    return peer.getPeerState() == PEPeer.TRANSFERING && !peer.isSeed() && peer.isInterested() && ( !peer.isSnubbed() || allow_snubbed );
  
public static voidperformChokeUnchoke(org.gudy.azureus2.core3.peer.impl.PEPeerTransport to_choke, org.gudy.azureus2.core3.peer.impl.PEPeerTransport to_unchoke)

  	if( to_choke != null && !to_choke.isChokedByMe() ) {  
  		to_choke.sendChoke();   		
  	}
  	
  	if( to_unchoke != null && to_unchoke.isChokedByMe() ) {
  		to_unchoke.sendUnChoke();
		}
  
public static voidperformChokes(java.util.ArrayList peers_to_choke, java.util.ArrayList peers_to_unchoke)
Send choke/unchoke messages to the given peers.

param
peers_to_choke
param
peers_to_unchoke

  	//do chokes
  	if( peers_to_choke != null ) {
  		for( int i=0; i < peers_to_choke.size(); i++ ) {
  			final PEPeerTransport peer = (PEPeerTransport)peers_to_choke.get( i );
			
  			if( !peer.isChokedByMe() ) {
  				peer.sendChoke(); 
  			}
  		}
  	}
		
		//do unchokes
  	if( peers_to_unchoke != null ) {
  		for( int i=0; i < peers_to_unchoke.size(); i++ ) {
  			final PEPeerTransport peer = (PEPeerTransport)peers_to_unchoke.get( i );
			
  			if( peer.isChokedByMe() ) {   //TODO add UnchokerUtil.isUnchokable() test here to be safe?
  				peer.sendUnChoke();
  			}
  		}  	
  	}
  
public static voidupdateLargestValueFirstSort(long new_value, long[] values, org.gudy.azureus2.core3.peer.impl.PEPeerTransport new_item, java.util.ArrayList items, int start_pos)
Update (if necessary) the given list with the given value while maintaining a largest-value-first (as seen so far) sort order. NOTE: You will need to initialize the values array to Long.MIN_VALUE if you want to store negative values!

param
new_value to use
param
values existing values array
param
new_item to insert
param
items existing items
param
start_pos index at which to start compare

	items.ensureCapacity( values.length );
    for( int i=start_pos; i < values.length; i++ ) {
      if( new_value >= values[ i ] ) {
        for( int j = values.length - 2; j >= i; j-- ) {  //shift displaced values to the right
          values[j + 1] = values[ j ];
        }
        
        if( items.size() == values.length ) {  //throw away last item if list too large 
            items.remove( values.length - 1 );
        }

        values[ i ] = new_value;
        items.add( i, new_item );
          
        return;
      }
    }