-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
632 lines (489 loc) · 21.5 KB
/
Node.java
File metadata and controls
632 lines (489 loc) · 21.5 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class Node {
private static final String ipDirectory = "inputs/", opDirectory = "outputs/", dataDirectory = "data/";
private int ID, duration, destID;
private String message;
File input, output, dataReceived; // file pointer for node files
private int[] neighbors; // list of existing neighbors
private HashMap<Integer, Integer> inTree; // stores latest intree for the node
private HashMap<Integer, ArrayList<Integer>> revInTree; // intree in reversed form, used for intree computation
private ArrayList<HashMap<Integer, Integer>> neighborIntreeList; // stores intrees of neighbors, used when transmitting data
Node(int ID, int duration, int destID, String message) {
this.ID = ID;
this.duration = duration;
this.destID = destID;
this.message = message;
// -1 indicates neighbors dont exist
neighbors = new int[10];
for (int i = 0; i < 10; i++) neighbors[i] = -1;
inTree = new HashMap<>();
revInTree = new HashMap<>();
neighborIntreeList = new ArrayList<>(10);
for (int i = 0; i < 10; i++) neighborIntreeList.add(null);
setupFiles();
}
// intializes all the node files
private void setupFiles() {
try {
input = new File(ipDirectory+"input_"+ID+".txt");
output = new File(opDirectory+"output_"+ID+".txt");
dataReceived = new File(dataDirectory+ID+"_received"+".txt");
if (!input.exists()) input.createNewFile();
if (!output.exists()) output.createNewFile();
if (!dataReceived.exists()) dataReceived.createNewFile();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// method to sent hello message
private void sendHello(BufferedWriter writer) {
try {
String msg = "hello "+ID;
writer.write(msg);
writer.write(System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}
}
// method to send intree message
private void sendRouting(BufferedWriter writer) {
// convert intree to string
String tree = "";
for (Map.Entry<Integer, Integer> entry : inTree.entrySet()) {
tree += " "+"("+entry.getKey()+"-"+entry.getValue()+")";
}
try {
String msg = "intree "+ID+tree;
writer.write(msg);
writer.write(System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}
}
// method to compute path for the source node
private String computePath() {
// Algo steps
// 1. Check node intree to find a path from dest to node and reverse it
// 2. As per the path, check the first node (say X) and find path to reach there
// 3. Using X intree, get path from node to X (say P). This path doesn't change.
// 4. Send message to first node in this path P
String dataMsg = "data "+ID+" ";
// check intree to see if dest exists
if (!inTree.containsKey(destID)) return null;
// if dest exists find first node in path
// also store this path in list
ArrayList<Integer> path = new ArrayList<>();
int node = destID;
while (node != ID) {
path.add(node);
node = inTree.get(node);
}
// check if nodes intrees is obtained
int firstNeigh = path.get(path.size()-1);
if (neighborIntreeList.get(firstNeigh) == null) return null;
HashMap<Integer, Integer> nodeInTree = neighborIntreeList.get(firstNeigh);
// check if you exist in this node intree
if (!nodeInTree.containsKey(ID)) return null;
// add src routing path
dataMsg += "(";
Integer node1 = nodeInTree.get(ID);
while (node1 != null) {
dataMsg += node1+" ";
node1 = nodeInTree.get(node1);
}
dataMsg = dataMsg.substring(0, dataMsg.length()-1);
dataMsg += ")";
// add path to data message
path.remove(path.size()-1);
while (!path.isEmpty()) {
dataMsg += " " + path.remove(path.size()-1);
}
dataMsg += " begin "+message;
return dataMsg;
}
// method to send data message
private void sendData(BufferedWriter writer) {
String dataMsg = computePath();
if (dataMsg == null) return;
try {
writer.write(dataMsg);
writer.write(System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}
}
// on receiving hello message, counter reset to 0
private void processHello(String msg) {
String neighID = msg.split(" ")[1];
neighbors[Integer.parseInt(neighID)] = 0;
}
// method to convert received intree message to intree data structure
private void buildInTree(HashMap<Integer, Integer> tree, String[] data) {
for (int d = 0; d < data.length; d++) {
String[] edge = data[d].split("-");
int src = Integer.parseInt(edge[0]);
int dest = Integer.parseInt(edge[1]);
tree.put(src, dest);
}
}
// helper method which reverses the intree
private void buildRevInTree(HashMap<Integer, ArrayList<Integer>> revInTree, HashMap<Integer, Integer> inTree) {
for (Map.Entry<Integer, Integer> entry : inTree.entrySet()) {
int src = entry.getKey();
int dest = entry.getValue();
if (!revInTree.containsKey(dest)) revInTree.put(dest, new ArrayList<Integer>());
ArrayList<Integer> l = revInTree.get(dest);
l.add(src);
revInTree.put(dest, l);
}
}
// helper method which computes hop count of all nodes in tree
private void computeHops(ArrayList<Integer> hops, HashMap<Integer, ArrayList<Integer>> revInTree, int node, int cnt, int hopVal) {
if (cnt == hopVal) {
hops.add(node);
return;
}
ArrayList<Integer> neighbors = revInTree.get(node);
if (neighbors == null) return;
for (Integer neigh : neighbors) {
computeHops(hops, revInTree, neigh, cnt+1, hopVal);
}
}
// main method which computes intree for node
private void computeInTree(String msg) {
// build neighbor intree
// msg format -> intree 4 (1-4) (3-4) (5-3) (2-1)
HashMap<Integer, Integer> neighborInTree = new HashMap<>();
HashMap<Integer, ArrayList<Integer>> revNeighborInTree = new HashMap<>();
String msg1 = msg.replace("(", "").replace(")", "").replace("intree ", "");
// if neighbor intree is the node itself, then add the edge
if (msg1.length() == 1) {
int neigh = Integer.parseInt(msg1);
inTree.put(neigh, ID);
revInTree = new HashMap<>();
buildRevInTree(revInTree, inTree);
return;
}
int neighbor = Integer.parseInt(msg1.split(" ")[0]);
String[] data = msg1.substring(2).split(" ");
buildInTree(neighborInTree, data); // convert data message into data structure
// add this neighbor intree to our neighbor intrees list
HashMap<Integer, Integer> neighTree = new HashMap<>();
for (Map.Entry<Integer, Integer> entry : neighborInTree.entrySet()) {
neighTree.put(entry.getKey(), entry.getValue());
}
neighborIntreeList.set(neighbor, neighTree);
// modify neighbor in-tree
if (neighborInTree.containsKey(ID)) neighborInTree.remove(ID);
neighborInTree.put(neighbor, ID);
// build reverse intree by reversing the edges
buildRevInTree(revNeighborInTree, neighborInTree);
int hopCnt = 1;
HashMap<Integer, Integer> newInTree = new HashMap<>();
// store old intree
HashMap<Integer, Integer> oldInTree = new HashMap<>();
for (Map.Entry<Integer, Integer> entry : inTree.entrySet()) {
oldInTree.put(entry.getKey(), entry.getValue());
}
// process all neighbors
while (true) {
ArrayList<Integer> l1 = new ArrayList<>();
ArrayList<Integer> l2 = new ArrayList<>();
// get all n neighbors
computeHops(l1, revInTree, ID, 0, hopCnt);
computeHops(l2, revNeighborInTree, ID, 0, hopCnt);
if (l1.isEmpty() && l2.isEmpty()) break;
// sort the lists
Collections.sort(l1);
Collections.sort(l2);
int c1 = 0, c2 = 0;
while (c1 < l1.size() && c2 < l2.size()) {
int v1 = l1.get(c1), v2 = l2.get(c2);
if (v1 < v2) {
// remove edge from neighbor tree and add in final tree
neighborInTree.remove(v1);
if (l2.contains(v1)) l2.remove(Integer.valueOf(v1));
newInTree.put(v1, inTree.get(v1));
c1++;
} else if (v1 > v2) {
// remove edge from intree and add in final tree
inTree.remove(v2);
if (l1.contains(v2)) l1.remove(Integer.valueOf(v2));
newInTree.put(v2, neighborInTree.get(v2));
c2++;
} else {
int temp1 = v1, temp2 = v2;
// if both node values same, break tie by checking parents
while (temp1 == temp2) {
if (inTree.containsKey(temp1)) temp1 = inTree.get(temp1);
if (neighborInTree.containsKey(temp2)) temp2 = neighborInTree.get(temp2);
// if reached root, just add the edge from intree
if (temp1 == ID && temp2 == ID) {
newInTree.put(v1, inTree.get(v1));
c1++; c2++;
break;
}
}
if (temp1 < temp2) {
// remove edge from neighbor tree and add in final tree
if (neighborInTree.containsKey(v1)) neighborInTree.remove(v1);
if (l2.contains(v1)) l2.remove(Integer.valueOf(v1));
newInTree.put(v1, inTree.get(v1));
c1++;
} else if (temp1 > temp2) {
// remove edge from intree and add in final tree
if (inTree.containsKey(v2)) inTree.remove(v2);
if (l1.contains(v2)) l1.remove(Integer.valueOf(v2));
newInTree.put(v2, neighborInTree.get(v2));
c2++;
}
}
}
// if all n hop nodes are processed of neighbor tree
while (c1 < l1.size()) {
int v1 = l1.get(c1);
if (neighborInTree.containsKey(v1)) neighborInTree.remove(v1);
if (l2.contains(v1)) l2.remove(Integer.valueOf(v1));
newInTree.put(v1, inTree.get(v1));
c1++;
}
// if all n hop nodes are processed of intree
while (c2 < l2.size()) {
int v2 = l2.get(c2);
if (inTree.containsKey(v2)) inTree.remove(v2);
if (l1.contains(v2)) l1.remove(Integer.valueOf(v2));
newInTree.put(v2, neighborInTree.get(v2));
c2++;
}
// recompute the reversed intree to compute hops
revInTree = new HashMap<>();
revNeighborInTree = new HashMap<>();
buildRevInTree(revInTree, inTree);
buildRevInTree(revNeighborInTree, neighborInTree);
hopCnt++;
}
inTree = newInTree; // update current intree to the new intree
revInTree = new HashMap<>();
buildRevInTree(revInTree, inTree); // built reversed intree
// check if intree changed, if yes send routing message
if (!oldInTree.equals(newInTree)) {
try {
BufferedWriter wBufferedWriter = new BufferedWriter(new FileWriter(output, true));
sendRouting(wBufferedWriter);
wBufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
// main method to update neighbors if anyone stopped working
private void processNeighbors() {
// increment counter of all neighbors
for (int i = 0; i < 10; i++) {
if (neighbors[i] != -1) neighbors[i]++;
// if hello message not received for 30 secs, node has stopped
if (neighbors[i] >= 30) {
// remove neighbors from lists
neighbors[i] = -1;
neighborIntreeList.set(i, null);
// update intree by removing neighbor
Iterator<Entry<Integer, Integer>> treeItr = inTree.entrySet().iterator();
while (treeItr.hasNext()) {
Map.Entry<Integer, Integer> elem = treeItr.next();
if ((int)elem.getKey() == i || (int)elem.getValue() == i) {
treeItr.remove();
}
}
// send the updated intree
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(output, true));
sendRouting(writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
};
}
}
// main method which process received data message
private void processData(String msg) {
// process received message
int src = Integer.parseInt(msg.substring(5, 6));
// get src routing path
int start, end;
start = msg.indexOf("(");
end = msg.indexOf(")");
String route = msg.substring(start+1, end);
// get remaining nodes
start = msg.indexOf(") ");
end = msg.indexOf("begin");
String nodes = msg.substring(start+2, end);
// msg string
start = msg.indexOf("begin");
String message = msg.substring(start);
// process the data message
// 1. If node part of src routing, then directly send the message
// 2. Else process again by getting intrees and routing
int nId = Integer.parseInt(route.substring(0, 1));
if (nId == ID) {
if (route.length() == 1) {
if (nodes.length() == 0) {
// this is the intended dest. write message in received file
String receivedMessage = "message from "+src+": "+message.substring(6);
try {
BufferedWriter dataWriter = new BufferedWriter(new FileWriter(dataReceived, true));
dataWriter.write(receivedMessage);
dataWriter.write(System.lineSeparator());
dataWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return;
} else {
// get new dest
int currDest = Integer.parseInt(nodes.substring(0, 1));
nodes = nodes.substring(2);
// check intree to see if dest exists
if (!inTree.containsKey(currDest)) return;
// if dest exists find first node in path
// also store this path in list
ArrayList<Integer> path = new ArrayList<>();
int node = inTree.get(currDest);
while (node != ID) {
path.add(node);
node = inTree.get(node);
}
// check if nodes intrees is obtained
HashMap<Integer, Integer> nodeInTree = null;
if (node == ID) {
node = currDest;
nodeInTree = neighborIntreeList.get(currDest);
} else if (node != ID) {
nodeInTree = neighborIntreeList.get(node);
}
if (nodeInTree == null) return;
// check if you exist in this node intree
if (!nodeInTree.containsKey(ID)) return;
// add src routing path
String newRoute = "";
newRoute += "(";
int node1 = nodeInTree.get(ID);
newRoute += node1;
while (node1 != node) {
node1 = nodeInTree.get(node1);
newRoute += " "+node1;
}
newRoute += ")";
// add remaining nodes
String newNodes = "";
while (!path.isEmpty()) {
newNodes += " " + path.remove(path.size()-1);
}
// send the updated data message
String finalMsg = "data "+src+" "+newRoute+newNodes+" "+nodes+message;
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(output, true));
writer.write(finalMsg);
writer.write(System.lineSeparator());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
// source routing, directly send the data
route = route.substring(2);
String newMsg = "data "+src+" ("+route+") "+nodes+message;
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(output, true));
writer.write(newMsg);
writer.write(System.lineSeparator());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
// main method to read and process the node input file
private long readInput(long lastLine) {
try {
if (lastLine < input.length()) {
BufferedReader reader = new BufferedReader(new FileReader(input));
reader.skip(lastLine);
String line = null;
while ((line = reader.readLine()) != null) {
String type = line.split(" ")[0];
switch(type) {
// hello message
case "hello":
processHello(line);
break;
// intree routing message
case "intree":
computeInTree(line);
break;
// main data message
case "data":
processData(line);
break;
}
}
reader.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return input.length();
}
// main method which starts the node and runs for the given duration
private void startNode() {
Object execution = new Object();
try {
synchronized(execution) {
long lastLine = 0; // keep track of the last line read from input file
for (int time = 0; time < duration; time++) {
BufferedWriter writer = new BufferedWriter(new FileWriter(output, true));
if (time%5 == 0) sendHello(writer);
if (time%10 == 0) sendRouting(writer);
if (time%15 == 0) if (destID != -1) sendData(writer);
lastLine = readInput(lastLine);
processNeighbors();
writer.close();
System.out.println("Node "+ID+" running");
execution.wait(1000);
}
}
} catch(InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int id = Integer.parseInt(args[0]);
int time = Integer.parseInt(args[1]);
int dest = Integer.parseInt(args[2]);
String msg = (dest != -1) ? args[3] : null;
Node node = new Node(id, time, dest, msg);
node.startNode();
}
}