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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Amine Ismail <amine.ismail@sophia.inria.fr>
18 * <amine.ismail@udcast.com>
19 */
20
21#include "udp-server.h"
22
23#include "packet-loss-counter.h"
24#include "seq-ts-header.h"
25
26#include "ns3/inet-socket-address.h"
27#include "ns3/inet6-socket-address.h"
28#include "ns3/ipv4-address.h"
29#include "ns3/log.h"
30#include "ns3/nstime.h"
31#include "ns3/packet.h"
32#include "ns3/simulator.h"
33#include "ns3/socket-factory.h"
34#include "ns3/socket.h"
35#include "ns3/uinteger.h"
36
37namespace ns3
38{
39
40NS_LOG_COMPONENT_DEFINE("UdpServer");
41
43
44TypeId
46{
47 static TypeId tid =
48 TypeId("ns3::UdpServer")
50 .SetGroupName("Applications")
51 .AddConstructor<UdpServer>()
52 .AddAttribute("Port",
53 "Port on which we listen for incoming packets.",
54 UintegerValue(100),
56 MakeUintegerChecker<uint16_t>())
57 .AddAttribute("Tos",
58 "The Type of Service used to send IPv4 packets. "
59 "All 8 bits of the TOS byte are set (including ECN bits).",
62 MakeUintegerChecker<uint8_t>())
63 .AddAttribute("PacketWindowSize",
64 "The size of the window used to compute the packet loss. This value "
65 "should be a multiple of 8.",
66 UintegerValue(32),
69 MakeUintegerChecker<uint16_t>(8, 256))
70 .AddTraceSource("Rx",
71 "A packet has been received",
73 "ns3::Packet::TracedCallback")
74 .AddTraceSource("RxWithAddresses",
75 "A packet has been received",
77 "ns3::Packet::TwoAddressTracedCallback");
78 return tid;
79}
80
82 : m_received(0),
83 m_lossCounter(0)
84{
85 NS_LOG_FUNCTION(this);
86}
87
89{
90 NS_LOG_FUNCTION(this);
91}
92
93uint16_t
95{
96 NS_LOG_FUNCTION(this);
98}
99
100void
102{
103 NS_LOG_FUNCTION(this << size);
105}
106
109{
110 NS_LOG_FUNCTION(this);
111 return m_lossCounter.GetLost();
112}
113
114uint64_t
116{
117 NS_LOG_FUNCTION(this);
118 return m_received;
119}
120
121void
123{
124 NS_LOG_FUNCTION(this);
125
126 if (!m_socket)
127 {
128 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
131 if (m_socket->Bind(local) == -1)
132 {
133 NS_FATAL_ERROR("Failed to bind socket");
134 }
135 }
136
137 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
139
140 if (!m_socket6)
141 {
142 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
145 if (m_socket6->Bind(local) == -1)
146 {
147 NS_FATAL_ERROR("Failed to bind socket");
148 }
149 }
150
152}
153
154void
156{
157 NS_LOG_FUNCTION(this);
158
159 if (m_socket)
160 {
162 }
163}
164
165void
167{
168 NS_LOG_FUNCTION(this << socket);
169 Ptr<Packet> packet;
170 Address from;
171 Address localAddress;
172 while ((packet = socket->RecvFrom(from)))
173 {
174 socket->GetSockName(localAddress);
175 m_rxTrace(packet);
176 m_rxTraceWithAddresses(packet, from, localAddress);
177 if (packet->GetSize() > 0)
178 {
179 uint32_t receivedSize = packet->GetSize();
180 SeqTsHeader seqTs;
181 packet->RemoveHeader(seqTs);
182 uint32_t currentSequenceNumber = seqTs.GetSeq();
184 {
185 NS_LOG_INFO("TraceDelay: RX " << receivedSize << " bytes from "
186 << InetSocketAddress::ConvertFrom(from).GetIpv4()
187 << " Sequence Number: " << currentSequenceNumber
188 << " Uid: " << packet->GetUid() << " TXtime: "
189 << seqTs.GetTs() << " RXtime: " << Simulator::Now()
190 << " Delay: " << Simulator::Now() - seqTs.GetTs());
191 }
193 {
194 NS_LOG_INFO("TraceDelay: RX " << receivedSize << " bytes from "
195 << Inet6SocketAddress::ConvertFrom(from).GetIpv6()
196 << " Sequence Number: " << currentSequenceNumber
197 << " Uid: " << packet->GetUid() << " TXtime: "
198 << seqTs.GetTs() << " RXtime: " << Simulator::Now()
199 << " Delay: " << Simulator::Now() - seqTs.GetTs());
200 }
201
202 m_lossCounter.NotifyReceived(currentSequenceNumber);
203 m_received++;
204 }
205 }
206}
207
208} // Namespace ns3
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
Ptr< Node > GetNode() const
Definition: application.cc:108
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.
Definition: ptr.h:77
Packet header to carry sequence number and timestamp.
Definition: seq-ts-header.h:45
Time GetTs() const
uint32_t GetSeq() const
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
Definition: socket.cc:434
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
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:72
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
A UDP server, receives UDP packets from a remote host.
Definition: udp-server.h:50
Ptr< Socket > m_socket6
IPv6 Socket.
Definition: udp-server.h:101
TracedCallback< Ptr< const Packet > > m_rxTrace
Callbacks for tracing the packet Rx events.
Definition: udp-server.h:106
~UdpServer() override
Definition: udp-server.cc:88
uint16_t m_port
Port on which we listen for incoming packets.
Definition: udp-server.h:98
uint64_t GetReceived() const
Returns the number of received packets.
Definition: udp-server.cc:115
void StartApplication() override
Application specific startup code.
Definition: udp-server.cc:122
Ptr< Socket > m_socket
IPv4 Socket.
Definition: udp-server.h:100
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
Definition: udp-server.cc:166
static TypeId GetTypeId()
Get the type ID.
Definition: udp-server.cc:45
PacketLossCounter m_lossCounter
Lost packet counter.
Definition: udp-server.h:103
void SetPacketWindowSize(uint16_t size)
Set the size of the window used for checking loss.
Definition: udp-server.cc:101
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:109
uint32_t GetLost() const
Returns the number of lost packets.
Definition: udp-server.cc:108
uint8_t m_tos
The packets Type of Service.
Definition: udp-server.h:99
uint64_t m_received
Number of received packets.
Definition: udp-server.h:102
uint16_t GetPacketWindowSize() const
Returns the size of the window used for checking loss.
Definition: udp-server.cc:94
void StopApplication() override
Application specific shutdown code.
Definition: udp-server.cc:155
Hold an unsigned integer type.
Definition: uinteger.h:45
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:749
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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:706