-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouterNode.java
executable file
·169 lines (139 loc) · 5.6 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
166
167
168
169
import javax.swing.*;
public class RouterNode {
private int myID;
private GuiTextArea myGUI;
private RouterSimulator sim;
private int[][] costs = new int[RouterSimulator.NUM_NODES][RouterSimulator.NUM_NODES];
private int[] myNbr = new int[RouterSimulator.NUM_NODES];
private int[] myRoutings = new int[RouterSimulator.NUM_NODES];
//--------------------------------------------------
public RouterNode(int ID, RouterSimulator sim, int[] costs) {
myID = ID;
this.sim = sim;
myGUI =new GuiTextArea(" Output window for Router #"+ ID + " ");
// Initialize all fields to infinite
for( int col = 0 ; col < RouterSimulator.NUM_NODES; col++ ){
for(int row = 0 ;row < RouterSimulator.NUM_NODES; row++ ){
this.costs[col][row] = RouterSimulator.INFINITY;
}
}
// Copy start costs for node to our table
System.arraycopy(costs, 0, this.myNbr, 0, RouterSimulator.NUM_NODES);
System.arraycopy(costs, 0, this.costs[myID], 0, RouterSimulator.NUM_NODES);
for( int i = 0; i < RouterSimulator.NUM_NODES; i++){
if(myNbr[i] == RouterSimulator.INFINITY)
myRoutings[i] = RouterSimulator.INFINITY;
else
myRoutings[i] = i;
}
sendAllUpdate();
}
public boolean updateCosts(){
boolean costChange = false;
for( int target = 0; target < RouterSimulator.NUM_NODES; target++){
if(target == myID)
continue;
int minRouteNbr = myRoutings[target];
int minCost = costs[myID][target];
for( int nbr = 0; nbr < RouterSimulator.NUM_NODES; nbr++ ){ // One Node (not this node)
if(nbr == myID)
continue;
int costToTarget = this.myNbr[nbr]/*dist to nbr*/ + this.costs[nbr][target]/*dist from nbr to target*/;
// check if nbr == myRoutings[target], .
if( costToTarget < minCost || ( nbr == myRoutings[target] && costToTarget != costs[myID][target] )){
minRouteNbr = nbr;
minCost = costToTarget;
}
}
if( minCost <= myNbr[target] && minCost != costs[myID][target]){
costs[myID][target] = minCost;
myRoutings[target] = minRouteNbr;
costChange = true;
}
if( minCost > myNbr[target] && minCost != costs[myID][target] ){
costs[myID][target] = myNbr[target];
myRoutings[target] = target;
costChange = true;
}
}
return costChange;
}
// Called by the simulator (executed) when a node receives an update from one of its neighbors
public void recvUpdate(RouterPacket pkt) {
if(pkt.destid != this.myID){
return;
}
System.arraycopy(pkt.mincost, 0, costs[pkt.sourceid], 0, RouterSimulator.NUM_NODES);
if(updateCosts()){
sendAllUpdate();
}
}
private void sendAllUpdate(){
// Send nodes dist-vector to neighbors.
for( int recieverID = 0 ; recieverID < RouterSimulator.NUM_NODES; ++recieverID ){
if( myID != recieverID && myNbr[recieverID] != RouterSimulator.INFINITY ){
RouterPacket newPkt = new RouterPacket(this.myID, recieverID, this.costs[myID]);
for( int route = 0; route < RouterSimulator.NUM_NODES; ++route){
if(myRoutings[route] == recieverID && route == recieverID)
newPkt.mincost[route] = RouterSimulator.INFINITY;
}
sendUpdate(newPkt);
}
}
}
//--------------------------------------------------
private void sendUpdate(RouterPacket pkt) {
sim.toLayer2(pkt);
}
// used for debugging and testing of code, and also for demonstration of your solution to the assignment.
// This method should print the distance vector table (i.e. the routing table) in a format that you and
// your lab assistant can read and understand.
public void printDistanceTable() {
myGUI.println("\nCurrent table for " + myID +
" at time " + sim.getClocktime());
myGUI.println("Distancetable:");
myGUI.print(String.format( "%7s %4s", "Dst", "|"));
for(int xtitle = 0; xtitle < RouterSimulator.NUM_NODES; ++xtitle){
myGUI.print( String.format( "%6s", xtitle ) );
}
myGUI.println("\n--------------------------------------------------------");
for(int col = 0; col < RouterSimulator.NUM_NODES; col++){
if( col == myID || myNbr[col] == RouterSimulator.INFINITY ) continue;
myGUI.print( String.format( "%1s %3s %3s", "nbr" , col, "|" ) );
for(int row = 0; row < RouterSimulator.NUM_NODES; row++){
myGUI.print( String.format( "%6s", this.costs[col][row] ) );
}
myGUI.println();
}
myGUI.println("Our distance vector and routes:");
myGUI.print(String.format( "%7s %4s", "Dst", "|"));
for(int xtitle = 0; xtitle < RouterSimulator.NUM_NODES; ++xtitle){
myGUI.print( String.format( "%6s", xtitle ) );
}
myGUI.println("\n--------------------------------------------------------");
myGUI.print( String.format( "%7s %3s", "Dist", "|" ) );
for(int row = 0; row < RouterSimulator.NUM_NODES; row++){
myGUI.print( String.format( "%6s", this.myNbr[row] ) );
}
myGUI.println();
myGUI.print( String.format( "%7s %3s", "Cost", "|" ) );
for(int row = 0; row < RouterSimulator.NUM_NODES; row++){
myGUI.print( String.format( "%6s", this.costs[myID][row] ) );
}
myGUI.println();
myGUI.print( String.format( "%7s %2s", "Route", "|" ) );
for(int row = 0; row < RouterSimulator.NUM_NODES; row++){
myGUI.print( String.format( "%6s", this.myRoutings[row] ) );
}
myGUI.println("\n");
}
// called by the simulator (executed) when the cost of link of a node is about to change
public void updateLinkCost(int dest, int newcost) {
myNbr[dest] = newcost;
costs[myID][dest] = newcost;
costs[dest][myID] = newcost;
myRoutings[dest] = dest;
if(updateCosts())
sendAllUpdate();
}
}