📄 btalgorithmpeerselectionatpeer.java
字号:
/*
* @(#)BTAlgorithmPeerSelectionAtPeer.java ver 1.2 6/20/2005
*
* Copyright 2005 Weishuai Yang (wyang@cs.binghamton.edu).
* All rights reserved.
*
*/
package gps.protocol.BT.algorithm;
import gps.Simulator;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Collections;
/**
* selects peers to send request
*
* @author Weishuai Yang
* @version 1.2, 6/20/2005
*/
public class BTAlgorithmPeerSelectionAtPeer {
/**
* default peer number to send request
*/
public static int DEFAULT_PEER_NUM_LIMIT=10;
/**
* peer number to send request
*/
public int peerNumLimit=DEFAULT_PEER_NUM_LIMIT;
/**
* random number generator
*/
protected Random rand=null;
/**
* constructs peer selection algorithm object
* @param n seed for random object
*/
public BTAlgorithmPeerSelectionAtPeer(int n){
rand=new Random(n+Integer.parseInt(Simulator.getInstance().getProperties().getProperty("RandomSeed")));
}
/**
* selects peers having documents from peers local record
* @param list local record of peers having this document
* @return peer list to send request to
*
* one difference of the slectPeers algorithm between peer side
* and tracker side is that selected nodes are removed from the
* passed-in list for the peer side selection, but not for tracker
* side selection.
*/
public ArrayList selectPeers(List list){
//this algorithm simply randomly select peerNumLimit of nodes.
ArrayList listClone=null;
if(list.size()<=peerNumLimit){
listClone=new ArrayList(list);
list.clear();
listClone.trimToSize();
Collections.shuffle(listClone, rand);
return listClone;
}
int index=0;
//for performance, different treatment for list size between
//peerNumLimit and 2*peerNumLimit
if(list.size()<2*peerNumLimit){
//remove list.size-peerNumLimit;
listClone=new ArrayList(list);
while(listClone.size()>peerNumLimit){
index=rand.nextInt()%listClone.size();
if(index<0) index=-index;
listClone.remove(index);
}
}
else {
//adding and increase to peerNumLimit
listClone=new ArrayList(peerNumLimit);
while(listClone.size()<peerNumLimit){
index=rand.nextInt()%list.size();
if(index<0) index=-index;
listClone.add(list.get(index));
}
}
//currently, the difference between the selection algorithm
//at tracker and peer is that, the selected elements are
//removed at peer, but not at tracker
list.removeAll(listClone);
Collections.shuffle(listClone, rand);
return listClone;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -