A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_OBJECT_ENSURE_REGISTERED (UdpServer);
41 
42 
43 TypeId
45 {
46  static TypeId tid = TypeId ("ns3::UdpServer")
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 
65  : m_lossCounter (0)
66 {
67  NS_LOG_FUNCTION (this);
68  m_received=0;
69 }
70 
72 {
73  NS_LOG_FUNCTION (this);
74 }
75 
76 uint16_t
78 {
79  return m_lossCounter.GetBitMapSize ();
80 }
81 
82 void
84 {
86 }
87 
88 uint32_t
89 UdpServer::GetLost (void) const
90 {
91  return m_lossCounter.GetLost ();
92 }
93 
94 uint32_t
96 {
97 
98  return m_received;
99 
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this);
107 }
108 
109 void
111 {
112  NS_LOG_FUNCTION (this);
113 
114  if (m_socket == 0)
115  {
116  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
119  m_port);
120  m_socket->Bind (local);
121  }
122 
124 
125  if (m_socket6 == 0)
126  {
127  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
130  m_port);
131  m_socket6->Bind (local);
132  }
133 
135 
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this);
142 
143  if (m_socket != 0)
144  {
146  }
147 }
148 
149 void
151 {
152  NS_LOG_FUNCTION (this << socket);
153  Ptr<Packet> packet;
154  Address from;
155  while ((packet = socket->RecvFrom (from)))
156  {
157  if (packet->GetSize () > 0)
158  {
159  SeqTsHeader seqTs;
160  packet->RemoveHeader (seqTs);
161  uint32_t currentSequenceNumber = seqTs.GetSeq ();
163  {
164  NS_LOG_INFO ("TraceDelay: RX " << packet->GetSize () <<
165  " bytes from "<< InetSocketAddress::ConvertFrom (from).GetIpv4 () <<
166  " Sequence Number: " << currentSequenceNumber <<
167  " Uid: " << packet->GetUid () <<
168  " TXtime: " << seqTs.GetTs () <<
169  " RXtime: " << Simulator::Now () <<
170  " Delay: " << Simulator::Now () - seqTs.GetTs ());
171  }
172  else if (Inet6SocketAddress::IsMatchingType (from))
173  {
174  NS_LOG_INFO ("TraceDelay: RX " << packet->GetSize () <<
175  " bytes from "<< Inet6SocketAddress::ConvertFrom (from).GetIpv6 () <<
176  " Sequence Number: " << currentSequenceNumber <<
177  " Uid: " << packet->GetUid () <<
178  " TXtime: " << seqTs.GetTs () <<
179  " RXtime: " << Simulator::Now () <<
180  " Delay: " << Simulator::Now () - seqTs.GetTs ());
181  }
182 
183  m_lossCounter.NotifyReceived (currentSequenceNumber);
184  m_received++;
185  }
186  }
187 }
188 
189 } // Namespace ns3