Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / transport / TriangularMatrixParser.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18
19 package peersim.transport;
20
21 import java.io.*;
22
23 import peersim.config.*;
24 import peersim.core.*;
25
26 /**
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.
31  * 
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 
35  * matrix" (see this 
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.
38  * 
39  * @author Alberto Montresor
40  * @version $Revision: 1.4 $
41  */
42 public class TriangularMatrixParser implements Control
43 {
44
45 // ---------------------------------------------------------------------
46 // Parameters
47 // ---------------------------------------------------------------------
48
49 /**
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.
55  * @config
56  */
57 private static final String PAR_FILE = "file";
58
59 /**
60  * The ratio between the time units used in the configuration file and the
61  * time units used in the Peersim simulator.
62  * @config
63  */
64 private static final String PAR_RATIO = "ratio";
65
66 // ---------------------------------------------------------------------
67 // Fields
68 // ---------------------------------------------------------------------
69
70 /** Name of the file containing the measurements. */
71 private String filename;
72
73 /** Ratio read from PAR_RATIO */
74 private double ratio;
75
76 // ---------------------------------------------------------------------
77 // Initialization
78 // ---------------------------------------------------------------------
79
80 /**
81  * Read the configuration parameters.
82  */
83 public TriangularMatrixParser(String prefix)
84 {
85         filename = Configuration.getString(prefix + "." + PAR_FILE);
86         ratio = Configuration.getDouble(prefix + "." + PAR_RATIO);
87 }
88
89 // ---------------------------------------------------------------------
90 // Methods
91 // ---------------------------------------------------------------------
92
93 /**
94  * Initializes static singleton {@link E2ENetwork} by reading a king data set.
95 * @return  always false
96 */
97 public boolean execute()
98 {
99         try {
100                 ObjectInputStream in = null;
101                 try {
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");
111                 }
112         
113                 // Read the number of nodes in the file (first four bytes).
114           int size = in.readInt();
115           
116                 // Reset the E2E network
117                 E2ENetwork.reset(size, true);
118                 System.err.println("TriangularMatrixParser: reading " + size + " rows");
119         
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.
123         
124                 // Reading data
125                 int count = 0;
126                 for (int r=0; r < size; r++) {
127                         for (int c = r+1; c < size; c++) {
128                                 int x = (int) (ratio*in.readInt());
129                                 count++;
130                                 E2ENetwork.setLatency(r,c,x);
131                         }
132                 }
133                 System.err.println("TriangularMatrixParser: Read " + count + " entries");
134         } catch (IOException e) {
135                 throw new RuntimeException(e.getMessage());
136         }
137         return false;
138 }
139
140 }