A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
37 /*
38  * This is our test object, an object that increments counters at
39  * various times and emits one of them as a trace source.
40  */
41 class Emitter : public Object
42 {
43 public:
44  static TypeId GetTypeId (void);
45  Emitter ();
46 private:
47  void DoInitialize (void);
48  void Emit (void);
49  void Count (void);
50 
51  TracedValue<double> m_counter; // normally this would be integer type
53 
54 };
55 
57  ;
58 
59 TypeId
60 Emitter::GetTypeId (void)
61 {
62  static TypeId tid = TypeId ("ns3::Emitter")
64  .SetParent<Object> ()
65  .AddTraceSource ("Counter",
66  "sample counter",
68  ;
69  return tid;
70 }
71 
72 Emitter::Emitter (void)
73 {
74  NS_LOG_FUNCTION (this);
75  m_counter = 0;
76  m_var = CreateObject<ExponentialRandomVariable> ();
77 }
78 
79 void
81 {
82  NS_LOG_FUNCTION (this);
83  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
84  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
85 }
86 
87 void
88 Emitter::Emit (void)
89 {
90  NS_LOG_FUNCTION (this);
91  NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ());
92  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
93 }
94 
95 void
96 Emitter::Count (void)
97 {
98  NS_LOG_FUNCTION (this);
99  NS_LOG_DEBUG ("Counting at " << Simulator::Now ().GetSeconds ());
100  m_counter += 1.0;
101  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
102 }
103 
104 int main (int argc, char *argv[])
105 {
106  CommandLine cmd;
107  cmd.Parse (argc, argv);
108 
109  //
110  // This Emitter has a trace source object that will emit values at
111  // random times.
112  //
113 
114  Ptr<Emitter> emitter = CreateObject<Emitter> ();
115  Names::Add ("/Names/Emitter", emitter);
116 
117  //
118  // This Probe will be hooked to the Emitter's trace source object by
119  // accessing it by path name in the Config database.
120  //
121 
122  Ptr<DoubleProbe> probe = CreateObject<DoubleProbe> ();
123  probe->SetName ("PathProbe");
124  Names::Add ("/Names/Probe", probe);
125 
126  // Note, no return value is checked here.
127  probe->ConnectByPath ("/Names/Emitter/Counter");
128 
129  //
130  // This file helper will be used to put data values into a file.
131  //
132 
133  // Create the file helper.
134  FileHelper fileHelper;
135 
136  // Configure the file to be written.
137  fileHelper.ConfigureFile ("file-helper-example",
139 
140  // Set the labels for this formatted output file.
141  fileHelper.Set2dFormat ("Time (Seconds) = %.3e\tCount = %.0f");
142 
143  // Write the values generated by the probe. The path that we
144  // provide helps to disambiguate the source of the trace.
145  fileHelper.WriteProbe ("ns3::DoubleProbe",
146  "/Names/Probe/Output",
147  "Output");
148 
149  // The Emitter object is not associated with an ns-3 node, so
150  // it won't get started automatically, so we need to do this ourselves
151  Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter);
152 
153  Simulator::Stop (Seconds (100.0));
154  Simulator::Run ();
156 
157  return 0;
158 }
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
TypeId AddConstructor(void)
Definition: type-id.h:418
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
static TypeId GetTypeId(void)
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr obj.
Definition: names.cc:616
virtual void ConnectByPath(std::string path)
connect to a trace source provided by a config path
Definition: double-probe.cc:94
int main(int argc, char *argv[])
Helper class used to put data values into a file.
Definition: file-helper.h:38
Parse command-line arguments.
Definition: command-line.h:152
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void Count(void)
void DoInitialize(void)
This method is called only once by Object::Initialize.
TracedValue< double > m_counter
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
void ConfigureFile(const std::string &outputFileNameWithoutExtension, enum FileAggregator::FileType fileType=FileAggregator::SPACE_SEPARATED)
Definition: file-helper.cc:69
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
void SetName(std::string name)
Set the object's name. All spaces are replaced by underscores.
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
void Initialize(void)
This method calls the virtual DoInitialize method on all the objects aggregated to this object...
Definition: object.cc:180
void WriteProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource)
Definition: file-helper.cc:91
void Parse(int argc, char *argv[])
Parse the program arguments.
a base class which provides memory management and object aggregation
Definition: object.h:63
a unique identifier for an interface.
Definition: type-id.h:49
void Emit(void)
void Set2dFormat(const std::string &format)
Sets the 2D format string for the C-style sprintf() function.
Definition: file-helper.cc:380