-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeerReversePriorityQueue.java
91 lines (86 loc) · 3.02 KB
/
PeerReversePriorityQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.concurrent.PriorityBlockingQueue;
import java.util.Comparator;
/**
* Write a description of class PeerPriorityQueue here.
* @author Shuhan Liu (sl1041) 154007082
* @author Nicole Heimbach (nsh43) 153002353
*/
public class PeerReversePriorityQueue
{
// instance variables - replace the example below with your own
private Comparator<Peer> comparer;
private PriorityBlockingQueue<Peer> queue;
private boolean uploaded; //determines if queue is used for uploaded or downloaded sort
private final int MAX_UNCHOKED = 10; //max number of unchoked peers = max size of this queue
/**
* Constructor for objects of class PeerPriorityQueue
*/
public PeerReversePriorityQueue(boolean uploaded)
{
if (uploaded) {
comparer = new Comparator<Peer>(){
public int compare (Peer x, Peer y){
RUBTClient.debugPrint(x.toString() + ", uploaded: " + x.uploaded);
RUBTClient.debugPrint(y.toString() + ", uploaded: " + y.uploaded);
if(x.uploaded > y.uploaded) return 1;
if(x.uploaded == y.uploaded) {
double rand = Math.random();
if (rand <= .33) return -1;
else if (rand <= .66) return 0;
return 1;
}
return -1;
}
};
}
else {
comparer = new Comparator<Peer>(){
public int compare (Peer x, Peer y){
RUBTClient.debugPrint(x.toString() + ", downloaded: " + x.downloaded);
RUBTClient.debugPrint(y.toString() + ", downloaded: " + y.downloaded);
if(x.downloaded > y.downloaded) return 1;
if(x.downloaded == y.downloaded) {
double rand = Math.random();
if (rand <= .33) return -1;
else if (rand <= .66) return 0;
return 1;
}
return -1;
}
};
}
queue = new PriorityBlockingQueue<Peer>(MAX_UNCHOKED, comparer);
}
public Comparator<Peer> getComparator(){
return comparer;
}
public PriorityBlockingQueue<Peer> getRarityQueue(){
return queue;
}
public boolean add(Peer peer){
return queue.add(peer);
}
public boolean updatePeerPriority(Peer peer){
if(queue.remove(peer)) return queue.add(peer);
return false;
}
public Peer peek(){
return queue.peek();
}
public void clear(){
queue.clear();
}
public boolean remove(Peer peer){
return queue.remove(peer);
}
public Peer dequeue(){
Peer slowestPeer = null;
try {
slowestPeer = queue.take();
}
catch(InterruptedException e){
//this is fine, do nothing
}
return slowestPeer;
}
}