A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
stats
examples
file-aggregator-example.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2013 University of Washington
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Mitch Watrous (watrous@u.washington.edu)
19
*/
20
21
#include "ns3/core-module.h"
22
#include "ns3/stats-module.h"
23
24
using namespace
ns3
;
25
26
namespace
{
27
28
//===========================================================================
29
// Function: CreateCommaSeparatedFile
30
//
31
//
32
// This function creates a file with 2 columns of values and separated
33
// by commas.
34
//===========================================================================
35
36
void
CreateCommaSeparatedFile
()
37
{
38
using namespace
std
;
39
40
string
fileName =
"file-aggregator-comma-separated.txt"
;
41
string
datasetContext =
"Dataset/Context/String"
;
42
43
// Create an aggregator.
44
Ptr<FileAggregator>
aggregator =
45
CreateObject<FileAggregator> (fileName,
FileAggregator::COMMA_SEPARATED
);
46
47
// aggregator must be turned on
48
aggregator->
Enable
();
49
50
double
time;
51
double
value;
52
53
// Create the 2-D dataset.
54
for
(time = -5.0; time <= +5.0; time += 1.0)
55
{
56
// Calculate the 2-D curve
57
//
58
// 2
59
// value = time .
60
//
61
value = time * time;
62
63
// Add this point to the plot.
64
aggregator->
Write2d
(datasetContext, time, value);
65
}
66
67
// Disable logging of data for the aggregator.
68
aggregator->
Disable
();
69
}
70
71
72
//===========================================================================
73
// Function: CreateSpaceSeparatedFile
74
//
75
//
76
// This function creates a file with 2 columns of values and separated
77
// by spaces.
78
//===========================================================================
79
80
void
CreateSpaceSeparatedFile
()
81
{
82
using namespace
std
;
83
84
string
fileName =
"file-aggregator-space-separated.txt"
;
85
string
datasetContext =
"Dataset/Context/String"
;
86
87
// Create an aggregator. Note that the default type is space
88
// separated.
89
Ptr<FileAggregator>
aggregator =
90
CreateObject<FileAggregator> (fileName);
91
92
// aggregator must be turned on
93
aggregator->
Enable
();
94
95
double
time;
96
double
value;
97
98
// Create the 2-D dataset.
99
for
(time = -5.0; time <= +5.0; time += 1.0)
100
{
101
// Calculate the 2-D curve
102
//
103
// 2
104
// value = time .
105
//
106
value = time * time;
107
108
// Add this point to the plot.
109
aggregator->
Write2d
(datasetContext, time, value);
110
}
111
112
// Disable logging of data for the aggregator.
113
aggregator->
Disable
();
114
}
115
116
117
//===========================================================================
118
// Function: CreateFormattedFile
119
//
120
//
121
// This function creates a file with formatted values.
122
//===========================================================================
123
124
void
CreateFormattedFile
()
125
{
126
using namespace
std
;
127
128
string
fileName =
"file-aggregator-formatted-values.txt"
;
129
string
datasetContext =
"Dataset/Context/String"
;
130
131
// Create an aggregator that will have formatted values.
132
Ptr<FileAggregator>
aggregator =
133
CreateObject<FileAggregator> (fileName,
FileAggregator::FORMATTED
);
134
135
// Set the format for the values.
136
aggregator->
Set2dFormat
(
"Time = %.3e\tValue = %.0f"
);
137
138
// aggregator must be turned on
139
aggregator->
Enable
();
140
141
double
time;
142
double
value;
143
144
// Create the 2-D dataset.
145
for
(time = -5.0; time < 5.5; time += 1.0)
146
{
147
// Calculate the 2-D curve
148
//
149
// 2
150
// value = time .
151
//
152
value = time * time;
153
154
// Add this point to the plot.
155
aggregator->
Write2d
(datasetContext, time, value);
156
}
157
158
// Disable logging of data for the aggregator.
159
aggregator->
Disable
();
160
}
161
162
163
}
// unnamed namespace
164
165
166
int
main (
int
argc,
char
*argv[])
167
{
168
CreateCommaSeparatedFile
();
169
CreateSpaceSeparatedFile
();
170
CreateFormattedFile
();
171
172
return
0;
173
}
ns3::DataCollectionObject::Enable
void Enable(void)
Set the status of an individual object.
Definition:
data-collection-object.cc:84
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:73
ns3::FileAggregator::FORMATTED
Definition:
file-aggregator.h:45
ns3::FileAggregator::Set2dFormat
void Set2dFormat(const std::string &format)
Sets the 2D format string for the C-style sprintf() function.
Definition:
file-aggregator.cc:117
std
STL namespace.
ns3::FileAggregator::COMMA_SEPARATED
Definition:
file-aggregator.h:47
ns3::FileAggregator::Write2d
void Write2d(std::string context, double v1, double v2)
Writes 2 values to the file.
Definition:
file-aggregator.cc:218
anonymous_namespace{file-aggregator-example.cc}::CreateFormattedFile
void CreateFormattedFile()
Definition:
file-aggregator-example.cc:124
anonymous_namespace{file-aggregator-example.cc}::CreateSpaceSeparatedFile
void CreateSpaceSeparatedFile()
Definition:
file-aggregator-example.cc:80
anonymous_namespace{file-aggregator-example.cc}::CreateCommaSeparatedFile
void CreateCommaSeparatedFile()
Definition:
file-aggregator-example.cc:36
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::DataCollectionObject::Disable
void Disable(void)
Unset the status of an individual object.
Definition:
data-collection-object.cc:91
Generated on Wed Nov 7 2018 10:02:10 for ns-3 by
1.8.14