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

📄 command.java

📁 使用Java语言编写模拟路由器程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* File: Command.java * * Author: Venkata Sastry Malladi * * This file contains the code for the class Command * that represents the Command program. It sends the commands in the * Event File to the active routers in the network and logs their responses * * Preferred Editor: emacs */import java.util.*;import java.io.*;import java.net.*;public class Command {	// a list of commands	LinkedList commands;	// the list of routers	RouterLocation rl;	// currently active routers	HashSet activeRouters;	// the log file	String logfile;	/*	 * Constructor	 * 	 * @eventfile: The file containing the events/commands @locationfile: The	 * Router Location File @logfile: The log file @topologyfile: The Topology	 * Configuration file	 * 	 * Use: To read the set of events and router locations	 */	public Command(String eventfile, String locationfile, String logfile,			String topologyfile) {		// create a new list of commands		commands = new LinkedList();		// read the Router Location File		rl = new RouterLocation(locationfile);		// get the list of initially active routers		Topology t = new Topology(topologyfile);		activeRouters = t.getRouterIds();		this.logfile = logfile;		BufferedReader in = null;		try {			// open the Event File			in = new BufferedReader(new FileReader(eventfile));		} catch (IOException e) {			System.out.println("Unable to open file: " + eventfile);			System.out.println("Quitting.");			System.exit(1);		}		try {			String line;			int linenumber = 1;			// read a command			line = in.readLine();			while (line != null) {				if (line.equals("") != true) {					// add it to the list of commands					commands.add(line);				}				linenumber++;				line = in.readLine();			}			in.close();		} catch (IOException ioe) {			System.out					.println("I/O error occured while reading from the file: "							+ eventfile);			System.out.println("Quitting.");			System.exit(1);		}		return;	}	/*	 * Method Name: addRouter	 * 	 * @args: the arguments to the addRouter command	 * 	 * Use: to add a router to the list of active routers	 */	String addRouter(String args) {		int routerId;		Socket s;		try {			// get the id of the router to be added			routerId = Integer.parseInt(args);			if (rl.contains(routerId) == false) {				return ("Router with ID " + args + " not found in the Router Location Table");			}			// open a connection to the router			s = new Socket(rl.getIp(routerId), rl.getCommandPort(routerId));			ObjectInputStream ois;			ObjectOutputStream oos;			// open the streams associated with the connection			ois = new ObjectInputStream(s.getInputStream());			oos = new ObjectOutputStream(s.getOutputStream());			// probe whether the router is up			oos.writeObject("addRouter");			String result = (String) ois.readObject();			ois.close();			oos.close();			s.close();			// if it is up, add it to the list of currently active routers			if (result.equals("true"))				activeRouters.add(new Integer(routerId));			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: getPackets	 * 	 * @args: the arguments to the packets command	 * 	 * Use: to get the number of distance vector packets received by a router	 */	String getPackets(String args) {		int routerId;		Socket s;		try {			// get the id of the router			routerId = Integer.parseInt(args);			if (rl.contains(routerId) == false) {				return ("Router with ID " + args + " 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");			}			// open a connection to the router			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("packets");			// receive the number of packets			String result = (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: displayRoutingTable	 * 	 * @args: the arguments to the display command	 * 	 * Use: to display the routing table	 */	String displayRoutingTable(String args) {		int routerId;		Socket s;		try {			// get the id of the router			routerId = Integer.parseInt(args);			if (rl.contains(routerId) == false) {				return ("Router with ID " + args + " 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");			}			// open a connection to the router			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("display");			// receive the routing table			String result = (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: getRoute	 * 	 * @args: the arguments to the route command	 * 	 * Use: to get the route between two routers	 */	String getRoute(String args) {		int router1;		int router2;		String[] fields;		String result = new String("");		Socket s;		int ttl = 16;		try {			// get the source and destination router ids			fields = args.split(",");			if (fields.length != 2) {				return ("Invalid arguments: " + args);			}			router1 = Integer.parseInt(fields[0]);			router2 = Integer.parseInt(fields[1]);			// until the destination is reached			do {				if ((rl.contains(router1) == false)						|| (rl.contains(router2) == false)) {					return ("One of the routers " + router1 + " or " + router2 + " not found in the Router Location Table");				}				if (activeRouters.contains(new Integer(router1)) == false) {					return ("Router with ID " + router1 + " not in the list of currently active routers");				}				if (activeRouters.contains(new Integer(router2)) == false) {					return ("Router with ID " + router2 + " not in the list of currently active routers");				}				// open a connection to the current source				s = new Socket(rl.getIp(router1), rl.getCommandPort(router1));				ObjectInputStream ois;				ObjectOutputStream oos;				ois = new ObjectInputStream(s.getInputStream());				oos = new ObjectOutputStream(s.getOutputStream());				// send the command				oos.writeObject("route:" + router2);				// receive the next hop				String reply = (String) ois.readObject();				if (reply.equals("error")) {					ois.close();					oos.close();					s.close();					return ("Router " + router2 + " not found in " + router1 + "'s Routing Table");				}				else if (reply.equals("inf")) {					ois.close();					oos.close();					s.close();					return ("Router " + router1							+ " does not have a route to Router " + router2);				}				else {					// current source = next hop					result = result + router1 + "->";					router1 = Integer.parseInt(reply);				}				--ttl;				// continue till the destination is reached or a routing loop is				// encountered			} while ((router1 != router2) && (ttl != 0));			if (ttl != 0) {				result = result + router2;			}			else {				result = result + "<ttl expired>";			}			return (result);		} catch (NumberFormatException nfe) {			return ("Invalid Router ID received in the process");		} catch (Exception ioe) {			return ("An I/O error occured while communication ");		}	}	/*	 * Method Name: step	 * 	 * @args: the arguments to the step command	 * 	 * Use: to trigger the distance vector exchange between the routers	 */	String step(String args) {		int routerId = -1;		String result = new String("");		Socket s;		try {			// if the command is step:*, send the command to all the active			// routers			if (args.equals("*")) {				Iterator i = activeRouters.iterator();				// for each active router				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");					}					// open a connection					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("step");					// read the response					result = result + routerId + ": "							+ (String) ois.readObject() + "\n";					ois.close();					oos.close();					s.close();				}			}			// if the command is step:x, send step to router x			else {				// get router id				routerId = Integer.parseInt(args);				if (rl.contains(routerId) == false) {					return ("Router with ID " + routerId + " not found in the Router Location Table");				}				// open a connection				s = new Socket(rl.getIp(routerId), rl.getCommandPort(routerId));

⌨️ 快捷键说明

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