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.vector;
23 import peersim.config.*;
24 import peersim.core.*;
27 * Initializes a protocol vector from data read from a file.
28 * The file format is as follows:
29 * lines starting with # or lines that contain only
30 * whitespace are ignored.
31 * From the rest of the lines the first field separated by whitespace is
32 * read. Only the first field is read from each line, the rest of the line
34 * The file can contain more values than necessary but
35 * enough values must be present.
39 public class InitVectFromFile extends VectControl
42 // --------------------------------------------------------------------------
44 // --------------------------------------------------------------------------
47 * The filename to load links from.
50 private static final String PAR_FILE = "file";
52 // --------------------------------------------------------------------------
54 // --------------------------------------------------------------------------
56 /** The file to be read */
57 private final String file;
59 // --------------------------------------------------------------------------
61 // --------------------------------------------------------------------------
64 * Standard constructor that reads the configuration parameters.
65 * Invoked by the simulation engine.
66 * @param prefix the configuration prefix for this class
68 public InitVectFromFile(String prefix)
71 file = Configuration.getString(prefix + "." + PAR_FILE);
74 // --------------------------------------------------------------------------
76 // --------------------------------------------------------------------------
79 * Initializes values from a file.
80 * The file format is as follows:
81 * lines starting with # or lines that contain only
82 * whitespace are ignored.
83 * From the rest of the lines the first field separated by whitespace is
84 * read. Only the first field is read from each line, the rest of the line
86 * The file can contain more values than necessary but
87 * enough values must be present.
88 * @throws RuntimeException if the file cannot be read or contains too few
90 * @return always false
92 public boolean execute() {
97 FileReader fr = new FileReader(file);
98 LineNumberReader lnr = new LineNumberReader(fr);
100 while ((line = lnr.readLine()) != null && i < Network.size()) {
101 if (line.startsWith("#"))
103 StringTokenizer st = new StringTokenizer(line);
104 if (!st.hasMoreTokens())
106 if( setter.isInteger() )
107 setter.set(i,Long.parseLong(st.nextToken()));
108 else setter.set(i,Double.parseDouble(st.nextToken()));
115 throw new RuntimeException("Unable to read file: " + e);
118 if (i < Network.size())
119 throw new RuntimeException(
120 "Too few values in file '" + file + "' (only "
121 + i + "); we need " + Network.size() + ".");
126 // --------------------------------------------------------------------------