|
|
| 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 |
|
| 63 |
TracedValue<Time> m_interval; |
| 64 |
Time m_last; |
| 65 |
Ptr<ExponentialRandomVariable> m_var; |
| 66 |
}; |
| 67 |
|
| 68 |
NS_OBJECT_ENSURE_REGISTERED (Emitter); |
| 69 |
|
| 70 |
TypeId |
| 71 |
Emitter::GetTypeId (void) |
| 72 |
{ |
| 73 |
static TypeId tid = TypeId ("ns3::Emitter") |
| 74 |
.AddConstructor<Emitter> () |
| 75 |
.SetParent<Object> () |
| 76 |
.AddTraceSource ("Interval", |
| 77 |
"Trace source", |
| 78 |
MakeTraceSourceAccessor (&Emitter::m_interval)) |
| 79 |
; |
| 80 |
return tid; |
| 81 |
} |
| 82 |
|
| 83 |
Emitter::Emitter (void) |
| 84 |
: m_interval (Seconds (0)), |
| 85 |
m_last (Seconds (0)) |
| 86 |
{ |
| 87 |
m_var = CreateObject<ExponentialRandomVariable> (); |
| 88 |
} |
| 89 |
|
| 90 |
void |
| 91 |
Emitter::DoInitialize (void) |
| 92 |
{ |
| 93 |
Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this); |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
Emitter::Emit (void) |
| 98 |
{ |
| 99 |
NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds () << " seconds"); |
| 100 |
m_interval = Simulator::Now () - m_last; |
| 101 |
m_last = Simulator::Now (); |
| 102 |
TimeProbe::SetValueByPath ("/Names/probe3", m_interval); |
| 103 |
Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this); |
| 104 |
} |
| 105 |
|
| 106 |
// This is a function to test hooking a raw function to the trace source |
| 107 |
void |
| 108 |
NotifyViaTraceSource (std::string context, Time oldVal, Time newVal) |
| 109 |
{ |
| 110 |
BooleanValue verbose; |
| 111 |
GlobalValue::GetValueByName ("verbose", verbose); |
| 112 |
if (verbose.Get ()) |
| 113 |
{ |
| 114 |
std::cout << "context: " << context << " old " << oldVal.GetSeconds () << " new " << newVal.GetSeconds () << std::endl; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// This is a function to test hooking it to the probe output |
| 119 |
void |
| 120 |
NotifyViaProbe (std::string context, double oldVal, double newVal) |
| 121 |
{ |
| 122 |
BooleanValue verbose; |
| 123 |
GlobalValue::GetValueByName ("verbose", verbose); |
| 124 |
if (verbose.Get ()) |
| 125 |
{ |
| 126 |
std::cout << "context: " << context << " old " << oldVal << " new " << newVal << std::endl; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
static ns3::GlobalValue g_verbose ("verbose", |
| 131 |
"Whether to enable verbose output", |
| 132 |
ns3::BooleanValue (false), |
| 133 |
ns3::MakeBooleanChecker ()); |
| 134 |
|
| 135 |
int main (int argc, char *argv[]) |
| 136 |
{ |
| 137 |
double stopTime = 100.0; |
| 138 |
bool verbose = false; |
| 139 |
|
| 140 |
CommandLine cmd; |
| 141 |
cmd.AddValue ("stopTime", "Time (seconds) to terminate simulation", stopTime); |
| 142 |
cmd.AddValue ("verbose", "Whether to enable verbose output", verbose); |
| 143 |
cmd.Parse (argc, argv); |
| 144 |
bool connected; |
| 145 |
|
| 146 |
// Set a global value, so that the callbacks can access it |
| 147 |
if (verbose) |
| 148 |
{ |
| 149 |
GlobalValue::Bind ("verbose", BooleanValue (true)); |
| 150 |
LogComponentEnable ("TimeProbeExample", LOG_LEVEL_ALL); |
| 151 |
} |
| 152 |
|
| 153 |
Ptr<Emitter> emitter = CreateObject<Emitter> (); |
| 154 |
Names::Add ("/Names/Emitter", emitter); |
| 155 |
|
| 156 |
// |
| 157 |
// The below shows typical functionality without a probe |
| 158 |
// (connect a sink function to a trace source) |
| 159 |
// |
| 160 |
connected = emitter->TraceConnect ("Interval", "raw trace source", MakeCallback (&NotifyViaTraceSource)); |
| 161 |
NS_ASSERT_MSG (connected, "Trace source not connected"); |
| 162 |
|
| 163 |
// |
| 164 |
// Next, we'll show several use cases of using a Probe to access and |
| 165 |
// filter the values of the underlying trace source |
| 166 |
// |
| 167 |
|
| 168 |
// |
| 169 |
// Probe1 will be hooked directly to the Emitter trace source object |
| 170 |
// |
| 171 |
|
| 172 |
// probe1 will be hooked to the Emitter trace source |
| 173 |
Ptr<TimeProbe> probe1 = CreateObject<TimeProbe> (); |
| 174 |
// the probe's name can serve as its context in the tracing |
| 175 |
probe1->SetName ("probe1"); |
| 176 |
|
| 177 |
// Connect the probe to the emitter's Interval |
| 178 |
connected = probe1->ConnectByObject ("Interval", emitter); |
| 179 |
NS_ASSERT_MSG (connected, "Trace source not connected to probe1"); |
| 180 |
|
| 181 |
// The probe itself should generate output. The context that we provide |
| 182 |
// to this probe (in this case, the probe name) will help to disambiguate |
| 183 |
// the source of the trace |
| 184 |
connected = probe1->TraceConnect ("Output", probe1->GetName (), MakeCallback (&NotifyViaProbe)); |
| 185 |
NS_ASSERT_MSG (connected, "Trace source not connected to probe1 Output"); |
| 186 |
|
| 187 |
// |
| 188 |
// Probe2 will be hooked to the Emitter trace source object by |
| 189 |
// accessing it by path name in the Config database |
| 190 |
// |
| 191 |
|
| 192 |
// Create another similar probe; this will hook up via a Config path |
| 193 |
Ptr<TimeProbe> probe2 = CreateObject<TimeProbe> (); |
| 194 |
probe2->SetName ("probe2"); |
| 195 |
|
| 196 |
// Note, no return value is checked here |
| 197 |
probe2->ConnectByPath ("/Names/Emitter/Interval"); |
| 198 |
|
| 199 |
// The probe itself should generate output. The context that we provide |
| 200 |
// to this probe (in this case, the probe name) will help to disambiguate |
| 201 |
// the source of the trace |
| 202 |
connected = probe2->TraceConnect ("Output", "probe2", MakeCallback (&NotifyViaProbe)); |
| 203 |
NS_ASSERT_MSG (connected, "Trace source not connected to probe2 Output"); |
| 204 |
|
| 205 |
// |
| 206 |
// Probe3 will be called by the emitter directly through the |
| 207 |
// static method SetValueByPath(). |
| 208 |
// |
| 209 |
Ptr<TimeProbe> probe3 = CreateObject<TimeProbe> (); |
| 210 |
probe3->SetName ("probe3"); |
| 211 |
|
| 212 |
// By adding to the config database, we can access it later |
| 213 |
Names::Add ("/Names/probe3", probe3); |
| 214 |
|
| 215 |
// The probe itself should generate output. The context that we provide |
| 216 |
// to this probe (in this case, the probe name) will help to disambiguate |
| 217 |
// the source of the trace |
| 218 |
connected = probe3->TraceConnect ("Output", "probe3", MakeCallback (&NotifyViaProbe)); |
| 219 |
NS_ASSERT_MSG (connected, "Trace source not connected to probe3 Output"); |
| 220 |
|
| 221 |
// Plot the interval values |
| 222 |
GnuplotHelper plotHelper; |
| 223 |
plotHelper.ConfigurePlot ("time-probe-example", |
| 224 |
"Emitter interarrivals vs. Time", |
| 225 |
"Simulation time (Seconds)", |
| 226 |
"Interarrival time (Seconds)", |
| 227 |
"png"); |
| 228 |
|
| 229 |
// Helper creates a TimeProbe and hooks it to the /Names/Emitter/Interval |
| 230 |
// source. Helper also takes the Output of the TimeProbe and plots it |
| 231 |
// as a dataset labeled 'Emitter Interarrival Time' |
| 232 |
plotHelper.PlotProbe ("ns3::TimeProbe", |
| 233 |
"/Names/Emitter/Interval", |
| 234 |
"Output", |
| 235 |
"Emitter Interarrival Time", |
| 236 |
GnuplotAggregator::KEY_INSIDE); |
| 237 |
|
| 238 |
// The Emitter object is not associated with an ns-3 node, so |
| 239 |
// it won't get started automatically, so we need to do this ourselves |
| 240 |
Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter); |
| 241 |
|
| 242 |
Simulator::Stop (Seconds (stopTime)); |
| 243 |
Simulator::Run (); |
| 244 |
Simulator::Destroy (); |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |