https://github.com/RobertN/Distance-Vector-Routing/blob/master/RouterSimulator.java 1 import java.util.Random; 2 3 /* ****************************************************************** 4 Project 4: implementing distributed, asynchronous, distance vector routing. 5 6 THIS IS THE MAIN ROUTINE. The TRACE and LINKCHANGES variables can be 7 changed to modify the simulator's output and behavior. 8 9 This code is a Java translation of code provided by Kurose and Ross. 10 11 Output GUIs added by Ch. Schuba 2007. 12 13 **********************************************************************/ 14 15 public class RouterSimulator { 16 17 public static final int NUM_NODES = 4; 18 public static final int INFINITY = 999; 19 20 public static final boolean LINKCHANGES = true; 21 22 public int TRACE = 3; /* for debugging */ 23 24 private GuiTextArea myGUI = null; 25 26 private RouterNode[] nodes; 27 28 private Random generator; 29 30 private int[][] connectcosts = new int[NUM_NODES][NUM_NODES]; 31 32 33 /***************************************************************** 34 ***************** NETWORK EMULATION CODE STARTS BELOW *********** 35 The code below emulates the layer 2 and below network environment: 36 - emulates the tranmission and delivery (with no loss and no 37 corruption) between two physically connected nodes 38 - calls the initializations routines rtinit0, etc., once before 39 beginning emulation 40 41 THERE IS NOT REASON THAT ANY STUDENT SHOULD HAVE TO READ OR UNDERSTAND 42 THE CODE BELOW. YOU SHOLD NOT TOUCH, OR REFERENCE (in your code) ANY 43 OF THE DATA STRUCTURES BELOW. If you're interested in how I designed 44 the emulator, you're welcome to look at the code - but again, you 45 should not have to, and you defeinitely should not have to modify 46 ******************************************************************/ 47 48 Event evlist = null; /* the event list */ 49 50 /* possible events: */ 51 final static int FROM_LAYER2 = 2; 52 final static int LINK_CHANGE = 10; 53 54 double clocktime = 0.000; 55 56 57 public static void main(String[] argv){ 58 RouterSimulator sim = new RouterSimulator(); 59 60 sim.runSimulation(); 61 } 62 63 RouterSimulator() /* initialize the simulator */ 64 { 65 double sum, avg; 66 Event evptr; 67 long seed = 1234; 68 69 String prop; 70 71 myGUI = new GuiTextArea(" Output window for Router Simulator "); 72 73 if((prop=System.getProperty("Trace"))!=null) 74 TRACE = Integer.parseInt(prop); 75 76 77 if((prop=System.getProperty("Seed"))!=null) 78 seed = Long.parseLong(prop); 79 80 generator = new Random(seed); 81 82 clocktime=0.0; /* initialize time to 0.0 */ 83 84 /* set initial costs */ 85 // remember that in java everything defaults to 0 86 connectcosts[0][1]=1; 87 connectcosts[0][2]=3; 88 connectcosts[0][3]=7; 89 connectcosts[1][0]=1; 90 connectcosts[1][2]=1; 91 connectcosts[1][3]=INFINITY; 92 connectcosts[2][0]=3; 93 connectcosts[2][1]=1; 94 connectcosts[2][3]=2; 95 connectcosts[3][0]=7; 96 connectcosts[3][1]=INFINITY; 97 connectcosts[3][2]=2; 98 99 nodes = new RouterNode[NUM_NODES]; 100 for(int i=0;i1) { 142 myGUI.println("MAIN: rcv event, t="+ 143 eventptr.evtime+ " at "+ 144 eventptr.eventity); 145 if (eventptr.evtype == FROM_LAYER2 ) { 146 myGUI.print(" src:"+eventptr.rtpktptr.sourceid); 147 myGUI.print(", dest:"+eventptr.rtpktptr.destid); 148 myGUI.println(", contents: "+ 149 eventptr.rtpktptr.mincost[0]+" "+ eventptr.rtpktptr.mincost[1]+ 150 " "+ 151 eventptr.rtpktptr.mincost[2]+" "+ 152 eventptr.rtpktptr.mincost[3]); 153 } 154 } 155 clocktime = eventptr.evtime; /* update time to next event time */ 156 if (eventptr.evtype == FROM_LAYER2 ) { 157 if(eventptr.eventity >=0 && eventptr.eventity < NUM_NODES) 158 nodes[eventptr.eventity].recvUpdate(eventptr.rtpktptr); 159 else { myGUI.println("Panic: unknown event entity\n"); System.exit(0); } 160 } 161 else if (eventptr.evtype == LINK_CHANGE ) { 162 // change link costs here if implemented 163 nodes[eventptr.eventity].updateLinkCost(eventptr.dest, 164 eventptr.cost); 165 nodes[eventptr.dest].updateLinkCost(eventptr.eventity, 166 eventptr.cost); 167 } 168 else 169 { myGUI.println("Panic: unknown event type\n"); System.exit(0); } 170 171 if(TRACE > 2) 172 for(int i=0;i3) { 195 myGUI.println(" INSERTEVENT: time is "+clocktime); 196 myGUI.println(" INSERTEVENT: future time will be "+ 197 p.evtime); 198 } 199 q = evlist; /* q points to header of list in which p struct inserted */ 200 if (q==null) { /* list is empty */ 201 evlist=p; 202 p.next=null; 203 p.prev=null; 204 } 205 else { 206 for (qold = q; q !=null && p.evtime > q.evtime; q=q.next) 207 qold=q; 208 if (q==null) { /* end of list */ 209 qold.next = p; 210 p.prev = qold; 211 p.next = null; 212 } 213 else if (q==evlist) { /* front of list */ 214 p.next=evlist; 215 p.prev=null; 216 p.next.prev=p; 217 evlist = p; 218 } 219 else { /* middle of list */ 220 p.next=q; 221 p.prev=q.prev; 222 q.prev.next=p; 223 q.prev=p; 224 } 225 } 226 } 227 228 void printevlist() 229 { 230 Event q; 231 myGUI.println("--------------\nEvent List Follows:"); 232 for(q = evlist; q!=null; q=q.next) { 233 myGUI.println("Event time: "+q.evtime+ 234 ", type: "+q.evtype+ 235 " entity: "+q.eventity); 236 } 237 myGUI.println("--------------"); 238 } 239 240 241 /************************** TOLAYER2 ***************/ 242 void toLayer2(RouterPacket packet){ 243 RouterPacket mypktptr; 244 Event evptr, q; 245 double lastime; 246 int i; 247 248 249 /* be nice: check if source and destination id's are reasonable */ 250 if (packet.sourceid<0 || packet.sourceid > NUM_NODES-1) { 251 myGUI.println("WARNING: illegal source id in your packet, ignoring packet!"); 252 return; 253 } 254 if (packet.destid<0 || packet.destid > NUM_NODES-1) { 255 myGUI.println("WARNING: illegal dest id in your packet, ignoring packet!"); 256 return; 257 } 258 if (packet.sourceid == packet.destid) { 259 myGUI.println("WARNING: source and destination id's the same, ignoring packet!"); 260 return; 261 } 262 if (connectcosts[packet.sourceid][packet.destid] == INFINITY) { 263 myGUI.println("WARNING: source and destination not connected, ignoring packet!"); 264 return; 265 } 266 267 /* make a copy of the packet student just gave me since may */ 268 /* be modified after we return back */ 269 mypktptr = (RouterPacket)packet.clone(); 270 271 if (TRACE>2) { 272 myGUI.print(" TOLAYER2: source: "+mypktptr.sourceid+ 273 " dest: "+ mypktptr.destid+ 274 " costs:"); 275 276 for (i=0; i2) 299 myGUI.println(" TOLAYER2: scheduling arrival on other side"); 300 insertevent(evptr); 301 } 302 } 303 304 class Event { 305 double evtime; /* event time */ 306 int evtype; /* event type code */ 307 int eventity; /* entity where event occurs */ 308 RouterPacket rtpktptr; /* ptr to packet (if any) assoc w/ this event */ 309 int dest, cost; /* for link cost change */ 310 Event prev; 311 Event next; 312 }