A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-client.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007,2008,2009 INRIA, UDCAST
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Amine Ismail <amine.ismail@sophia.inria.fr>
7 * <amine.ismail@udcast.com>
8 */
9
10#include "udp-client.h"
11
12#include "seq-ts-header.h"
13
14#include "ns3/address-utils.h"
15#include "ns3/log.h"
16#include "ns3/nstime.h"
17#include "ns3/packet.h"
18#include "ns3/simulator.h"
19#include "ns3/socket-factory.h"
20#include "ns3/socket.h"
21#include "ns3/uinteger.h"
22
23#include <cstdio>
24#include <cstdlib>
25
26namespace ns3
27{
28
29NS_LOG_COMPONENT_DEFINE("UdpClient");
30
32
35{
36 static TypeId tid =
37 TypeId("ns3::UdpClient")
39 .SetGroupName("Applications")
40 .AddConstructor<UdpClient>()
41 .AddAttribute(
42 "MaxPackets",
43 "The maximum number of packets the application will send (zero means infinite)",
44 UintegerValue(100),
47 .AddAttribute("Interval",
48 "The time to wait between packets",
52 // NS_DEPRECATED_3_44
53 .AddAttribute(
54 "RemoteAddress",
55 "The destination Address of the outbound packets",
58 // this is needed to indicate which version of the function overload to use
59 static_cast<void (UdpClient::*)(const Address&)>(&UdpClient::SetRemote),
63 "Replaced by Remote in ns-3.44.")
64 // NS_DEPRECATED_3_44
65 .AddAttribute("RemotePort",
66 "The destination port of the outbound packets",
71 "Replaced by Remote in ns-3.44.")
72 .AddAttribute("PacketSize",
73 "Size of packets generated. The minimum packet size is 12 bytes which is "
74 "the size of the header carrying the sequence number and the time stamp.",
75 UintegerValue(1024),
78 .AddTraceSource("TxWithAddresses",
79 "A new packet is created and sent",
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
96
97void
98UdpClient::SetRemote(const Address& ip, uint16_t port)
99{
100 NS_LOG_FUNCTION(this << ip << port);
101 SetRemote(ip);
102 SetPort(port);
103}
104
105void
107{
108 NS_LOG_FUNCTION(this << addr);
109 if (!addr.IsInvalid())
110 {
111 m_peer = addr;
112 if (m_peerPort)
113 {
115 }
116 }
117}
118
121{
122 return m_peer;
123}
124
125void
127{
128 NS_LOG_FUNCTION(this << port);
129 if (m_peer.IsInvalid())
130 {
131 // save for later
133 return;
134 }
136 {
138 }
139}
140
141uint16_t
143{
144 if (m_peer.IsInvalid())
145 {
146 return m_peerPort.value_or(UdpClient::DEFAULT_PORT);
147 }
149 {
151 }
153 {
155 }
157}
158
159void
161{
162 NS_LOG_FUNCTION(this);
163
164 m_socket->SetAllowBroadcast(true);
165
166#ifdef NS3_LOG_ENABLE
167 std::stringstream peerAddressStringStream;
169 {
170 peerAddressStringStream << InetSocketAddress::ConvertFrom(m_peer).GetIpv4() << ":"
172 ;
173 }
175 {
176 peerAddressStringStream << Inet6SocketAddress::ConvertFrom(m_peer).GetIpv6() << ":"
178 }
179 m_peerString = peerAddressStringStream.str();
180#endif // NS3_LOG_ENABLE
181
183}
184
185void
187{
188 NS_LOG_FUNCTION(this);
189 NS_ASSERT(m_sendEvent.IsExpired());
190
191 Address from;
192 Address to;
193 m_socket->GetSockName(from);
194 m_socket->GetPeerName(to);
195 SeqTsHeader seqTs;
196 seqTs.SetSeq(m_sent);
198 auto p = Create<Packet>(m_size - seqTs.GetSerializedSize());
199
200 // Trace before adding header, for consistency with PacketSink
201 m_txTrace(p);
202 m_txTraceWithAddresses(p, from, to);
203
204 p->AddHeader(seqTs);
205
206 if ((m_socket->Send(p)) >= 0)
207 {
208 ++m_sent;
209 m_totalTx += p->GetSize();
210#ifdef NS3_LOG_ENABLE
211 NS_LOG_INFO("TraceDelay TX " << m_size << " bytes to " << m_peerString << " Uid: "
212 << p->GetUid() << " Time: " << (Simulator::Now()).As(Time::S));
213#endif // NS3_LOG_ENABLE
214 }
215#ifdef NS3_LOG_ENABLE
216 else
217 {
218 NS_LOG_INFO("Error while sending " << m_size << " bytes to " << m_peerString);
219 }
220#endif // NS3_LOG_ENABLE
221
222 if (m_sent < m_count || m_count == 0)
223 {
225 }
226}
227
228void
234
235uint64_t
237{
238 return m_totalTx;
239}
240
241} // 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.
Ipv6Address GetIpv6() const
Get the IPv6 address.
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
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.
Packet header to carry sequence number and timestamp.
void SetSeq(uint32_t seq)
uint32_t GetSerializedSize() const override
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
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:595
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.
@ 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 client.
Definition udp-client.h:37
Time m_interval
Packet inter-send time.
Definition udp-client.h:95
std::optional< uint16_t > m_peerPort
Remote peer port (deprecated) // NS_DEPRECATED_3_44.
Definition udp-client.h:100
uint64_t GetTotalTx() const
uint64_t m_totalTx
Total bytes sent.
Definition udp-client.h:99
~UdpClient() override
Definition udp-client.cc:92
std::string m_peerString
Remote peer address string.
Definition udp-client.h:104
uint32_t m_sent
Counter for sent packets.
Definition udp-client.h:98
void SetPort(uint16_t port)
Set the remote port (temporary function until deprecated attributes are removed).
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_txTraceWithAddresses
Callbacks for tracing the packet Tx events, includes source and destination addresses.
Definition udp-client.h:92
void CancelEvents() override
Cancel all pending events.
static constexpr uint16_t DEFAULT_PORT
default port
Definition udp-client.h:48
void Send()
Send a packet.
static TypeId GetTypeId()
Get the type ID.
Definition udp-client.cc:34
EventId m_sendEvent
Event to send the next packet.
Definition udp-client.h:101
uint32_t m_size
Size of the sent packet (including the SeqTsHeader).
Definition udp-client.h:96
uint32_t m_count
Maximum number of packets the application will send.
Definition udp-client.h:94
uint16_t GetPort() const
Get the remote port (temporary function until deprecated attributes are removed).
Address GetRemote() const
Get the remote address (temporary function until deprecated attributes are removed).
void SetRemote(const Address &ip, uint16_t port)
set the remote address and port
Definition udp-client.cc:98
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
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_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition abort.h:65
#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.