A Discrete-Event Network Simulator
API
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 
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 
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 
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 {
171 
172  return 0;
173 }
void Enable(void)
Set the status of an individual object.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
void Set2dFormat(const std::string &format)
Sets the 2D format string for the C-style sprintf() function.
STL namespace.
void Write2d(std::string context, double v1, double v2)
Writes 2 values to the file.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Disable(void)
Unset the status of an individual object.