A Discrete-Event Network Simulator
API
udp-client-server-test.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: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
19  * <amine.ismail@udcast.com>
20  */
21 
22 #include <fstream>
23 #include "ns3/log.h"
24 #include "ns3/abort.h"
25 #include "ns3/config.h"
26 #include "ns3/string.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/inet-socket-address.h"
29 #include "ns3/internet-stack-helper.h"
30 #include "ns3/ipv4-address-helper.h"
31 #include "ns3/udp-client-server-helper.h"
32 #include "ns3/udp-echo-helper.h"
33 #include "ns3/simple-net-device.h"
34 #include "ns3/simple-channel.h"
35 #include "ns3/test.h"
36 #include "ns3/simulator.h"
37 
38 using namespace ns3;
39 
53 {
54 public:
56  virtual ~UdpClientServerTestCase ();
57 
58 private:
59  virtual void DoRun (void);
60 
61 };
62 
64  : TestCase ("Test that all the udp packets generated by an udpClient application are correctly received by an udpServer application")
65 {
66 }
67 
69 {
70 }
71 
73 {
75  n.Create (2);
76 
77  InternetStackHelper internet;
78  internet.Install (n);
79 
80  // link the two nodes
81  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice> ();
82  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice> ();
83  n.Get (0)->AddDevice (txDev);
84  n.Get (1)->AddDevice (rxDev);
85  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
86  rxDev->SetChannel (channel1);
87  txDev->SetChannel (channel1);
89  d.Add (txDev);
90  d.Add (rxDev);
91 
92  Ipv4AddressHelper ipv4;
93 
94  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
95  Ipv4InterfaceContainer i = ipv4.Assign (d);
96 
97  uint16_t port = 4000;
98  UdpServerHelper server (port);
99  ApplicationContainer apps = server.Install (n.Get (1));
100  apps.Start (Seconds (1.0));
101  apps.Stop (Seconds (10.0));
102 
103  uint32_t MaxPacketSize = 1024;
104  Time interPacketInterval = Seconds (1.);
105  uint32_t maxPacketCount = 10;
106  UdpClientHelper client (i.GetAddress (1), port);
107  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
108  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
109  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
110  apps = client.Install (n.Get (0));
111  apps.Start (Seconds (2.0));
112  apps.Stop (Seconds (10.0));
113 
114  Simulator::Run ();
115  Simulator::Destroy ();
116 
117  NS_TEST_ASSERT_MSG_EQ (server.GetServer ()->GetLost (), 0, "Packets were lost !");
118  NS_TEST_ASSERT_MSG_EQ (server.GetServer ()->GetReceived (), 8, "Did not receive expected number of packets !");
119 }
120 
127 {
128 public:
131 
132 private:
133  virtual void DoRun (void);
134 
135 };
136 
138  : TestCase ("Test that all the udp packets generated by an udpTraceClient application are correctly received by an udpServer application")
139 {
140 }
141 
143 {
144 }
145 
147 {
149  n.Create (2);
150 
151  InternetStackHelper internet;
152  internet.Install (n);
153 
154  // link the two nodes
155  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice> ();
156  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice> ();
157  n.Get (0)->AddDevice (txDev);
158  n.Get (1)->AddDevice (rxDev);
159  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
160  rxDev->SetChannel (channel1);
161  txDev->SetChannel (channel1);
163  d.Add (txDev);
164  d.Add (rxDev);
165 
166  Ipv4AddressHelper ipv4;
167  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
168  Ipv4InterfaceContainer i = ipv4.Assign (d);
169 
170  uint16_t port = 4000;
171  UdpServerHelper server (port);
172  ApplicationContainer apps = server.Install (n.Get (1));
173  apps.Start (Seconds (1.0));
174  apps.Stop (Seconds (10.0));
175 
176  uint32_t MaxPacketSize = 1400 - 28; // ip/udp header
177  UdpTraceClientHelper client (i.GetAddress (1), port,"");
178  client.SetAttribute ("MaxPacketSize", UintegerValue (MaxPacketSize));
179  apps = client.Install (n.Get (0));
180  apps.Start (Seconds (2.0));
181  apps.Stop (Seconds (10.0));
182 
183  Simulator::Run ();
184  Simulator::Destroy ();
185 
186  NS_TEST_ASSERT_MSG_EQ (server.GetServer ()->GetLost (), 0, "Packets were lost !");
187  NS_TEST_ASSERT_MSG_EQ (server.GetServer ()->GetReceived (), 247, "Did not receive expected number of packets !");
188 }
189 
190 
196 {
197 public:
199  virtual ~PacketLossCounterTestCase ();
200 
201 private:
202  virtual void DoRun (void);
203 
204 };
205 
207  : TestCase ("Test that all the PacketLossCounter class checks loss correctly in different cases")
208 {
209 }
210 
212 {
213 }
214 
216 {
217  PacketLossCounter lossCounter (32);
218  lossCounter.NotifyReceived (32); //out of order
219  for (uint32_t i = 0; i < 64; i++)
220  {
221  lossCounter.NotifyReceived (i);
222  }
223 
224  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 0, "Check that 0 packets are lost");
225 
226  for (uint32_t i = 65; i < 128; i++) // drop (1) seqNum 64
227  {
228  lossCounter.NotifyReceived (i);
229  }
230  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 1, "Check that 1 packet is lost");
231 
232  for (uint32_t i = 134; i < 200; i++) // drop seqNum 128,129,130,131,132,133
233  {
234  lossCounter.NotifyReceived (i);
235  }
236  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 7, "Check that 7 (6+1) packets are lost");
237 
238  // reordering without loss
239  lossCounter.NotifyReceived (205);
240  lossCounter.NotifyReceived (206);
241  lossCounter.NotifyReceived (207);
242  lossCounter.NotifyReceived (200);
243  lossCounter.NotifyReceived (201);
244  lossCounter.NotifyReceived (202);
245  lossCounter.NotifyReceived (203);
246  lossCounter.NotifyReceived (204);
247  for (uint32_t i = 205; i < 250; i++)
248  {
249  lossCounter.NotifyReceived (i);
250  }
251  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 7, "Check that 7 (6+1) packets are lost even when reordering happens");
252 
253  // reordering with loss
254  lossCounter.NotifyReceived (255);
255  // drop (2) seqNum 250, 251
256  lossCounter.NotifyReceived (252);
257  lossCounter.NotifyReceived (253);
258  lossCounter.NotifyReceived (254);
259  for (uint32_t i = 256; i < 300; i++)
260  {
261  lossCounter.NotifyReceived (i);
262  }
263  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 9, "Check that 9 (6+1+2) packet are lost");
264 }
265 
271 {
272 public:
275 
276 private:
277  virtual void DoRun (void);
278 
279 };
280 
282  : TestCase ("Test that the UdpEchoClient::SetFill class sets packet size (bug 1378)")
283 {
284 }
285 
287 {
288 }
289 
291 {
293  nodes.Create (2);
294 
295  InternetStackHelper internet;
296  internet.Install (nodes);
297 
298  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice> ();
299  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice> ();
300  nodes.Get (0)->AddDevice (txDev);
301  nodes.Get (1)->AddDevice (rxDev);
302  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
303  rxDev->SetChannel (channel1);
304  txDev->SetChannel (channel1);
306  d.Add (txDev);
307  d.Add (rxDev);
308 
309  Ipv4AddressHelper ipv4;
310 
311  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
313 
314  uint16_t port = 5000;
316  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
317  serverApps.Start (Seconds (1.0));
318  serverApps.Stop (Seconds (10.0));
319  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), port);
320  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
321  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
322  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
323 
324  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
325 
326  uint8_t arry[64];
327  uint8_t i;
328  for (i = 0; i < 64; i++)
329  {
330  arry[i] = i;
331  }
332  echoClient.SetFill (clientApps.Get (0), &(arry[0]), (uint32_t)64, (uint32_t)64);
333 
334  clientApps.Start (Seconds (2.0));
335  clientApps.Stop (Seconds (10.0));
336 
337  Simulator::Run ();
338  Simulator::Destroy ();
339 }
340 
341 
342 
350 {
351 public:
353 };
354 
356  : TestSuite ("udp-client-server", UNIT)
357 {
358  AddTestCase (new UdpTraceClientServerTestCase, TestCase::QUICK);
359  AddTestCase (new UdpClientServerTestCase, TestCase::QUICK);
360  AddTestCase (new PacketLossCounterTestCase, TestCase::QUICK);
361  AddTestCase (new UdpEchoClientSetFillTestCase, TestCase::QUICK);
362 }
363 
holds a vector of ns3::Application pointers.
Test that all the udp packets generated by an udpTraceClient application are correctly received by an...
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void NotifyReceived(uint32_t seq)
Record a successfully received packet.
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
A suite of tests to run.
Definition: test.h:1343
serverApps
Definition: first.py:52
Create an application which sends a UDP packet and waits for an echo of this packet.
Test that all the PacketLossCounter class checks loss correctly in different cases.
echoServer
Definition: first.py:50
virtual void DoRun(void)
Implementation to actually run this TestCase.
aggregate IP/TCP/UDP functionality to existing Nodes.
virtual void DoRun(void)
Implementation to actually run this TestCase.
encapsulates test code
Definition: test.h:1153
uint16_t port
Definition: dsdv-manet.cc:45
Create a server application which waits for input UDP packets and sends them back to the original sen...
nodes
Definition: first.py:32
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
AttributeValue implementation for Time.
Definition: nstime.h:1353
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Create UdpTraceClient application which sends UDP packets based on a trace file of an MPEG4 stream...
echoClient
Definition: first.py:56
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:166
Ptr< UdpServer > GetServer(void)
Return the last created server.
virtual void DoRun(void)
Implementation to actually run this TestCase.
holds a vector of ns3::NetDevice pointers
UDP client and server TestSuite.
Create a server application which waits for input UDP packets and uses the information carried into t...
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
uint32_t GetLost(void) const
Get the number of lost packets.
Test that all the UDP packets generated by an UdpClient application are correctly received by an UdpS...
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A class to count the number of lost packets.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
ApplicationContainer Install(NodeContainer c)
Create one UDP server application on each of the Nodes in the NodeContainer.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
interfaces
Definition: first.py:48
clientApps
Definition: first.py:61
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static UdpClientServerTestSuite udpClientServerTestSuite
Static variable for test initialization.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
virtual void DoRun(void)
Implementation to actually run this TestCase.