A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-server.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-server.h"
11
12#include "packet-loss-counter.h"
13#include "seq-ts-header.h"
14
15#include "ns3/inet-socket-address.h"
16#include "ns3/inet6-socket-address.h"
17#include "ns3/ipv4-address.h"
18#include "ns3/log.h"
19#include "ns3/nstime.h"
20#include "ns3/packet.h"
21#include "ns3/simulator.h"
22#include "ns3/socket-factory.h"
23#include "ns3/socket.h"
24#include "ns3/uinteger.h"
25
26namespace ns3
27{
28
29NS_LOG_COMPONENT_DEFINE("UdpServer");
30
32
33TypeId
35{
36 static TypeId tid =
37 TypeId("ns3::UdpServer")
39 .SetGroupName("Applications")
40 .AddConstructor<UdpServer>()
41 .AddAttribute("PacketWindowSize",
42 "The size of the window used to compute the packet loss. This value "
43 "should be a multiple of 8.",
44 UintegerValue(32),
48 .AddTraceSource("Rx",
49 "A packet has been received",
51 "ns3::Packet::TracedCallback")
52 .AddTraceSource("RxWithAddresses",
53 "A packet has been received",
55 "ns3::Packet::TwoAddressTracedCallback");
56 return tid;
57}
58
60 : SinkApplication(DEFAULT_PORT),
61 m_socket{nullptr},
62 m_socket6{nullptr},
63 m_received{0},
64 m_lossCounter{0}
65{
66 NS_LOG_FUNCTION(this);
67}
68
73
74uint16_t
80
81void
83{
84 NS_LOG_FUNCTION(this << size);
86}
87
90{
91 NS_LOG_FUNCTION(this);
92 return m_lossCounter.GetLost();
93}
94
95uint64_t
97{
98 NS_LOG_FUNCTION(this);
99 return m_received;
100}
101
102void
104{
105 NS_LOG_FUNCTION(this);
106
107 if (!m_socket)
108 {
109 auto tid = TypeId::LookupByName("ns3::UdpSocketFactory");
111 auto local = m_local;
112 if (local.IsInvalid())
113 {
115 NS_LOG_INFO(this << " Binding on port " << m_port << " / " << local << ".");
116 }
117 else
118 {
120 {
122 NS_LOG_INFO(this << " Binding on " << ipv4 << " port " << m_port << " / " << m_local
123 << ".");
124 }
126 {
128 NS_LOG_INFO(this << " Binding on " << ipv6 << " port " << m_port << " / " << m_local
129 << ".");
130 }
131 }
132 if (m_socket->Bind(local) == -1)
133 {
134 NS_FATAL_ERROR("Failed to bind socket");
135 }
137 }
138
139 if (m_local.IsInvalid() && !m_socket6)
140 {
141 // local address is not specified, so create another socket to also listen to all IPv6
142 // addresses
143 auto tid = TypeId::LookupByName("ns3::UdpSocketFactory");
146 if (m_socket6->Bind(local) == -1)
147 {
148 NS_FATAL_ERROR("Failed to bind socket");
149 }
151 }
152}
153
154void
156{
157 NS_LOG_FUNCTION(this);
158
159 if (m_socket)
160 {
162 }
163 if (m_socket6)
164 {
166 }
167}
168
169void
171{
172 NS_LOG_FUNCTION(this << socket);
173 Address from;
174 while (auto packet = socket->RecvFrom(from))
175 {
176 Address localAddress;
177 socket->GetSockName(localAddress);
178 m_rxTrace(packet);
179 m_rxTraceWithAddresses(packet, from, localAddress);
180 if (packet->GetSize() > 0)
181 {
182 const auto receivedSize = packet->GetSize();
183 SeqTsHeader seqTs;
184 packet->RemoveHeader(seqTs);
185 const auto currentSequenceNumber = seqTs.GetSeq();
187 {
188 NS_LOG_INFO("TraceDelay: RX " << receivedSize << " bytes from "
189 << InetSocketAddress::ConvertFrom(from).GetIpv4()
190 << " Sequence Number: " << currentSequenceNumber
191 << " Uid: " << packet->GetUid() << " TXtime: "
192 << seqTs.GetTs() << " RXtime: " << Simulator::Now()
193 << " Delay: " << Simulator::Now() - seqTs.GetTs());
194 }
196 {
197 NS_LOG_INFO("TraceDelay: RX " << receivedSize << " bytes from "
198 << Inet6SocketAddress::ConvertFrom(from).GetIpv6()
199 << " Sequence Number: " << currentSequenceNumber
200 << " Uid: " << packet->GetUid() << " TXtime: "
201 << seqTs.GetTs() << " RXtime: " << Simulator::Now()
202 << " Delay: " << Simulator::Now() - seqTs.GetTs());
203 }
204
205 m_lossCounter.NotifyReceived(currentSequenceNumber);
206 m_received++;
207 }
208 }
209}
210
211} // Namespace ns3
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
Ptr< Node > GetNode() const
An Inet6 address class.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
Ipv6Address GetIpv6() const
Get the IPv6 address.
an Inet address class
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 Ipv4Address GetAny()
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
static bool IsMatchingType(const Address &address)
If the Address matches the type.
void NotifyReceived(uint32_t seq)
Record a successfully received packet.
void SetBitMapSize(uint16_t size)
Set the size of the window used to compute the packet loss.
uint16_t GetBitMapSize() const
Return the size of the window used to compute the packet loss.
uint32_t GetLost() const
Get the number of lost packets.
Smart pointer class similar to boost::intrusive_ptr.
Packet header to carry sequence number and timestamp.
Time GetTs() const
uint32_t GetSeq() const
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Base class for sink applications.
Address m_local
Local address to bind to (address and port)
uint32_t m_port
Local port to bind to.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition socket.cc:117
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
a unique identifier for an interface.
Definition type-id.h:48
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
A UDP server, receives UDP packets from a remote host.
Definition udp-server.h:38
Ptr< Socket > m_socket6
IPv6 Socket (used if only port is specified)
Definition udp-server.h:91
TracedCallback< Ptr< const Packet > > m_rxTrace
Callbacks for tracing the packet Rx events.
Definition udp-server.h:96
~UdpServer() override
Definition udp-server.cc:69
uint64_t GetReceived() const
Returns the number of received packets.
Definition udp-server.cc:96
void StartApplication() override
Application specific startup code.
Ptr< Socket > m_socket
Socket.
Definition udp-server.h:90
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
static TypeId GetTypeId()
Get the type ID.
Definition udp-server.cc:34
PacketLossCounter m_lossCounter
Lost packet counter.
Definition udp-server.h:93
void SetPacketWindowSize(uint16_t size)
Set the size of the window used for checking loss.
Definition udp-server.cc:82
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callbacks for tracing the packet Rx events, includes source and destination addresses.
Definition udp-server.h:99
uint32_t GetLost() const
Returns the number of lost packets.
Definition udp-server.cc:89
uint64_t m_received
Number of received packets.
Definition udp-server.h:92
uint16_t GetPacketWindowSize() const
Returns the size of the window used for checking loss.
Definition udp-server.cc:75
void StopApplication() override
Application specific shutdown code.
Hold an unsigned integer type.
Definition uinteger.h:34
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
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#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< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
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