A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
udp-server.cc
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 NS_OBJECT_ENSURE_REGISTERED (UdpServer);
41 
42 
43 TypeId
44 UdpServer::GetTypeId (void)
45 {
46  static TypeId tid = TypeId ("ns3::UdpServer")
47  .SetParent<Application> ()
48  .AddConstructor<UdpServer> ()
49  .AddAttribute ("Port",
50  "Port on which we listen for incoming packets.",
51  UintegerValue (100),
52  MakeUintegerAccessor (&UdpServer::m_port),
53  MakeUintegerChecker<uint16_t> ())
54  .AddAttribute ("PacketWindowSize",
55  "The size of the window used to compute the packet loss. This value should be a multiple of 8.",
56  UintegerValue (32),
57  MakeUintegerAccessor (&UdpServer::GetPacketWindowSize,
59  MakeUintegerChecker<uint16_t> (8,256))
60  ;
61  return tid;
62 }
63 
64 UdpServer::UdpServer ()
65  : m_lossCounter (0)
66 {
67  NS_LOG_FUNCTION (this);
68  m_received=0;
69 }
70 
71 UdpServer::~UdpServer ()
72 {
73  NS_LOG_FUNCTION (this);
74 }
75 
76 uint16_t
78 {
79  NS_LOG_FUNCTION (this);
80  return m_lossCounter.GetBitMapSize ();
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION (this << size);
87  m_lossCounter.SetBitMapSize (size);
88 }
89 
90 uint32_t
91 UdpServer::GetLost (void) const
92 {
93  NS_LOG_FUNCTION (this);
94  return m_lossCounter.GetLost ();
95 }
96 
97 uint32_t
99 {
100  NS_LOG_FUNCTION (this);
101  return m_received;
102 }
103 
104 void
106 {
107  NS_LOG_FUNCTION (this);
109 }
110 
111 void
113 {
114  NS_LOG_FUNCTION (this);
115 
116  if (m_socket == 0)
117  {
118  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
119  m_socket = Socket::CreateSocket (GetNode (), tid);
121  m_port);
122  m_socket->Bind (local);
123  }
124 
125  m_socket->SetRecvCallback (MakeCallback (&UdpServer::HandleRead, this));
126 
127  if (m_socket6 == 0)
128  {
129  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
130  m_socket6 = Socket::CreateSocket (GetNode (), tid);
132  m_port);
133  m_socket6->Bind (local);
134  }
135 
136  m_socket6->SetRecvCallback (MakeCallback (&UdpServer::HandleRead, this));
137 
138 }
139 
140 void
142 {
143  NS_LOG_FUNCTION (this);
144 
145  if (m_socket != 0)
146  {
147  m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
148  }
149 }
150 
151 void
152 UdpServer::HandleRead (Ptr<Socket> socket)
153 {
154  NS_LOG_FUNCTION (this << socket);
155  Ptr<Packet> packet;
156  Address from;
157  while ((packet = socket->RecvFrom (from)))
158  {
159  if (packet->GetSize () > 0)
160  {
161  SeqTsHeader seqTs;
162  packet->RemoveHeader (seqTs);
163  uint32_t currentSequenceNumber = seqTs.GetSeq ();
165  {
166  NS_LOG_INFO ("TraceDelay: RX " << packet->GetSize () <<
167  " bytes from "<< InetSocketAddress::ConvertFrom (from).GetIpv4 () <<
168  " Sequence Number: " << currentSequenceNumber <<
169  " Uid: " << packet->GetUid () <<
170  " TXtime: " << seqTs.GetTs () <<
171  " RXtime: " << Simulator::Now () <<
172  " Delay: " << Simulator::Now () - seqTs.GetTs ());
173  }
174  else if (Inet6SocketAddress::IsMatchingType (from))
175  {
176  NS_LOG_INFO ("TraceDelay: RX " << packet->GetSize () <<
177  " bytes from "<< Inet6SocketAddress::ConvertFrom (from).GetIpv6 () <<
178  " Sequence Number: " << currentSequenceNumber <<
179  " Uid: " << packet->GetUid () <<
180  " TXtime: " << seqTs.GetTs () <<
181  " RXtime: " << Simulator::Now () <<
182  " Delay: " << Simulator::Now () - seqTs.GetTs ());
183  }
184 
185  m_lossCounter.NotifyReceived (currentSequenceNumber);
186  m_received++;
187  }
188  }
189 }
190 
191 } // Namespace ns3
uint32_t RemoveHeader(Header &header)
Definition: packet.cc:285
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)
Definition: log.h:311
uint32_t GetSeq(void) const
uint16_t GetPacketWindowSize() const
Definition: udp-server.cc:77
uint32_t GetLost(void) const
Definition: udp-server.cc:91
uint64_t GetUid(void) const
Definition: packet.cc:412
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
void SetPacketWindowSize(uint16_t size)
Set the size of the window used for checking loss. This value should be a multiple of 8...
Definition: udp-server.cc:84
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
uint32_t GetSize(void) const
Definition: packet.h:620
#define NS_LOG_INFO(msg)
Definition: log.h:264
a polymophic address class
Definition: address.h:86
virtual void StartApplication(void)
Application specific startup code.
Definition: udp-server.cc:112
Ptr< Node > GetNode() const
Definition: application.cc:103
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
Definition: socket.cc:70
virtual void StopApplication(void)
Application specific shutdown code.
Definition: udp-server.cc:141
Packet header for Udp client/server application The header is made of a 32bits sequence number follow...
Definition: seq-ts-header.h:35
virtual void DoDispose(void)
Definition: application.cc:82
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
static InetSocketAddress ConvertFrom(const Address &address)
Callback< R > MakeNullCallback(void)
Definition: callback.h:781
static Time Now(void)
Definition: simulator.cc:179
Time GetTs(void) const
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
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.
virtual void DoDispose(void)
Definition: udp-server.cc:105
a unique identifier for an interface.
Definition: type-id.h:44
static bool IsMatchingType(const Address &address)
static TypeId LookupByName(std::string name)
Definition: type-id.cc:415
uint32_t GetReceived(void) const
returns the number of received packets
Definition: udp-server.cc:98