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
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("RxWithAddresses",
49 "A packet has been received",
51 "ns3::Packet::TwoAddressTracedCallback");
52 return tid;
53}
54
61
66
67uint16_t
69{
70 return m_lossCounter.GetBitMapSize();
71}
72
73void
75{
76 NS_LOG_FUNCTION(this << size);
77 m_lossCounter.SetBitMapSize(size);
78}
79
82{
83 return m_lossCounter.GetLost();
84}
85
86uint64_t
88{
89 return m_received;
90}
91
92void
94{
95 NS_LOG_FUNCTION(this);
96
97 auto local = m_local;
98 if (local.IsInvalid())
99 {
101 NS_LOG_INFO(this << " Binding on port " << m_port << " / " << local << ".");
102 }
104 {
106 NS_LOG_INFO(this << " Binding on " << ipv4 << " port " << m_port << " / " << m_local
107 << ".");
108 }
110 {
112 NS_LOG_INFO(this << " Binding on " << ipv6 << " port " << m_port << " / " << m_local
113 << ".");
114 }
115 if (m_socket->Bind(local) == -1)
116 {
117 NS_FATAL_ERROR("Failed to bind socket");
118 }
119 m_socket->SetRecvCallback(MakeCallback(&UdpServer::HandleRead, this));
120
121 if (m_local.IsInvalid())
122 {
124 if (m_socket6->Bind(local) == -1)
125 {
126 NS_FATAL_ERROR("Failed to bind socket");
127 }
128 m_socket6->SetRecvCallback(MakeCallback(&UdpServer::HandleRead, this));
129 }
130}
131
132void
134{
135 NS_LOG_FUNCTION(this << socket);
136 Address from;
137 while (auto packet = socket->RecvFrom(from))
138 {
139 Address localAddress;
140 socket->GetSockName(localAddress);
142 m_rxTrace(packet, from);
143 m_rxTraceWithAddresses(packet, from, localAddress);
144 if (packet->GetSize() > 0)
145 {
146 const auto receivedSize = packet->GetSize();
147 SeqTsHeader seqTs;
148 packet->RemoveHeader(seqTs);
149 const auto currentSequenceNumber = seqTs.GetSeq();
151 {
152 NS_LOG_INFO("TraceDelay: RX " << receivedSize << " bytes from "
153 << InetSocketAddress::ConvertFrom(from).GetIpv4()
154 << " Sequence Number: " << currentSequenceNumber
155 << " Uid: " << packet->GetUid() << " TXtime: "
156 << seqTs.GetTs() << " RXtime: " << Simulator::Now()
157 << " Delay: " << Simulator::Now() - seqTs.GetTs());
158 }
160 {
161 NS_LOG_INFO("TraceDelay: RX " << receivedSize << " bytes from "
162 << Inet6SocketAddress::ConvertFrom(from).GetIpv6()
163 << " Sequence Number: " << currentSequenceNumber
164 << " Uid: " << packet->GetUid() << " TXtime: "
165 << seqTs.GetTs() << " RXtime: " << Simulator::Now()
166 << " Delay: " << Simulator::Now() - seqTs.GetTs());
167 }
168
169 m_lossCounter.NotifyReceived(currentSequenceNumber);
170 m_received++;
171 }
172 }
173}
174
175} // Namespace ns3
a polymophic address class
Definition address.h:90
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
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
ns3::TracedCallback< Ptr< const Packet > > m_rxTraceWithoutAddress
Callbacks for tracing the packet Rx events.
Ptr< Socket > m_socket6
IPv6 Socket (used if only port is specified).
TypeId m_protocolTid
Protocol TypeId value.
SinkApplication(uint16_t defaultPort=0)
Constructor.
Address m_local
Local address to bind to (address and port).
Ptr< Socket > m_socket
Socket (IPv4 or IPv6, depending on local address).
uint32_t m_port
Local port to bind to.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
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
A UDP server, receives UDP packets from a remote host.
Definition udp-server.h:38
~UdpServer() override
Definition udp-server.cc:62
uint64_t GetReceived() const
Returns the number of received packets.
Definition udp-server.cc:87
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
void DoStartApplication() override
Application specific startup code for child subclasses.
Definition udp-server.cc:93
static TypeId GetTypeId()
Get the type ID.
Definition udp-server.cc:34
PacketLossCounter m_lossCounter
Lost packet counter.
Definition udp-server.h:90
static constexpr uint16_t DEFAULT_PORT
default port
Definition udp-server.h:40
void SetPacketWindowSize(uint16_t size)
Set the size of the window used for checking loss.
Definition udp-server.cc:74
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:93
uint32_t GetLost() const
Returns the number of lost packets.
Definition udp-server.cc:81
uint64_t m_received
Number of received packets.
Definition udp-server.h:89
uint16_t GetPacketWindowSize() const
Returns the size of the window used for checking loss.
Definition udp-server.cc:68
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
#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