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
wifi-example-apps.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License version 2 as
5
* published by the Free Software Foundation;
6
*
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
11
*
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
*
16
* Authors: Joe Kopena <tjkopena@cs.drexel.edu>
17
*
18
* These applications are used in the WiFi Distance Test experiment,
19
* described and implemented in test02.cc. That file should be in the
20
* same place as this file. The applications have two very simple
21
* jobs, they just generate and receive packets. We could use the
22
* standard Application classes included in the NS-3 distribution.
23
* These have been written just to change the behavior a little, and
24
* provide more examples.
25
*
26
*/
27
28
#include <ostream>
29
30
#include "ns3/core-module.h"
31
#include "ns3/network-module.h"
32
#include "ns3/internet-module.h"
33
34
#include "ns3/stats-module.h"
35
36
#include "
wifi-example-apps.h
"
37
38
using namespace
ns3;
39
40
NS_LOG_COMPONENT_DEFINE
(
"WiFiDistanceApps"
);
41
42
TypeId
43
Sender::GetTypeId
(
void
)
44
{
45
static
TypeId
tid =
TypeId
(
"Sender"
)
46
.
SetParent
<
Application
> ()
47
.AddConstructor<Sender> ()
48
.AddAttribute (
"PacketSize"
,
"The size of packets transmitted."
,
49
UintegerValue
(64),
50
MakeUintegerAccessor (&
Sender::m_pktSize
),
51
MakeUintegerChecker<uint32_t>(1))
52
.AddAttribute (
"Destination"
,
"Target host address."
,
53
Ipv4AddressValue
(
"255.255.255.255"
),
54
MakeIpv4AddressAccessor (&
Sender::m_destAddr
),
55
MakeIpv4AddressChecker ())
56
.AddAttribute (
"Port"
,
"Destination app port."
,
57
UintegerValue
(1603),
58
MakeUintegerAccessor (&
Sender::m_destPort
),
59
MakeUintegerChecker<uint32_t>())
60
.AddAttribute (
"NumPackets"
,
"Total number of packets to send."
,
61
UintegerValue
(30),
62
MakeUintegerAccessor (&
Sender::m_numPkts
),
63
MakeUintegerChecker<uint32_t>(1))
64
.AddAttribute (
"Interval"
,
"Delay between transmissions."
,
65
StringValue
(
"ns3::ConstantRandomVariable[Constant=0.5]"
),
66
MakePointerAccessor (&
Sender::m_interval
),
67
MakePointerChecker <RandomVariableStream>())
68
.AddTraceSource (
"Tx"
,
"A new packet is created and is sent"
,
69
MakeTraceSourceAccessor
(&
Sender::m_txTrace
))
70
;
71
return
tid;
72
}
73
74
75
Sender::Sender
()
76
{
77
NS_LOG_FUNCTION_NOARGS
();
78
m_interval = CreateObject<ConstantRandomVariable> ();
79
m_socket = 0;
80
}
81
82
Sender::~Sender
()
83
{
84
NS_LOG_FUNCTION_NOARGS
();
85
}
86
87
void
88
Sender::DoDispose
(
void
)
89
{
90
NS_LOG_FUNCTION_NOARGS
();
91
92
m_socket = 0;
93
// chain up
94
Application::DoDispose
();
95
}
96
97
void
Sender::StartApplication
()
98
{
99
NS_LOG_FUNCTION_NOARGS
();
100
101
if
(m_socket == 0) {
102
Ptr<SocketFactory>
socketFactory = GetNode ()->GetObject<
SocketFactory
>
103
(
UdpSocketFactory::GetTypeId
());
104
m_socket = socketFactory->
CreateSocket
();
105
m_socket->Bind ();
106
}
107
108
m_count = 0;
109
110
Simulator::Cancel
(m_sendEvent);
111
m_sendEvent =
Simulator::ScheduleNow
(&
Sender::SendPacket
,
this
);
112
113
// end Sender::StartApplication
114
}
115
116
void
Sender::StopApplication
()
117
{
118
NS_LOG_FUNCTION_NOARGS
();
119
Simulator::Cancel
(m_sendEvent);
120
// end Sender::StopApplication
121
}
122
123
void
Sender::SendPacket
()
124
{
125
// NS_LOG_FUNCTION_NOARGS ();
126
NS_LOG_INFO
(
"Sending packet at "
<<
Simulator::Now
() <<
" to "
<<
127
m_destAddr);
128
129
Ptr<Packet>
packet = Create<Packet>(m_pktSize);
130
131
TimestampTag
timestamp;
132
timestamp.
SetTimestamp
(
Simulator::Now
());
133
packet->
AddByteTag
(timestamp);
134
135
// Could connect the socket since the address never changes; using SendTo
136
// here simply because all of the standard apps do not.
137
m_socket->SendTo (packet, 0,
InetSocketAddress
(m_destAddr, m_destPort));
138
139
// Report the event to the trace.
140
m_txTrace (packet);
141
142
if
(++m_count < m_numPkts) {
143
m_sendEvent =
Simulator::Schedule
(Seconds (m_interval->GetValue ()),
144
&
Sender::SendPacket
,
this
);
145
}
146
147
// end Sender::SendPacket
148
}
149
150
151
152
153
//----------------------------------------------------------------------
154
//-- Receiver
155
//------------------------------------------------------
156
TypeId
157
Receiver::GetTypeId
(
void
)
158
{
159
static
TypeId
tid =
TypeId
(
"Receiver"
)
160
.
SetParent
<
Application
> ()
161
.AddConstructor<Receiver> ()
162
.AddAttribute (
"Port"
,
"Listening port."
,
163
UintegerValue
(1603),
164
MakeUintegerAccessor (&
Receiver::m_port
),
165
MakeUintegerChecker<uint32_t>())
166
;
167
return
tid;
168
}
169
170
Receiver::Receiver
() :
171
m_calc (0),
172
m_delay (0)
173
{
174
NS_LOG_FUNCTION_NOARGS
();
175
m_socket
= 0;
176
}
177
178
Receiver::~Receiver
()
179
{
180
NS_LOG_FUNCTION_NOARGS
();
181
}
182
183
void
184
Receiver::DoDispose
(
void
)
185
{
186
NS_LOG_FUNCTION_NOARGS
();
187
188
m_socket
= 0;
189
// chain up
190
Application::DoDispose
();
191
}
192
193
void
194
Receiver::StartApplication
()
195
{
196
NS_LOG_FUNCTION_NOARGS
();
197
198
if
(
m_socket
== 0) {
199
Ptr<SocketFactory>
socketFactory =
GetNode
()->
GetObject
<
SocketFactory
>
200
(
UdpSocketFactory::GetTypeId
());
201
m_socket
= socketFactory->
CreateSocket
();
202
InetSocketAddress
local =
203
InetSocketAddress
(Ipv4Address::GetAny (),
m_port
);
204
m_socket->Bind (local);
205
}
206
207
m_socket
->
SetRecvCallback
(
MakeCallback
(&
Receiver::Receive
,
this
));
208
209
// end Receiver::StartApplication
210
}
211
212
void
213
Receiver::StopApplication
()
214
{
215
NS_LOG_FUNCTION_NOARGS
();
216
217
if
(
m_socket
!= 0) {
218
m_socket
->
SetRecvCallback
(
MakeNullCallback
<
void
,
Ptr<Socket>
> ());
219
}
220
221
// end Receiver::StopApplication
222
}
223
224
void
225
Receiver::SetCounter
(
Ptr
<
CounterCalculator<>
> calc)
226
{
227
m_calc
= calc;
228
// end Receiver::SetCounter
229
}
230
void
231
Receiver::SetDelayTracker
(
Ptr<TimeMinMaxAvgTotalCalculator>
delay)
232
{
233
m_delay
= delay;
234
// end Receiver::SetDelayTracker
235
}
236
237
void
238
Receiver::Receive
(
Ptr<Socket>
socket)
239
{
240
// NS_LOG_FUNCTION (this << socket << packet << from);
241
242
Ptr<Packet>
packet;
243
Address
from;
244
while
((packet = socket->
RecvFrom
(from))) {
245
if
(InetSocketAddress::IsMatchingType (from)) {
246
NS_LOG_INFO
(
"Received "
<< packet->
GetSize
() <<
" bytes from "
<<
247
InetSocketAddress::ConvertFrom (from).GetIpv4 ());
248
}
249
250
TimestampTag
timestamp;
251
// Should never not be found since the sender is adding it, but
252
// you never know.
253
if
(packet->
FindFirstMatchingByteTag
(timestamp)) {
254
Time
tx = timestamp.
GetTimestamp
();
255
256
if
(
m_delay
!= 0) {
257
m_delay
->
Update
(
Simulator::Now
() - tx);
258
}
259
}
260
261
if
(
m_calc
!= 0) {
262
m_calc
->
Update
();
263
}
264
265
// end receiving packets
266
}
267
268
// end Receiver::Receive
269
}
270
271
272
273
274
//----------------------------------------------------------------------
275
//-- TimestampTag
276
//------------------------------------------------------
277
TypeId
278
TimestampTag::GetTypeId
(
void
)
279
{
280
static
TypeId
tid =
TypeId
(
"TimestampTag"
)
281
.
SetParent
<
Tag
> ()
282
.AddConstructor<TimestampTag> ()
283
.AddAttribute (
"Timestamp"
,
284
"Some momentous point in time!"
,
285
EmptyAttributeValue
(),
286
MakeTimeAccessor (&
TimestampTag::GetTimestamp
),
287
MakeTimeChecker
())
288
;
289
return
tid;
290
}
291
TypeId
292
TimestampTag::GetInstanceTypeId
(
void
)
const
293
{
294
return
GetTypeId
();
295
}
296
297
uint32_t
298
TimestampTag::GetSerializedSize
(
void
)
const
299
{
300
return
8;
301
}
302
void
303
TimestampTag::Serialize
(
TagBuffer
i)
const
304
{
305
int64_t t =
m_timestamp
.
GetNanoSeconds
();
306
i.
Write
((
const
uint8_t *)&t, 8);
307
}
308
void
309
TimestampTag::Deserialize
(
TagBuffer
i)
310
{
311
int64_t t;
312
i.
Read
((uint8_t *)&t, 8);
313
m_timestamp
= NanoSeconds (t);
314
}
315
316
void
317
TimestampTag::SetTimestamp
(
Time
time)
318
{
319
m_timestamp
= time;
320
}
321
Time
322
TimestampTag::GetTimestamp
(
void
)
const
323
{
324
return
m_timestamp
;
325
}
326
327
void
328
TimestampTag::Print
(std::ostream &os)
const
329
{
330
os <<
"t="
<<
m_timestamp
;
331
}
examples
stats
wifi-example-apps.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2