📄 routerlocation.java
字号:
/* File: RouterLocation.java * * This file contains the code for the class RouterLocation, * which represents the Router Location File * * The Command and the Router programs use this when they want to communicate * with the routers in the network */import java.net.*;import java.util.*;import java.util.regex.*;import java.io.*;class RouterLocation { // a set of entries, each entry containing information about a router HashSet entries; /* * Constructor * * @filename: The name of the Router Location File * * Use: To read the Router Location file and initialize the list of entries */ public RouterLocation(String filename) { entries = new HashSet(); BufferedReader in = null; // open the file for reading try { in = new BufferedReader(new FileReader(filename)); } catch (IOException e) { System.out.println("Unable to open file: " + filename); System.out.println("Quitting."); System.exit(1); } try { String[] fields; String line; InetAddress ip = null; int commandPort = -1; int routerPort = -1; int routerId = -1; int linenumber = 1; // read a line from the file line = in.readLine(); while (line != null) { if (line.equals("") == false) { try { // split this line using "," as a delimiter fields = line.split(","); if (fields.length != 4) { System.out.println("Line " + linenumber + " in file " + filename + " : Invalid entry"); System.out.println("Quitting."); System.exit(1); } // get the individual elements - the IP, command and // router ports, and the router Id ip = InetAddress.getByName(fields[0]); commandPort = Integer.parseInt(fields[1]); routerPort = Integer.parseInt(fields[2]); routerId = Integer.parseInt(fields[3]); } catch (PatternSyntaxException pse) { System.out.println("Pattern Syntax Exception in RouterLocation.java"); System.out.println("Quitting."); System.exit(1); } // invalid entries in the file catch (NumberFormatException nfe) { System.out.println("Line number " + linenumber + " in file " + filename + ": Invalid number"); System.out.println("Quitting."); System.exit(1); } // invalid IP address catch (UnknownHostException uhe) { System.out.println("Line number " + linenumber + " in file " + filename + ": Invalid IP"); System.out.println("Quitting."); System.exit(1); } // add the entry to the list entries.add(new RouterLocationEntry(ip, commandPort, routerPort, routerId)); } linenumber++; line = in.readLine(); } in.close(); } catch (IOException ioe) { System.out.println("I/O error occured while reading from the file: " + filename); System.out.println("Quitting."); System.exit(1); } return; } /* * Method Name: getIp * * @id: the id of the router whose IP is required * * Use: This is used to get the IP address of a router, given its Id */ InetAddress getIp(int id) { RouterLocationEntry r; Iterator i = entries.iterator(); while (i.hasNext()) { r = (RouterLocationEntry) i.next(); if (r.getRouterId() == id) { return r.getIp(); } } return null; } /* * Method Name: getCommandPort * * @id: the id of the router whose command port is required * * Use: This is used to get the command port of a router, given its Id */ int getCommandPort(int id) { RouterLocationEntry r; Iterator i = entries.iterator(); while (i.hasNext()) { r = (RouterLocationEntry) i.next(); if (r.getRouterId() == id) { return r.getCommandPort(); } } return -1; } /* * Method Name: getRouterPort * * @id: the id of the router whose router port is required * * Use: This is used to get the router port of a router, given its Id */ int getRouterPort(int id) { RouterLocationEntry r; Iterator i = entries.iterator(); while (i.hasNext()) { r = (RouterLocationEntry) i.next(); if (r.getRouterId() == id) { return r.getRouterPort(); } } return -1; } /* * Method Name: contains * * @id: the id of a router * * Use: Used to find whether an entry for a router with given Id exists in * the list of entries */ boolean contains(int id) { Integer ID = new Integer(id); Iterator i = entries.iterator(); while (i.hasNext()) { if (((RouterLocationEntry) i.next()).getRouterId() == id) { return true; } } return false; } /* * Method Name: getRouterIds * * Use: To get the list of all router id's in the list */ HashSet getRouterIds() { HashSet ids = new HashSet(); Iterator i = entries.iterator(); while (i.hasNext()) { ids.add(new Integer(((RouterLocationEntry) i.next()).getRouterId())); } return ids; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -