-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistanceVector.java
More file actions
128 lines (104 loc) · 4.07 KB
/
Copy pathDistanceVector.java
File metadata and controls
128 lines (104 loc) · 4.07 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
/*
* Distance Vector class
* nrh51 -- csds325
*/
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class DistanceVector implements Serializable {
private static final int INFINITY = Integer.MAX_VALUE;
private final String ownerId;
private final Map<String, Integer> distances;
private final Map<String, String> nextHops;
private boolean updated;
public DistanceVector(String ownerId, Map<String, Integer> distances, Map<String, String> nextHops) {
this.ownerId = ownerId;
this.distances = new HashMap<>(distances);
this.nextHops = new HashMap<>(nextHops);
this.updated = false;
}
public String getOwnerId() {
return ownerId;
}
public Map<String, Integer> getDistances() {
return new HashMap<>(distances);
}
public int getDistance(String destination) {
return distances.getOrDefault(destination, INFINITY);
}
public String getNextHop(String destination) {
return nextHops.get(destination);
}
public boolean updateDistances(Map<String, Integer> newDistances) {
boolean changed = false;
for (Map.Entry<String, Integer> entry : newDistances.entrySet()) {
String dest = entry.getKey();
int distance = entry.getValue();
Integer currentDistance = distances.get(dest);
if (currentDistance == null || !currentDistance.equals(distance)) {
distances.put(dest, distance);
changed = true;
}
}
if (changed) {
updated = true;
}
return changed;
}
public boolean updateUsingBellmanFord(DistanceVector neighborDV) {
boolean changed = false;
String neighborId = neighborDV.getOwnerId();
int costToNeighbor = getDistance(neighborId);
if (costToNeighbor == INFINITY) {
return false; // Can't reach this neighbor
}
// For each destination in neighbor's DV
for (Map.Entry<String, Integer> entry : neighborDV.getDistances().entrySet()) {
String dest = entry.getKey();
int neighborDistToDest = entry.getValue();
// Skip if same as this router
if (dest.equals(ownerId)) {
continue;
}
// Calculate potential new distance
int newDist;
if (neighborDistToDest == INFINITY) {
newDist = INFINITY;
} else {
newDist = costToNeighbor + neighborDistToDest;
}
// Current distance to destination
int currentDist = getDistance(dest);
/* debug
System.out.println("DEBUG: Evaluating path to " + dest +
" via " + neighborId +
" - current: " + currentDist +
", new: " + newDist);
System.out.println("DEBUG: Evaluating path to " + dest +
" via " + neighborId +
" cost to neighbor: " + costToNeighbor +
" neighbor distance to dest:" + neighborDistToDest);
*/
// Update if found better path
if (newDist < currentDist){
distances.put(dest, newDist);
/* debug
System.out.println("DEBUG: UPDATING path to " + dest +
" - old next hop: " + nextHops.getOrDefault(dest, "none") +
", new next hop: " + neighborId);
*/
String nextHopToNeighbor = getNextHop(neighborId);
nextHops.put(dest, nextHopToNeighbor);
changed = true;
updated = true;
}
}
return changed;
}
public boolean wasUpdated() {
return updated;
}
public void resetUpdateFlag() {
updated = false;
}
}