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:
45  static TypeId GetTypeId (void);
46  Emitter ();
47 private:
48  void DoInitialize (void);
49  void Emit (void);
50  void Count (void);
51 
52  TracedValue<double> m_counter; // normally this would be integer type
54 
55 };
56 
58 
59 TypeId
61 {
62  static TypeId tid = TypeId ("ns3::Emitter")
63  .SetParent<Object> ()
64  .SetGroupName ("Stats")
65  .AddConstructor<Emitter> ()
66  .AddTraceSource ("Counter",
67  "sample counter",
69  "ns3::TracedValueCallback::Double")
70  ;
71  return tid;
72 }
73 
75 {
76  NS_LOG_FUNCTION (this);
77  m_counter = 0;
78  m_var = CreateObject<ExponentialRandomVariable> ();
79 }
80 
81 void
83 {
84  NS_LOG_FUNCTION (this);
85  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
86  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
87 }
88 
89 void
91 {
92  NS_LOG_FUNCTION (this);
93  NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ());
94  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
95 }
96 
97 void
99 {
100  NS_LOG_FUNCTION (this);
101  NS_LOG_DEBUG ("Counting at " << Simulator::Now ().GetSeconds ());
102  m_counter += 1.0;
103  DoubleProbe::SetValueByPath ("/Names/StaticallyAccessedProbe", m_counter);
104  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
105 }
106 
107 // This is a function to test hooking a raw function to the trace source
108 void
109 NotifyViaTraceSource (std::string context, double oldVal, double newVal)
110 {
111  NS_LOG_DEBUG ("context: " << context << " old " << oldVal << " new " << newVal);
112 }
113 
114 // This is a function to test hooking it to the probe output
115 void
116 NotifyViaProbe (std::string context, double oldVal, double newVal)
117 {
118  NS_LOG_DEBUG ("context: " << context << " old " << oldVal << " new " << newVal);
119 }
120 
121 int main (int argc, char *argv[])
122 {
123  CommandLine cmd (__FILE__);
124  cmd.Parse (argc, argv);
125  bool connected;
126 
127  Ptr<Emitter> emitter = CreateObject<Emitter> ();
128  Names::Add ("/Names/Emitter", emitter);
129 
130  //
131  // The below shows typical functionality without a probe
132  // (connect a sink function to a trace source)
133  //
134  connected = emitter->TraceConnect ("Counter", "sample context", MakeCallback (&NotifyViaTraceSource));
135  NS_ASSERT_MSG (connected, "Trace source not connected");
136 
137  //
138  // Next, we'll show several use cases of using a Probe to access and
139  // filter the values of the underlying trace source
140  //
141 
142  //
143  // Probe1 will be hooked directly to the Emitter trace source object
144  //
145 
146  // probe1 will be hooked to the Emitter trace source
147  Ptr<DoubleProbe> probe1 = CreateObject<DoubleProbe> ();
148  // the probe's name can serve as its context in the tracing
149  probe1->SetName ("ObjectProbe");
150 
151  // Connect the probe to the emitter's Counter
152  connected = probe1->ConnectByObject ("Counter", emitter);
153  NS_ASSERT_MSG (connected, "Trace source not connected to probe1");
154 
155  // The probe itself should generate output. The context that we provide
156  // to this probe (in this case, the probe name) will help to disambiguate
157  // the source of the trace
158  connected = probe1->TraceConnect ("Output", probe1->GetName (), MakeCallback (&NotifyViaProbe));
159  NS_ASSERT_MSG (connected, "Trace source not connected to probe1 Output");
160 
161  //
162  // Probe2 will be hooked to the Emitter trace source object by
163  // accessing it by path name in the Config database
164  //
165 
166  // Create another similar probe; this will hook up via a Config path
167  Ptr<DoubleProbe> probe2 = CreateObject<DoubleProbe> ();
168  probe2->SetName ("PathProbe");
169 
170  // Note, no return value is checked here
171  probe2->ConnectByPath ("/Names/Emitter/Counter");
172 
173  // The probe itself should generate output. The context that we provide
174  // to this probe (in this case, the probe name) will help to disambiguate
175  // the source of the trace
176  connected = probe2->TraceConnect ("Output", "/Names/Probes/PathProbe/Output", MakeCallback (&NotifyViaProbe));
177  NS_ASSERT_MSG (connected, "Trace source not connected to probe2 Output");
178 
179  //
180  // Probe3 will be called by the emitter directly through the
181  // static method SetValueByPath().
182  //
183  Ptr<DoubleProbe> probe3 = CreateObject<DoubleProbe> ();
184  probe3->SetName ("StaticallyAccessedProbe");
185  // We must add it to the config database
186  Names::Add ("/Names/Probes", probe3->GetName (), probe3);
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 = probe3->TraceConnect ("Output", "/Names/Probes/StaticallyAccessedProbe/Output", MakeCallback (&NotifyViaProbe));
192  NS_ASSERT_MSG (connected, "Trace source not connected to probe3 Output");
193 
194  // The Emitter object is not associated with an ns-3 node, so
195  // it won't get started automatically, so we need to do this ourselves
197 
198  Simulator::Stop (Seconds (100.0));
199  Simulator::Run ();
201 
202  return 0;
203 }
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
#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
void NotifyViaProbe(std::string context, double oldVal, double newVal)
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
TracedValue< double > m_counter
cmd
Definition: second.py:35
static TypeId GetTypeId(void)
Register this type.
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<Object> obj.
Definition: names.cc:768
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
Parse command-line arguments.
Definition: command-line.h:226
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
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:195
#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
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:180
void SetName(std::string name)
Set the object&#39;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:273
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Connect a TraceSource to a Callback with a context.
Definition: object-base.cc:306
void NotifyViaTraceSource(std::string context, double oldVal, double newVal)
A base class which provides memory management and object aggregation.
Definition: object.h:87
std::string GetName(void) const
Get the object&#39;s name.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
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:1642
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
void Emit(void)