import java.util.Random; import java.util.List; import java.util.Iterator; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application that makes use of an instance of the RouterSimulator ** class to carry out a simulation of a router receiving and sending packets. ** ** Author: R. McCloskey ** Date: Oct.-Nov. 2026 */ public class RouterSimulationApp { // input variables: // ---------------- private static int seed, minPackSize, maxPackSize, queueCap, bandwidth, simulationLength; private static double arrivalProb; // error message // ------------- private static String ABORT_MESSAGE = "Execution aborted."; /* This main() method directs the simulation. ** There are six inputs, all but one being an integer. ** (1) A seed for the pseudo-random number generator (int) ** (2) the minimum packet size (int) ** (3) the maximum packet size (int) ** (4) the probability that, during a given tick, a packet arrives (double) ** (5) the capacity (in bytes) of the router's queue ** (6) the link bandwidth of the router's output medium (i.e., the number ** of bytes that can be transmitted per tick) (int) ** (7) the length of the simulation (i.e., the number of "ticks" to be ** simulated) ** ** If there is a single command line argument, it is taken to be the ** name of a file containing the seven inputs. Otherwise, the seven ** inputs should be provided via the command line arguments. If the ** number of command line arguments is neither one nor seven, the ** program aborts. */ public static void main(String[] args) { System.out.println("Welcome to RouterSimulationApp\n"); // Verify that either one or seven command line arguments were provided. if (args.length != 1 && args.length != 7) { System.out.print("# arguments provided was " + args.length); System.out.println("; expecting either one or seven"); System.out.println(ABORT_MESSAGE); System.exit(0); } // Get the inputs if (args.length == 1) { // The inputs are in the file named by the lone command line arg getInputsFromFile(args[0]); } else { // args.length == 7) // The inputs are the command line arguments getInputsFromArgs(args); } // Display the input values displayInputs(); // Create an object that simulates a router. Random rand = new Random(seed); RouterSimulator sim = null; try { sim = new RouterSimulator(minPackSize, maxPackSize, arrivalProb, rand, queueCap, bandwidth); } catch (Exception e) { e.printStackTrace(System.out); System.out.println(ABORT_MESSAGE); } // Run the simulation for the number of steps specified by the // last input value. for (int i = 1; i <= simulationLength; i++) { sim.advanceOneTick(); // To sketch what's happened so far: System.out.printf("\nAfter %d ticks:\n", i); System.out.printf(" # packets to have arrived: %d\n", sim.numPacketsArrived()); System.out.printf(" # packets fully transmitted: %d\n", sim.numPacketsTransmitted()); System.out.printf(" # packets dropped: %d\n", sim.numPacketsDropped()); System.out.printf(" # packets in queue: %d\n", sim.numPacketsInQueue()); } // Summarize what happened during the simulation: System.out.printf("\n\nAverage delay of fully transmitted packets: %f\n", sim.averageDelay()); System.out.println("\nFully transmitted packets:"); displayIter(sim.transmittedIter()); System.out.println("\nDropped packets:"); displayIter(sim.droppedIter()); System.out.println("\nPackets on the queue:"); displayIter(sim.queueIter()); System.out.println("\nGoodbye."); } // utility methods // --------------- /* For each packet in the given list, displays its "image" ** (i.e., the String yielded by its toString() method). */ /* private static void displayPacketList(List list) { for (Packet p : list) { System.out.println(p); } } */ /* For each object provided by the given Iterator, displays its ** "image" (i.e., the String yielded by its toString() method). */ private static void displayIter(Iterator iter) { while (iter.hasNext()) { System.out.println(iter.next()); } } /* Given the name of a file, reads the inputs from that file and ** assigns them to the input variables (making the required data ** type translations). It is assumed that each of the seven inputs ** is on a separate line, with any text following it on the same ** line to be ignored. */ private static void getInputsFromFile(String fileName) { try { Scanner input = new Scanner(new File(fileName)); seed = input.nextInt(); input.nextLine(); minPackSize = input.nextInt(); input.nextLine(); maxPackSize = input.nextInt(); input.nextLine(); arrivalProb = input.nextDouble(); input.nextLine(); queueCap = input.nextInt(); input.nextLine(); bandwidth = input.nextInt(); input.nextLine(); simulationLength = input.nextInt(); input.nextLine(); } catch (FileNotFoundException e) { System.out.println("Input file named \"" + fileName + "\" not found."); System.out.println("Execution aborted."); System.exit(0); } catch (Exception e) { e.printStackTrace(System.out); System.out.println(ABORT_MESSAGE); } } /* Given an array containing the inputs, assigns them to the ** input variables (making the required data type translations). */ private static void getInputsFromArgs(String[] args) { try { seed = Integer.parseInt(args[0]); minPackSize = Integer.parseInt(args[1]); maxPackSize = Integer.parseInt(args[2]); arrivalProb = Double.parseDouble(args[3]); queueCap = Integer.parseInt(args[4]); bandwidth = Integer.parseInt(args[5]); simulationLength = Integer.parseInt(args[6]); } catch (Exception e) { e.printStackTrace(System.out); System.out.println(ABORT_MESSAGE); } } private static void displayInputs() { System.out.println("Inputs:"); System.out.println(" Seed for Random: " + seed); System.out.println(" Minimum packet size: " + minPackSize); System.out.println(" Maximum packet size: " + maxPackSize); System.out.println(" Arrival Probability: " + arrivalProb); System.out.println(" Queue Capacity: " + queueCap); System.out.println(" Bandwidth: " + bandwidth); System.out.println(" Ticks to simulate: " + simulationLength); } }