-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutenode.java
148 lines (121 loc) · 5.01 KB
/
routenode.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
import java.util.LinkedList;
public class routenode {
public static void main(String[] args) {
// Needs at least six args
if (args.length < 6) {
System.out.println("Usage: routenode dv <r/p> <update-interval> <local-port> <neighbor1-port> <cost-1> <neighbor2-port> <cost-2> ... [last] [<cost-change>]");
System.exit(0);
}
if (args[0].equals("dv")) {
int port = Integer.parseInt(args[3]);
boolean last = false;
boolean trigger = false;
int triggerCost = 0;
// Find number of neighbors in args
int numNeighbors = 0;
for (int i = 4; i < args.length; i += 2) {
if (args[i].equals("last")) {
last = true;
// check if triggered cost change
if (i+1 < args.length) {
trigger = true;
triggerCost = Integer.parseInt(args[i+1]);
}
break;
}
numNeighbors++;
}
// Generate arrays of neighbors and distance vectors
int[] neighbors = new int[numNeighbors];
DistanceVector[] initialVectors = new DistanceVector[numNeighbors];
for (int i = 0; i < numNeighbors; i++) {
neighbors[i] = Integer.parseInt(args[i*2 + 4]);
int cost = Integer.parseInt(args[i*2 + 5]);
initialVectors[i] = new DistanceVector(neighbors[i], cost, neighbors[i]);
}
DVNode node = null;
// Create node and initialize table
if (args[1].equals("r")) {
node = new DVNode(port, neighbors, initialVectors);
} else if (args[1].equals("p")) {
node = new DVNode(port, neighbors, initialVectors, true);
} else {
System.out.println("bruh");
System.exit(1);
}
System.out.println(node.getTable());
if (last) {
node.broadcastTable();
if (trigger) {
// loop for thirty seconds and trigger cost change
long startTime = System.currentTimeMillis();
long currentTime = System.currentTimeMillis();
while (currentTime - startTime < 30 * 1000) {
node.receive();
currentTime = System.currentTimeMillis();
}
// enter final loop
node.linkCostChange(triggerCost);
while (true) {
node.receive();
}
}
}
// Start main loop
while (true) {
node.receive();
}
} else if (args[0].equals("ls")) {
int port = Integer.parseInt(args[3]);
boolean last = false;
boolean trigger = false;
int triggerCost = 0;
// Find number of neighbors in args
int numNeighbors = 0;
for (int i = 4; i < args.length; i += 2) {
if (args[i].equals("last")) {
last = true;
// check if triggered cost change
if (i+1 < args.length) {
trigger = true;
triggerCost = Integer.parseInt(args[i+1]);
}
break;
}
numNeighbors++;
}
// Generate arrays of neighbors and links
int[] neighbors = new int[numNeighbors];
LinkedList<LinkState> links = new LinkedList<>();
for (int i = 0; i < numNeighbors; i++) {
neighbors[i] = Integer.parseInt(args[i*2 + 4]);
int cost = Integer.parseInt(args[i*2 + 5]);
links.add(new LinkState(port, neighbors[i], cost));
}
// create link node object
int UPDATE_INTERVAL = Integer.parseInt(args[2]) * 1000 + (int) (Math.random() * 1000);
LSNode node = new LSNode(port, links, UPDATE_INTERVAL);
// if last broadcast table
if (last) {
node.broadcastTable();
if (trigger) {
// loop for thirty seconds and trigger cost change
long startTime = System.currentTimeMillis();
long currentTime = System.currentTimeMillis();
while (currentTime - startTime < 30 * 1000) {
node.receive();
currentTime = System.currentTimeMillis();
}
// enter final loop
node.linkCostChange(triggerCost);
while (true) {
node.receive();
}
}
}
while (true) {
node.receive();
}
}
}
}