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 
38 class Emitter : public Object
39 {
40 public:
45  static TypeId GetTypeId (void);
46  Emitter ();
47 private:
48  void DoInitialize (void);
49 
51  void Emit (void);
53  void Count (void);
54 
57 
58 };
59 
61 
62 TypeId
64 {
65  static TypeId tid = TypeId ("ns3::Emitter")
66  .SetParent<Object> ()
67  .SetGroupName ("Stats")
68  .AddConstructor<Emitter> ()
69  .AddTraceSource ("Counter",
70  "sample counter",
72  "ns3::TracedValueCallback::Double")
73  ;
74  return tid;
75 }
76 
78 {
79  NS_LOG_FUNCTION (this);
80  m_counter = 0;
81  m_var = CreateObject<ExponentialRandomVariable> ();
82 }
83 
84 void
86 {
87  NS_LOG_FUNCTION (this);
88  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
89  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
90 }
91 
92 void
94 {
95  NS_LOG_FUNCTION (this);
96  NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().As (Time::S));
97  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
98 }
99 
100 void
102 {
103  NS_LOG_FUNCTION (this);
104  NS_LOG_DEBUG ("Counting at " << Simulator::Now ().As (Time::S));
105  m_counter += 1.0;
106  DoubleProbe::SetValueByPath ("/Names/StaticallyAccessedProbe", m_counter);
107  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
108 }
109 
110 // This is a function to test hooking a raw function to the trace source
111 void
112 NotifyViaTraceSource (std::string context, double oldVal, double newVal)
113 {
114  NS_LOG_DEBUG ("context: " << context << " old " << oldVal << " new " << newVal);
115 }
116 
117 // This is a function to test hooking it to the probe output
118 void
119 NotifyViaProbe (std::string context, double oldVal, double newVal)
120 {
121  NS_LOG_DEBUG ("context: " << context << " old " << oldVal << " new " << newVal);
122 }
123 
124 int main (int argc, char *argv[])
125 {
126  CommandLine cmd (__FILE__);
127  cmd.Parse (argc, argv);
128  bool connected;
129 
130  Ptr<Emitter> emitter = CreateObject<Emitter> ();
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 = emitter->TraceConnect ("Counter", "sample context", MakeCallback (&NotifyViaTraceSource));
138  NS_ASSERT_MSG (connected, "Trace source not connected");
139 
140  //
141  // Next, we'll show several use cases of using a Probe to access and
142  // filter the values of the underlying trace source
143  //
144 
145  //
146  // Probe1 will be hooked directly to the Emitter trace source object
147  //
148 
149  // probe1 will be hooked to the Emitter trace source
150  Ptr<DoubleProbe> probe1 = CreateObject<DoubleProbe> ();
151  // the probe's name can serve as its context in the tracing
152  probe1->SetName ("ObjectProbe");
153 
154  // Connect the probe to the emitter's Counter
155  connected = probe1->ConnectByObject ("Counter", emitter);
156  NS_ASSERT_MSG (connected, "Trace source not connected to probe1");
157 
158  // The probe itself should generate output. The context that we provide
159  // to this probe (in this case, the probe name) will help to disambiguate
160  // the source of the trace
161  connected = probe1->TraceConnect ("Output", probe1->GetName (), MakeCallback (&NotifyViaProbe));
162  NS_ASSERT_MSG (connected, "Trace source not connected to probe1 Output");
163 
164  //
165  // Probe2 will be hooked to the Emitter trace source object by
166  // accessing it by path name in the Config database
167  //
168 
169  // Create another similar probe; this will hook up via a Config path
170  Ptr<DoubleProbe> probe2 = CreateObject<DoubleProbe> ();
171  probe2->SetName ("PathProbe");
172 
173  // Note, no return value is checked here
174  probe2->ConnectByPath ("/Names/Emitter/Counter");
175 
176  // The probe itself should generate output. The context that we provide
177  // to this probe (in this case, the probe name) will help to disambiguate
178  // the source of the trace
179  connected = probe2->TraceConnect ("Output", "/Names/Probes/PathProbe/Output", MakeCallback (&NotifyViaProbe));
180  NS_ASSERT_MSG (connected, "Trace source not connected to probe2 Output");
181 
182  //
183  // Probe3 will be called by the emitter directly through the
184  // static method SetValueByPath().
185  //
186  Ptr<DoubleProbe> probe3 = CreateObject<DoubleProbe> ();
187  probe3->SetName ("StaticallyAccessedProbe");
188  // We must add it to the config database
189  Names::Add ("/Names/Probes", probe3->GetName (), probe3);
190 
191  // The probe itself should generate output. The context that we provide
192  // to this probe (in this case, the probe name) will help to disambiguate
193  // the source of the trace
194  connected = probe3->TraceConnect ("Output", "/Names/Probes/StaticallyAccessedProbe/Output", MakeCallback (&NotifyViaProbe));
195  NS_ASSERT_MSG (connected, "Trace source not connected to probe3 Output");
196 
197  // The Emitter object is not associated with an ns-3 node, so
198  // it won't get started automatically, so we need to do this ourselves
200 
201  Simulator::Stop (Seconds (100.0));
202  Simulator::Run ();
204 
205  return 0;
206 }
This is our test object, an object that increments counters at various times and emits one of them as...
TracedValue< double > m_counter
Sample counter, normally this would be integer type.
void Count(void)
Counts how many times this function is called.
void Emit(void)
Generate data - actually this function is not traced.
void DoInitialize(void)
Initialize() implementation.
Ptr< ExponentialRandomVariable > m_var
Random number generator.
static TypeId GetTypeId(void)
Register this type.
Parse command-line arguments.
Definition: command-line.h:229
static void SetValueByPath(std::string path, double value)
Set a probe value by its name in the Config system.
Definition: double-probe.cc:77
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
void NotifyViaProbe(std::string context, double oldVal, double newVal)
void NotifyViaTraceSource(std::string context, double oldVal, double newVal)
#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:88
#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.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35