2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.transport;
23 import peersim.config.*;
24 import peersim.core.Control;
27 * Initializes static singleton {@link E2ENetwork} by reading a king data set.
29 * @author Alberto Montresor
30 * @version $Revision: 1.9 $
32 public class KingParser implements Control
35 // ---------------------------------------------------------------------
37 // ---------------------------------------------------------------------
40 * The file containing the King measurements.
43 private static final String PAR_FILE = "file";
46 * The ratio between the time units used in the configuration file and the
47 * time units used in the Peersim simulator.
50 private static final String PAR_RATIO = "ratio";
52 // ---------------------------------------------------------------------
54 // ---------------------------------------------------------------------
56 /** Name of the file containing the King measurements. */
57 private String filename;
60 * Ratio between the time units used in the configuration file and the time
61 * units used in the Peersim simulator.
65 /** Prefix for reading parameters */
66 private String prefix;
68 // ---------------------------------------------------------------------
70 // ---------------------------------------------------------------------
73 * Read the configuration parameters.
75 public KingParser(String prefix)
78 ratio = Configuration.getDouble(prefix + "." + PAR_RATIO, 1);
79 filename = Configuration.getString(prefix + "." + PAR_FILE, null);
82 // ---------------------------------------------------------------------
84 // ---------------------------------------------------------------------
87 * Initializes static singleton {@link E2ENetwork} by reading a king data set.
88 * @return always false
90 public boolean execute()
92 BufferedReader in = null;
93 if (filename != null) {
95 in = new BufferedReader(new FileReader(filename));
96 } catch (FileNotFoundException e) {
97 throw new IllegalParameterException(prefix + "." + PAR_FILE, filename
101 in = new BufferedReader( new InputStreamReader(
102 ClassLoader.getSystemResourceAsStream("t-king.map")
106 // XXX If the file format is not correct, we will get quite obscure
107 // exceptions. To be improved.
110 // Skip initial lines
114 while ((line = in.readLine()) != null && !line.startsWith("node")) lc++;
115 while (line != null && line.startsWith("node")) {
118 line = in.readLine();
120 } catch (IOException e) {
121 System.err.println("KingParser: " + filename + ", line " + lc + ":");
123 try { in.close(); } catch (IOException e1) { };
126 E2ENetwork.reset(size, true);
128 System.err.println("KingParser: " + filename + ", line " + lc + ":");
129 System.err.println("No latency matrix contained in the specified file");
130 try { in.close(); } catch (IOException e1) { };
134 System.err.println("KingParser: read " + size + " entries");
138 StringTokenizer tok = new StringTokenizer(line, ", ");
139 if (tok.countTokens() != 3) {
140 System.err.println("KingParser: " + filename + ", line " + lc + ":");
141 System.err.println("Specified line does not contain a <node1, node2, latency> triple");
142 try { in.close(); } catch (IOException e1) { };
145 int n1 = Integer.parseInt(tok.nextToken()) - 1;
146 int n2 = Integer.parseInt(tok.nextToken()) - 1;
147 int latency = (int) (Double.parseDouble(tok.nextToken()) * ratio);
148 E2ENetwork.setLatency(n1, n2, latency);
150 line = in.readLine();
151 } while (line != null);
155 } catch (IOException e) {
156 System.err.println("KingParser: " + filename + ", line " + lc + ":");
158 try { in.close(); } catch (IOException e1) { };