⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 command.java

📁 使用Java语言编写模拟路由器程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				ObjectInputStream ois;				ObjectOutputStream oos;				ois = new ObjectInputStream(s.getInputStream());				oos = new ObjectOutputStream(s.getOutputStream());				// send the command				oos.writeObject("step");				// receive the response				result = (String) ois.readObject();				ois.close();				oos.close();				s.close();			}			return (result);		} catch (NumberFormatException nfe) {			return ("Found Invalid Router Id");		} catch (Exception ioe) {			return ("An I/O error occured while communication with Router " + routerId);		}	}	/*	 * Method Name: update	 * 	 * @args: the arguments to the update command	 * 	 * Use: to update the weight of the link between two routers	 */	String update(String args) {		String[] fields;		int routerId;		Socket s;		try {			fields = args.split(",");			if (fields.length != 3) {				return ("Invalid args: " + args);			}			// get the source router			routerId = Integer.parseInt(fields[0]);			if (rl.contains(routerId) == false) {				return ("Router with ID " + fields[0] + " not found in the Router Location Table");			}			if (activeRouters.contains(new Integer(routerId)) == false) {				return ("Router with ID " + routerId + " not in the list of currently active routers");			}			s = new Socket(rl.getIp(routerId), rl.getCommandPort(routerId));			ObjectInputStream ois;			ObjectOutputStream oos;			ois = new ObjectInputStream(s.getInputStream());			oos = new ObjectOutputStream(s.getOutputStream());			// send the command			oos.writeObject("updateto:" + fields[1] + ":" + fields[2]);			// receive the response			String result = fields[0] + ": " + (String) ois.readObject() + "\n";			ois.close();			oos.close();			s.close();			// get the destination router			routerId = Integer.parseInt(fields[1]);			if (rl.contains(routerId) == false) {				return ("Router with ID " + fields[1] + " not found in the Router Location Table");			}			if (activeRouters.contains(new Integer(routerId)) == false) {				return ("Router with ID " + routerId + " not in the list of currently active routers");			}			s = new Socket(rl.getIp(routerId), rl.getCommandPort(routerId));			ois = new ObjectInputStream(s.getInputStream());			oos = new ObjectOutputStream(s.getOutputStream());			// send the command			oos.writeObject("updatefrom:" + fields[0] + ":" + fields[2]);			// read the response			result += fields[1] + ": " + (String) ois.readObject();			ois.close();			oos.close();			s.close();			return (result);		} catch (NumberFormatException nfe) {			return ("Invalid Router ID: " + args);		} catch (Exception ioe) {			return ("An I/O error occured while communication with Router " + args);		}	}	/*	 * Method Name: deleteRouter	 * 	 * @args: the arguments to the deleteRouter command	 * 	 * Use: to delete a router from the network	 */	String deleteRouter(String args) {		// check whether the router is in the list of active routers		// check why multiple threads are getting created		int routerId = -1;		String result = new String("");		Socket s;		try {			// get the router id			routerId = Integer.parseInt(args);			if (rl.contains(routerId) == false) {				return ("Router with ID " + routerId + " not found in the Router Location Table");			}			Iterator i = activeRouters.iterator();			// for all active routers			while (i.hasNext()) {				routerId = ((Integer) i.next()).intValue();				if (rl.contains(routerId) == false) {					result = result							+ ("Router with ID " + routerId + " (currently in the list of active routers) not found in the Router Location Table\n");				}				if (activeRouters.contains(new Integer(routerId)) == false) {					return ("Router with ID " + routerId + " not in the list of currently active routers");				}				s = new Socket(rl.getIp(routerId), rl.getCommandPort(routerId));				ObjectInputStream ois;				ObjectOutputStream oos;				ois = new ObjectInputStream(s.getInputStream());				oos = new ObjectOutputStream(s.getOutputStream());				// send the command				oos.writeObject("deleteRouter:" + args);				// receive the response				result = result + routerId + ": " + (String) ois.readObject()						+ "\n";				ois.close();				oos.close();				s.close();			}		} catch (NumberFormatException nfe) {			return ("Found Invalid Router Id");		} catch (Exception ioe) {			return ("An I/O error occured while communication with Router " + routerId);		}		// remove the router from the list of active routers		activeRouters.remove(new Integer(Integer.parseInt(args)));		return (result);	}	/*	 * Method Name: start	 * 	 * @s: flag to indicate whether or not to wait for the user to press	 * 'return' before the execution of the command Use: To perform a sequential	 * execution of commands in the event file	 */	void start(boolean s) {		DataInputStream d = new DataInputStream(System.in);		try {			// open the logfile			BufferedWriter out = new BufferedWriter(new FileWriter(logfile));			String result;			Iterator i = commands.iterator();			// for each command			while (i.hasNext()) {				String command = (String) i.next();				String[] fields = command.split(":", 2);				if (s == true) {					System.out							.print("Press any key to execute the next command ("									+ command + ") ");					try {						d.readByte();					} catch (Exception e) {						System.out								.println("An error occured while reading from the standard input");						System.out.println("Quitting.");						System.exit(1);					}					System.out.println("");				}				// print the command to the screen and the logfile				System.out.println("[" + command + "]");				out.write("[" + command + "]\n");				if (fields.length != 2) {					System.out.println("\nInvalid Command\n");					out.write("\nInvalid Arguments\n");				}				else if (fields[0].equals("addRouter")) {					result = addRouter(fields[1]);					System.out.println(result);					out.write(result + "\n");				}				else if (fields[0].equals("deleteRouter")) {					result = deleteRouter(fields[1]);					System.out.println(result);					out.write(result + "\n");				}				else if (fields[0].equals("update")) {					result = update(fields[1]);					System.out.println(result);					out.write(result + "\n");				}				else if (fields[0].equals("packets")) {					result = getPackets(fields[1]);					System.out.println(result);					out.write(result + "\n");				}				else if (fields[0].equals("route")) {					result = getRoute(fields[1]);					System.out.println(result);					out.write(result + "\n");				}				else if (fields[0].equals("display")) {					result = displayRoutingTable(fields[1]);					System.out.println(result);					out.write(result + "\n");				}				else if (fields[0].equals("step")) {					result = step(fields[1]);					System.out.println(result);					out.write(result + "\n");				}				else {					System.out.println("\nInvalid Command\n");					out.write("\nInvalid Command\n");				}			}			out.close();		} catch (IOException ioe) {			System.out.println("Unable to write to the log file: " + logfile);			System.out.println("Quitting.");			System.exit(1);		}		return;	}	/*	 * Method Name: main	 * 	 * @args: The command line arguments	 * 	 * Use: To start the execution of the command program	 */	public static void main(String[] args) {		String ef = "event.txt";		String rlf = "rlf.txt";		String of = "log.txt";		String tcf = "tcf.txt";		int id = -1;		boolean s = false;		if (args.length == 0) {			System.out.println("Usage: java Command <options>");			System.out.println("where possible options include:");			System.out.println(" -h       : Print a synopsis of options");			System.out					.println(" -f <ef>  : Override the default Event File (ef.txt)");			System.out					.println(" -l <rlf> : Override the default Router Location File (rlf.txt)");			System.out					.println(" -t <tcf> : Override the default Topology Configuration File (tcf.txt)");			System.out					.println(" -o <of>  : Override the default Output File (log.txt)");			System.out.println(" -s       : To 'step' through the events");			System.exit(1);		}		else {			for (int i = 0; i < (args.length); i++) {				if (args[i].equals("-h")) {					System.out.println("Usage: java Command <options>");					System.out.println("where possible options include:");					System.out							.println(" -h       : Print a synopsis of options");					System.out							.println(" -f <ef>  : Override the default Event File (ef.txt)");					System.out							.println(" -l <rlf> : Override the default Router Location File (rlf.txt)");					System.out							.println(" -t <tcf> : Override the default Topology Configuration File (tcf.txt)");					System.out							.println(" -o <of>  : Override the default Output File (log.txt)");					System.out							.println(" -s       : To 'step' through the events");					System.exit(0);				}				else if (args[i].equals("-s")) {					s = true;				}				else if (args[i].equals("-f")) {					if (i == (args.length - 1)) {						System.out								.println("Please specify the value for option '-f'");						System.exit(1);					} else {						ef = args[i + 1];						i++;					}				}				else if (args[i].equals("-l")) {					if (i == (args.length - 1)) {						System.out								.println("Please specify the value for option '-l'");						System.exit(1);					} else {						rlf = args[i + 1];						i++;					}				}				else if (args[i].equals("-o")) {					if (i == (args.length - 1)) {						System.out								.println("Please specify the value for option '-o'");						System.exit(1);					} else {						of = args[i + 1];						i++;					}				}				else if (args[i].equals("-t")) {					if (i == (args.length - 1)) {						System.out								.println("Please specify the value for option '-t'");						System.exit(1);					} else {						tcf = args[i + 1];						i++;					}				}				else {					System.out.println("Invalid option " + args[i]);					System.exit(1);				}			}			Command c = new Command(ef, rlf, of, tcf);			c.start(s);		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -