A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
double-probe-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 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 */
18
19/*
20 * This example is designed to show the main features of an
21 * ns3::DoubleProbe.
22 */
23
24#include "ns3/core-module.h"
25#include "ns3/double-probe.h"
26
27#include <string>
28
29using namespace ns3;
30
31NS_LOG_COMPONENT_DEFINE("DoubleProbeExample");
32
37class Emitter : public Object
38{
39 public:
44 static TypeId GetTypeId();
45 Emitter();
46
47 private:
48 void DoInitialize() override;
49
51 void Emit();
53 void Count();
54
57};
58
60
63{
64 static TypeId tid = TypeId("ns3::Emitter")
66 .SetGroupName("Stats")
67 .AddConstructor<Emitter>()
68 .AddTraceSource("Counter",
69 "sample counter",
71 "ns3::TracedValueCallback::Double");
72 return tid;
73}
74
76{
77 NS_LOG_FUNCTION(this);
78 m_counter = 0;
79 m_var = CreateObject<ExponentialRandomVariable>();
80}
81
82void
84{
85 NS_LOG_FUNCTION(this);
88}
89
90void
92{
93 NS_LOG_FUNCTION(this);
94 NS_LOG_DEBUG("Emitting at " << Simulator::Now().As(Time::S));
96}
97
98void
100{
101 NS_LOG_FUNCTION(this);
102 NS_LOG_DEBUG("Counting at " << Simulator::Now().As(Time::S));
103 m_counter += 1.0;
104 DoubleProbe::SetValueByPath("/Names/StaticallyAccessedProbe", m_counter);
106}
107
115void
116NotifyViaTraceSource(std::string context, double oldVal, double newVal)
117{
118 NS_LOG_DEBUG("context: " << context << " old " << oldVal << " new " << newVal);
119}
120
128void
129NotifyViaProbe(std::string context, double oldVal, double newVal)
130{
131 NS_LOG_DEBUG("context: " << context << " old " << oldVal << " new " << newVal);
132}
133
134int
135main(int argc, char* argv[])
136{
137 CommandLine cmd(__FILE__);
138 cmd.Parse(argc, argv);
139 bool connected;
140
141 Ptr<Emitter> emitter = CreateObject<Emitter>();
142 Names::Add("/Names/Emitter", emitter);
143
144 //
145 // The below shows typical functionality without a probe
146 // (connect a sink function to a trace source)
147 //
148 connected =
149 emitter->TraceConnect("Counter", "sample context", MakeCallback(&NotifyViaTraceSource));
150 NS_ASSERT_MSG(connected, "Trace source not connected");
151
152 //
153 // Next, we'll show several use cases of using a Probe to access and
154 // filter the values of the underlying trace source
155 //
156
157 //
158 // Probe1 will be hooked directly to the Emitter trace source object
159 //
160
161 // probe1 will be hooked to the Emitter trace source
162 Ptr<DoubleProbe> probe1 = CreateObject<DoubleProbe>();
163 // the probe's name can serve as its context in the tracing
164 probe1->SetName("ObjectProbe");
165
166 // Connect the probe to the emitter's Counter
167 connected = probe1->ConnectByObject("Counter", emitter);
168 NS_ASSERT_MSG(connected, "Trace source not connected to probe1");
169
170 // The probe itself should generate output. The context that we provide
171 // to this probe (in this case, the probe name) will help to disambiguate
172 // the source of the trace
173 connected = probe1->TraceConnect("Output", probe1->GetName(), MakeCallback(&NotifyViaProbe));
174 NS_ASSERT_MSG(connected, "Trace source not connected to probe1 Output");
175
176 //
177 // Probe2 will be hooked to the Emitter trace source object by
178 // accessing it by path name in the Config database
179 //
180
181 // Create another similar probe; this will hook up via a Config path
182 Ptr<DoubleProbe> probe2 = CreateObject<DoubleProbe>();
183 probe2->SetName("PathProbe");
184
185 // Note, no return value is checked here
186 probe2->ConnectByPath("/Names/Emitter/Counter");
187
188 // The probe itself should generate output. The context that we provide
189 // to this probe (in this case, the probe name) will help to disambiguate
190 // the source of the trace
191 connected = probe2->TraceConnect("Output",
192 "/Names/Probes/PathProbe/Output",
194 NS_ASSERT_MSG(connected, "Trace source not connected to probe2 Output");
195
196 //
197 // Probe3 will be called by the emitter directly through the
198 // static method SetValueByPath().
199 //
200 Ptr<DoubleProbe> probe3 = CreateObject<DoubleProbe>();
201 probe3->SetName("StaticallyAccessedProbe");
202 // We must add it to the config database
203 Names::Add("/Names/Probes", probe3->GetName(), probe3);
204
205 // The probe itself should generate output. The context that we provide
206 // to this probe (in this case, the probe name) will help to disambiguate
207 // the source of the trace
208 connected = probe3->TraceConnect("Output",
209 "/Names/Probes/StaticallyAccessedProbe/Output",
211 NS_ASSERT_MSG(connected, "Trace source not connected to probe3 Output");
212
213 // The Emitter object is not associated with an ns-3 node, so
214 // it won't get started automatically, so we need to do this ourselves
216
217 Simulator::Stop(Seconds(100.0));
220
221 return 0;
222}
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.
void Emit()
Generate data - actually this function is not traced.
TracedValue< double > m_counter
Sample counter, normally this would be integer type.
void Count()
Counts how many times this function is called.
Ptr< ExponentialRandomVariable > m_var
Random number generator.
Parse command-line arguments.
Definition: command-line.h:232
static void SetValueByPath(std::string path, double value)
Set a probe value by its name in the Config system.
Definition: double-probe.cc:78
double GetValue(double mean, double bound)
Get the next random value drawn from the distribution.
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:214
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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
Trace classes with value semantics.
Definition: traced-value.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:932
void NotifyViaProbe(std::string context, double oldVal, double newVal)
This is a function to test hooking it to the probe output.
void NotifyViaTraceSource(std::string context, double oldVal, double newVal)
This is a function to test hooking a raw function to the trace source,.
#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:86
#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.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
ns cmd
Definition: second.py:40