-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSTable.java
More file actions
200 lines (154 loc) · 6.06 KB
/
LSTable.java
File metadata and controls
200 lines (154 loc) · 6.06 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
import java.util.Date;
import java.util.LinkedList;
public class LSTable {
private int port;
private LinkedList<Integer> nodes;
private LinkedList<LinkState> links; // represents edges in the graph
public LSTable(int port) {
this.port = port;
this.nodes = new LinkedList<>();
this.links = new LinkedList<>();
}
public void sortLinks() {
// Sort the list
this.nodes.sort((node1, node2) -> node1.compareTo(node2));
}
// Updates a links fields given a link with the same ports, returns whether the link was updated
public boolean updateLink(LinkState link) {
for (int i = 0; i < this.links.size(); i++) {
int[] currentPorts = links.get(i).getPorts();
int[] newPorts = link.getPorts();
// linearly search list
if ((currentPorts[0] == newPorts[0] && currentPorts[1] == newPorts[1]) ||
(currentPorts[1] == newPorts[0] && currentPorts[0] == newPorts[1])) {
// check if the link was the same cost
if (links.get(i).getCost() == link.getCost()) {
return false;
} else {
this.links.set(i, link);
return true;
}
}
}
return false;
}
// returns a link if it has identical endpoints (regardless of order) as input, null otherwise
public LinkState getLink(LinkState link) {
for (int i = 0; i < this.links.size(); i++) {
int[] currentPorts = links.get(i).getPorts();
int[] newPorts = link.getPorts();
if ((currentPorts[0] == newPorts[0] && currentPorts[1] == newPorts[1]) ||
(currentPorts[1] == newPorts[0] && currentPorts[0] == newPorts[1])) {
return this.links.get(i);
}
}
return null;
}
public Integer getNode(int node) {
for (int i = 0; i < this.nodes.size(); i++) {
if (node == this.nodes.get(i)) {
return this.nodes.get(i);
}
}
return null;
}
// adds link and node to both, returns whether not anything new was added
public boolean addLink(LinkState link) {
if (this.getLink(link) == null) {
this.links.add(link);
int[] ports = link.getPorts();
if (this.getNode(ports[0]) == null) {
this.nodes.add(ports[0]);
}
if (this.getNode(ports[1]) == null) {
this.nodes.add(ports[1]);
}
return true;
}
return false;
}
// the logic of LSA
public LSPathTable djikstra() {
LSPathTable output = new LSPathTable(this.port);
LinkedList<Integer> queue = new LinkedList<>();
LinkedList<Integer> processed = new LinkedList<>();
queue.offer(this.port);
int currentNode;
while (queue.size() > 0) {
currentNode = queue.poll();
// don't process if processed already
boolean done = false;
for (int num : processed) {
if (num == currentNode) {
done = true;
}
}
if (done) {
continue;
}
processed.add(currentNode);
LinkedList<LinkState> currentEdges = getEdgesWithNode(currentNode, this.links);
currentEdges.sort((link1, link2) -> link1.getCost() - link2.getCost());
for (LinkState edge : currentEdges) {
int dest = edge.getPorts()[0] == currentNode ? edge.getPorts()[1] : edge.getPorts()[0]; // destination node for this edge
// make sure edge doesn't point to source
if (dest == this.port) {
continue;
}
// add dest to queue if it hasn't been processed yet
done = false;
for (int num : processed) {
if (num == dest) {
done = true;
}
}
if (!done)
{
queue.add(dest);
}
int currentCost = output.getCost(dest);
int newCost = output.getCost(currentNode) + edge.getCost();
if (newCost < currentCost) {
output.setCost(dest, newCost);
LinkedList<Integer> newPath = new LinkedList<>(output.getPath(currentNode));
newPath.add(dest);
output.setPath(dest, newPath);
}
}
}
return output;
}
// helper method that returns edges with a node given the node and a list of edges
public static LinkedList<LinkState> getEdgesWithNode(int node, LinkedList<LinkState> edges) {
LinkedList<LinkState> output = new LinkedList<>();
for (LinkState edge : edges) {
int[] ports = edge.getPorts();
if (node == ports[0] || node == ports[1]) {
output.add(edge);
}
}
return output;
}
public LinkedList<LinkState> getLinks() {
return this.links;
}
public static void main(String[] args) {
//test
LSTable table = new LSTable(1);
table.addLink(new LinkState(1, 2, 1));
table.addLink(new LinkState(1, 3, 50));
table.addLink(new LinkState(2, 3, 2));
table.addLink(new LinkState(2, 4, 8));
table.addLink(new LinkState(3, 4, 5));
System.out.println(table.djikstra());
}
@Override
public String toString() {
Date date = new Date();
String output = "[" + date.getTime() + "] Node " + this.port + " Network Topology\n";
for (LinkState link : this.links) {
output += "- (" + link.getCost() + ") from Node " + link.getPorts()[0] + " to Node " + link.getPorts()[1] + "\n";
}
return output;
}
}