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