A Discrete-Event Network Simulator
API
file-aggregator-example.cc
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 #include "ns3/core-module.h"
21 #include "ns3/stats-module.h"
22 
23 using namespace ns3;
24 
25 namespace
26 {
27 
32 void
34 {
35  std::string fileName = "file-aggregator-comma-separated.txt";
36  std::string datasetContext = "Dataset/Context/String";
37 
38  // Create an aggregator.
39  Ptr<FileAggregator> aggregator =
40  CreateObject<FileAggregator>(fileName, FileAggregator::COMMA_SEPARATED);
41 
42  // aggregator must be turned on
43  aggregator->Enable();
44 
45  double time;
46  double value;
47 
48  // Create the 2-D dataset.
49  for (time = -5.0; time <= +5.0; time += 1.0)
50  {
51  // Calculate the 2-D curve
52  //
53  // 2
54  // value = time .
55  //
56  value = time * time;
57 
58  // Add this point to the plot.
59  aggregator->Write2d(datasetContext, time, value);
60  }
61 
62  // Disable logging of data for the aggregator.
63  aggregator->Disable();
64 }
65 
70 void
72 {
73  std::string fileName = "file-aggregator-space-separated.txt";
74  std::string datasetContext = "Dataset/Context/String";
75 
76  // Create an aggregator. Note that the default type is space
77  // separated.
78  Ptr<FileAggregator> aggregator = CreateObject<FileAggregator>(fileName);
79 
80  // aggregator must be turned on
81  aggregator->Enable();
82 
83  double time;
84  double value;
85 
86  // Create the 2-D dataset.
87  for (time = -5.0; time <= +5.0; time += 1.0)
88  {
89  // Calculate the 2-D curve
90  //
91  // 2
92  // value = time .
93  //
94  value = time * time;
95 
96  // Add this point to the plot.
97  aggregator->Write2d(datasetContext, time, value);
98  }
99 
100  // Disable logging of data for the aggregator.
101  aggregator->Disable();
102 }
103 
107 void
109 {
110  std::string fileName = "file-aggregator-formatted-values.txt";
111  std::string datasetContext = "Dataset/Context/String";
112 
113  // Create an aggregator that will have formatted values.
114  Ptr<FileAggregator> aggregator =
115  CreateObject<FileAggregator>(fileName, FileAggregator::FORMATTED);
116 
117  // Set the format for the values.
118  aggregator->Set2dFormat("Time = %.3e\tValue = %.0f");
119 
120  // aggregator must be turned on
121  aggregator->Enable();
122 
123  double time;
124  double value;
125 
126  // Create the 2-D dataset.
127  for (time = -5.0; time < 5.5; time += 1.0)
128  {
129  // Calculate the 2-D curve
130  //
131  // 2
132  // value = time .
133  //
134  value = time * time;
135 
136  // Add this point to the plot.
137  aggregator->Write2d(datasetContext, time, value);
138  }
139 
140  // Disable logging of data for the aggregator.
141  aggregator->Disable();
142 }
143 
144 } // unnamed namespace
145 
146 int
147 main(int argc, char* argv[])
148 {
152 
153  return 0;
154 }
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
void CreateCommaSeparatedFile()
This function creates a file with 2 columns of values and separated by commas.
void CreateFormattedFile()
This function creates a file with formatted values.
void CreateSpaceSeparatedFile()
This function creates a file with 2 columns of values and separated by commas.
Every class exported by the ns3 library is enclosed in the ns3 namespace.