A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  ;
65  return tid;
66 }
67 
69 {
70  NS_LOG_FUNCTION (this);
71  m_counter = 0;
72  m_var = CreateObject<ExponentialRandomVariable> ();
73 }
74 
75 void
77 {
78  NS_LOG_FUNCTION (this);
79  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
80  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION (this);
87  NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ());
88  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION (this);
95  NS_LOG_DEBUG ("Counting at " << Simulator::Now ().GetSeconds ());
96  m_counter += 1.0;
97  DoubleProbe::SetValueByPath ("/Names/StaticallyAccessedProbe", m_counter);
98  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
99 }
100 
101 // This is a function to test hooking a raw function to the trace source
102 void
103 NotifyViaTraceSource (std::string context, double oldVal, double newVal)
104 {
105  NS_LOG_DEBUG ("context: " << context << " old " << oldVal << " new " << newVal);
106 }
107 
108 // This is a function to test hooking it to the probe output
109 void
110 NotifyViaProbe (std::string context, double oldVal, double newVal)
111 {
112  NS_LOG_DEBUG ("context: " << context << " old " << oldVal << " new " << newVal);
113 }
114 
115 int main (int argc, char *argv[])
116 {
117  CommandLine cmd;
118  cmd.Parse (argc, argv);
119  bool connected;
120 
121  Ptr<Emitter> emitter = CreateObject<Emitter> ();
122  Names::Add ("/Names/Emitter", emitter);
123 
124  //
125  // The below shows typical functionality without a probe
126  // (connect a sink function to a trace source)
127  //
128  connected = emitter->TraceConnect ("Counter", "sample context", MakeCallback (&NotifyViaTraceSource));
129  NS_ASSERT_MSG (connected, "Trace source not connected");
130 
131  //
132  // Next, we'll show several use cases of using a Probe to access and
133  // filter the values of the underlying trace source
134  //
135 
136  //
137  // Probe1 will be hooked directly to the Emitter trace source object
138  //
139 
140  // probe1 will be hooked to the Emitter trace source
141  Ptr<DoubleProbe> probe1 = CreateObject<DoubleProbe> ();
142  // the probe's name can serve as its context in the tracing
143  probe1->SetName ("ObjectProbe");
144 
145  // Connect the probe to the emitter's Counter
146  connected = probe1->ConnectByObject ("Counter", emitter);
147  NS_ASSERT_MSG (connected, "Trace source not connected to probe1");
148 
149  // The probe itself should generate output. The context that we provide
150  // to this probe (in this case, the probe name) will help to disambiguate
151  // the source of the trace
152  connected = probe1->TraceConnect ("Output", probe1->GetName (), MakeCallback (&NotifyViaProbe));
153  NS_ASSERT_MSG (connected, "Trace source not connected to probe1 Output");
154 
155  //
156  // Probe2 will be hooked to the Emitter trace source object by
157  // accessing it by path name in the Config database
158  //
159 
160  // Create another similar probe; this will hook up via a Config path
161  Ptr<DoubleProbe> probe2 = CreateObject<DoubleProbe> ();
162  probe2->SetName ("PathProbe");
163 
164  // Note, no return value is checked here
165  probe2->ConnectByPath ("/Names/Emitter/Counter");
166 
167  // The probe itself should generate output. The context that we provide
168  // to this probe (in this case, the probe name) will help to disambiguate
169  // the source of the trace
170  connected = probe2->TraceConnect ("Output", "/Names/Probes/PathProbe/Output", MakeCallback (&NotifyViaProbe));
171  NS_ASSERT_MSG (connected, "Trace source not connected to probe2 Output");
172 
173  //
174  // Probe3 will be called by the emitter directly through the
175  // static method SetValueByPath().
176  //
177  Ptr<DoubleProbe> probe3 = CreateObject<DoubleProbe> ();
178  probe3->SetName ("StaticallyAccessedProbe");
179  // We must add it to the config database
180  Names::Add ("/Names/Probes", probe3->GetName (), probe3);
181 
182  // The probe itself should generate output. The context that we provide
183  // to this probe (in this case, the probe name) will help to disambiguate
184  // the source of the trace
185  connected = probe3->TraceConnect ("Output", "/Names/Probes/StaticallyAccessedProbe/Output", MakeCallback (&NotifyViaProbe));
186  NS_ASSERT_MSG (connected, "Trace source not connected to probe3 Output");
187 
188  // The Emitter object is not associated with an ns-3 node, so
189  // it won't get started automatically, so we need to do this ourselves
190  Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter);
191 
192  Simulator::Stop (Seconds (100.0));
193  Simulator::Run ();
195 
196  return 0;
197 }
#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:418
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
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 until one of:
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
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:825
static TypeId GetTypeId(void)
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr obj.
Definition: names.cc:615
virtual void ConnectByPath(std::string path)
connect to a trace source provided by a config path
Definition: double-probe.cc:93
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:75
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
Parse command-line arguments.
Definition: command-line.h:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void Count(void)
void DoInitialize(void)
This method is called only once by Object::Initialize.
TracedValue< double > m_counter
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
int main(int argc, char *argv[])
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
#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:84
virtual bool ConnectByObject(std::string traceSource, Ptr< Object > obj)
connect to a trace source attribute provided by a given object
Definition: double-probe.cc:84
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
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:213
void Initialize(void)
This method calls the virtual DoInitialize method on all the objects aggregated to this object...
Definition: object.cc:179
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Definition: object-base.cc:297
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:64
a unique identifier for an interface.
Definition: type-id.h:49
void Emit(void)