A Discrete-Event Network Simulator
API
file-helper-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  * This is based on double-probe-example.cc.
18  *
19  * Author: Mitch Watrous (watrous@u.washington.edu)
20  */
21 
22 /*
23  * This example is designed to show the main features of an
24  * ns3::FileHelper.
25  */
26 
27 #include "ns3/core-module.h"
28 #include "ns3/stats-module.h"
29 
30 #include <string>
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE("FileHelperExample");
35 
41 class Emitter : public Object
42 {
43  public:
48  static TypeId GetTypeId();
50 
51  private:
52  void DoInitialize() override;
54  void Count();
55 
58 };
59 
61 
62 TypeId
64 {
65  static TypeId tid = TypeId("ns3::Emitter")
66  .SetParent<Object>()
67  .SetGroupName("Stats")
68  .AddConstructor<Emitter>()
69  .AddTraceSource("Counter",
70  "sample counter",
72  "ns3::TracedValueCallback::Double");
73  return tid;
74 }
75 
77 {
78  NS_LOG_FUNCTION(this);
79  m_counter = 0;
80  m_var = CreateObject<ExponentialRandomVariable>();
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION(this);
87  Simulator::Schedule(Seconds(m_var->GetValue()), &Emitter::Count, this);
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION(this);
94  NS_LOG_DEBUG("Counting at " << Simulator::Now().As(Time::S));
95  m_counter += 1.0;
96  Simulator::Schedule(Seconds(m_var->GetValue()), &Emitter::Count, this);
97 }
98 
99 int
100 main(int argc, char* argv[])
101 {
102  CommandLine cmd(__FILE__);
103  cmd.Parse(argc, argv);
104 
105  //
106  // This Emitter has a trace source object that will emit values at
107  // random times.
108  //
109 
110  Ptr<Emitter> emitter = CreateObject<Emitter>();
111  Names::Add("/Names/Emitter", emitter);
112 
113  //
114  // This file helper will be used to put data values into a file.
115  //
116 
117  // Create the file helper.
118  FileHelper fileHelper;
119 
120  // Configure the file to be written.
121  fileHelper.ConfigureFile("file-helper-example", FileAggregator::FORMATTED);
122 
123  // Set the labels for this formatted output file.
124  fileHelper.Set2dFormat("Time (Seconds) = %.3f\tCount = %.0f");
125 
126  // Write the values generated by the probe. The path that we
127  // provide helps to disambiguate the source of the trace.
128  fileHelper.WriteProbe("ns3::Uinteger32Probe", "/Names/Emitter/Counter", "Output");
129 
130  // The Emitter object is not associated with an ns-3 node, so
131  // it won't get started automatically, so we need to do this ourselves
133 
134  Simulator::Stop(Seconds(100.0));
135  Simulator::Run();
137 
138  return 0;
139 }
This is our test object, an object that increments counters at various times and emits one of them as...
void DoInitialize() override
Initialize() implementation.
static TypeId GetTypeId()
Register this type.
TracedValue< uint32_t > m_counter
Simple counter.
TracedValue< double > m_counter
Sample counter, normally this would be integer type.
static TypeId GetTypeId()
Register this type.
void Count()
Counts how many time this function is called.
Parse command-line arguments.
Definition: command-line.h:232
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 WriteProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource)
Definition: file-helper.cc:91
void ConfigureFile(const std::string &outputFileNameWithoutExtension, FileAggregator::FileType fileType=FileAggregator::SPACE_SEPARATED)
Definition: file-helper.cc:69
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:775
A base class which provides memory management and object aggregation.
Definition: object.h:89
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:186
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
@ S
second
Definition: nstime.h:116
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40