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.*;
25 import peersim.util.*;
28 * Dump the content of a vector in a file. Each line
29 * represent a single node.
30 * Values are dumped to a file whose name is obtained from a
31 * configurable prefix (set by {@value #PAR_BASENAME}), a number that is
32 * increased before each dump by one, and the extension ".vec".
34 * This observer class can observe any protocol field containing a
35 * primitive value, provided that the field is associated with a getter method
40 public class ValueDumper extends VectControl {
43 // --------------------------------------------------------------------------
45 // --------------------------------------------------------------------------
48 * This is the base name of the file where the values are saved. The full name
49 * will be baseName+cycleid+".vec".
52 private static final String PAR_BASENAME = "outf";
54 // --------------------------------------------------------------------------
56 // --------------------------------------------------------------------------
58 /** Prefix name of this observer */
59 private final String prefix;
61 /** Base name of the file to be written */
62 private final String baseName;
64 private final FileNameGenerator fng;
66 // --------------------------------------------------------------------------
68 // --------------------------------------------------------------------------
71 * Standard constructor that reads the configuration parameters.
72 * Invoked by the simulation engine.
73 * @param prefix the configuration prefix for this class
75 public ValueDumper(String prefix) {
79 baseName = Configuration.getString(prefix + "." + PAR_BASENAME, null);
80 if(baseName!=null) fng = new FileNameGenerator(baseName,".vec");
84 // --------------------------------------------------------------------------
86 // --------------------------------------------------------------------------
89 * Dump the content of a vector in a file. Each line
90 * represent a single node.
91 * Values are dumped to a file whose name is obtained from a
92 * configurable prefix (set by {@value #PAR_BASENAME}), a number that is
93 * increased before each dump by one, and the extension ".vec".
94 * @return always false
95 * @throws RuntimeException if there is an I/O problem
97 public boolean execute() {
100 System.out.print(prefix + ": ");
102 // initialize output streams
103 if (baseName != null)
105 String filename = fng.nextCounterName();
106 System.out.println("writing "+filename);
108 new PrintStream(new FileOutputStream(filename));
109 for (int i = 0; i < Network.size(); ++i)
111 pstr.println(getter.get(i));
117 System.out.println();
118 for (int i = 0; i < Network.size(); ++i)
120 System.out.println(getter.get(i));
124 catch (IOException e)
126 throw new RuntimeException(prefix + ": Unable to write to file: " + e);
132 // ---------------------------------------------------------------------