import java.io.File; import java.util.*; public class Assign6 { private String slevel, payload; //Options to be read into each packet private int dest, option; //Options to be read into a packet or a command private Router r; // The Router instantiation public Assign6(int x) { r = new Router(x); } //Routine to read in the info for the ADDPACKET command private boolean addP (Scanner cmdData) { Packet p= new Packet(); //Create a new packet and fill dest = cmdData.nextInt(); slevel = cmdData.next(); cmdData.nextLine(); payload = cmdData.nextLine(); if (!p.setPkDestPort(dest)) { System.err.println ("Destination not valid in packet"); return false; } //This part sets the service level if (slevel.equals("H")) p.setPkServiceLevel(Packet.ServiceLevel.HIGH); else p.setPkServiceLevel(Packet.ServiceLevel.NORMAL); //Now set the payload if (!p.setPkPayload(payload)) { System.err.println ("Payload not valid in packet"); return false; } // Call the router addPacket routine r.addPacket(p); return true; } //Routine to read in commands and exercise the router private boolean readCommand (Scanner cmdData){ try { String cmd = cmdData.next(); if (cmd.equals("END")) return false; if (cmd.equals("ADDPACKET")) { if (!addP(cmdData)) System.out.println ("Packet not added"); } else if (cmd.equals("PRINT")) { option = cmdData.nextInt(); if (!r.print(option)) System.err.println ("Print option not recognized"); } else if (cmd.equals("PROCESSPACKET")) { r.processPacket(); } else if (cmd.equals("REMOVEPACKET")) { option = cmdData.nextInt(); if (r.removePacket(option) == null) System.out.println ("Packet not found"); } return true; } catch (Exception e) { System.out.println ("Error in input file format"); return false; } } // Client driver public static void main (String[] args) { Scanner cmdData = null; int hpct=0; try { //Open the input file and read in the percentages cmdData = new Scanner(new File(args[0])); hpct = cmdData.nextInt(); } catch (Exception e) { e.printStackTrace(); System.out.println ("Usage java Assign6 inputfile"); System.exit(0); } //Fire up the assignment and start reading commands Assign6 A6 = new Assign6(hpct); while (A6.readCommand(cmdData)); } }