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.config;
23 import peersim.cdsim.*;
24 import peersim.edsim.*;
25 import peersim.util.*;
29 * This is utility tool that checks whether a config file can be loaded
30 * or not, without actually performing the simulation. All the error
31 * messages generated by controls and protocols when initialized are
32 * reported. This is useful to check all the configuration files in a
35 public class CheckConfig {
37 //========================== parameters ================================
38 //======================================================================
41 * This is the prefix of the config properties whose value vary during
42 * a set of experiments.
45 private static final String PAR_RANGE = "range";
48 // ========================== static constants ==========================
49 // ======================================================================
51 /** {@link CDSimulator} */
52 protected static final int CDSIM = 0;
54 /** {@link EDSimulator} */
55 protected static final int EDSIM = 1;
57 protected static final int UNKNOWN = -1;
59 /** the class names of simulators used */
60 protected static final String[] simName = {
61 CDSimulator.class.getCanonicalName(),
62 EDSimulator.class.getCanonicalName(),
68 // ========================== methods ===================================
69 // ======================================================================
72 * Returns the numeric id of the simulator to invoke. At the moment this can
73 * be {@link #CDSIM}, {@link #EDSIM} or {@link #UNKNOWN}.
75 protected static int getSimID() {
77 if( CDSimulator.isConfigurationCycleDriven())
81 else if( EDSimulator.isConfigurationEventDriven() )
88 // ----------------------------------------------------------------------
91 * Loads the configuration and checks the configuration files against
92 * simple configuration errors, such as missing classes, missing
93 * parameters or syntax errors.
95 * The goal is to provide a mechanism to test a configuration file,
96 * without having to perform the actual simulations (that could be
97 * time-consuming) and without necessarily blocking after the first
98 * error encountered. It may be useful, for example, when a major
99 * refactoring of your code requires a thorough check on all your
100 * configuration files.
102 * Loading the configuration is currently done with the help of
103 * constructing an instance of {@link ParsedProperties} using the
104 * constructor {@link ParsedProperties#ParsedProperties(String[])},
105 * in the same way as the normal simulator.
107 * After loading the configuration, the collection of nodes forming a
108 * Network is instantiated, together with all protocols forming a
109 * node. Initialization controls are executed, and then the simulation
112 * For each error encountered, a message is printed ons standard error,
113 * and the initialization keeps going without interruption. If multiple
114 * errors are present, an error message for each of them is printed.
115 * Apart from errors, default choices are also printed as warnings, to
116 * allow developers to spot subtle configuration errors such as missing
117 * parameters that defaults to standard values.
119 * @param args passed on to
120 * {@link ParsedProperties#ParsedProperties(String[])}
122 public static void main(String[] args)
125 System.setErr(new NullPrintStream());
126 Properties prop = new ParsedProperties(args);
127 Configuration.setConfig( prop, true );
130 final int SIMID = getSimID();
131 if( SIMID == UNKNOWN )
134 "Simulator: unable to identify configuration, exiting.");
140 // XXX could be done through reflection, but
141 // this is easier to read.
145 // Set cycles to 0, so no simulation is ever performed.
146 prop.setProperty(CDSimulator.PAR_CYCLES, "0");
147 CDSimulator.nextExperiment();
150 // Set endtime to 0, so no simulation is ever performed.
151 prop.setProperty(EDSimulator.PAR_ENDTIME, "0");
152 EDSimulator.nextExperiment();
156 } catch (MissingParameterException e) {
157 System.out.println(e.getMessage());
159 } catch (IllegalParameterException e) {
160 System.out.println(e.getMessage());
166 * Parses a collection of range specifications, identifies the set
167 * of parameters that will change during the simulation and
168 * instantiates them with the first value of their ranges.
170 private static void parseRanges(Properties prop)
173 String[] ranges = Configuration.getNames(PAR_RANGE);
175 for (int i = 0; i < ranges.length; i++) {
176 String[] array = Configuration.getString(ranges[i]).split(";");
177 if (array.length != 2) {
178 throw new IllegalParameterException(ranges[i],
179 " should be formatted as <parameter>;<value list>");
181 String[] values = StringListParser.parseList(array[1]);
182 prop.setProperty(array[0], values[0]);