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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8/*
9 * This example is designed to show the main features of an
10 * ns3::DoubleProbe.
11 */
12
13#include "ns3/core-module.h"
14#include "ns3/double-probe.h"
15
16#include <string>
17
18using namespace ns3;
19
20NS_LOG_COMPONENT_DEFINE("DoubleProbeExample");
21
22/**
23 * This is our test object, an object that increments counters at
24 * various times and emits one of them as a trace source.
25 */
26class Emitter : public Object
27{
28 public:
29 /**
30 * Register this type.
31 * \return The TypeId.
32 */
33 static TypeId GetTypeId();
34 Emitter();
35
36 private:
37 void DoInitialize() override;
38
39 /// Generate data - actually this function is not traced.
40 void Emit();
41 /// Counts how many times this function is called.
42 void Count();
43
44 TracedValue<double> m_counter; //!< Sample counter, normally this would be integer type
45 Ptr<ExponentialRandomVariable> m_var; //!< Random number generator
46};
47
49
52{
53 static TypeId tid = TypeId("ns3::Emitter")
55 .SetGroupName("Stats")
56 .AddConstructor<Emitter>()
57 .AddTraceSource("Counter",
58 "sample counter",
60 "ns3::TracedValueCallback::Double");
61 return tid;
62}
63
70
71void
78
79void
81{
82 NS_LOG_FUNCTION(this);
83 NS_LOG_DEBUG("Emitting at " << Simulator::Now().As(Time::S));
84 Simulator::Schedule(Seconds(m_var->GetValue()), &Emitter::Emit, this);
85}
86
87void
89{
90 NS_LOG_FUNCTION(this);
91 NS_LOG_DEBUG("Counting at " << Simulator::Now().As(Time::S));
92 m_counter += 1.0;
93 DoubleProbe::SetValueByPath("/Names/StaticallyAccessedProbe", m_counter);
94 Simulator::Schedule(Seconds(m_var->GetValue()), &Emitter::Count, this);
95}
96
97/**
98 * This is a function to test hooking a raw function to the trace source,
99 *
100 * \param context The trace context.
101 * \param oldVal Old value.
102 * \param newVal New value.
103 */
104void
105NotifyViaTraceSource(std::string context, double oldVal, double newVal)
106{
107 NS_LOG_DEBUG("context: " << context << " old " << oldVal << " new " << newVal);
108}
109
110/**
111 * This is a function to test hooking it to the probe output
112 *
113 * \param context The trace context.
114 * \param oldVal Old value.
115 * \param newVal New value.
116 */
117void
118NotifyViaProbe(std::string context, double oldVal, double newVal)
119{
120 NS_LOG_DEBUG("context: " << context << " old " << oldVal << " new " << newVal);
121}
122
123int
124main(int argc, char* argv[])
125{
126 CommandLine cmd(__FILE__);
127 cmd.Parse(argc, argv);
128 bool connected;
129
131 Names::Add("/Names/Emitter", emitter);
132
133 //
134 // The below shows typical functionality without a probe
135 // (connect a sink function to a trace source)
136 //
137 connected =
138 emitter->TraceConnect("Counter", "sample context", MakeCallback(&NotifyViaTraceSource));
139 NS_ASSERT_MSG(connected, "Trace source not connected");
140
141 //
142 // Next, we'll show several use cases of using a Probe to access and
143 // filter the values of the underlying trace source
144 //
145
146 //
147 // Probe1 will be hooked directly to the Emitter trace source object
148 //
149
150 // probe1 will be hooked to the Emitter trace source
152 // the probe's name can serve as its context in the tracing
153 probe1->SetName("ObjectProbe");
154
155 // Connect the probe to the emitter's Counter
156 connected = probe1->ConnectByObject("Counter", emitter);
157 NS_ASSERT_MSG(connected, "Trace source not connected to probe1");
158
159 // The probe itself should generate output. The context that we provide
160 // to this probe (in this case, the probe name) will help to disambiguate
161 // the source of the trace
162 connected = probe1->TraceConnect("Output", probe1->GetName(), MakeCallback(&NotifyViaProbe));
163 NS_ASSERT_MSG(connected, "Trace source not connected to probe1 Output");
164
165 //
166 // Probe2 will be hooked to the Emitter trace source object by
167 // accessing it by path name in the Config database
168 //
169
170 // Create another similar probe; this will hook up via a Config path
172 probe2->SetName("PathProbe");
173
174 // Note, no return value is checked here
175 probe2->ConnectByPath("/Names/Emitter/Counter");
176
177 // The probe itself should generate output. The context that we provide
178 // to this probe (in this case, the probe name) will help to disambiguate
179 // the source of the trace
180 connected = probe2->TraceConnect("Output",
181 "/Names/Probes/PathProbe/Output",
183 NS_ASSERT_MSG(connected, "Trace source not connected to probe2 Output");
184
185 //
186 // Probe3 will be called by the emitter directly through the
187 // static method SetValueByPath().
188 //
190 probe3->SetName("StaticallyAccessedProbe");
191 // We must add it to the config database
192 Names::Add("/Names/Probes", probe3->GetName(), probe3);
193
194 // The probe itself should generate output. The context that we provide
195 // to this probe (in this case, the probe name) will help to disambiguate
196 // the source of the trace
197 connected = probe3->TraceConnect("Output",
198 "/Names/Probes/StaticallyAccessedProbe/Output",
200 NS_ASSERT_MSG(connected, "Trace source not connected to probe3 Output");
201
202 // The Emitter object is not associated with an ns-3 node, so
203 // it won't get started automatically, so we need to do this ourselves
205
206 Simulator::Stop(Seconds(100.0));
209
210 return 0;
211}
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.
static void SetValueByPath(std::string path, double value)
Set a probe value by its name in the Config system.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
A base class which provides memory management and object aggregation.
Definition object.h:78
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition object.cc:203
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
@ S
second
Definition nstime.h:105
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:684