ArrayList best_peers = new ArrayList();
long[] bests = new long[ max_to_unchoke ]; //ensure we never rank more peers than needed
//fill slots with peers who we are currently downloading the fastest from
for( int i=0; i < all_peers.size(); i++ ) {
PEPeerTransport peer = (PEPeerTransport)all_peers.get( i );
if( peer.isInteresting() && UnchokerUtil.isUnchokable( peer, false ) ) { //viable peer found
long rate = peer.getStats().getSmoothDataReceiveRate();
if( rate > 256 ) { //filter out really slow peers
UnchokerUtil.updateLargestValueFirstSort( rate, bests, peer, best_peers, 0 );
}
}
}
//if we havent yet picked enough slots
if( best_peers.size() < max_to_unchoke ) {
int start_pos = best_peers.size();
//fill the remaining slots with peers that we have downloaded from in the past
for( int i=0; i < all_peers.size(); i++ ) {
PEPeerTransport peer = (PEPeerTransport)all_peers.get( i );
if( peer.isInteresting() && UnchokerUtil.isUnchokable( peer, false ) && !best_peers.contains( peer ) ) { //viable peer found
long uploaded_ratio = peer.getStats().getTotalDataBytesSent() / (peer.getStats().getTotalDataBytesReceived() + (DiskManager.BLOCK_SIZE-1));
//make sure we haven't already uploaded several times as much data as they've sent us
if( uploaded_ratio <3) {
UnchokerUtil.updateLargestValueFirstSort( peer.getStats().getTotalDataBytesReceived(), bests, peer, best_peers, start_pos );
}
}
}
}
return best_peers;