A Discrete-Event Network Simulator
API
time-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) 2014 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 // This example is designed to show the main features of an ns3::TimeProbe.
21 // A test object is used to emit values through a trace source. The
22 // example shows three ways to use a ns3::TimeProbe to hook the output
23 // of this trace source (in addition to hooking the raw trace source).
24 //
25 // It produces two types of output. By default, it will generate a
26 // gnuplot of interarrival times. If the '--verbose=1' argument is passed,
27 // it will also generate debugging output of the form (for example):
28 //
29 // Emitting at 96.5378 seconds
30 // context: raw trace source old 0.293343 new 0.00760254
31 // context: probe1 old 0.293343 new 0.00760254
32 // context: probe2 old 0.293343 new 0.00760254
33 // context: probe3 old 0.293343 new 0.00760254
34 //
35 // The stopTime defaults to 100 seconds but can be changed by an argument.
36 //
37 
38 #include <string>
39 
40 #include "ns3/core-module.h"
41 #include "ns3/time-probe.h"
42 #include "ns3/gnuplot-helper.h"
43 
44 using namespace ns3;
45 
46 NS_LOG_COMPONENT_DEFINE ("TimeProbeExample");
47 
48 //
49 // This is our test object, an object that emits values according to
50 // a Poisson arrival process. It emits a traced Time value as a
51 // trace source; this takes the value of interarrival time
52 //
53 class Emitter : public Object
54 {
55 public:
56  static TypeId GetTypeId (void);
57  Emitter ();
58 private:
59  void DoInitialize (void);
60 // void Emit (void);
61  void Emit (void);
62 
66 };
67 
69 
70 TypeId
71 Emitter::GetTypeId (void)
72 {
73  static TypeId tid = TypeId ("ns3::Emitter")
75  .SetParent<Object> ()
76  .AddTraceSource ("Interval",
77  "Trace source",
79  "ns3::Time::TracedValueCallback")
80  ;
81  return tid;
82 }
83 
84 Emitter::Emitter (void)
85  : m_interval (Seconds (0)),
86  m_last (Seconds (0))
87 {
88  m_var = CreateObject<ExponentialRandomVariable> ();
89 }
90 
91 void
93 {
94  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
95 }
96 
97 void
98 Emitter::Emit (void)
99 {
100  NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds () << " seconds");
102  m_last = Simulator::Now ();
103  TimeProbe::SetValueByPath ("/Names/probe3", m_interval);
104  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, 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, Time oldVal, Time newVal)
110 {
112  GlobalValue::GetValueByName ("verbose", verbose);
113  if (verbose.Get ())
114  {
115  std::cout << "context: " << context << " old " << oldVal.GetSeconds () << " new " << newVal.GetSeconds () << std::endl;
116  }
117 }
118 
119 // This is a function to test hooking it to the probe output
120 void
121 NotifyViaProbe (std::string context, double oldVal, double newVal)
122 {
124  GlobalValue::GetValueByName ("verbose", verbose);
125  if (verbose.Get ())
126  {
127  std::cout << "context: " << context << " old " << oldVal << " new " << newVal << std::endl;
128  }
129 }
130 
131 static ns3::GlobalValue g_verbose ("verbose",
132  "Whether to enable verbose output",
133  ns3::BooleanValue (false),
135 
136 int main (int argc, char *argv[])
137 {
138  double stopTime = 100.0;
139  bool verbose = false;
140 
141  CommandLine cmd;
142  cmd.AddValue ("stopTime", "Time (seconds) to terminate simulation", stopTime);
143  cmd.AddValue ("verbose", "Whether to enable verbose output", verbose);
144  cmd.Parse (argc, argv);
145  bool connected;
146 
147  // Set a global value, so that the callbacks can access it
148  if (verbose)
149  {
150  GlobalValue::Bind ("verbose", BooleanValue (true));
151  LogComponentEnable ("TimeProbeExample", LOG_LEVEL_ALL);
152  }
153 
154  Ptr<Emitter> emitter = CreateObject<Emitter> ();
155  Names::Add ("/Names/Emitter", emitter);
156 
157  //
158  // The below shows typical functionality without a probe
159  // (connect a sink function to a trace source)
160  //
161  connected = emitter->TraceConnect ("Interval", "raw trace source", MakeCallback (&NotifyViaTraceSource));
162  NS_ASSERT_MSG (connected, "Trace source not connected");
163 
164  //
165  // Next, we'll show several use cases of using a Probe to access and
166  // filter the values of the underlying trace source
167  //
168 
169  //
170  // Probe1 will be hooked directly to the Emitter trace source object
171  //
172 
173  // probe1 will be hooked to the Emitter trace source
174  Ptr<TimeProbe> probe1 = CreateObject<TimeProbe> ();
175  // the probe's name can serve as its context in the tracing
176  probe1->SetName ("probe1");
177 
178  // Connect the probe to the emitter's Interval
179  connected = probe1->ConnectByObject ("Interval", emitter);
180  NS_ASSERT_MSG (connected, "Trace source not connected to probe1");
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 = probe1->TraceConnect ("Output", probe1->GetName (), MakeCallback (&NotifyViaProbe));
186  NS_ASSERT_MSG (connected, "Trace source not connected to probe1 Output");
187 
188  //
189  // Probe2 will be hooked to the Emitter trace source object by
190  // accessing it by path name in the Config database
191  //
192 
193  // Create another similar probe; this will hook up via a Config path
194  Ptr<TimeProbe> probe2 = CreateObject<TimeProbe> ();
195  probe2->SetName ("probe2");
196 
197  // Note, no return value is checked here
198  probe2->ConnectByPath ("/Names/Emitter/Interval");
199 
200  // The probe itself should generate output. The context that we provide
201  // to this probe (in this case, the probe name) will help to disambiguate
202  // the source of the trace
203  connected = probe2->TraceConnect ("Output", "probe2", MakeCallback (&NotifyViaProbe));
204  NS_ASSERT_MSG (connected, "Trace source not connected to probe2 Output");
205 
206  //
207  // Probe3 will be called by the emitter directly through the
208  // static method SetValueByPath().
209  //
210  Ptr<TimeProbe> probe3 = CreateObject<TimeProbe> ();
211  probe3->SetName ("probe3");
212 
213  // By adding to the config database, we can access it later
214  Names::Add ("/Names/probe3", probe3);
215 
216  // The probe itself should generate output. The context that we provide
217  // to this probe (in this case, the probe name) will help to disambiguate
218  // the source of the trace
219  connected = probe3->TraceConnect ("Output", "probe3", MakeCallback (&NotifyViaProbe));
220  NS_ASSERT_MSG (connected, "Trace source not connected to probe3 Output");
221 
222  // Plot the interval values
223  GnuplotHelper plotHelper;
224  plotHelper.ConfigurePlot ("time-probe-example",
225  "Emitter interarrivals vs. Time",
226  "Simulation time (Seconds)",
227  "Interarrival time (Seconds)",
228  "png");
229 
230  // Helper creates a TimeProbe and hooks it to the /Names/Emitter/Interval
231  // source. Helper also takes the Output of the TimeProbe and plots it
232  // as a dataset labeled 'Emitter Interarrival Time'
233  plotHelper.PlotProbe ("ns3::TimeProbe",
234  "/Names/Emitter/Interval",
235  "Output",
236  "Emitter Interarrival Time",
237  GnuplotAggregator::KEY_INSIDE);
238 
239  // The Emitter object is not associated with an ns-3 node, so
240  // it won't get started automatically, so we need to do this ourselves
241  Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter);
242 
243  Simulator::Stop (Seconds (stopTime));
244  Simulator::Run ();
245  Simulator::Destroy ();
246 
247  return 0;
248 }
virtual bool ConnectByObject(std::string traceSource, Ptr< Object > obj)
connect to a trace source attribute provided by a given object
Definition: time-probe.cc:87
bool Get(void) const
Definition: boolean.cc:51
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
TypeId AddConstructor(void)
Definition: type-id.h:460
AttributeValue implementation for Boolean.
Definition: boolean.h:34
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
std::string GetName(void) const
Get the object's name.
void PlotProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource, const std::string &title, enum GnuplotAggregator::KeyLocation keyLocation=GnuplotAggregator::KEY_INSIDE)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
hold a so-called 'global value'.
Definition: global-value.h:54
static TypeId GetTypeId(void)
double stopTime
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
TracedValue< Time > m_interval
void ConfigurePlot(const std::string &outputFileNameWithoutExtension, const std::string &title, const std::string &xLegend, const std::string &yLegend, const std::string &terminalType="png")
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:334
virtual void ConnectByPath(std::string path)
connect to a trace source provided by a config path
Definition: time-probe.cc:96
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:351
Ptr< ExponentialRandomVariable > m_var
void NotifyViaProbe(std::string context, double oldVal, double newVal)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1296
Parse command-line arguments.
Definition: command-line.h:201
void DoInitialize(void)
Initialize() implementation.
Helper class used to make gnuplot plots.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
static ns3::GlobalValue g_verbose("verbose","Whether to enable verbose output", ns3::BooleanValue(false), ns3::MakeBooleanChecker())
#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:90
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:491
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:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:866
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
double GetValue(double mean, double bound)
Returns a random double from an exponential distribution with the specified mean and upper bound...
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Connect a TraceSource to a Callback with a context.
Definition: object-base.cc:311
Print everything.
Definition: log.h:112
void Parse(int argc, char *argv[])
Parse the program arguments.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:330
A base class which provides memory management and object aggregation.
Definition: object.h:87
void NotifyViaTraceSource(std::string context, Time oldVal, Time newVal)
a unique identifier for an interface.
Definition: type-id.h:57
bool verbose
void Emit(void)