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.*;
27 * Initializes static singleton {@link E2ENetwork} by reading a trace
28 * file containing the latency distance measured between a set of
29 * "virtual" routers. Latency is assumed to be symmetric, so the
30 * latency between x and y is equal to the latency to y and x.
32 * The format of the file is as follows: all values are stored as
33 * integers. The first value is the number of nodes considered.
34 * The rest of the values correspond to a "strictly upper triangular
36 * <a href="http://en.wikipedia.org/w/index.php?title=Triangular_matrix&oldid=82411128">
37 * link</a>), ordered first by row than by column.
39 * @author Alberto Montresor
40 * @version $Revision: 1.4 $
42 public class TriangularMatrixParser implements Control
45 // ---------------------------------------------------------------------
47 // ---------------------------------------------------------------------
50 * This configuration parameter identifies the filename of the file
51 * containing the measurements. First, the file is used as a pathname
52 * in the local file system. If no file can be identified in this way,
53 * the file is searched in the local classpath. If the file cannot be
54 * identified again, an error message is reported.
57 private static final String PAR_FILE = "file";
60 * The ratio between the time units used in the configuration file and the
61 * time units used in the Peersim simulator.
64 private static final String PAR_RATIO = "ratio";
66 // ---------------------------------------------------------------------
68 // ---------------------------------------------------------------------
70 /** Name of the file containing the measurements. */
71 private String filename;
73 /** Ratio read from PAR_RATIO */
76 // ---------------------------------------------------------------------
78 // ---------------------------------------------------------------------
81 * Read the configuration parameters.
83 public TriangularMatrixParser(String prefix)
85 filename = Configuration.getString(prefix + "." + PAR_FILE);
86 ratio = Configuration.getDouble(prefix + "." + PAR_RATIO);
89 // ---------------------------------------------------------------------
91 // ---------------------------------------------------------------------
94 * Initializes static singleton {@link E2ENetwork} by reading a king data set.
95 * @return always false
97 public boolean execute()
100 ObjectInputStream in = null;
102 in = new ObjectInputStream(
103 new BufferedInputStream(
104 new FileInputStream(filename)));
105 System.err.println("TriangularMatrixParser: Reading " + filename + " from local file system");
106 } catch (FileNotFoundException e) {
107 in = new ObjectInputStream(
108 new BufferedInputStream(
109 ClassLoader.getSystemResourceAsStream(filename)));
110 System.err.println("TriangularMatrixParser: Reading " + filename + " through the class loader");
113 // Read the number of nodes in the file (first four bytes).
114 int size = in.readInt();
116 // Reset the E2E network
117 E2ENetwork.reset(size, true);
118 System.err.println("TriangularMatrixParser: reading " + size + " rows");
120 // If the file format is not correct, data will be read
121 // incorrectly. Probably a good way to spot this is the
122 // presence of negative delays, or an end of file.
126 for (int r=0; r < size; r++) {
127 for (int c = r+1; c < size; c++) {
128 int x = (int) (ratio*in.readInt());
130 E2ENetwork.setLatency(r,c,x);
133 System.err.println("TriangularMatrixParser: Read " + count + " entries");
134 } catch (IOException e) {
135 throw new RuntimeException(e.getMessage());