/** ************************************************** */ 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("--------------"); }
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 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"); }
/** ************************ 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); }