Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / vector / ValueDumper.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.vector;
20
21 import java.io.*;
22
23 import peersim.config.*;
24 import peersim.core.*;
25 import peersim.util.*;
26
27 /**
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".
33  * <p>
34  * This observer class can observe any protocol field containing a 
35  * primitive value, provided that the field is associated with a getter method 
36  * that reads it.
37  * @see VectControl
38  * @see peersim.vector
39  */
40 public class ValueDumper extends VectControl {
41
42
43 // --------------------------------------------------------------------------
44 // Parameter names
45 // --------------------------------------------------------------------------
46
47 /**
48  * This is the base name of the file where the values are saved. The full name
49  * will be baseName+cycleid+".vec".
50  * @config
51  */
52 private static final String PAR_BASENAME = "outf";
53
54 // --------------------------------------------------------------------------
55 // Fields
56 // --------------------------------------------------------------------------
57
58 /** Prefix name of this observer */
59 private final String prefix;
60
61 /** Base name of the file to be written */
62 private final String baseName;
63
64 private final FileNameGenerator fng;
65
66 // --------------------------------------------------------------------------
67 // Constructor
68 // --------------------------------------------------------------------------
69
70 /**
71  * Standard constructor that reads the configuration parameters.
72  * Invoked by the simulation engine.
73  * @param prefix the configuration prefix for this class
74  */
75 public ValueDumper(String prefix) {
76
77         super(prefix);
78         this.prefix = prefix;
79         baseName = Configuration.getString(prefix + "." + PAR_BASENAME, null);
80         if(baseName!=null) fng = new FileNameGenerator(baseName,".vec");
81         else fng = null;
82 }
83
84 // --------------------------------------------------------------------------
85 // Methods
86 // --------------------------------------------------------------------------
87
88 /**
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
96  */
97 public boolean execute() {
98 try
99 {
100         System.out.print(prefix + ": ");
101         
102         // initialize output streams
103         if (baseName != null)
104         {
105                 String filename = fng.nextCounterName();
106                 System.out.println("writing "+filename);
107                 PrintStream pstr =
108                         new PrintStream(new FileOutputStream(filename));
109                 for (int i = 0; i < Network.size(); ++i)
110                 {
111                         pstr.println(getter.get(i));
112                 }
113                 pstr.close();
114         }
115         else
116         {
117                 System.out.println();
118                 for (int i = 0; i < Network.size(); ++i)
119                 {
120                         System.out.println(getter.get(i));
121                 }
122         }
123 }
124 catch (IOException e)
125 {
126         throw new RuntimeException(prefix + ": Unable to write to file: " + e);
127 }
128
129         return false;
130 }
131
132 // ---------------------------------------------------------------------
133
134 }