-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreferredNeighborsHandler.java
195 lines (175 loc) · 7.75 KB
/
PreferredNeighborsHandler.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.util.*;
// This class is used to change preferred neightbors.
public class PreferredNeighborsHandler implements Runnable {
private Map<Integer, NeighborInfo> neighborsMap;
private Map<Integer, NeighborInfo> interestedNeighborMap;
private Map<Integer, NeighborInfo> prefferedNeighbors;
private List<NeighborInfo> downLoadRateList;
private Commoncfg commoncfg;
private Bitfield myBitfield;
private Logger logger;
private int myPeerID;
public PreferredNeighborsHandler(Commoncfg commoncfg, Map<Integer, NeighborInfo> neighborsMap, Bitfield myBitfield, Logger logger, int myPeerID) {
this.myBitfield = myBitfield;
this.neighborsMap = neighborsMap;
this.commoncfg = commoncfg;
interestedNeighborMap = new HashMap<>();
downLoadRateList = new ArrayList<>();
prefferedNeighbors = new HashMap<>();
this.logger = logger;
this.myPeerID = myPeerID;
}
public void run() {
try {
while (true) {
selectPrefferedNeighbors();
if (!prefferedNeighbors.isEmpty()) {
System.out.println("Select preffered neighbors as follows: ");
for (Integer peerID : prefferedNeighbors.keySet()) {
System.out.println("Peer: " + peerID);
}
}
Map<Integer, Integer> prevPieceNumMap = getPieceNum();
Thread.sleep(commoncfg.getUnchoking_Interval() * 1000);
Map<Integer, Integer> nowPieceNumMap = getPieceNum();
calculateAndUpdateDownloadRate(prevPieceNumMap, nowPieceNumMap);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private synchronized void selectPrefferedNeighbors() throws Exception {
if (!neighborsMap.isEmpty()) {
Map<Integer, NeighborInfo> prevPreffered = getPrevPrefferedNeighbors();
int count;
getInteresetedNeighbor();
sortByDownloadRate();
boolean a = false;
if (a) {
System.out.println("To select Preffered Neighbors");
}
if (!prefferedNeighbors.isEmpty()) {
prefferedNeighbors.clear();
}
int numofPreffered = commoncfg.getNum_Of_PreferredNeighbors();
if (numofPreffered <= 0) {
throw new Exception("The number of preferred neighbors can not be less or equal than 0 !");
} else if (numofPreffered >= interestedNeighborMap.size()) {
count = interestedNeighborMap.size();
} else {
count = commoncfg.getNum_Of_PreferredNeighbors();
}
if (myBitfield.isHasCompleteFile() == true) {
Set<Integer> set = new HashSet<Integer>(count);
while (set.size() < count) {
Random random = new Random();
int index = random.nextInt(downLoadRateList.size());
set.add(index);
}
Iterator<Integer> iterator = set.iterator();
while (iterator.hasNext()) {
NeighborInfo newPreffered = downLoadRateList.get(iterator.next());
prefferedNeighbors.put(newPreffered.getPeerID(), newPreffered);
neighborsMap.get(newPreffered.getPeerID()).setPreferred(true);
neighborsMap.get(newPreffered.getPeerID()).setChoked(false);
}
} else {
for (int i = downLoadRateList.size() - 1; i > downLoadRateList.size() - 1 - count; i--) {
NeighborInfo newPreffered = downLoadRateList.get(i);
prefferedNeighbors.put(newPreffered.getPeerID(), newPreffered);
neighborsMap.get(newPreffered.getPeerID()).setPreferred(true);
neighborsMap.get(newPreffered.getPeerID()).setChoked(false);
}
}
if (!prevPreffered.equals(prefferedNeighbors)) {
logger.PrefNeighbours("" + myPeerID, prefferedNeighbors);
}
if (prevPreffered != null) {
for (Integer peerID : prefferedNeighbors.keySet()) {
if (!prevPreffered.containsKey(peerID)) {
Message msg = new UnchokeMessage();
System.out.println("Send an unchoke message");
neighborsMap.get(peerID).getClient().send(msg.getMessageBytes());
}
}
} else {
for (Integer peerID : prefferedNeighbors.keySet()) {
Message msg = new UnchokeMessage();
System.out.println("Send an unchoke message");
neighborsMap.get(peerID).getClient().send(msg.getMessageBytes());
}
}
for (Integer peerID : neighborsMap.keySet()) {
NeighborInfo neighbor = neighborsMap.get(peerID);
if ((neighbor.isOptimisticallyUnchoked() == false && neighbor.isChoked() == false)
&& (neighbor.isPreferred() == false && neighbor.isChoked() == false)) {
Message msg = new ChokeMessage();
System.out.println("Send a choke message");
neighbor.getClient().send(msg.getMessageBytes());
}
}
}
}
private void sortByDownloadRate() {
downLoadRateList.clear();
boolean a = true;
if (!a) {
System.out.println("To sort by download rate");
}
for (Integer peerID : interestedNeighborMap.keySet()) {
downLoadRateList.add(interestedNeighborMap.get(peerID));
}
Collections.sort(downLoadRateList);
}
private void getInteresetedNeighbor() {
interestedNeighborMap.clear();
boolean a = true;
if (!a) {
System.out.println("To get Interested Neighbor");
}
for (Integer peerID : neighborsMap.keySet()) {
NeighborInfo neighbor = neighborsMap.get(peerID);
if (neighbor.isInterest() == true) {
interestedNeighborMap.put(peerID, neighbor);
}
}
}
private Map<Integer, Integer> getPieceNum() {
Map<Integer, Integer> pieceNumMap = new HashMap<>();
boolean a = true;
if (!a) {
System.out.println("To get piece number");
}
for (Integer peerID : neighborsMap.keySet()) {
NeighborInfo neighbor = neighborsMap.get(peerID);
if (neighbor != null) {
pieceNumMap.put(neighbor.getPeerID(), neighbor.getDownloadedPieceNum());
}
}
return pieceNumMap;
}
private void calculateAndUpdateDownloadRate(Map<Integer, Integer> prev, Map<Integer, Integer> now) {
boolean a = true;
if (!a) {
System.out.println("To calculate and update downloadrate");
}
for (Integer peerID : prev.keySet()) {
int pieceNumDiff = now.get(peerID) - prev.get(peerID);
if (pieceNumDiff >= 0) {
double downloadRate = pieceNumDiff * commoncfg.getPieceSize() * 1.0 / commoncfg.getUnchoking_Interval();
neighborsMap.get(peerID).setDownloadRate(downloadRate);
}
}
}
private Map<Integer, NeighborInfo> getPrevPrefferedNeighbors() {
Map<Integer, NeighborInfo> prev = new HashMap<Integer, NeighborInfo>();
boolean a = true;
if (!a) {
System.out.println("To get prevpreffered neighbors");
}
for (Integer peerID : this.prefferedNeighbors.keySet()) {
prev.put(peerID, this.prefferedNeighbors.get(peerID));
}
return prev;
}
}