forked from RobertN/Distance-Vector-Routing
/
RouterNode.java
165 lines (131 loc) · 4.95 KB
/
RouterNode.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import javax.swing.*;
public class RouterNode {
private int myID;
private GuiTextArea myGUI;
private RouterSimulator sim;
private int[] costs = new int[RouterSimulator.NUM_NODES];
private int[] routes = new int[RouterSimulator.NUM_NODES];
private int[][] neighbourVectors = new int[RouterSimulator.NUM_NODES][RouterSimulator.NUM_NODES];
private boolean usePoisonedReverse = true;
//--------------------------------------------------
public RouterNode(int ID, RouterSimulator sim, int[] costs) {
myID = ID;
this.sim = sim;
myGUI =new GuiTextArea(" Output window for Router #"+ ID + " ");
System.arraycopy(costs, 0, this.costs, 0, RouterSimulator.NUM_NODES);
for(int i = 0; i < RouterSimulator.NUM_NODES; i++) {
routes[i] = i;
for(int j = 0; j < RouterSimulator.NUM_NODES; j++) {
if(i == myID)
neighbourVectors[i][j] = costs[j];
else
neighbourVectors[i][j] = RouterSimulator.INFINITY;
}
}
sendVectors();
}
//--------------------------------------------------
private void sendVectors() {
for(int i = 0; i < RouterSimulator.NUM_NODES; i++) {
if(i != myID) {
if(costs[i] != RouterSimulator.INFINITY) {
int[] c = new int[RouterSimulator.NUM_NODES];
System.arraycopy(costs, 0, c, 0, RouterSimulator.NUM_NODES);
if(usePoisonedReverse) {
// check if we are using the active node in some route
for(int r = 0; r < RouterSimulator.NUM_NODES; r++) {
if(routes[r] == i) {
c[r] = RouterSimulator.INFINITY;
}
}
}
RouterPacket pkt = new RouterPacket(myID, i, c);
sendUpdate(pkt);
}
}
}
}
//--------------------------------------------------
private boolean recalculateCost() {
int cost = 0;
boolean dirty = false;
for(int i = 0; i < RouterSimulator.NUM_NODES; i++) {
for(int dest = 0; dest < RouterSimulator.NUM_NODES; dest++) {
cost = neighbourVectors[i][dest];
if(i != myID)
cost += neighbourVectors[myID][i];
if(cost < costs[dest]) {
costs[dest] = cost;
routes[dest] = i;
dirty = true;
}
}
}
return dirty;
}
//--------------------------------------------------
public void recvUpdate(RouterPacket pkt) {
System.arraycopy(pkt.mincost, 0, neighbourVectors[pkt.sourceid], 0, RouterSimulator.NUM_NODES);
boolean dirty = recalculateCost();
if(dirty) {
sendVectors();
}
}
//--------------------------------------------------
private void sendUpdate(RouterPacket pkt) {
sim.toLayer2(pkt);
}
//--------------------------------------------------
public void printDistanceTable() {
myGUI.print("Current state for router " + myID + " at time " + sim.getClocktime() + " \n\n");
myGUI.print("Distancetable: \n");
for(int i = 0; i < RouterSimulator.NUM_NODES; i++) {
myGUI.print("\t" + i);
}
myGUI.print("\n");
for(int i = 0; i < 12*(1+RouterSimulator.NUM_NODES); i++) {
myGUI.print("-");
}
myGUI.print("\n");
for(int i = 0; i < RouterSimulator.NUM_NODES; i++) {
if(i != myID) {
myGUI.print("" + i);
for(int j = 0; j < RouterSimulator.NUM_NODES; j++) {
myGUI.print("\t" + neighbourVectors[i][j]);
}
myGUI.print("\n");
}
}
myGUI.print("\n");
myGUI.println("Our distance vector and routes:");
myGUI.print("dst");
for(int i = 0; i < RouterSimulator.NUM_NODES; i++) {
myGUI.print("\t" + i);
}
myGUI.print("\n");
for(int i = 0; i < 12*(1+RouterSimulator.NUM_NODES); i++) {
myGUI.print("-");
}
myGUI.print("\n");
myGUI.print("cost");
for(int i = 0; i < RouterSimulator.NUM_NODES; i++) {
myGUI.print("\t" + costs[i]);
}
myGUI.print("\n");
myGUI.print("route");
for(int i = 0; i < RouterSimulator.NUM_NODES; i++) {
myGUI.print("\t" + routes[i]);
}
myGUI.print("\n");
myGUI.print("\n");
myGUI.print("\n");
}
//--------------------------------------------------
public void updateLinkCost(int dest, int newcost) {
neighbourVectors[myID][dest] = newcost;
boolean dirty = recalculateCost();
if(dirty) {
sendVectors();
}
}
}