import java.util.Random; import java.util.List; import java.util.LinkedList; import java.util.Iterator; /* An instance of this class can be used to carry out a simplistic tick-based ** simulation of a router in a packet-switched network. Such a device acts as a ** "traffic controller", moving data from its source to its intended ** destination across interconnected networks. ** ** Authors: Gemini, R. McCloskey, and ** Collaborated with: ... ** Known defects: ... */ public class RouterSimulator { // instance constants // ------------------ private final double ARRIVAL_PROB; // probability of a packet arriving during a tick private final int QUEUE_CAPACITY; // in bytes private final int BANDWIDTH; // # bytes that can be transmitted per tick private final int MIN_PACKET_SIZE; // in bytes private final int MAX_PACKET_SIZE; // in bytes private final Random RAND; // instance variables // ------------------ private int tickCntr; // keeps track of time private List droppedPackets; // list of packets that were dropped private List transmittedPackets; // list of fully-transmitted packets private int numPacketsArrived; private Queue queue; // holds packets waiting to be transmitted private int bytesInQueue; // Sum of sizes of packets currently in the queue // constructor // ----------- /* Initializes this router simulator to be that described by the ** given minimum and maximum packet sizes, the probability that a ** packet arrives at a given "tick", a pseudo-random number generator, ** the capacity of the router's queue (max sum of # bytes of packets ** in the queue), and the number of bytes (of one or more packets) that ** the router can transmit during one tick. */ public RouterSimulator(int minPacketSize, int maxPacketSize, double arriveProb, Random rand, int qCapacity, int speed) { if (minPacketSize > maxPacketSize) { throw new IllegalArgumentException("min packet size > max packet size"); } if (arriveProb < 0 || arriveProb > 1) { throw new IllegalArgumentException("arriveProb wrong"); } if (qCapacity < minPacketSize) { throw new IllegalArgumentException("queue cannot hold a single packet!"); } if (speed <= 0) { throw new IllegalArgumentException("bandwidth must be positive"); } MIN_PACKET_SIZE = minPacketSize; MAX_PACKET_SIZE = maxPacketSize; ARRIVAL_PROB = arriveProb; QUEUE_CAPACITY = qCapacity; BANDWIDTH = speed; RAND = rand; queue = new QueueViaLink1(); droppedPackets = new LinkedList(); transmittedPackets = new LinkedList(); } /* Simulates the actions taken by the router during one tick. */ public void advanceOneTick() { // STUB } /* Returns the # of packets that have arrived so far. */ public int numPacketsArrived() { return numPacketsArrived; } /* Returns the # of packets that have been (fully) transmitted so far. */ public int numPacketsTransmitted() { return transmittedPackets.size(); } /* Returns an iterator over the list of packets that have been fully ** transmitted so far. */ public Iterator transmittedIter() { return transmittedPackets.iterator(); } /* Returns the # of packets that have been dropped so far. */ public int numPacketsDropped() { return droppedPackets.size(); } /* Returns an iterator over the list of packets that have been ** dropped so far. */ public Iterator droppedIter() { return droppedPackets.iterator(); } /* Returns the # of packets in the queue waiting to be transmitted. */ public int numPacketsInQueue() { return queue.sizeOf(); } /* Returns an iterator over the queue. */ public Iterator queueIter() { return queue.iterator(); } /* Returns the average delay (i.e., node residence time) of the packets ** that have been fully transmitted. */ public double averageDelay() { int totalDelay = 0; for (Packet p : transmittedPackets) { totalDelay = totalDelay + p.nodeResidenceTime(); } return (double)totalDelay / transmittedPackets.size(); } // utility methods // --------------- /* Creates and returns (a reference to) a new instance of the Packet class ** having the given ID and arrival time values. The size of the packet is ** a value in the range [MIN_PACKET_SIZE .. MAX_PACKET_SIZE], with each ** value in that range being equally likely. */ private Packet makePacket(int ID, int arrivalTime) { final int minMaxDelta = MAX_PACKET_SIZE - MIN_PACKET_SIZE; int size = RAND.nextInt(minMaxDelta + 1) + MIN_PACKET_SIZE; return new Packet(ID, arrivalTime, size); } }