A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
file-helper.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 University of Washington
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU 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 General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mitch Watrous (watrous@u.washington.edu)
18 */
19
20#ifndef FILE_HELPER_H
21#define FILE_HELPER_H
22
23#include "ns3/file-aggregator.h"
24#include "ns3/object-factory.h"
25#include "ns3/probe.h"
26#include "ns3/ptr.h"
27#include "ns3/time-series-adaptor.h"
28
29#include <map>
30#include <string>
31
32namespace ns3
33{
34
35/**
36 * \ingroup stats
37 * \brief Helper class used to put data values into a file.
38 **/
40{
41 public:
42 /**
43 * Constructs a file helper that will create a space separated file
44 * named "file-helper.txt" unless it is later configured otherwise.
45 */
46 FileHelper();
47
48 /**
49 * \param outputFileNameWithoutExtension name of output file to
50 * write with no extension
51 * \param fileType type of file to write.
52 *
53 * Constructs a file helper that will create a file named
54 * outputFileNameWithoutExtension plus possible extra information
55 * from wildcard matches plus ".txt" with values printed as
56 * specified by fileType. The default file type is space-separated.
57 */
58 FileHelper(const std::string& outputFileNameWithoutExtension,
60
61 virtual ~FileHelper();
62
63 /**
64 * \param outputFileNameWithoutExtension name of output file to
65 * write with no extension
66 * \param fileType type of file to write.
67 *
68 * Configures file related parameters for this file helper so that
69 * it will create a file named outputFileNameWithoutExtension plus
70 * possible extra information from wildcard matches plus ".txt" with
71 * values printed as specified by fileType. The default file type
72 * is space-separated.
73 */
74 void ConfigureFile(const std::string& outputFileNameWithoutExtension,
76
77 /**
78 * \param typeId the type ID for the probe used when it is created.
79 * \param path Config path for underlying trace source to be probed
80 * \param probeTraceSource the probe trace source to access.
81 *
82 * Creates output files generated by hooking the ns-3 trace source
83 * with a probe, and then writing the values from the
84 * probeTraceSource. The output file names will have the text stored
85 * in m_outputFileNameWithoutExtension plus ".txt", and will consist
86 * of the 'newValue' at each timestamp.
87 *
88 * This method will create one or more probes according to the TypeId
89 * provided, connect the probe(s) to the trace source specified by
90 * the config path, and hook the probeTraceSource(s) to the downstream
91 * aggregator.
92 *
93 * If the config path has more than one match in the system
94 * (e.g. there is a wildcard), then one output file for each match
95 * will be created. The output file names will contain the text in
96 * m_outputFileNameWithoutExtension plus the matched characters for
97 * each of the wildcards in the config path, separated by dashes,
98 * plus ".txt". For example, if the value in
99 * m_outputFileNameWithoutExtension is the string
100 * "packet-byte-count", and there are two wildcards in the path,
101 * then output file names like "packet-byte-count-0-0.txt" or
102 * "packet-byte-count-12-9.txt" will be possible as names for the
103 * files that will be created.
104 *
105 * A fatal error will result if an unknown probe type is used.
106 */
107 void WriteProbe(const std::string& typeId,
108 const std::string& path,
109 const std::string& probeTraceSource);
110
111 /**
112 * \param adaptorName the timeSeriesAdaptor's name.
113 *
114 * \brief Adds a time series adaptor to be used to write the file.
115 */
116 void AddTimeSeriesAdaptor(const std::string& adaptorName);
117
118 /**
119 * \param aggregatorName the aggregator's name.
120 * \param outputFileName name of the file to write.
121 * \param onlyOneAggregator indicates if more than one aggregator
122 * should be created or not.
123 *
124 * \brief Adds an aggregator to be used to write values to files.
125 */
126 void AddAggregator(const std::string& aggregatorName,
127 const std::string& outputFileName,
128 bool onlyOneAggregator);
129
130 /**
131 * \param probeName the probe's name.
132 * \return Ptr to the probe
133 * \brief Gets the specified probe.
134 */
135 Ptr<Probe> GetProbe(std::string probeName) const;
136
137 /**
138 * \return Ptr to a FileAggregator object
139 * \brief Gets the single aggregator that is always constructed.
140 *
141 * This function is non-const because an aggregator may be lazily
142 * created by this method.
143 */
145
146 /**
147 * \param aggregatorName name for aggregator.
148 * \param outputFileName name of output file to write.
149 * \return Ptr to a FileAggregator object
150 * \brief Gets one of the multiple aggregators from the map.
151 *
152 * This function is non-const because an aggregator may be lazily
153 * created by this method.
154 */
155 Ptr<FileAggregator> GetAggregatorMultiple(const std::string& aggregatorName,
156 const std::string& outputFileName);
157
158 /**
159 * \param heading the heading string.
160 *
161 * \brief Sets the heading string that will be printed on the first
162 * line of the file.
163 *
164 * Note that the heading string will only be printed if it has been
165 * set by calling this function.
166 */
167 void SetHeading(const std::string& heading);
168
169 /**
170 * \param format the 1D format string.
171 *
172 * \brief Sets the 1D format string for the C-style sprintf()
173 * function.
174 */
175 void Set1dFormat(const std::string& format);
176
177 /**
178 * \param format the 2D format string.
179 *
180 * \brief Sets the 2D format string for the C-style sprintf()
181 * function.
182 */
183 void Set2dFormat(const std::string& format);
184
185 /**
186 * \param format the 3D format string.
187 *
188 * \brief Sets the 3D format string for the C-style sprintf()
189 * function.
190 */
191 void Set3dFormat(const std::string& format);
192
193 /**
194 * \param format the 4D format string.
195 *
196 * \brief Sets the 4D format string for the C-style sprintf()
197 * function.
198 */
199 void Set4dFormat(const std::string& format);
200
201 /**
202 * \param format the 5D format string.
203 *
204 * \brief Sets the 5D format string for the C-style sprintf()
205 * function.
206 */
207 void Set5dFormat(const std::string& format);
208
209 /**
210 * \param format the 6D format string.
211 *
212 * \brief Sets the 6D format string for the C-style sprintf()
213 * function.
214 */
215 void Set6dFormat(const std::string& format);
216
217 /**
218 * \param format the 7D format string.
219 *
220 * \brief Sets the 7D format string for the C-style sprintf()
221 * function.
222 */
223 void Set7dFormat(const std::string& format);
224
225 /**
226 * \param format the 8D format string.
227 *
228 * \brief Sets the 8D format string for the C-style sprintf()
229 * function.
230 */
231 void Set8dFormat(const std::string& format);
232
233 /**
234 * \param format the 9D format string.
235 *
236 * \brief Sets the 9D format string for the C-style sprintf()
237 * function.
238 */
239 void Set9dFormat(const std::string& format);
240
241 /**
242 * \param format the 10D format string.
243 *
244 * \brief Sets the 10D format string for the C-style sprintf()
245 * function.
246 */
247 void Set10dFormat(const std::string& format);
248
249 private:
250 /**
251 * \param typeId the type ID for the probe used when it is created.
252 * \param probeName the probe's name.
253 * \param path Config path to access the probe
254 *
255 * \brief Adds a probe to be used to write values to files.
256 */
257 void AddProbe(const std::string& typeId, const std::string& probeName, const std::string& path);
258
259 /**
260 * \param typeId the type ID for the probe used when it is created.
261 * \param matchIdentifier this string is used to make the probe's
262 * context be unique.
263 * \param path Config path to access the probe.
264 * \param probeTraceSource the probe trace source to access.
265 * \param outputFileNameWithoutExtension name of output file to
266 * write with no extension
267 * \param onlyOneAggregator indicates if more than one aggregator
268 * should be created or not.
269 *
270 * \brief Connects the probe to the aggregator.
271 *
272 * A fatal error will result if an unknown probe type is used.
273 */
274 void ConnectProbeToAggregator(const std::string& typeId,
275 const std::string& matchIdentifier,
276 const std::string& path,
277 const std::string& probeTraceSource,
278 const std::string& outputFileNameWithoutExtension,
279 bool onlyOneAggregator);
280
281 /// Used to create the probes and collectors as they are added.
283
284 /// The single aggregator that is always created in the constructor.
286
287 /// Maps aggregator names to aggregators when multiple aggregators
288 /// are needed.
289 std::map<std::string, Ptr<FileAggregator>> m_aggregatorMap;
290
291 /// Maps probe names to probes.
292 std::map<std::string, std::pair<Ptr<Probe>, std::string>> m_probeMap;
293
294 /// Maps time series adaptor names to time series adaptors.
295 std::map<std::string, Ptr<TimeSeriesAdaptor>> m_timeSeriesAdaptorMap;
296
297 /// Number of file probes that have been created.
299
300 /// Determines the kind of file written by the aggregator.
302
303 /// The name of the output file to created without its extension.
305
306 /// Indicates if the heading line for the file has been set.
308
309 /// Heading line for the outputfile.
310 std::string m_heading;
311
312 std::string m_1dFormat; //!< Format string for 1D format C-style sprintf() function.
313 std::string m_2dFormat; //!< Format string for 2D format C-style sprintf() function.
314 std::string m_3dFormat; //!< Format string for 3D format C-style sprintf() function.
315 std::string m_4dFormat; //!< Format string for 4D format C-style sprintf() function.
316 std::string m_5dFormat; //!< Format string for 5D format C-style sprintf() function.
317 std::string m_6dFormat; //!< Format string for 6D format C-style sprintf() function.
318 std::string m_7dFormat; //!< Format string for 7D format C-style sprintf() function.
319 std::string m_8dFormat; //!< Format string for 8D format C-style sprintf() function.
320 std::string m_9dFormat; //!< Format string for 9D format C-style sprintf() function.
321 std::string m_10dFormat; //!< Format string for 10D format C-style sprintf() function.
322
323}; // class FileHelper
324
325} // namespace ns3
326
327#endif // FILE_HELPER_H
FileType
The type of file written by the aggregator.
Helper class used to put data values into a file.
Definition: file-helper.h:40
void Set2dFormat(const std::string &format)
Sets the 2D format string for the C-style sprintf() function.
Definition: file-helper.cc:376
void SetHeading(const std::string &heading)
Sets the heading string that will be printed on the first line of the file.
Definition: file-helper.cc:359
void Set5dFormat(const std::string &format)
Sets the 5D format string for the C-style sprintf() function.
Definition: file-helper.cc:400
void AddTimeSeriesAdaptor(const std::string &adaptorName)
Adds a time series adaptor to be used to write the file.
Definition: file-helper.cc:215
Ptr< FileAggregator > GetAggregatorSingle()
Gets the single aggregator that is always constructed.
Definition: file-helper.cc:305
std::string m_6dFormat
Format string for 6D format C-style sprintf() function.
Definition: file-helper.h:317
std::map< std::string, std::pair< Ptr< Probe >, std::string > > m_probeMap
Maps probe names to probes.
Definition: file-helper.h:292
void Set3dFormat(const std::string &format)
Sets the 3D format string for the C-style sprintf() function.
Definition: file-helper.cc:384
Ptr< Probe > GetProbe(std::string probeName) const
Gets the specified probe.
Definition: file-helper.cc:286
std::map< std::string, Ptr< TimeSeriesAdaptor > > m_timeSeriesAdaptorMap
Maps time series adaptor names to time series adaptors.
Definition: file-helper.h:295
void WriteProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource)
Definition: file-helper.cc:91
std::map< std::string, Ptr< FileAggregator > > m_aggregatorMap
Maps aggregator names to aggregators when multiple aggregators are needed.
Definition: file-helper.h:289
FileHelper()
Constructs a file helper that will create a space separated file named "file-helper....
Definition: file-helper.cc:36
void Set6dFormat(const std::string &format)
Sets the 6D format string for the C-style sprintf() function.
Definition: file-helper.cc:408
ObjectFactory m_factory
Used to create the probes and collectors as they are added.
Definition: file-helper.h:282
uint32_t m_fileProbeCount
Number of file probes that have been created.
Definition: file-helper.h:298
std::string m_heading
Heading line for the outputfile.
Definition: file-helper.h:310
std::string m_outputFileNameWithoutExtension
The name of the output file to created without its extension.
Definition: file-helper.h:304
std::string m_8dFormat
Format string for 8D format C-style sprintf() function.
Definition: file-helper.h:319
std::string m_9dFormat
Format string for 9D format C-style sprintf() function.
Definition: file-helper.h:320
void ConfigureFile(const std::string &outputFileNameWithoutExtension, FileAggregator::FileType fileType=FileAggregator::SPACE_SEPARATED)
Definition: file-helper.cc:69
bool m_hasHeadingBeenSet
Indicates if the heading line for the file has been set.
Definition: file-helper.h:307
std::string m_2dFormat
Format string for 2D format C-style sprintf() function.
Definition: file-helper.h:313
void AddAggregator(const std::string &aggregatorName, const std::string &outputFileName, bool onlyOneAggregator)
Adds an aggregator to be used to write values to files.
Definition: file-helper.cc:236
void Set1dFormat(const std::string &format)
Sets the 1D format string for the C-style sprintf() function.
Definition: file-helper.cc:368
void AddProbe(const std::string &typeId, const std::string &probeName, const std::string &path)
Adds a probe to be used to write values to files.
Definition: file-helper.cc:179
std::string m_1dFormat
Format string for 1D format C-style sprintf() function.
Definition: file-helper.h:312
std::string m_10dFormat
Format string for 10D format C-style sprintf() function.
Definition: file-helper.h:321
std::string m_4dFormat
Format string for 4D format C-style sprintf() function.
Definition: file-helper.h:315
std::string m_7dFormat
Format string for 7D format C-style sprintf() function.
Definition: file-helper.h:318
void Set4dFormat(const std::string &format)
Sets the 4D format string for the C-style sprintf() function.
Definition: file-helper.cc:392
void Set7dFormat(const std::string &format)
Sets the 7D format string for the C-style sprintf() function.
Definition: file-helper.cc:416
FileAggregator::FileType m_fileType
Determines the kind of file written by the aggregator.
Definition: file-helper.h:301
Ptr< FileAggregator > m_aggregator
The single aggregator that is always created in the constructor.
Definition: file-helper.h:285
virtual ~FileHelper()
Definition: file-helper.cc:63
std::string m_5dFormat
Format string for 5D format C-style sprintf() function.
Definition: file-helper.h:316
std::string m_3dFormat
Format string for 3D format C-style sprintf() function.
Definition: file-helper.h:314
void Set8dFormat(const std::string &format)
Sets the 8D format string for the C-style sprintf() function.
Definition: file-helper.cc:424
Ptr< FileAggregator > GetAggregatorMultiple(const std::string &aggregatorName, const std::string &outputFileName)
Gets one of the multiple aggregators from the map.
Definition: file-helper.cc:339
void Set10dFormat(const std::string &format)
Sets the 10D format string for the C-style sprintf() function.
Definition: file-helper.cc:440
void ConnectProbeToAggregator(const std::string &typeId, const std::string &matchIdentifier, const std::string &path, const std::string &probeTraceSource, const std::string &outputFileNameWithoutExtension, bool onlyOneAggregator)
Connects the probe to the aggregator.
Definition: file-helper.cc:448
void Set9dFormat(const std::string &format)
Sets the 9D format string for the C-style sprintf() function.
Definition: file-helper.cc:432
Instantiate subclasses of ns3::Object.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Every class exported by the ns3 library is enclosed in the ns3 namespace.