- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouterNode.java
More file actions
Latest commit
executable file
·169 lines (139 loc) · 5.6 KB
/
Copy pathRouterNode.java
File metadata and controls
executable file
·169 lines (139 loc) · 5.6 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
importjavax.swing.*;
publicclassRouterNode {
privateintmyID;
privateGuiTextAreamyGUI;
privateRouterSimulatorsim;
privateint[][] costs = newint[RouterSimulator.NUM_NODES][RouterSimulator.NUM_NODES];
privateint[] myNbr = newint[RouterSimulator.NUM_NODES];
privateint[] myRoutings = newint[RouterSimulator.NUM_NODES];
//--------------------------------------------------
publicRouterNode(intID, RouterSimulatorsim, int[] costs) {
myID = ID;
this.sim = sim;
myGUI =newGuiTextArea(" Output window for Router #"+ ID + " ");
// Initialize all fields to infinite
for( intcol = 0 ; col < RouterSimulator.NUM_NODES; col++ ){
for(introw = 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( inti = 0; i < RouterSimulator.NUM_NODES; i++){
if(myNbr[i] == RouterSimulator.INFINITY)
myRoutings[i] = RouterSimulator.INFINITY;
else
myRoutings[i] = i;
}
sendAllUpdate();
}
publicbooleanupdateCosts(){
booleancostChange = false;
for( inttarget = 0; target < RouterSimulator.NUM_NODES; target++){
if(target == myID)
continue;
intminRouteNbr = myRoutings[target];
intminCost = costs[myID][target];
for( intnbr = 0; nbr < RouterSimulator.NUM_NODES; nbr++ ){ // One Node (not this node)
if(nbr == myID)
continue;
intcostToTarget = 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;
}
}
returncostChange;
}
// Called by the simulator (executed) when a node receives an update from one of its neighbors
publicvoidrecvUpdate(RouterPacketpkt) {
if(pkt.destid != this.myID){
return;
}
System.arraycopy(pkt.mincost, 0, costs[pkt.sourceid], 0, RouterSimulator.NUM_NODES);
if(updateCosts()){
sendAllUpdate();
}
}
privatevoidsendAllUpdate(){
// Send nodes dist-vector to neighbors.
for( intrecieverID = 0 ; recieverID < RouterSimulator.NUM_NODES; ++recieverID ){
if( myID != recieverID && myNbr[recieverID] != RouterSimulator.INFINITY ){
RouterPacketnewPkt = newRouterPacket(this.myID, recieverID, this.costs[myID]);
for( introute = 0; route < RouterSimulator.NUM_NODES; ++route){
if(myRoutings[route] == recieverID && route == recieverID)
newPkt.mincost[route] = RouterSimulator.INFINITY;
}
sendUpdate(newPkt);
}
}
}
//--------------------------------------------------
privatevoidsendUpdate(RouterPacketpkt) {
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.
publicvoidprintDistanceTable() {
myGUI.println("\nCurrent table for " + myID +
" at time " + sim.getClocktime());
myGUI.println("Distancetable:");
myGUI.print(String.format( "%7s %4s", "Dst", "|"));
for(intxtitle = 0; xtitle < RouterSimulator.NUM_NODES; ++xtitle){
myGUI.print( String.format( "%6s", xtitle ) );
}
myGUI.println("\n--------------------------------------------------------");
for(intcol = 0; col < RouterSimulator.NUM_NODES; col++){
if( col == myID || myNbr[col] == RouterSimulator.INFINITY ) continue;
myGUI.print( String.format( "%1s %3s %3s", "nbr" , col, "|" ) );
for(introw = 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(intxtitle = 0; xtitle < RouterSimulator.NUM_NODES; ++xtitle){
myGUI.print( String.format( "%6s", xtitle ) );
}
myGUI.println("\n--------------------------------------------------------");
myGUI.print( String.format( "%7s %3s", "Dist", "|" ) );
for(introw = 0; row < RouterSimulator.NUM_NODES; row++){
myGUI.print( String.format( "%6s", this.myNbr[row] ) );
}
myGUI.println();
myGUI.print( String.format( "%7s %3s", "Cost", "|" ) );
for(introw = 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(introw = 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
publicvoidupdateLinkCost(intdest, intnewcost) {
myNbr[dest] = newcost;
costs[myID][dest] = newcost;
costs[dest][myID] = newcost;
myRoutings[dest] = dest;
if(updateCosts())
sendAllUpdate();
}
}