-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSPathTable.java
More file actions
78 lines (58 loc) · 1.88 KB
/
LSPathTable.java
File metadata and controls
78 lines (58 loc) · 1.88 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
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
public class LSPathTable {
int source;
LinkedList<Integer> nodes;
HashMap<Integer, LinkedList<Integer>> paths;
HashMap<Integer, Integer> costs;
public LSPathTable(int source) {
this.source = source;
this.paths = new HashMap<>();
this.costs = new HashMap<>();
this.nodes = new LinkedList<>();
this.costs.put(source, 0);
this.paths.put(source, new LinkedList<>());
}
public void initPath(int dest) {
this.paths.put(dest, new LinkedList<>());
}
public void setCost(int dest, int cost) {
this.costs.put(dest, cost);
}
public int getCost(int dest) {
Integer cost = this.costs.get(dest);
if (cost == null) {
this.setCost(dest, Integer.MAX_VALUE);
this.nodes.add(dest);
}
return this.costs.get(dest);
}
public LinkedList<Integer> getPath(int dest) {
LinkedList<Integer> path = this.paths.get(dest);
if (path == null) {
this.initPath(dest);
}
return this.paths.get(dest);
}
public void setPath(int dest, LinkedList<Integer> path) {
this.paths.put(dest, path);
}
@Override
public String toString() {
Date date = new Date();
String output = "[" + date.getTime() + "] Node " + this.source + " Routing Table\n";
// sort nodes
Collections.sort(this.nodes);
for (int node : this.nodes) {
output += "- (" + this.costs.get(node) + ") -> Node " + node + "; ";
// check if next hop
if (this.getPath(node).size() > 1) {
output += "Next hop -> Node " + this.getPath(node).get(0);
}
output += "\n";
}
return output;
}
}