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