-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouterSimulator.java
executable file
·318 lines (256 loc) · 9.2 KB
/
RouterSimulator.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import java.util.Random;
/* ******************************************************************
Project 4: implementing distributed, asynchronous, distance vector routing.
THIS IS THE MAIN ROUTINE. The TRACE and LINKCHANGES variables can be
changed to modify the simulator's output and behavior.
This code is a Java translation of code provided by Kurose and Ross.
Output GUIs added by Ch. Schuba 2007.
**********************************************************************/
public class RouterSimulator {
public static final int NUM_NODES = 5;
public static final int INFINITY = 999;
public static final boolean LINKCHANGES = true;
public int TRACE = 1; /* for debugging */
private GuiTextArea myGUI = null;
private RouterNode[] nodes;
private Random generator;
private int[][] connectcosts = new int[NUM_NODES][NUM_NODES];
/*****************************************************************
***************** NETWORK EMULATION CODE STARTS BELOW ***********
The code below emulates the layer 2 and below network environment:
- emulates the tranmission and delivery (with no loss and no
corruption) between two physically connected nodes
- calls the initializations routines rtinit0, etc., once before
beginning emulation
THERE IS NOT REASON THAT ANY STUDENT SHOULD HAVE TO READ OR UNDERSTAND
THE CODE BELOW. YOU SHOLD NOT TOUCH, OR REFERENCE (in your code) ANY
OF THE DATA STRUCTURES BELOW. If you're interested in how I designed
the emulator, you're welcome to look at the code - but again, you
should not have to, and you defeinitely should not have to modify
******************************************************************/
Event evlist = null; /* the event list */
/* possible events: */
final static int FROM_LAYER2 = 2;
final static int LINK_CHANGE = 10;
double clocktime = 0.000;
public static void main(String[] argv){
RouterSimulator sim = new RouterSimulator();
sim.runSimulation();
}
RouterSimulator() /* initialize the simulator */
{
double sum, avg;
Event evptr;
long seed = 1234;
String prop;
myGUI = new GuiTextArea(" Output window for Router Simulator ");
if((prop=System.getProperty("Trace"))!=null)
TRACE = Integer.parseInt(prop);
if((prop=System.getProperty("Seed"))!=null)
seed = Long.parseLong(prop);
generator = new Random(seed);
clocktime=0.0; /* initialize time to 0.0 */
/* set initial costs */
// remember that in java everything defaults to 0
connectcosts[0][1]=1;
connectcosts[0][2]=3;
connectcosts[0][3]=7;
connectcosts[0][4]=1;
connectcosts[1][0]=1;
connectcosts[1][2]=1;
connectcosts[1][3]=INFINITY;
connectcosts[1][4]=1;
connectcosts[2][0]=3;
connectcosts[2][1]=1;
connectcosts[2][3]=2;
connectcosts[2][4]=4;
connectcosts[3][0]=7;
connectcosts[3][1]=INFINITY;
connectcosts[3][2]=2;
connectcosts[3][4]=INFINITY;
connectcosts[4][0]=1;
connectcosts[4][1]=1;
connectcosts[4][2]=4;
connectcosts[4][3]=INFINITY;
nodes = new RouterNode[NUM_NODES];
for(int i=0;i<NUM_NODES;i++)
nodes[i] = new RouterNode(i,this,connectcosts[i]);
/* initialize future link changes */
if (LINKCHANGES) {
evptr = new Event();
evptr.evtime = 10000.0;
evptr.evtype = LINK_CHANGE;
evptr.eventity = 0;
evptr.rtpktptr = null;
evptr.dest = 3;
evptr.cost = 1;
insertevent(evptr);
evptr = new Event();
evptr.evtype = LINK_CHANGE;
evptr.evtime = 20000.0;
evptr.eventity = 0;
evptr.rtpktptr = null;
evptr.dest = 1;
evptr.cost = 6;
insertevent(evptr);
}
}
void runSimulation(){
Event eventptr;
while (true) {
eventptr = evlist; /* get next event to simulate */
if (eventptr==null)
break;
evlist = evlist.next; /* remove this event from event list */
if (evlist!=null)
evlist.prev=null;
if (TRACE>1) {
myGUI.println("MAIN: rcv event, t="+
eventptr.evtime+ " at "+
eventptr.eventity);
if (eventptr.evtype == FROM_LAYER2 ) {
myGUI.print(" src:"+eventptr.rtpktptr.sourceid);
myGUI.print(", dest:"+eventptr.rtpktptr.destid);
myGUI.println(", contents: "+
eventptr.rtpktptr.mincost[0]+" "+ eventptr.rtpktptr.mincost[1]+
" "+
eventptr.rtpktptr.mincost[2]+" "+
eventptr.rtpktptr.mincost[3]);
}
}
clocktime = eventptr.evtime; /* update time to next event time */
if (eventptr.evtype == FROM_LAYER2 ) {
if(eventptr.eventity >=0 && eventptr.eventity < NUM_NODES)
nodes[eventptr.eventity].recvUpdate(eventptr.rtpktptr);
else { myGUI.println("Panic: unknown event entity\n"); System.exit(0); }
}
else if (eventptr.evtype == LINK_CHANGE ) {
// change link costs here if implemented
nodes[eventptr.eventity].updateLinkCost(eventptr.dest,
eventptr.cost);
nodes[eventptr.dest].updateLinkCost(eventptr.eventity,
eventptr.cost);
}
else
{ myGUI.println("Panic: unknown event type\n"); System.exit(0); }
if(TRACE > 2)
for(int i=0;i<NUM_NODES;i++)
nodes[i].printDistanceTable();
}
myGUI.println("\nSimulator terminated at t="+clocktime+
", no packets in medium\n");
}
public double getClocktime() {
return clocktime;
}
/********************* EVENT HANDLINE ROUTINES *******/
/* The next set of routines handle the event list */
/*****************************************************/
void insertevent(Event p){
Event q,qold;
if (TRACE>3) {
myGUI.println(" INSERTEVENT: time is "+clocktime);
myGUI.println(" INSERTEVENT: future time will be "+
p.evtime);
}
q = evlist; /* q points to header of list in which p struct inserted */
if (q==null) { /* list is empty */
evlist=p;
p.next=null;
p.prev=null;
}
else {
for (qold = q; q !=null && p.evtime > q.evtime; q=q.next)
qold=q;
if (q==null) { /* end of list */
qold.next = p;
p.prev = qold;
p.next = null;
}
else if (q==evlist) { /* front of list */
p.next=evlist;
p.prev=null;
p.next.prev=p;
evlist = p;
}
else { /* middle of list */
p.next=q;
p.prev=q.prev;
q.prev.next=p;
q.prev=p;
}
}
}
void printevlist()
{
Event q;
myGUI.println("--------------\nEvent List Follows:");
for(q = evlist; q!=null; q=q.next) {
myGUI.println("Event time: "+q.evtime+
", type: "+q.evtype+
" entity: "+q.eventity);
}
myGUI.println("--------------");
}
/************************** TOLAYER2 ***************/
void toLayer2(RouterPacket packet){
RouterPacket mypktptr;
Event evptr, q;
double lastime;
int i;
/* be nice: check if source and destination id's are reasonable */
if (packet.sourceid<0 || packet.sourceid > NUM_NODES-1) {
myGUI.println("WARNING: illegal source id in your packet, ignoring packet!");
return;
}
if (packet.destid<0 || packet.destid > NUM_NODES-1) {
myGUI.println("WARNING: illegal dest id in your packet, ignoring packet!");
return;
}
if (packet.sourceid == packet.destid) {
myGUI.println("WARNING: source and destination id's the same, ignoring packet!");
return;
}
if (connectcosts[packet.sourceid][packet.destid] == INFINITY) {
myGUI.println("WARNING: source and destination not connected, ignoring packet!");
return;
}
/* make a copy of the packet student just gave me since may */
/* be modified after we return back */
mypktptr = (RouterPacket)packet.clone();
if (TRACE>2) {
myGUI.print(" TOLAYER2: source: "+mypktptr.sourceid+
" dest: "+ mypktptr.destid+
" costs:");
for (i=0; i<NUM_NODES; i++)
myGUI.print(mypktptr.mincost[i]+" ");
myGUI.println();
}
/* create future event for arrival of packet at the other side */
evptr = new Event();
evptr.evtype = FROM_LAYER2; /* packet will pop out from layer3 */
evptr.eventity = packet.destid; /* event occurs at other entity */
evptr.rtpktptr = mypktptr; /* save ptr to my copy of packet */
/* finally, compute the arrival time of packet at the other end.
medium can not reorder, so make sure packet arrives between 1
and 10 time units after the latest arrival time of packets
currently in the medium on their way to the destination */
lastime = clocktime;
for (q=evlist; q!=null ; q = q.next)
if ( (q.evtype==FROM_LAYER2 && q.eventity==evptr.eventity) )
lastime = q.evtime;
evptr.evtime = lastime + 9*generator.nextDouble() + 1;
if (TRACE>2)
myGUI.println(" TOLAYER2: scheduling arrival on other side");
insertevent(evptr);
}
}
class Event {
double evtime; /* event time */
int evtype; /* event type code */
int eventity; /* entity where event occurs */
RouterPacket rtpktptr; /* ptr to packet (if any) assoc w/ this event */
int dest, cost; /* for link cost change */
Event prev;
Event next;
}