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 
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::UdpClientHelper::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
Definition: udp-client-server-helper.cc:87
ns3::UdpServerHelper::GetServer
Ptr< UdpServer > GetServer(void)
Return the last created server.
Definition: udp-client-server-helper.cc:63
ns3::UdpClientHelper
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Definition: udp-client-server-helper.h:94
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
UdpClientServerTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: udp-client-server-test.cc:72
UdpEchoClientSetFillTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: udp-client-server-test.cc:290
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
UdpTraceClientServerTestCase::UdpTraceClientServerTestCase
UdpTraceClientServerTestCase()
Definition: udp-client-server-test.cc:137
ns3::UdpEchoClientHelper
Create an application which sends a UDP packet and waits for an echo of this packet.
Definition: udp-echo-helper.h:107
UdpEchoClientSetFillTestCase::~UdpEchoClientSetFillTestCase
virtual ~UdpEchoClientSetFillTestCase()
Definition: udp-client-server-test.cc:286
ns3::SimpleNetDevice::SetChannel
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
Definition: simple-net-device.cc:274
ns3::UdpTraceClientHelper
Create UdpTraceClient application which sends UDP packets based on a trace file of an MPEG4 stream.
Definition: udp-client-server-helper.h:159
PacketLossCounterTestCase
Test that all the PacketLossCounter class checks loss correctly in different cases.
Definition: udp-client-server-test.cc:196
PacketLossCounterTestCase::PacketLossCounterTestCase
PacketLossCounterTestCase()
Definition: udp-client-server-test.cc:206
first.nodes
nodes
Definition: first.py:32
ns3::UdpServerHelper
Create a server application which waits for input UDP packets and uses the information carried into t...
Definition: udp-client-server-helper.h:38
ns3::PacketLossCounter::NotifyReceived
void NotifyReceived(uint32_t seq)
Record a successfully received packet.
Definition: packet-loss-counter.cc:110
ns3::TestCase
encapsulates test code
Definition: test.h:1154
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::UdpTraceClientHelper::Install
ApplicationContainer Install(NodeContainer c)
Definition: udp-client-server-helper.cc:133
UdpEchoClientSetFillTestCase::UdpEchoClientSetFillTestCase
UdpEchoClientSetFillTestCase()
Definition: udp-client-server-test.cc:281
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
UdpClientServerTestCase
Test that all the UDP packets generated by an UdpClient application are correctly received by an UdpS...
Definition: udp-client-server-test.cc:53
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
UdpTraceClientServerTestCase::~UdpTraceClientServerTestCase
virtual ~UdpTraceClientServerTestCase()
Definition: udp-client-server-test.cc:142
ns3::UdpTraceClientHelper::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
Definition: udp-client-server-helper.cc:127
first.echoClient
echoClient
Definition: first.py:56
first.interfaces
interfaces
Definition: first.py:48
UdpTraceClientServerTestCase
Test that all the udp packets generated by an udpTraceClient application are correctly received by an...
Definition: udp-client-server-test.cc:127
UdpClientServerTestCase::UdpClientServerTestCase
UdpClientServerTestCase()
Definition: udp-client-server-test.cc:63
ns3::TestSuite
A suite of tests to run.
Definition: test.h:1344
UdpClientServerTestCase::~UdpClientServerTestCase
virtual ~UdpClientServerTestCase()
Definition: udp-client-server-test.cc:68
first.clientApps
clientApps
Definition: first.py:61
NS_TEST_ASSERT_MSG_EQ
#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
UdpClientServerTestSuite::UdpClientServerTestSuite
UdpClientServerTestSuite()
Definition: udp-client-server-test.cc:355
udpClientServerTestSuite
static UdpClientServerTestSuite udpClientServerTestSuite
Static variable for test initialization.
Definition: udp-client-server-test.cc:364
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
UdpEchoClientSetFillTestCase
Test fix for Bug 1378
Definition: udp-client-server-test.cc:271
first.serverApps
serverApps
Definition: first.py:52
ns3::UdpClientHelper::Install
ApplicationContainer Install(NodeContainer c)
Definition: udp-client-server-helper.cc:93
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::UdpServerHelper::Install
ApplicationContainer Install(NodeContainer c)
Create one UDP server application on each of the Nodes in the NodeContainer.
Definition: udp-client-server-helper.cc:47
PacketLossCounterTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: udp-client-server-test.cc:215
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::PacketLossCounter
A class to count the number of lost packets.
Definition: packet-loss-counter.h:46
ns3::Ipv4InterfaceContainer::GetAddress
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Definition: ipv4-interface-container.cc:59
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::UdpEchoServerHelper
Create a server application which waits for input UDP packets and sends them back to the original sen...
Definition: udp-echo-helper.h:38
ns3::PacketLossCounter::GetLost
uint32_t GetLost(void) const
Get the number of lost packets.
Definition: packet-loss-counter.cc:70
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::NetDeviceContainer::Add
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Definition: net-device-container.cc:67
UdpTraceClientServerTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: udp-client-server-test.cc:146
UdpClientServerTestSuite
UDP client and server TestSuite.
Definition: udp-client-server-test.cc:350
PacketLossCounterTestCase::~PacketLossCounterTestCase
virtual ~PacketLossCounterTestCase()
Definition: udp-client-server-test.cc:211
sample-rng-plot.n
n
Definition: sample-rng-plot.py:37
first.echoServer
echoServer
Definition: first.py:50
port
uint16_t port
Definition: dsdv-manet.cc:45