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("Port",
42 "Port on which we listen for incoming packets.",
43 UintegerValue(100),
46 .AddAttribute("Tos",
47 "The Type of Service used to send IPv4 packets. "
48 "All 8 bits of the TOS byte are set (including ECN bits).",
52 .AddAttribute("PacketWindowSize",
53 "The size of the window used to compute the packet loss. This value "
54 "should be a multiple of 8.",
55 UintegerValue(32),
59 .AddTraceSource("Rx",
60 "A packet has been received",
62 "ns3::Packet::TracedCallback")
63 .AddTraceSource("RxWithAddresses",
64 "A packet has been received",
66 "ns3::Packet::TwoAddressTracedCallback");
67 return tid;
68}
69
71 : m_received(0),
72 m_lossCounter(0)
73{
74 NS_LOG_FUNCTION(this);
75}
76
81
82uint16_t
88
89void
91{
92 NS_LOG_FUNCTION(this << size);
94}
95
98{
99 NS_LOG_FUNCTION(this);
100 return m_lossCounter.GetLost();
101}
102
103uint64_t
105{
106 NS_LOG_FUNCTION(this);
107 return m_received;
108}
109
110void
112{
113 NS_LOG_FUNCTION(this);
114
115 if (!m_socket)
116 {
117 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
120 if (m_socket->Bind(local) == -1)
121 {
122 NS_FATAL_ERROR("Failed to bind socket");
123 }
124 }
125
126 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
128
129 if (!m_socket6)
130 {
131 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
134 if (m_socket6->Bind(local) == -1)
135 {
136 NS_FATAL_ERROR("Failed to bind socket");
137 }
138 }
139
141}
142
143void
145{
146 NS_LOG_FUNCTION(this);
147
148 if (m_socket)
149 {
151 }
152}
153
154void
156{
157 NS_LOG_FUNCTION(this << socket);
158 Ptr<Packet> packet;
159 Address from;
160 Address localAddress;
161 while ((packet = socket->RecvFrom(from)))
162 {
163 socket->GetSockName(localAddress);
164 m_rxTrace(packet);
165 m_rxTraceWithAddresses(packet, from, localAddress);
166 if (packet->GetSize() > 0)
167 {
168 uint32_t receivedSize = packet->GetSize();
169 SeqTsHeader seqTs;
170 packet->RemoveHeader(seqTs);
171 uint32_t currentSequenceNumber = seqTs.GetSeq();
173 {
174 NS_LOG_INFO("TraceDelay: RX " << receivedSize << " bytes from "
175 << InetSocketAddress::ConvertFrom(from).GetIpv4()
176 << " Sequence Number: " << currentSequenceNumber
177 << " Uid: " << packet->GetUid() << " TXtime: "
178 << seqTs.GetTs() << " RXtime: " << Simulator::Now()
179 << " Delay: " << Simulator::Now() - seqTs.GetTs());
180 }
182 {
183 NS_LOG_INFO("TraceDelay: RX " << receivedSize << " bytes from "
184 << Inet6SocketAddress::ConvertFrom(from).GetIpv6()
185 << " Sequence Number: " << currentSequenceNumber
186 << " Uid: " << packet->GetUid() << " TXtime: "
187 << seqTs.GetTs() << " RXtime: " << Simulator::Now()
188 << " Delay: " << Simulator::Now() - seqTs.GetTs());
189 }
190
191 m_lossCounter.NotifyReceived(currentSequenceNumber);
192 m_received++;
193 }
194 }
195}
196
197} // Namespace ns3
a polymophic address class
Definition address.h:90
The base class for all ns3 applications.
Definition application.h:51
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.
an Inet address class
static bool IsMatchingType(const Address &address)
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.
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
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
Definition socket.cc:423
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:39
Ptr< Socket > m_socket6
IPv6 Socket.
Definition udp-server.h:90
TracedCallback< Ptr< const Packet > > m_rxTrace
Callbacks for tracing the packet Rx events.
Definition udp-server.h:95
~UdpServer() override
Definition udp-server.cc:77
uint16_t m_port
Port on which we listen for incoming packets.
Definition udp-server.h:87
uint64_t GetReceived() const
Returns the number of received packets.
void StartApplication() override
Application specific startup code.
Ptr< Socket > m_socket
IPv4 Socket.
Definition udp-server.h:89
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:92
void SetPacketWindowSize(uint16_t size)
Set the size of the window used for checking loss.
Definition udp-server.cc:90
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:98
uint32_t GetLost() const
Returns the number of lost packets.
Definition udp-server.cc:97
uint8_t m_tos
The packets Type of Service.
Definition udp-server.h:88
uint64_t m_received
Number of received packets.
Definition udp-server.h:91
uint16_t GetPacketWindowSize() const
Returns the size of the window used for checking loss.
Definition udp-server.cc:83
void StopApplication() override
Application specific shutdown code.
Hold an unsigned integer type.
Definition uinteger.h:34
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.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
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
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35