A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
49
Ptr<ExponentialRandomVariable>
m_var
;
50
51
};
52
53
NS_OBJECT_ENSURE_REGISTERED
(
Emitter
);
54
55
TypeId
56
Emitter::GetTypeId
(
void
)
57
{
58
static
TypeId
tid =
TypeId
(
"ns3::Emitter"
)
59
.
AddConstructor
<
Emitter
> ()
60
.SetParent<Object> ()
61
.AddTraceSource (
"Counter"
,
62
"sample counter"
,
63
MakeTraceSourceAccessor
(&
Emitter::m_counter
))
64
;
65
return
tid;
66
}
67
68
Emitter::Emitter
(
void
)
69
{
70
NS_LOG_FUNCTION
(
this
);
71
m_counter = 0;
72
m_var = CreateObject<ExponentialRandomVariable> ();
73
}
74
75
void
76
Emitter::DoInitialize
(
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
84
Emitter::Emit
(
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
92
Emitter::Count
(
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
();
194
Simulator::Destroy
();
195
196
return
0;
197
}
src
stats
examples
double-probe-example.cc
Generated on Fri Aug 30 2013 01:43:02 for ns-3 by
1.8.1.2