📄 topology.java
字号:
/* File: Topology.java * * This file contains the code for the class Topology * which represents the initial topology of the network of routers * * This object is built from the Topology Configuration File * Each Router uses this object to learn about its initial neighbors * and constructs the initial routing table. */import java.util.*;import java.util.regex.*;import java.io.*;class Topology { // The topology is represented by a list of entries, // each entry representing an edge/link in the network LinkedList entries; /* * Constructor * * Use: To read the Topology Configuration File and construct the object * * @filename: the name of the Topology Configuration File */ public Topology(String filename) { entries = new LinkedList(); 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; int router1 = -1; int router2 = -1; int weight = -1; int linenumber = 1; // read a line line = in.readLine(); while (line != null) { if (line.equals("") != true) { try { // split the entry using "," as a delimiter fields = line.split(","); if (fields.length != 3) { System.out.println("Line " + linenumber + " in file " + filename + " : Invalid entry"); System.out.println("Quitting."); System.exit(1); } // get the individual elements - origin and destination // nodes, and the weight router1 = Integer.parseInt(fields[0]); router2 = Integer.parseInt(fields[1]); weight = Integer.parseInt(fields[2]); } catch (PatternSyntaxException pse) { System.out .println("Pattern Syntax Exception in RouterLocation.java"); System.out.println("Quitting."); System.exit(1); } // invalid entry in the file catch (NumberFormatException nfe) { System.out.println("Line number " + linenumber + " in file " + filename + ": Invalid number"); System.out.println("Quitting."); System.exit(1); } // add an entry to the listcorresponding to this edge entries .add(new TopologyFileEntry(router1, router2, weight)); } 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: getRouterIds * * Use: To get the list of router ids in the initial topology */ HashSet getRouterIds() { HashSet h = new HashSet(); Iterator i = entries.iterator(); while (i.hasNext()) { h.add(new Integer(((TopologyFileEntry) i.next()).getRouter1())); } return h; } /* * Method Name: getEntries * * Use: to get the entries in the Topology Configuration File */ LinkedList getEntries() { return entries; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -