A Discrete-Event Network Simulator
API
double-probe-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 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  */
19 
20 /*
21  * This example is designed to show the main features of an
22  * ns3::DoubleProbe.
23  */
24 
25 #include <string>
26 
27 #include "ns3/core-module.h"
28 #include "ns3/double-probe.h"
29 
30 using namespace ns3;
31 
32 NS_LOG_COMPONENT_DEFINE ("DoubleProbeExample");
33 
34 /*
35  * This is our test object, an object that increments counters at
36  * various times and emits one of them as a trace source.
37  */
38 class Emitter : public Object
39 {
40 public:
41  static TypeId GetTypeId (void);
42  Emitter ();
43 private:
44  void DoInitialize (void);
45  void Emit (void);
46  void Count (void);
47 
48  TracedValue<double> m_counter; // normally this would be integer type
50 
51 };
52 
54 
55 TypeId
57 {
58  static TypeId tid = TypeId ("ns3::Emitter")
60  .SetParent<Object> ()
61  .AddTraceSource ("Counter",
62  "sample counter",
64  "ns3::TracedValue::DoubleCallback")
65  ;
66  return tid;
67 }
68 
70 {
71  NS_LOG_FUNCTION (this);
72  m_counter = 0;
73  m_var = CreateObject<ExponentialRandomVariable> ();
74 }
75 
76 void
78 {
79  NS_LOG_FUNCTION (this);
80  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
81  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
82 }
83 
84 void
86 {
87  NS_LOG_FUNCTION (this);
88  NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ());
89  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
90 }
91 
92 void
94 {
95  NS_LOG_FUNCTION (this);
96  NS_LOG_DEBUG ("Counting at " << Simulator::Now ().GetSeconds ());
97  m_counter += 1.0;
98  DoubleProbe::SetValueByPath ("/Names/StaticallyAccessedProbe", m_counter);
99  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
100 }
101 
102 // This is a function to test hooking a raw function to the trace source
103 void
104 NotifyViaTraceSource (std::string context, double oldVal, double newVal)
105 {
106  NS_LOG_DEBUG ("context: " << context << " old " << oldVal << " new " << newVal);
107 }
108 
109 // This is a function to test hooking it to the probe output
110 void
111 NotifyViaProbe (std::string context, double oldVal, double newVal)
112 {
113  NS_LOG_DEBUG ("context: " << context << " old " << oldVal << " new " << newVal);
114 }
115 
116 int main (int argc, char *argv[])
117 {
118  CommandLine cmd;
119  cmd.Parse (argc, argv);
120  bool connected;
121 
122  Ptr<Emitter> emitter = CreateObject<Emitter> ();
123  Names::Add ("/Names/Emitter", emitter);
124 
125  //
126  // The below shows typical functionality without a probe
127  // (connect a sink function to a trace source)
128  //
129  connected = emitter->TraceConnect ("Counter", "sample context", MakeCallback (&NotifyViaTraceSource));
130  NS_ASSERT_MSG (connected, "Trace source not connected");
131 
132  //
133  // Next, we'll show several use cases of using a Probe to access and
134  // filter the values of the underlying trace source
135  //
136 
137  //
138  // Probe1 will be hooked directly to the Emitter trace source object
139  //
140 
141  // probe1 will be hooked to the Emitter trace source
142  Ptr<DoubleProbe> probe1 = CreateObject<DoubleProbe> ();
143  // the probe's name can serve as its context in the tracing
144  probe1->SetName ("ObjectProbe");
145 
146  // Connect the probe to the emitter's Counter
147  connected = probe1->ConnectByObject ("Counter", emitter);
148  NS_ASSERT_MSG (connected, "Trace source not connected to probe1");
149 
150  // The probe itself should generate output. The context that we provide
151  // to this probe (in this case, the probe name) will help to disambiguate
152  // the source of the trace
153  connected = probe1->TraceConnect ("Output", probe1->GetName (), MakeCallback (&NotifyViaProbe));
154  NS_ASSERT_MSG (connected, "Trace source not connected to probe1 Output");
155 
156  //
157  // Probe2 will be hooked to the Emitter trace source object by
158  // accessing it by path name in the Config database
159  //
160 
161  // Create another similar probe; this will hook up via a Config path
162  Ptr<DoubleProbe> probe2 = CreateObject<DoubleProbe> ();
163  probe2->SetName ("PathProbe");
164 
165  // Note, no return value is checked here
166  probe2->ConnectByPath ("/Names/Emitter/Counter");
167 
168  // The probe itself should generate output. The context that we provide
169  // to this probe (in this case, the probe name) will help to disambiguate
170  // the source of the trace
171  connected = probe2->TraceConnect ("Output", "/Names/Probes/PathProbe/Output", MakeCallback (&NotifyViaProbe));
172  NS_ASSERT_MSG (connected, "Trace source not connected to probe2 Output");
173 
174  //
175  // Probe3 will be called by the emitter directly through the
176  // static method SetValueByPath().
177  //
178  Ptr<DoubleProbe> probe3 = CreateObject<DoubleProbe> ();
179  probe3->SetName ("StaticallyAccessedProbe");
180  // We must add it to the config database
181  Names::Add ("/Names/Probes", probe3->GetName (), probe3);
182 
183  // The probe itself should generate output. The context that we provide
184  // to this probe (in this case, the probe name) will help to disambiguate
185  // the source of the trace
186  connected = probe3->TraceConnect ("Output", "/Names/Probes/StaticallyAccessedProbe/Output", MakeCallback (&NotifyViaProbe));
187  NS_ASSERT_MSG (connected, "Trace source not connected to probe3 Output");
188 
189  // The Emitter object is not associated with an ns-3 node, so
190  // it won't get started automatically, so we need to do this ourselves
192 
193  Simulator::Stop (Seconds (100.0));
194  Simulator::Run ();
196 
197  return 0;
198 }
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
TypeId AddConstructor(void)
Definition: type-id.h:460
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
std::string GetName(void) const
Get the object's name.
void NotifyViaProbe(std::string context, double oldVal, double newVal)
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
TracedValue< double > m_counter
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:819
static TypeId GetTypeId(void)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr obj.
Definition: names.cc:622
virtual void ConnectByPath(std::string path)
connect to a trace source provided by a config path
Definition: double-probe.cc:95
Ptr< ExponentialRandomVariable > m_var
static void SetValueByPath(std::string path, double value)
Set a probe value by its name in the Config system.
Definition: double-probe.cc:77
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1296
Parse command-line arguments.
Definition: command-line.h:201
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
void Count(void)
void DoInitialize(void)
Initialize() implementation.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
virtual bool ConnectByObject(std::string traceSource, Ptr< Object > obj)
connect to a trace source attribute provided by a given object
Definition: double-probe.cc:86
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
void SetName(std::string name)
Set the object's name. All spaces are replaced by underscores.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:866
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Connect a TraceSource to a Callback with a context.
Definition: object-base.cc:311
void NotifyViaTraceSource(std::string context, double oldVal, double newVal)
void Parse(int argc, char *argv[])
Parse the program arguments.
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:57
void Emit(void)