A Discrete-Event Network Simulator
API
udp-server.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007,2008,2009 INRIA, UDCAST
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Amine Ismail <amine.ismail@sophia.inria.fr>
19  * <amine.ismail@udcast.com>
20  */
21 
22 #include "ns3/log.h"
23 #include "ns3/ipv4-address.h"
24 #include "ns3/nstime.h"
25 #include "ns3/inet-socket-address.h"
26 #include "ns3/inet6-socket-address.h"
27 #include "ns3/socket.h"
28 #include "ns3/simulator.h"
29 #include "ns3/socket-factory.h"
30 #include "ns3/packet.h"
31 #include "ns3/uinteger.h"
32 #include "packet-loss-counter.h"
33 
34 #include "seq-ts-header.h"
35 #include "udp-server.h"
36 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("UdpServer");
40 
41 NS_OBJECT_ENSURE_REGISTERED (UdpServer);
42 
43 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::UdpServer")
49  .SetGroupName("Applications")
50  .AddConstructor<UdpServer> ()
51  .AddAttribute ("Port",
52  "Port on which we listen for incoming packets.",
53  UintegerValue (100),
55  MakeUintegerChecker<uint16_t> ())
56  .AddAttribute ("PacketWindowSize",
57  "The size of the window used to compute the packet loss. This value should be a multiple of 8.",
58  UintegerValue (32),
61  MakeUintegerChecker<uint16_t> (8,256))
62  ;
63  return tid;
64 }
65 
67  : m_lossCounter (0)
68 {
69  NS_LOG_FUNCTION (this);
70  m_received=0;
71 }
72 
74 {
75  NS_LOG_FUNCTION (this);
76 }
77 
78 uint16_t
80 {
81  NS_LOG_FUNCTION (this);
82  return m_lossCounter.GetBitMapSize ();
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION (this << size);
90 }
91 
92 uint32_t
93 UdpServer::GetLost (void) const
94 {
95  NS_LOG_FUNCTION (this);
96  return m_lossCounter.GetLost ();
97 }
98 
99 uint64_t
101 {
102  NS_LOG_FUNCTION (this);
103  return m_received;
104 }
105 
106 void
108 {
109  NS_LOG_FUNCTION (this);
111 }
112 
113 void
115 {
116  NS_LOG_FUNCTION (this);
117 
118  if (m_socket == 0)
119  {
120  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
123  m_port);
124  m_socket->Bind (local);
125  }
126 
128 
129  if (m_socket6 == 0)
130  {
131  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
134  m_port);
135  m_socket6->Bind (local);
136  }
137 
139 
140 }
141 
142 void
144 {
145  NS_LOG_FUNCTION (this);
146 
147  if (m_socket != 0)
148  {
150  }
151 }
152 
153 void
155 {
156  NS_LOG_FUNCTION (this << socket);
157  Ptr<Packet> packet;
158  Address from;
159  while ((packet = socket->RecvFrom (from)))
160  {
161  if (packet->GetSize () > 0)
162  {
163  SeqTsHeader seqTs;
164  packet->RemoveHeader (seqTs);
165  uint32_t currentSequenceNumber = seqTs.GetSeq ();
167  {
168  NS_LOG_INFO ("TraceDelay: RX " << packet->GetSize () <<
169  " bytes from "<< InetSocketAddress::ConvertFrom (from).GetIpv4 () <<
170  " Sequence Number: " << currentSequenceNumber <<
171  " Uid: " << packet->GetUid () <<
172  " TXtime: " << seqTs.GetTs () <<
173  " RXtime: " << Simulator::Now () <<
174  " Delay: " << Simulator::Now () - seqTs.GetTs ());
175  }
176  else if (Inet6SocketAddress::IsMatchingType (from))
177  {
178  NS_LOG_INFO ("TraceDelay: RX " << packet->GetSize () <<
179  " bytes from "<< Inet6SocketAddress::ConvertFrom (from).GetIpv6 () <<
180  " Sequence Number: " << currentSequenceNumber <<
181  " Uid: " << packet->GetUid () <<
182  " TXtime: " << seqTs.GetTs () <<
183  " RXtime: " << Simulator::Now () <<
184  " Delay: " << Simulator::Now () - seqTs.GetTs ());
185  }
186 
187  m_lossCounter.NotifyReceived (currentSequenceNumber);
188  m_received++;
189  }
190  }
191 }
192 
193 } // Namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
an Inet address class
Ipv4Address GetIpv4(void) const
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t GetSeq(void) const
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
void NotifyReceived(uint32_t seq)
Record a successfully received packet.
uint16_t GetPacketWindowSize() const
Returns the size of the window used for checking loss.
Definition: udp-server.cc:79
uint32_t GetLost(void) const
Returns the number of lost packets.
Definition: udp-server.cc:93
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:368
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
void SetPacketWindowSize(uint16_t size)
Set the size of the window used for checking loss.
Definition: udp-server.cc:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
Definition: udp-server.cc:154
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
Callback< R > MakeNullCallback(void)
Definition: callback.h:1635
uint16_t m_port
Port on which we listen for incoming packets.
Definition: udp-server.h:98
a polymophic address class
Definition: address.h:90
virtual void StartApplication(void)
Application specific startup code.
Definition: udp-server.cc:114
virtual ~UdpServer()
Definition: udp-server.cc:73
The base class for all ns3 applications.
Definition: application.h:60
Hold an unsigned integer type.
Definition: uinteger.h:44
uint32_t GetLost(void) const
Get the number of lost packets.
Ptr< Node > GetNode() const
Definition: application.cc:104
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
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:71
virtual void StopApplication(void)
Application specific shutdown code.
Definition: udp-server.cc:143
Packet header for UDP client/server application.
Definition: seq-ts-header.h:36
uint64_t m_received
Number of received packets.
Definition: udp-server.h:101
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
void SetBitMapSize(uint16_t size)
Set the size of the window used to compute the packet loss.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
static TypeId GetTypeId(void)
Get the type ID.
Definition: udp-server.cc:45
Time GetTs(void) const
Ptr< Socket > m_socket6
IPv6 Socket.
Definition: udp-server.h:100
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
Ptr< Socket > m_socket
IPv4 Socket.
Definition: udp-server.h:99
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
A UDP server, receives UDP packets from a remote host.
Definition: udp-server.h:46
virtual void DoDispose(void)
Destructor implementation.
Definition: udp-server.cc:107
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:45
a unique identifier for an interface.
Definition: type-id.h:58
uint64_t GetReceived(void) const
Returns the number of received packets.
Definition: udp-server.cc:100
uint16_t GetBitMapSize(void) const
Return the size of the window used to compute the packet loss.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
PacketLossCounter m_lossCounter
Lost packet counter.
Definition: udp-server.h:102
static bool IsMatchingType(const Address &address)
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:813