-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStateRouting.java
More file actions
401 lines (331 loc) · 14.1 KB
/
LinkedStateRouting.java
File metadata and controls
401 lines (331 loc) · 14.1 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
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class LinkedStateRouting {
private HashMap<Integer, Integer> nodeToIndex;
private HashMap<Integer, Integer> indexToNode;
private LinkedHashMap<Integer, Integer> multicastSession;
private int[][] graph;
private int startNode;
private int nodeCount;
// crate graph representation of input file using adjacency matrix
public void parseFile(Scanner input) {
nodeToIndex = new HashMap<Integer, Integer>();
indexToNode = new HashMap<Integer, Integer>();
int multicastTimeout = 0;
int multicastStartNode = 0; // the node from event A
graph = new int[10000][10000]; // MAKE DYNAMIC
nodeCount = 0;
// int prevSeqNo = -1; // keep track of previous sequence number
while (input.hasNextLine()) {
String line = input.nextLine(); // read a line
if (!line.startsWith("#") && !line.isEmpty()) { // ignore if the line contains # or is blank
String modified = line.replaceAll("[<,>]", " "); // get all '<', ',' and '>' with " "
Scanner temp = new Scanner(modified);
int timeStamp = temp.nextInt(); // read the timestamp
// System.out.println("Timestamp: " + timeStamp);
String event = temp.next(); // read the event type - I, L or F
// System.out.println("Event: " + event);
if (event.equals("I")) {
startNode = temp.nextInt();
checkNode(startNode);
int processorNode = startNode;
// System.out.println("starting node: " + startNode);
nodeToIndex.put(startNode, nodeCount); // starting node always associated with index 0
indexToNode.put(nodeCount, startNode);
graph[0][0] = 0; // distance of starting node to itself is 0
// read in rest of <node, weight>
while (temp.hasNext()) {
int node = temp.nextInt();
int weight = temp.nextInt();
// System.out.println("<" + node + ", " + weight + ">");
constructGraph(processorNode, node, weight);
}
} else if (event.equals("L")) {
// System.out.println("*** type L ***");
int processorNode = temp.nextInt();
checkNode(startNode);
// System.out.println("processor node: " + processorNode);
int currSeqNo = temp.nextInt(); // LEAVE UNCOMMENTED!!!
// System.out.println("Sequence number: " + currSeqNo);
// if sequence number of LSP is greater to previous sequence number
// if (currSeqNo > prevSeqNo) {
// read in rest of <node, weight>
while (temp.hasNext()) {
int node = temp.nextInt();
checkNode(startNode);
int weight = temp.nextInt();
// System.out.println("<" + node + ", " + weight + ">");
constructGraph(processorNode, node, weight);
}
// discard if LSP has smaller or equal sequence number
// to max seen so far
// } else {
// System.out.println("LSP has smaller or equal sequence
// number to max seen so far - discarding LSP");
// }
// prevSeqNo = currSeqNo; // update previous sequence number
} else if (event.equals("A")) {
// System.out.println("*** type A ***");
int node = temp.nextInt();
multicastStartNode = node;
int sessionAddress = temp.nextInt();
checkMultiAddress(sessionAddress); // check session address
multicastTimeout = temp.nextInt();
// System.out.println( "node " + node + " initilized new multicast session with address " + sessionAddress);
multicastSession = new LinkedHashMap<Integer, Integer>();
multicastSession.put(node, sessionAddress);
} else if (event.equals("J")) {
// System.out.println("*** type J ***");
int node = temp.nextInt();
int sessionAddress = temp.nextInt();
checkMultiAddress(sessionAddress); // check session address
// check if the session exist - if not then drop the packet
if (multicastSession.containsValue(sessionAddress)) {
multicastSession.put(node, sessionAddress);
// System.out.println(node + " added to session " + sessionAddress);
} else {
System.out.println("session that " + node + " is requsting to join does not exist - dropping packet");
dropMulticastPacket(multicastTimeout, sessionAddress, node);
}
} else if (event.equals("F")) {
int node = temp.nextInt();
int nodeID = nodeToIndex.get(node); // get ID associated with node
// F belonging to unicast is followed by one int after the
// "F" - multicast is followed by two ints
// e.g., 35 F 4109 vs 67 F 1203 5664
if (!temp.hasNextInt()) {
// System.out.println("*** F is of type unicast ***");
int source = nodeToIndex.get(startNode); // source should always be 0 since it is the starting node
// System.out.println("source: " + startNode + "(id#" + source + "), destination: " + node + "(id#" + nodeID + ")");
ArrayList<Integer> shortestPath = dijkstra(graph, source, nodeID);
// print out the shortest path for testing
// System.out.println("shortest path to " + node + ":");
// for (int i = 0; i < shortestPath.size(); i++)
// System.out.print(shortestPath.get(i) + " ");
// System.out.println();
// for (int i = 0; i < shortestPath.size(); i++) {
// int nextHop = shortestPath.get(i);
// forwardUnicastPacket(timeStamp, nodeID, nextHop);
// }
int nextHop = shortestPath.get(0);
forwardUnicastPacket(timeStamp, nodeID, nextHop);
} else {
// System.out.println("*** F is of type multicast ***");
int multicastGroup = temp.nextInt();
// System.out.println("node: " + node + "(id#" + nodeID + "), multicase group: " + multicastGroup);
// drop packet if multicast session does not exist
if(!multicastSession.containsValue(multicastGroup)) {
// System.out.println("session does not exist..dropping packet");
dropMulticastPacket(timeStamp, multicastGroup, node);
} else {
// iterate through all the nodes (keys) in map and
// compute shortest path back to sender
// (multicastStartNode)
ArrayList<Integer> listOfNextHops = new ArrayList<Integer>(); // to store list of next hops
// print multicast session for testing
// for (Integer key : multicastSession.keySet()) {
// System.out.println(key + ":" + multicastSession.get(key));
// }
boolean hasPath = false;
for (Integer key : multicastSession.keySet()) {
if (key != node && key != multicastStartNode) { // to avoid comparing node to itself and starting node to itself
// System.out.println("getting shortest path from " + node + " to " + key);
ArrayList<Integer> shortestPath = dijkstra(graph, nodeToIndex.get(key), nodeToIndex.get(node)); // shortest path from endpoint to sender
// if shortest path goes through the router
// (start node), add first hop towards that
// endpoint to list of hopes through which the
// packet is forwarded
if (shortestPath.contains(nodeToIndex.get(startNode))) {
// System.out.println("shortest path for node " + key + " DOES goe through router (" + startNode + ")");
hasPath = true;
// print out path
// for (Integer i : shortestPath)
// System.out.print(i + " ");
// System.out.println();
int indexOfStartNode = shortestPath.indexOf(nodeToIndex.get(startNode));
if(indexOfStartNode - 1 == -1) {
if(!listOfNextHops.contains(key)) {
listOfNextHops.add(key);
// System.out.println(key + " added to hops");
}
} else {
if(!listOfNextHops.contains(indexToNode.get(shortestPath.get(indexOfStartNode - 1)))) {
listOfNextHops.add(indexToNode.get(shortestPath.get(indexOfStartNode - 1)));
// System.out.println(indexToNode.get(shortestPath.get(indexOfStartNode - 1)) + " added to hops");
}
}
}
}
}
// drop packet if router lies on none of shortest paths back to source
if(!hasPath) {
// System.out.println("doesn't have path - dropping packet");
dropMulticastPacket(timeStamp, multicastGroup, node);
}
forwardMulticastPacket(timeStamp, multicastGroup, listOfNextHops, listOfNextHops.size());
}
}
// else if event.equals("Q")
} else {
// System.out.println("*** type Q ***");
int node = temp.nextInt();
int sessionAddress = temp.nextInt();
if (multicastSession.size() != 0) {
multicastSession.remove(node); // remove node from
// HashMap
// System.out.println(node + " has left the session(" + sessionAddress + ")");
// if session is of size 0
}
}
}
}
input.close(); // close Scanner
// printGraph();
}
private void constructGraph(int processorNode, int node, int weight) {
// System.out.println("[" + processorNode + ", " + node + ", " + weight
// + "]");
// either add processor node to map or get index of it if not contained
// in map
int processorIndex = 0;
if (!nodeToIndex.containsKey(processorNode)) {
// System.out.println("graph DOES NOT contain processor node");
nodeCount++;
processorIndex = nodeCount; // update index of processor node
nodeToIndex.put(processorNode, nodeCount); // add node and index to
// map e.g., node 4325
// (first node read) ->
// 0
indexToNode.put(nodeCount, processorNode);
// System.out.println("processor node " + processorNode + " -> " + nodeCount + " added to map");
// else get the ID assoicated with the node
} else {
// System.out.println("graph DOES contain processor node");
processorIndex = nodeToIndex.get(processorNode);
// System.out.println("retrieved processor node " + processorNode + " -> " + processorIndex);
}
// either add non-processor node to map or get index of it if not
// contained in map
int nodeIndex = 0;
if (!nodeToIndex.containsKey(node)) {
// System.out.println("graph DOES NOT contain node");
nodeCount++;
nodeIndex = nodeCount;
nodeToIndex.put(node, nodeCount); // add node and index to map e.g.,
// node 4325 (first node read)
// -> 0
indexToNode.put(nodeCount, node);
// System.out.println(node + " -> " + nodeCount + " added to map");
// else get the ID associated with the node
} else {
// System.out.println("graph DOES contain node");
nodeIndex = nodeToIndex.get(node);
// System.out.println("retrieved " + node + " -> " + nodeIndex);
}
// update graph
graph[processorIndex][nodeIndex] = weight;
graph[nodeIndex][processorIndex] = weight; // mirror graph diagonally
// from top-left to
// bottom-right
// System.out.println("graph at " + "(" + processorIndex + ", " + nodeIndex + ") " + " is " + weight);
// System.out.println("graph at " + "(" + nodeIndex + ", " + processorIndex + ") " + " is " + weight);
}
public ArrayList<Integer> dijkstra(int graph[][], int src, int dest) {
int V = graph.length;
int dist[] = new int[V]; // vertices included in SPT
boolean visited[] = new boolean[V]; // vertices not yet included in SPT
int pathToDest[] = new int[V]; // shortest path from source to destination
for (int i = 0; i < V; i++) {
dist[i] = Integer.MAX_VALUE;
pathToDest[src] = -1;
}
dist[src] = 0;
for (int i = 0; i < V - 1; i++) {
// find the min distance
int min = Integer.MAX_VALUE;
int minDist = 0;
for (int v = 0; v < V; v++) {
if (visited[v] == false && dist[v] <= min) {
min = dist[v];
minDist = v;
}
}
if(minDist == dest)
break;
// System.out.println("min distance: " + minDist);
visited[minDist] = true;
// System.out.println(minDist + " is marked as TRUE");
for (int j = 0; j < V; j++) {
if (!visited[j] && graph[minDist][j] != 0 && dist[minDist] + graph[minDist][j] < dist[j]) {
int totalDist = dist[minDist] + graph[minDist][j];
dist[j] = totalDist;
// System.out.println("new distance from " + minDist + " to " + j + " is now " + totalDist);
pathToDest[j] = minDist;
// System.out.println(minDist + " added to path at index " + j);
}
}
}
ArrayList<Integer> shortestPath = new ArrayList<Integer>();
createPath(pathToDest, shortestPath, dest);
return shortestPath;
}
private void createPath(int[] path, ArrayList<Integer> shortestPath, int j) {
if(path[j] == -1)
return;
createPath(path, shortestPath, path[j]);
// System.out.print(" " + j);
shortestPath.add(j);
}
public void forwardUnicastPacket(int timeNow, int destination, int nextHop) {
System.out.println("FU " + timeNow + " " + indexToNode.get(destination) + " " + indexToNode.get(nextHop));
}
public void forwardMulticastPacket(int timeNow, int destination, ArrayList<Integer> nextHops, int listLen) {
// reorder hops from least to greatest
Collections.sort(nextHops);
for(int i = 0; i < listLen; i++) {
System.out.println("FMP " + timeNow + " " + destination + " " + nextHops.get(i));
}
}
public void dropMulticastPacket(int timeNow, int destination, int source) {
System.out.println("DMP " + timeNow + " " + destination + " " + source);
}
// print graph
public void printGraph() {
for (int r = 0; r < graph.length; r++) {
for (int c = 0; c < graph[r].length; c++)
System.out.print(graph[r][c] + " ");
System.out.println();
}
}
// check if a node is valid
private void checkNode(int node) {
if (node > 0 && node < 32767)
return;
else
throw new IllegalArgumentException();
}
// check if a multicast address is in range
private void checkMultiAddress(int address) {
if (address > 32768 && address < 65536)
return;
else
throw new IllegalArgumentException();
}
public static void main(String[] args) {
LinkedStateRouting lsr = new LinkedStateRouting();
// read in file
try {
File file = new File(args[0]);
// File file = new File("/Users/jasonvl/Desktop/CompSci/CSCI280/Project2/src/multicast_trace.txt");
//File file = new File("/Users/jasonvl/Desktop/CompSci/CSCI280/Project2/src/bigger_trace_3.txt");
Scanner input = new Scanner(file);
lsr.parseFile(input);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}