Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / transport / KingParser.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 import java.util.*;
23 import peersim.config.*;
24 import peersim.core.Control;
25
26 /**
27  * Initializes static singleton {@link E2ENetwork} by reading a king data set.
28  * 
29  * @author Alberto Montresor
30  * @version $Revision: 1.9 $
31  */
32 public class KingParser implements Control
33 {
34
35 // ---------------------------------------------------------------------
36 // Parameters
37 // ---------------------------------------------------------------------
38
39 /**
40  * The file containing the King measurements.
41  * @config
42  */
43 private static final String PAR_FILE = "file";
44
45 /**
46  * The ratio between the time units used in the configuration file and the
47  * time units used in the Peersim simulator.
48  * @config
49  */
50 private static final String PAR_RATIO = "ratio";
51
52 // ---------------------------------------------------------------------
53 // Fields
54 // ---------------------------------------------------------------------
55
56 /** Name of the file containing the King measurements. */
57 private String filename;
58
59 /**
60  * Ratio between the time units used in the configuration file and the time
61  * units used in the Peersim simulator.
62  */
63 private double ratio;
64
65 /** Prefix for reading parameters */
66 private String prefix;
67
68 // ---------------------------------------------------------------------
69 // Initialization
70 // ---------------------------------------------------------------------
71
72 /**
73  * Read the configuration parameters.
74  */
75 public KingParser(String prefix)
76 {
77         this.prefix = prefix;
78         ratio = Configuration.getDouble(prefix + "." + PAR_RATIO, 1);
79         filename = Configuration.getString(prefix + "." + PAR_FILE, null);
80 }
81
82 // ---------------------------------------------------------------------
83 // Methods
84 // ---------------------------------------------------------------------
85
86 /**
87  * Initializes static singleton {@link E2ENetwork} by reading a king data set.
88 * @return  always false
89 */
90 public boolean execute()
91 {
92         BufferedReader in = null;
93         if (filename != null) {
94                 try {
95                         in = new BufferedReader(new FileReader(filename));
96                 } catch (FileNotFoundException e) {
97                         throw new IllegalParameterException(prefix + "." + PAR_FILE, filename
98                                         + " does not exist");
99                 }
100         } else {
101                 in = new BufferedReader( new InputStreamReader(
102                                                 ClassLoader.getSystemResourceAsStream("t-king.map")
103                                         )       );
104         }
105                 
106         // XXX If the file format is not correct, we will get quite obscure
107         // exceptions. To be improved.
108
109         String line = null;
110         // Skip initial lines
111         int size = 0;
112         int lc = 1;
113         try {
114                 while ((line = in.readLine()) != null && !line.startsWith("node")) lc++;
115                 while (line != null && line.startsWith("node")) {
116                         size++;
117                         lc++;
118                         line = in.readLine();
119                 }
120         } catch (IOException e) {
121                 System.err.println("KingParser: " + filename + ", line " + lc + ":");
122                 e.printStackTrace();
123                 try { in.close(); } catch (IOException e1) { };
124                 System.exit(1);
125         }
126         E2ENetwork.reset(size, true);
127         if (line == null) {
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) { };
131                 System.exit(1);
132         }
133         
134         System.err.println("KingParser: read " + size + " entries");
135         
136         try {
137                 do {
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) { };
143                                 System.exit(1);
144                         }
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);
149                         lc++;
150                         line = in.readLine();
151                 } while (line != null);
152                 
153                 in.close();
154         
155         } catch (IOException e) {
156                 System.err.println("KingParser: " + filename + ", line " + lc + ":");
157                 e.printStackTrace();
158                 try { in.close(); } catch (IOException e1) { };
159                 System.exit(1);
160         }
161
162
163         return false;
164 }
165
166 }