-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeopleRankWithDecisionEngine.java
More file actions
303 lines (247 loc) · 9.6 KB
/
Copy pathPeopleRankWithDecisionEngine.java
File metadata and controls
303 lines (247 loc) · 9.6 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* People Rank
* by J. P. Denata
*/
package routing;
import core.Connection;
import core.DTNHost;
import core.Message;
import core.Settings;
import core.SimClock;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import routing.community.Duration;
public class PeopleRankWithDecisionEngine implements RoutingDecisionEngine, NodeRanking {
/** identifier for damping factor */
protected static final String DAMPING = "dampingFactor";
/** identifier for threshold */
protected static final String THRESHOLD = "threshold";
protected static final double DAMPING_DEFAULT = 0.5;
protected static final int THRESHOLD_DEFAULT = 3;
protected double dampingFactor = 0.4;
protected double peopleRank;
protected int threshold;
protected Map<DTNHost, Double> startTime;
protected Map<DTNHost, List<Duration>> connectionHistory;
protected Map<DTNHost, Information<Double, Integer>> friends;
public Map<DTNHost, List<Duration>> getConnectionHistory() {
return connectionHistory;
}
public PeopleRankWithDecisionEngine(Settings s) {
if (s.contains(DAMPING)) {
dampingFactor = s.getDouble(DAMPING);
} else {
dampingFactor = DAMPING_DEFAULT;
}
if (s.contains(THRESHOLD)) {
threshold = s.getInt(THRESHOLD);
} else {
threshold = THRESHOLD_DEFAULT;
}
}
public PeopleRankWithDecisionEngine(PeopleRankWithDecisionEngine proto) {
this.peopleRank = 0.0;
this.startTime = new HashMap<>();
this.connectionHistory = new HashMap<>();
this.friends = new HashMap<>();
}
@Override
public void doExchangeForNewConnection(Connection con, DTNHost peer) {
DTNHost myHost = con.getOtherNode(peer);
PeopleRankWithDecisionEngine prde = getDecisionRouterFrom(peer);
// record the start time of the meeting in the map
this.startTime.put(peer, SimClock.getTime());
if (this.friends.containsKey(peer)) {
// send host information to peer
int newSize = getFriendshipSize();
Information<Double, Integer> data = new Information<>(getPeopleRank(), newSize);
send(myHost, peer, data);
// receive info from peer, update the map
data = receive(peer);
this.friends.put(peer, data);
// update people rank for host and peer
prde.updatePeopleRank(peer);
updatePeopleRank(myHost);
}
}
/**
* Method to calculate the total duration of meetings between the host and peer.
* @param peer DTNHost counterpart
* @return total meeting time in seconds
*/
private double calculateTotalMeetingTimeWith(DTNHost peer) {
// if never met, return 0
if (!connectionHistory.containsKey(peer)) {
return 0;
}
double total = 0;
List<Duration> meetingList = connectionHistory.get(peer);
for (Duration duration : meetingList) {
total += (duration.end - duration.start);
}
return total;
}
/**
* Method used to send people rank information and friendship count
* from the host node to the peer node.
*
* @param peer Target DTNHost peer node
* @param information People rank and friendship data from host
*/
private void send(DTNHost myHost, DTNHost peer, Information<Double, Integer> information) {
PeopleRankWithDecisionEngine prde = getDecisionRouterFrom(peer);
// update info on peer
prde.friends.put(myHost, information);
prde.updatePeopleRank(peer);
}
/**
* Method used to receive people rank information and friendship count
* from the peer node.
*
* @param peer DTNHost peer node
*/
private Information receive(DTNHost peer) {
PeopleRankWithDecisionEngine prde = getDecisionRouterFrom(peer);
double peerRank = prde.getPeopleRank();
int peerFriends = prde.friends.size();
return new Information(peerRank, ++peerFriends);
}
/**
* Method used to update people rank value after receiving
* latest information from peer
*
* @param myHost
*/
public void updatePeopleRank(DTNHost myHost) {
double sigma = 0;
//calculate sigma value
for (Map.Entry<DTNHost, Information<Double, Integer>> entry : friends.entrySet()) {
double tempPeopleRank = entry.getValue().getPeopleRank();
int size = entry.getValue().getSize();
if (size == 0) {
sigma += 0;
} else {
sigma += (tempPeopleRank / size);
}
}
// change/update people rank value
this.peopleRank = (1 - dampingFactor) + dampingFactor * sigma;
// update in each friend's map that stores this people rank
for (Map.Entry<DTNHost, Information<Double, Integer>> e : friends.entrySet()) {
PeopleRankWithDecisionEngine de = getDecisionRouterFrom(e.getKey());
de.friends.put(myHost, new Information<>(this.peopleRank, this.friends.size()));
}
}
/**
* Returns the value of PeR(i) from node i
* @return people rank value of the node
*/
@Override
public double getPeopleRank() {
return this.peopleRank;
}
/**
* Returns the value of F(i) from node i
* @return friendship count of the node
*/
public int getFriendshipSize() {
return this.friends.size();
}
@Override
public boolean shouldSendMessageToHost(Message m, DTNHost otherHost, DTNHost thisHost) {
PeopleRankWithDecisionEngine prde = getDecisionRouterFrom(otherHost);
// forward message if destination node equals destination OR PeR(peer) >= PeR(host)
return (prde.getPeopleRank() >= this.peopleRank || m.getTo().equals(otherHost));
}
// convert DTNHost to Router Decision Engine
public PeopleRankWithDecisionEngine getDecisionRouterFrom(DTNHost h) {
MessageRouter otherRouter = h.getRouter();
assert otherRouter instanceof DecisionEngineRouter : "Not a Decision Engine Router";
return (PeopleRankWithDecisionEngine) ((DecisionEngineRouter) otherRouter).getDecisionEngine();
}
@Override
public void connectionDown(DTNHost thisHost, DTNHost peer) {
// check the initial connection time
double start = check(peer);
double end = SimClock.getTime();
/* check or create list for connectionHistory */
List<Duration> history;
// if first time, create a duration list in connectionHistory map
if (!connectionHistory.containsKey(peer)) {
history = new ArrayList<>();
connectionHistory.put(peer, history);
}
// if previously met, get the duration list from the map
else {
history = connectionHistory.get(peer);
}
// add start and end time information to the list
Duration meeting = new Duration(start, end);
history.add(meeting);
// remove temporary meeting time data after completion
startTime.remove(peer);
// check between host-peer. If not friends yet...
if (!this.friends.containsKey(peer)) {
// check total meeting time to compare with the threshold
double totalMeeting = calculateTotalMeetingTimeWith(peer);
//if it meets the threshold
if (totalMeeting >= threshold) {
PeopleRankWithDecisionEngine prde = getDecisionRouterFrom(peer);
int peerSize = prde.getFriendshipSize();
Information<Double, Integer> peerInfo = new Information<>(prde.peopleRank, ++peerSize);
// add peer to the friend's map of the host
this.friends.put(peer, peerInfo);
// add host to the friend's map of the peer
Information<Double, Integer> ourInfo = new Information<>(peopleRank, friends.size());
prde.friends.put(thisHost, ourInfo);
}
}
}
/**
* Method to check if the host and peer have met or not
* @param thisHost Host
* @param peer Peer
* @return start time when host-peer connection is up
*/
private double check(DTNHost peer) {
if (startTime.containsKey(peer)) {
return startTime.get(peer);
}
return 0;
}
public Map<DTNHost, Information<Double, Integer>> getFriends() {
return friends;
}
@Override
public void connectionUp(DTNHost thisHost, DTNHost peer) {
}
@Override
public boolean newMessage(Message m) {
return true;
}
@Override
public boolean isFinalDest(Message m, DTNHost aHost) {
return m.getTo() == aHost;
}
@Override
public boolean shouldSaveReceivedMessage(Message m, DTNHost thisHost) {
return m.getTo() != thisHost;
}
@Override
public boolean shouldDeleteSentMessage(Message m, DTNHost otherHost) {
return m.getTo() == otherHost;
}
@Override
public boolean shouldDeleteOldMessage(Message m, DTNHost hostReportingOld) {
return m.getTo() == hostReportingOld;
}
@Override
public void update(DTNHost thisHost) {
}
@Override
public RoutingDecisionEngine replicate() {
return new PeopleRankWithDecisionEngine(this);
}
}