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
onoff-application.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
//
3
// Copyright (c) 2006 Georgia Tech Research Corporation
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
// Author: George F. Riley<riley@ece.gatech.edu>
19
//
20
21
// ns3 - On/Off Data Source Application class
22
// George F. Riley, Georgia Tech, Spring 2007
23
// Adapted from ApplicationOnOff in GTNetS.
24
25
#include "ns3/log.h"
26
#include "ns3/address.h"
27
#include "ns3/inet-socket-address.h"
28
#include "ns3/inet6-socket-address.h"
29
#include "ns3/node.h"
30
#include "ns3/nstime.h"
31
#include "ns3/data-rate.h"
32
#include "ns3/random-variable-stream.h"
33
#include "ns3/socket.h"
34
#include "ns3/simulator.h"
35
#include "ns3/socket-factory.h"
36
#include "ns3/packet.h"
37
#include "ns3/uinteger.h"
38
#include "ns3/trace-source-accessor.h"
39
#include "
onoff-application.h
"
40
#include "ns3/udp-socket-factory.h"
41
#include "ns3/string.h"
42
#include "ns3/pointer.h"
43
44
NS_LOG_COMPONENT_DEFINE
(
"OnOffApplication"
);
45
46
namespace
ns3 {
47
48
NS_OBJECT_ENSURE_REGISTERED
(OnOffApplication);
49
50
TypeId
51
OnOffApplication::GetTypeId
(
void
)
52
{
53
static
TypeId
tid =
TypeId
(
"ns3::OnOffApplication"
)
54
.
SetParent
<
Application
> ()
55
.AddConstructor<OnOffApplication> ()
56
.AddAttribute (
"DataRate"
,
"The data rate in on state."
,
57
DataRateValue
(
DataRate
(
"500kb/s"
)),
58
MakeDataRateAccessor (&
OnOffApplication::m_cbrRate
),
59
MakeDataRateChecker ())
60
.AddAttribute (
"PacketSize"
,
"The size of packets sent in on state"
,
61
UintegerValue
(512),
62
MakeUintegerAccessor (&
OnOffApplication::m_pktSize
),
63
MakeUintegerChecker<uint32_t> (1))
64
.AddAttribute (
"Remote"
,
"The address of the destination"
,
65
AddressValue
(),
66
MakeAddressAccessor (&
OnOffApplication::m_peer
),
67
MakeAddressChecker ())
68
.AddAttribute (
"OnTime"
,
"A RandomVariableStream used to pick the duration of the 'On' state."
,
69
StringValue
(
"ns3::ConstantRandomVariable[Constant=1.0]"
),
70
MakePointerAccessor (&
OnOffApplication::m_onTime
),
71
MakePointerChecker <RandomVariableStream>())
72
.AddAttribute (
"OffTime"
,
"A RandomVariableStream used to pick the duration of the 'Off' state."
,
73
StringValue
(
"ns3::ConstantRandomVariable[Constant=1.0]"
),
74
MakePointerAccessor (&
OnOffApplication::m_offTime
),
75
MakePointerChecker <RandomVariableStream>())
76
.AddAttribute (
"MaxBytes"
,
77
"The total number of bytes to send. Once these bytes are sent, "
78
"no packet is sent again, even in on state. The value zero means "
79
"that there is no limit."
,
80
UintegerValue
(0),
81
MakeUintegerAccessor (&
OnOffApplication::m_maxBytes
),
82
MakeUintegerChecker<uint32_t> ())
83
.AddAttribute (
"Protocol"
,
"The type of protocol to use."
,
84
TypeIdValue
(
UdpSocketFactory::GetTypeId
()),
85
MakeTypeIdAccessor (&
OnOffApplication::m_tid
),
86
MakeTypeIdChecker ())
87
.AddTraceSource (
"Tx"
,
"A new packet is created and is sent"
,
88
MakeTraceSourceAccessor
(&
OnOffApplication::m_txTrace
))
89
;
90
return
tid;
91
}
92
93
94
OnOffApplication::OnOffApplication
()
95
: m_socket (0),
96
m_connected (false),
97
m_residualBits (0),
98
m_lastStartTime (
Seconds
(0)),
99
m_totBytes (0)
100
{
101
NS_LOG_FUNCTION
(
this
);
102
}
103
104
OnOffApplication::~OnOffApplication
()
105
{
106
NS_LOG_FUNCTION
(
this
);
107
}
108
109
void
110
OnOffApplication::SetMaxBytes
(uint32_t maxBytes)
111
{
112
NS_LOG_FUNCTION
(
this
<< maxBytes);
113
m_maxBytes
= maxBytes;
114
}
115
116
Ptr<Socket>
117
OnOffApplication::GetSocket
(
void
)
const
118
{
119
NS_LOG_FUNCTION
(
this
);
120
return
m_socket
;
121
}
122
123
int64_t
124
OnOffApplication::AssignStreams
(int64_t stream)
125
{
126
NS_LOG_FUNCTION
(
this
<< stream);
127
m_onTime
->
SetStream
(stream);
128
m_offTime
->
SetStream
(stream + 1);
129
return
2;
130
}
131
132
void
133
OnOffApplication::DoDispose
(
void
)
134
{
135
NS_LOG_FUNCTION
(
this
);
136
137
m_socket
= 0;
138
// chain up
139
Application::DoDispose
();
140
}
141
142
// Application Methods
143
void
OnOffApplication::StartApplication
()
// Called at time specified by Start
144
{
145
NS_LOG_FUNCTION
(
this
);
146
147
// Create the socket if not already
148
if
(!
m_socket
)
149
{
150
m_socket
=
Socket::CreateSocket
(
GetNode
(),
m_tid
);
151
m_socket
->
Bind
();
152
m_socket
->
Connect
(
m_peer
);
153
m_socket
->
SetAllowBroadcast
(
true
);
154
m_socket
->
ShutdownRecv
();
155
156
m_socket
->
SetConnectCallback
(
157
MakeCallback
(&
OnOffApplication::ConnectionSucceeded
,
this
),
158
MakeCallback
(&
OnOffApplication::ConnectionFailed
,
this
));
159
}
160
// Insure no pending event
161
CancelEvents
();
162
// If we are not yet connected, there is nothing to do here
163
// The ConnectionComplete upcall will start timers at that time
164
//if (!m_connected) return;
165
ScheduleStartEvent
();
166
}
167
168
void
OnOffApplication::StopApplication
()
// Called at time specified by Stop
169
{
170
NS_LOG_FUNCTION
(
this
);
171
172
CancelEvents
();
173
if
(
m_socket
!= 0)
174
{
175
m_socket
->
Close
();
176
}
177
else
178
{
179
NS_LOG_WARN
(
"OnOffApplication found null socket to close in StopApplication"
);
180
}
181
}
182
183
void
OnOffApplication::CancelEvents
()
184
{
185
NS_LOG_FUNCTION
(
this
);
186
187
if
(
m_sendEvent
.
IsRunning
())
188
{
// Cancel the pending send packet event
189
// Calculate residual bits since last packet sent
190
Time
delta (
Simulator::Now
() -
m_lastStartTime
);
191
int64x64_t bits = delta.
To
(
Time::S
) *
m_cbrRate
.
GetBitRate
();
192
m_residualBits
+= bits.GetHigh ();
193
}
194
Simulator::Cancel
(
m_sendEvent
);
195
Simulator::Cancel
(
m_startStopEvent
);
196
}
197
198
// Event handlers
199
void
OnOffApplication::StartSending
()
200
{
201
NS_LOG_FUNCTION
(
this
);
202
m_lastStartTime
=
Simulator::Now
();
203
ScheduleNextTx
();
// Schedule the send packet event
204
ScheduleStopEvent
();
205
}
206
207
void
OnOffApplication::StopSending
()
208
{
209
NS_LOG_FUNCTION
(
this
);
210
CancelEvents
();
211
212
ScheduleStartEvent
();
213
}
214
215
// Private helpers
216
void
OnOffApplication::ScheduleNextTx
()
217
{
218
NS_LOG_FUNCTION
(
this
);
219
220
if
(
m_maxBytes
== 0 ||
m_totBytes
<
m_maxBytes
)
221
{
222
uint32_t bits =
m_pktSize
* 8 -
m_residualBits
;
223
NS_LOG_LOGIC
(
"bits = "
<< bits);
224
Time
nextTime (
Seconds
(bits /
225
static_cast<double>(
m_cbrRate
.
GetBitRate
())));
// Time till next packet
226
NS_LOG_LOGIC
(
"nextTime = "
<< nextTime);
227
m_sendEvent
=
Simulator::Schedule
(nextTime,
228
&
OnOffApplication::SendPacket
,
this
);
229
}
230
else
231
{
// All done, cancel any pending events
232
StopApplication
();
233
}
234
}
235
236
void
OnOffApplication::ScheduleStartEvent
()
237
{
// Schedules the event to start sending data (switch to the "On" state)
238
NS_LOG_FUNCTION
(
this
);
239
240
Time
offInterval =
Seconds
(
m_offTime
->
GetValue
());
241
NS_LOG_LOGIC
(
"start at "
<< offInterval);
242
m_startStopEvent
=
Simulator::Schedule
(offInterval, &
OnOffApplication::StartSending
,
this
);
243
}
244
245
void
OnOffApplication::ScheduleStopEvent
()
246
{
// Schedules the event to stop sending data (switch to "Off" state)
247
NS_LOG_FUNCTION
(
this
);
248
249
Time
onInterval =
Seconds
(
m_onTime
->
GetValue
());
250
NS_LOG_LOGIC
(
"stop at "
<< onInterval);
251
m_startStopEvent
=
Simulator::Schedule
(onInterval, &
OnOffApplication::StopSending
,
this
);
252
}
253
254
255
void
OnOffApplication::SendPacket
()
256
{
257
NS_LOG_FUNCTION
(
this
);
258
259
NS_ASSERT
(
m_sendEvent
.
IsExpired
());
260
Ptr<Packet>
packet = Create<Packet> (
m_pktSize
);
261
m_txTrace
(packet);
262
m_socket
->
Send
(packet);
263
m_totBytes
+=
m_pktSize
;
264
if
(
InetSocketAddress::IsMatchingType
(
m_peer
))
265
{
266
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds ()
267
<<
"s on-off application sent "
268
<< packet->GetSize () <<
" bytes to "
269
<<
InetSocketAddress::ConvertFrom
(
m_peer
).
GetIpv4
()
270
<<
" port "
<<
InetSocketAddress::ConvertFrom
(
m_peer
).
GetPort
()
271
<<
" total Tx "
<<
m_totBytes
<<
" bytes"
);
272
}
273
else
if
(
Inet6SocketAddress::IsMatchingType
(
m_peer
))
274
{
275
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds ()
276
<<
"s on-off application sent "
277
<< packet->GetSize () <<
" bytes to "
278
<<
Inet6SocketAddress::ConvertFrom
(
m_peer
).
GetIpv6
()
279
<<
" port "
<<
Inet6SocketAddress::ConvertFrom
(
m_peer
).
GetPort
()
280
<<
" total Tx "
<<
m_totBytes
<<
" bytes"
);
281
}
282
m_lastStartTime
=
Simulator::Now
();
283
m_residualBits
= 0;
284
ScheduleNextTx
();
285
}
286
287
288
void
OnOffApplication::ConnectionSucceeded
(
Ptr<Socket>
socket)
289
{
290
NS_LOG_FUNCTION
(
this
<< socket);
291
m_connected
=
true
;
292
}
293
294
void
OnOffApplication::ConnectionFailed
(
Ptr<Socket>
socket)
295
{
296
NS_LOG_FUNCTION
(
this
<< socket);
297
}
298
299
300
}
// Namespace ns3
src
applications
model
onoff-application.cc
Generated on Fri Dec 21 2012 19:00:31 for ns-3 by
1.8.1.2