A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-echo-client.cc
Go to the documentation of this file.
1/*
2 * Copyright 2007 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6#include "udp-echo-client.h"
7
8#include "ns3/address-utils.h"
9#include "ns3/log.h"
10#include "ns3/nstime.h"
11#include "ns3/packet.h"
12#include "ns3/simulator.h"
13#include "ns3/socket-factory.h"
14#include "ns3/socket.h"
15#include "ns3/trace-source-accessor.h"
16#include "ns3/uinteger.h"
17
18namespace ns3
19{
20
21NS_LOG_COMPONENT_DEFINE("UdpEchoClientApplication");
22
24
27{
28 static TypeId tid =
29 TypeId("ns3::UdpEchoClient")
31 .SetGroupName("Applications")
32 .AddConstructor<UdpEchoClient>()
33 .AddAttribute(
34 "MaxPackets",
35 "The maximum number of packets the application will send (zero means infinite)",
36 UintegerValue(100),
39 .AddAttribute("Interval",
40 "The time to wait between packets",
44 // NS_DEPRECATED_3_44
45 .AddAttribute(
46 "RemoteAddress",
47 "The destination Address of the outbound packets",
50 // this is needed to indicate which version of the function overload to use
51 static_cast<void (UdpEchoClient::*)(const Address&)>(&UdpEchoClient::SetRemote),
55 "Replaced by Remote in ns-3.44.")
56 // NS_DEPRECATED_3_44
57 .AddAttribute("RemotePort",
58 "The destination port of the outbound packets",
63 "Replaced by Remote in ns-3.44.")
64 .AddAttribute(
65 "PacketSize",
66 "Size of echo data in outbound packets",
67 UintegerValue(100),
70 .AddTraceSource("Rx",
71 "A packet has been received",
73 "ns3::Packet::TracedCallback")
74 .AddTraceSource("TxWithAddresses",
75 "A new packet is created and is sent",
77 "ns3::Packet::TwoAddressTracedCallback")
78 .AddTraceSource("RxWithAddresses",
79 "A packet has been received",
81 "ns3::Packet::TwoAddressTracedCallback");
82 return tid;
83}
84
86 : SourceApplication(false)
87{
88 NS_LOG_FUNCTION(this);
89 m_protocolTid = TypeId::LookupByName("ns3::UdpSocketFactory");
90}
91
93{
94 NS_LOG_FUNCTION(this);
95
96 delete[] m_data;
97 m_data = nullptr;
98 m_dataSize = 0;
99}
100
101void
103{
104 NS_LOG_FUNCTION(this << ip << port);
105 SetRemote(ip);
106 SetPort(port);
107}
108
109void
111{
112 NS_LOG_FUNCTION(this << addr);
113 if (!addr.IsInvalid())
114 {
115 m_peer = addr;
116 if (m_peerPort)
117 {
119 }
120 }
121}
122
125{
126 return m_peer;
127}
128
129void
131{
132 NS_LOG_FUNCTION(this << port);
133 if (m_peer.IsInvalid())
134 {
135 // save for later
137 return;
138 }
140 {
142 }
143}
144
145uint16_t
147{
148 if (m_peer.IsInvalid())
149 {
151 }
153 {
155 }
157 {
159 }
161}
162
163void
165{
166 NS_LOG_FUNCTION(this);
167 m_socket->SetRecvCallback(MakeCallback(&UdpEchoClient::HandleRead, this));
168 m_socket->SetAllowBroadcast(true);
170}
171
172void
174{
175 NS_LOG_FUNCTION(this << dataSize);
176
177 //
178 // If the client is setting the echo packet data size this way, we infer
179 // that she doesn't care about the contents of the packet at all, so
180 // neither will we.
181 //
182 delete[] m_data;
183 m_data = nullptr;
184 m_dataSize = 0;
185 m_size = dataSize;
186}
187
190{
191 return m_size;
192}
193
194void
195UdpEchoClient::SetFill(std::string fill)
196{
197 NS_LOG_FUNCTION(this << fill);
198
199 uint32_t dataSize = fill.size() + 1;
200
201 if (dataSize != m_dataSize)
202 {
203 delete[] m_data;
204 m_data = new uint8_t[dataSize];
205 m_dataSize = dataSize;
206 }
207
208 memcpy(m_data, fill.c_str(), dataSize);
209
210 //
211 // Overwrite packet size attribute.
212 //
213 m_size = dataSize;
214}
215
216void
217UdpEchoClient::SetFill(uint8_t fill, uint32_t dataSize)
218{
219 NS_LOG_FUNCTION(this << fill << dataSize);
220 if (dataSize != m_dataSize)
221 {
222 delete[] m_data;
223 m_data = new uint8_t[dataSize];
224 m_dataSize = dataSize;
225 }
226
227 memset(m_data, fill, dataSize);
228
229 //
230 // Overwrite packet size attribute.
231 //
232 m_size = dataSize;
233}
234
235void
236UdpEchoClient::SetFill(uint8_t* fill, uint32_t fillSize, uint32_t dataSize)
237{
238 NS_LOG_FUNCTION(this << fill << fillSize << dataSize);
239 if (dataSize != m_dataSize)
240 {
241 delete[] m_data;
242 m_data = new uint8_t[dataSize];
243 m_dataSize = dataSize;
244 }
245
246 if (fillSize >= dataSize)
247 {
248 memcpy(m_data, fill, dataSize);
249 m_size = dataSize;
250 return;
251 }
252
253 //
254 // Do all but the final fill.
255 //
256 uint32_t filled = 0;
257 while (filled + fillSize < dataSize)
258 {
259 memcpy(&m_data[filled], fill, fillSize);
260 filled += fillSize;
261 }
262
263 //
264 // Last fill may be partial
265 //
266 memcpy(&m_data[filled], fill, dataSize - filled);
267
268 //
269 // Overwrite packet size attribute.
270 //
271 m_size = dataSize;
272}
273
274void
280
281void
283{
284 NS_LOG_FUNCTION(this);
285
286 NS_ASSERT(m_sendEvent.IsExpired());
287
288 Ptr<Packet> p;
289 if (m_dataSize)
290 {
291 //
292 // If m_dataSize is non-zero, we have a data buffer of the same size that we
293 // are expected to copy and send. This state of affairs is created if one of
294 // the Fill functions is called. In this case, m_size must have been set
295 // to agree with m_dataSize
296 //
298 "UdpEchoClient::Send(): m_size and m_dataSize inconsistent");
299 NS_ASSERT_MSG(m_data, "UdpEchoClient::Send(): m_dataSize but no m_data");
301 }
302 else
303 {
304 //
305 // If m_dataSize is zero, the client has indicated that it doesn't care
306 // about the data itself either by specifying the data size by setting
307 // the corresponding attribute or by not calling a SetFill function. In
308 // this case, we don't worry about it either. But we do allow m_size
309 // to have a value different from the (zero) m_dataSize.
310 //
312 }
313 Address localAddress;
314 m_socket->GetSockName(localAddress);
315 // call to the trace sinks before the packet is actually sent,
316 // so that tags added to the packet can be sent as well
317 m_txTrace(p);
318 m_txTraceWithAddresses(p, localAddress, m_peer);
319 m_socket->Send(p);
320 ++m_sent;
321
323 {
324 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " client sent " << m_size
325 << " bytes to " << InetSocketAddress::ConvertFrom(m_peer).GetIpv4()
327 }
329 {
330 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " client sent " << m_size
331 << " bytes to " << Inet6SocketAddress::ConvertFrom(m_peer).GetIpv6()
333 }
334
335 if (m_sent < m_count || m_count == 0)
336 {
338 }
339}
340
341void
343{
344 NS_LOG_FUNCTION(this << socket);
345 Address from;
346 while (auto packet = socket->RecvFrom(from))
347 {
349 {
350 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " client received "
351 << packet->GetSize() << " bytes from "
352 << InetSocketAddress::ConvertFrom(from).GetIpv4() << " port "
353 << InetSocketAddress::ConvertFrom(from).GetPort());
354 }
356 {
357 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " client received "
358 << packet->GetSize() << " bytes from "
359 << Inet6SocketAddress::ConvertFrom(from).GetIpv6() << " port "
360 << Inet6SocketAddress::ConvertFrom(from).GetPort());
361 }
362 Address localAddress;
363 socket->GetSockName(localAddress);
364 m_rxTrace(packet);
365 m_rxTraceWithAddresses(packet, from, localAddress);
366 }
367}
368
369void
375
376} // Namespace ns3
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
AttributeValue implementation for Address.
Definition address.h:275
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
uint16_t GetPort() const
Get the port.
static bool IsMatchingType(const Address &addr)
If the address match.
static bool IsMatchingType(const Address &address)
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
static bool IsMatchingType(const Address &address)
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition simulator.cc:274
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
TypeId m_protocolTid
Protocol TypeId value.
SourceApplication(bool allowPacketSocket=true)
Constructor.
Ptr< Socket > m_socket
Socket.
TracedCallback< Ptr< const Packet > > m_txTrace
Traced Callback: transmitted packets.
Address m_peer
Peer address.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
@ S
second
Definition nstime.h:107
AttributeValue implementation for Time.
Definition nstime.h:1456
a unique identifier for an interface.
Definition type-id.h:49
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
@ DEPRECATED
Attribute or trace source is deprecated; user is warned.
Definition type-id.h:65
A Udp Echo client.
void SetFill(std::string fill)
Set the data fill of the packet (what is sent as data to the server) to the zero-terminated contents ...
void CancelEvents() override
Cancel all pending events.
Time m_interval
Packet inter-send time.
void Send()
Send a packet.
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
static constexpr uint16_t DEFAULT_PORT
default port
EventId m_sendEvent
Event to send the next packet.
uint32_t m_size
Size of the sent packet.
void SetRemote(const Address &ip, uint16_t port)
set the remote address and port
uint32_t GetDataSize() const
Get the number of data bytes that will be sent to the server.
uint16_t GetPort() const
Get the remote port (temporary function until deprecated attributes are removed).
uint32_t m_count
Maximum number of packets the application will send.
std::optional< uint16_t > m_peerPort
Remote peer port (deprecated) // NS_DEPRECATED_3_44.
static TypeId GetTypeId()
Get the type ID.
uint8_t * m_data
packet payload data
void SetPort(uint16_t port)
Set the remote port (temporary function until deprecated attributes are removed).
uint32_t m_sent
Counter for sent packets.
void ScheduleTransmit(Time dt)
Schedule the next packet transmission.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_txTraceWithAddresses
Callbacks for tracing the packet Tx events, includes source and destination addresses.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callbacks for tracing the packet Rx events, includes source and destination addresses.
void SetDataSize(uint32_t dataSize)
Set the data size of the packet (the number of bytes that are sent as data to the server).
TracedCallback< Ptr< const Packet > > m_rxTrace
Callbacks for tracing the packet Rx events.
Address GetRemote() const
Get the remote address (temporary function until deprecated attributes are removed).
uint32_t m_dataSize
packet payload size (must be equal to m_size)
void DoStartApplication() override
Application specific startup code for child subclasses.
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
Ptr< const AttributeChecker > MakeAddressChecker()
Definition address.cc:169
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition address.h:275
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition nstime.h:1457
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1477
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition uinteger.h:35
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:439
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1369
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Address ConvertToSocketAddress(const Address &address, uint16_t port)
Convert IPv4/IPv6 address with port to a socket address.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684