📄 networkreader.java
字号:
/* * Title: GridSim Toolkit * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation * of Parallel and Distributed Systems such as Clusters and Grids * Licence: GPL - http://www.gnu.org/copyleft/gpl.html */package gridsim.util;import gridsim.net.*;import java.util.*;import java.io.*;/** * This is an utility class, which parses a file and constructs the * network topology automatically. <br> * The file that defines the network has the following form: <br> <br> * <tt> * # specify the number of routers<br> * number_of_routers<br> * <br> * # specify the name of each router and (optional) logging facility<br> * router_name1 [true/false]<br> * router_name2 [true/false]<br> * router_name3 [true/false]<br> * ... // other router names<br> * <br> * # linking two routers. NOTE: the router name is case sensitive!<br> * router_name1 router_name2 baud_rate(GB/s) prop_delay(ms) mtu(byte) <br> * router_name1 router_name3 baud_rate(GB/s) prop_delay(ms) mtu(byte) <br> * ... // linking other routers<br> * </tt> * <br> * NOTE: <tt>[]</tt> means an optional parameter for logging activities * inside a router. * If it is not given, then by default the value is false. * * @author Uros Cibej and Anthony Sulistio * @since GridSim Toolkit 4.0 */public class NetworkReader{ /** * Creates a network topology that uses a FIFO packet scheduler * @param filename the name of the file containing the description of * the network topology * @return the list of Routers of the network or <tt>null</tt> if an error * occurs * @see gridsim.net.FIFOScheduler */ public static LinkedList createFIFO(String filename) { LinkedList routerList = null; try { FileReader fileReader = new FileReader(filename); BufferedReader buffer = new BufferedReader(fileReader); routerList = createNetworkFIFO(buffer); } catch (Exception exp) { System.out.println("NetworkReader: File not found."); routerList = null; } return routerList; } /** * Creates a network topology that uses a SCFQ packet scheduler * @param filename the name of the file containing the description of * the network topology * @param weight a linear array of the weights to be assigned to * different classes of traffic. * @return the list of Routers of the network or <tt>null</tt> if an error * occurs * @see gridsim.net.SCFQScheduler */ public static LinkedList createSCFQ(String filename, double[] weight) { if (weight == null) { return null; } LinkedList routerList = null; try { FileReader fileReader = new FileReader(filename); BufferedReader buffer = new BufferedReader(fileReader); routerList = createNetworkSCFQ(buffer, weight); } catch (Exception exp) { System.out.println("NetworkReader: File not found or " + "weight[] is null."); routerList = null; } return routerList; } /** * Creates a network topology that uses a Rate controlled packet scheduler * @param filename the name of the file containing the description of * the network topology * @param percentage a linear array of bandwidth percentage to be assigned * to different classes of traffic. * @return the list of Routers of the network or <tt>null</tt> if an error * occurs * @see gridsim.net.RateControlledScheduler */ public static LinkedList createRate(String filename, double[] percentage) { if (percentage == null) { return null; } LinkedList routerList = null; try { // check whether the total percentage is greater than 100% double total = 0; int MAX_LIMIT = 100; for (int i = 0; i < percentage.length; i++) { total += percentage[i]; if (total > MAX_LIMIT) { System.out.println("NetworkReader: total percentage = " + total + ", which is > 100%"); return null; } } FileReader fileReader = new FileReader(filename); BufferedReader buffer = new BufferedReader(fileReader); routerList = createNetworkRate(buffer,percentage.length,percentage); } catch (Exception exp) { System.out.println("NetworkReader: File not found or " + "percentage[] is null."); routerList = null; } return routerList; } /** * Gets a Router object from the list * @param name a router name * @param routerList a list containing the Router objects * @return a Router object or <tt>null</tt> if not found */ public static Router getRouter(String name, LinkedList routerList) { if (name == null || routerList == null || name.length() == 0) { return null; } Router router = null; try { Iterator it = routerList.iterator(); while ( it.hasNext() ) { router = (Router) it.next(); if (router.get_name().equals(name) == true) { break; } else { router = null; } } } catch (Exception e) { router = null; } return router; } /** * Creates a number of routers from a given buffered reader * @param buf a Buffered Reader object * @param rate a flag denotes the type of Router will be using * @return a list of Router objects or <tt>null</tt> if an error occurs */ private static LinkedList createRouter(BufferedReader buf, boolean rate) throws Exception { String line = null; StringTokenizer str = null; int num_router = -1; // num of routers // parse each line while ((line = buf.readLine()) != null) { str = new StringTokenizer(line); if (str.hasMoreTokens() == false) { // ignore newlines continue; } String num = str.nextToken(); // get next token if (num.startsWith("#") == true) { // ignore comments continue; } // get the num of router if (num_router == -1) { num_router = (new Integer(num)).intValue(); break; } } LinkedList routerList = new LinkedList(); Router router = null; // a Router object String name = null; // router name String flag = null; // a flag to denote logging router or not boolean log = false; // then for each line, get the router name for (int i = 1; i <= num_router; i++) { log = false; line = buf.readLine(); str = new StringTokenizer(line); if (str.hasMoreTokens() == false) // ignore newlines { i--; continue; } name = str.nextToken(); // get next token if (name.startsWith("#") == true) // ignore comments { i--; continue; } if (str.hasMoreTokens() == true) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -