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
udp-echo-client.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright 2007 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
#include "ns3/log.h"
19
#include "ns3/ipv4-address.h"
20
#include "ns3/ipv6-address.h"
21
#include "ns3/nstime.h"
22
#include "ns3/inet-socket-address.h"
23
#include "ns3/inet6-socket-address.h"
24
#include "ns3/socket.h"
25
#include "ns3/simulator.h"
26
#include "ns3/socket-factory.h"
27
#include "ns3/packet.h"
28
#include "ns3/uinteger.h"
29
#include "ns3/trace-source-accessor.h"
30
#include "
udp-echo-client.h
"
31
32
namespace
ns3 {
33
34
NS_LOG_COMPONENT_DEFINE
(
"UdpEchoClientApplication"
);
35
NS_OBJECT_ENSURE_REGISTERED
(UdpEchoClient);
36
37
TypeId
38
UdpEchoClient::GetTypeId
(
void
)
39
{
40
static
TypeId
tid =
TypeId
(
"ns3::UdpEchoClient"
)
41
.
SetParent
<
Application
> ()
42
.AddConstructor<UdpEchoClient> ()
43
.AddAttribute (
"MaxPackets"
,
44
"The maximum number of packets the application will send"
,
45
UintegerValue
(100),
46
MakeUintegerAccessor (&
UdpEchoClient::m_count
),
47
MakeUintegerChecker<uint32_t> ())
48
.AddAttribute (
"Interval"
,
49
"The time to wait between packets"
,
50
TimeValue
(
Seconds
(1.0)),
51
MakeTimeAccessor (&
UdpEchoClient::m_interval
),
52
MakeTimeChecker ())
53
.AddAttribute (
"RemoteAddress"
,
54
"The destination Address of the outbound packets"
,
55
AddressValue
(),
56
MakeAddressAccessor (&
UdpEchoClient::m_peerAddress
),
57
MakeAddressChecker ())
58
.AddAttribute (
"RemotePort"
,
59
"The destination port of the outbound packets"
,
60
UintegerValue
(0),
61
MakeUintegerAccessor (&
UdpEchoClient::m_peerPort
),
62
MakeUintegerChecker<uint16_t> ())
63
.AddAttribute (
"PacketSize"
,
"Size of echo data in outbound packets"
,
64
UintegerValue
(100),
65
MakeUintegerAccessor (&
UdpEchoClient::SetDataSize
,
66
&
UdpEchoClient::GetDataSize
),
67
MakeUintegerChecker<uint32_t> ())
68
.AddTraceSource (
"Tx"
,
"A new packet is created and is sent"
,
69
MakeTraceSourceAccessor
(&
UdpEchoClient::m_txTrace
))
70
;
71
return
tid;
72
}
73
74
UdpEchoClient::UdpEchoClient
()
75
{
76
NS_LOG_FUNCTION
(
this
);
77
m_sent
= 0;
78
m_socket
= 0;
79
m_sendEvent
=
EventId
();
80
m_data
= 0;
81
m_dataSize
= 0;
82
}
83
84
UdpEchoClient::~UdpEchoClient
()
85
{
86
NS_LOG_FUNCTION
(
this
);
87
m_socket
= 0;
88
89
delete
[]
m_data
;
90
m_data
= 0;
91
m_dataSize
= 0;
92
}
93
94
void
95
UdpEchoClient::SetRemote
(
Address
ip, uint16_t
port
)
96
{
97
NS_LOG_FUNCTION
(
this
<< ip << port);
98
m_peerAddress
= ip;
99
m_peerPort
=
port
;
100
}
101
102
void
103
UdpEchoClient::SetRemote
(
Ipv4Address
ip, uint16_t
port
)
104
{
105
NS_LOG_FUNCTION
(
this
<< ip << port);
106
m_peerAddress
=
Address
(ip);
107
m_peerPort
=
port
;
108
}
109
110
void
111
UdpEchoClient::SetRemote
(
Ipv6Address
ip, uint16_t
port
)
112
{
113
NS_LOG_FUNCTION
(
this
<< ip << port);
114
m_peerAddress
=
Address
(ip);
115
m_peerPort
=
port
;
116
}
117
118
void
119
UdpEchoClient::DoDispose
(
void
)
120
{
121
NS_LOG_FUNCTION
(
this
);
122
Application::DoDispose
();
123
}
124
125
void
126
UdpEchoClient::StartApplication
(
void
)
127
{
128
NS_LOG_FUNCTION
(
this
);
129
130
if
(
m_socket
== 0)
131
{
132
TypeId
tid =
TypeId::LookupByName
(
"ns3::UdpSocketFactory"
);
133
m_socket
=
Socket::CreateSocket
(
GetNode
(), tid);
134
if
(
Ipv4Address::IsMatchingType
(
m_peerAddress
) ==
true
)
135
{
136
m_socket
->
Bind
();
137
m_socket
->
Connect
(
InetSocketAddress
(
Ipv4Address::ConvertFrom
(
m_peerAddress
),
m_peerPort
));
138
}
139
else
if
(
Ipv6Address::IsMatchingType
(
m_peerAddress
) ==
true
)
140
{
141
m_socket
->
Bind6
();
142
m_socket
->
Connect
(
Inet6SocketAddress
(
Ipv6Address::ConvertFrom
(
m_peerAddress
),
m_peerPort
));
143
}
144
}
145
146
m_socket
->
SetRecvCallback
(
MakeCallback
(&
UdpEchoClient::HandleRead
,
this
));
147
148
ScheduleTransmit
(
Seconds
(0.));
149
}
150
151
void
152
UdpEchoClient::StopApplication
()
153
{
154
NS_LOG_FUNCTION
(
this
);
155
156
if
(
m_socket
!= 0)
157
{
158
m_socket
->
Close
();
159
m_socket
->
SetRecvCallback
(
MakeNullCallback
<
void
,
Ptr<Socket>
> ());
160
m_socket
= 0;
161
}
162
163
Simulator::Cancel
(
m_sendEvent
);
164
}
165
166
void
167
UdpEchoClient::SetDataSize
(uint32_t dataSize)
168
{
169
NS_LOG_FUNCTION
(
this
<< dataSize);
170
171
//
172
// If the client is setting the echo packet data size this way, we infer
173
// that she doesn't care about the contents of the packet at all, so
174
// neither will we.
175
//
176
delete
[]
m_data
;
177
m_data
= 0;
178
m_dataSize
= 0;
179
m_size
= dataSize;
180
}
181
182
uint32_t
183
UdpEchoClient::GetDataSize
(
void
)
const
184
{
185
NS_LOG_FUNCTION
(
this
);
186
return
m_size
;
187
}
188
189
void
190
UdpEchoClient::SetFill
(std::string fill)
191
{
192
NS_LOG_FUNCTION
(
this
<< fill);
193
194
uint32_t dataSize = fill.size () + 1;
195
196
if
(dataSize !=
m_dataSize
)
197
{
198
delete
[]
m_data
;
199
m_data
=
new
uint8_t [dataSize];
200
m_dataSize
= dataSize;
201
}
202
203
memcpy (
m_data
, fill.c_str (), dataSize);
204
205
//
206
// Overwrite packet size attribute.
207
//
208
m_size
= dataSize;
209
}
210
211
void
212
UdpEchoClient::SetFill
(uint8_t fill, uint32_t dataSize)
213
{
214
NS_LOG_FUNCTION
(
this
<< fill << dataSize);
215
if
(dataSize !=
m_dataSize
)
216
{
217
delete
[]
m_data
;
218
m_data
=
new
uint8_t [dataSize];
219
m_dataSize
= dataSize;
220
}
221
222
memset (
m_data
, fill, dataSize);
223
224
//
225
// Overwrite packet size attribute.
226
//
227
m_size
= dataSize;
228
}
229
230
void
231
UdpEchoClient::SetFill
(uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
232
{
233
NS_LOG_FUNCTION
(
this
<< fill << fillSize << dataSize);
234
if
(dataSize !=
m_dataSize
)
235
{
236
delete
[]
m_data
;
237
m_data
=
new
uint8_t [dataSize];
238
m_dataSize
= dataSize;
239
}
240
241
if
(fillSize >= dataSize)
242
{
243
memcpy (
m_data
, fill, dataSize);
244
m_size
= dataSize;
245
return
;
246
}
247
248
//
249
// Do all but the final fill.
250
//
251
uint32_t filled = 0;
252
while
(filled + fillSize < dataSize)
253
{
254
memcpy (&
m_data
[filled], fill, fillSize);
255
filled += fillSize;
256
}
257
258
//
259
// Last fill may be partial
260
//
261
memcpy (&
m_data
[filled], fill, dataSize - filled);
262
263
//
264
// Overwrite packet size attribute.
265
//
266
m_size
= dataSize;
267
}
268
269
void
270
UdpEchoClient::ScheduleTransmit
(
Time
dt)
271
{
272
NS_LOG_FUNCTION
(
this
<< dt);
273
m_sendEvent
=
Simulator::Schedule
(dt, &
UdpEchoClient::Send
,
this
);
274
}
275
276
void
277
UdpEchoClient::Send
(
void
)
278
{
279
NS_LOG_FUNCTION
(
this
);
280
281
NS_ASSERT
(
m_sendEvent
.
IsExpired
());
282
283
Ptr<Packet>
p;
284
if
(
m_dataSize
)
285
{
286
//
287
// If m_dataSize is non-zero, we have a data buffer of the same size that we
288
// are expected to copy and send. This state of affairs is created if one of
289
// the Fill functions is called. In this case, m_size must have been set
290
// to agree with m_dataSize
291
//
292
NS_ASSERT_MSG
(
m_dataSize
==
m_size
,
"UdpEchoClient::Send(): m_size and m_dataSize inconsistent"
);
293
NS_ASSERT_MSG
(
m_data
,
"UdpEchoClient::Send(): m_dataSize but no m_data"
);
294
p = Create<Packet> (
m_data
,
m_dataSize
);
295
}
296
else
297
{
298
//
299
// If m_dataSize is zero, the client has indicated that she doesn't care
300
// about the data itself either by specifying the data size by setting
301
// the corresponding atribute or by not calling a SetFill function. In
302
// this case, we don't worry about it either. But we do allow m_size
303
// to have a value different from the (zero) m_dataSize.
304
//
305
p = Create<Packet> (
m_size
);
306
}
307
// call to the trace sinks before the packet is actually sent,
308
// so that tags added to the packet can be sent as well
309
m_txTrace
(p);
310
m_socket
->
Send
(p);
311
312
++
m_sent
;
313
314
if
(
Ipv4Address::IsMatchingType
(
m_peerAddress
))
315
{
316
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds () <<
"s client sent "
<<
m_size
<<
" bytes to "
<<
317
Ipv4Address::ConvertFrom
(
m_peerAddress
) <<
" port "
<<
m_peerPort
);
318
}
319
else
if
(
Ipv6Address::IsMatchingType
(
m_peerAddress
))
320
{
321
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds () <<
"s client sent "
<<
m_size
<<
" bytes to "
<<
322
Ipv6Address::ConvertFrom
(
m_peerAddress
) <<
" port "
<<
m_peerPort
);
323
}
324
325
if
(
m_sent
<
m_count
)
326
{
327
ScheduleTransmit
(
m_interval
);
328
}
329
}
330
331
void
332
UdpEchoClient::HandleRead
(
Ptr<Socket>
socket)
333
{
334
NS_LOG_FUNCTION
(
this
<< socket);
335
Ptr<Packet>
packet;
336
Address
from;
337
while
((packet = socket->
RecvFrom
(from)))
338
{
339
if
(
InetSocketAddress::IsMatchingType
(from))
340
{
341
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds () <<
"s client received "
<< packet->
GetSize
() <<
" bytes from "
<<
342
InetSocketAddress::ConvertFrom
(from).
GetIpv4
() <<
" port "
<<
343
InetSocketAddress::ConvertFrom
(from).
GetPort
());
344
}
345
else
if
(
Inet6SocketAddress::IsMatchingType
(from))
346
{
347
NS_LOG_INFO
(
"At time "
<<
Simulator::Now
().GetSeconds () <<
"s client received "
<< packet->
GetSize
() <<
" bytes from "
<<
348
Inet6SocketAddress::ConvertFrom
(from).
GetIpv6
() <<
" port "
<<
349
Inet6SocketAddress::ConvertFrom
(from).
GetPort
());
350
}
351
}
352
}
353
354
}
// Namespace ns3
src
applications
model
udp-echo-client.cc
Generated on Tue May 14 2013 11:08:16 for ns-3 by
1.8.1.2