A Discrete-Event Network Simulator
API
loopback.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 IITP RAS
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  * Authors: Pavel Boyko <boyko@iitp.ru>
19  */
20 
21 #include "ns3/test.h"
22 #include "ns3/simulator.h"
23 #include "ns3/socket-factory.h"
24 #include "ns3/udp-socket-factory.h"
25 #include "ns3/mobility-helper.h"
26 #include "ns3/double.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/string.h"
29 #include "ns3/boolean.h"
30 #include "ns3/yans-wifi-helper.h"
31 #include "ns3/internet-stack-helper.h"
32 #include "ns3/ipv4-address-helper.h"
33 #include "ns3/abort.h"
34 #include "ns3/udp-echo-helper.h"
35 #include "ns3/mobility-model.h"
36 #include "ns3/pcap-file.h"
37 #include "ns3/aodv-helper.h"
38 #include "ns3/v4ping.h"
39 #include "ns3/config.h"
40 #include "ns3/constant-position-mobility-model.h"
41 #include "ns3/names.h"
42 #include <sstream>
43 
44 namespace ns3 {
45 namespace aodv {
46 
52 class LoopbackTestCase : public TestCase
53 {
54  uint32_t m_count;
58  uint16_t m_echoSendPort;
59  uint16_t m_echoReplyPort;
60 
65  void SendData (Ptr<Socket> socket);
70  void ReceivePkt (Ptr<Socket> socket);
75  void EchoData (Ptr<Socket> socket);
76 
77 public:
79  void DoRun ();
80 };
81 
83  : TestCase ("UDP Echo 127.0.0.1 test"),
84  m_count (0)
85 {
86  m_echoSendPort = 1233;
87  m_echoReplyPort = 1234;
88 }
89 
91 {
92  Ptr<Packet> receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
93 
94  m_count++;
95 }
96 
97 void
99 {
100  Address from;
101  Ptr<Packet> receivedPacket = socket->RecvFrom (std::numeric_limits<uint32_t>::max (), 0, from);
102 
105 
106  receivedPacket->RemoveAllPacketTags ();
107  receivedPacket->RemoveAllByteTags ();
108 
109  socket->SendTo (receivedPacket, 0, to);
110 }
111 
112 void
114 {
116  socket->SendTo (Create<Packet> (123), 0, realTo);
117 
118  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (1.0),
119  &LoopbackTestCase::SendData, this, socket);
120 }
121 
122 void
124 {
126  nodes.Create (1);
127  Ptr<MobilityModel> m = CreateObject<ConstantPositionMobilityModel> ();
128  m->SetPosition (Vector (0, 0, 0));
129  nodes.Get (0)->AggregateObject (m);
130  // Setup WiFi
131  WifiMacHelper wifiMac;
132  wifiMac.SetType ("ns3::AdhocWifiMac");
133  YansWifiPhyHelper wifiPhy;
135  wifiPhy.SetChannel (wifiChannel.Create ());
137  wifi.SetStandard (WIFI_STANDARD_80211a);
138  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", StringValue ("2200"));
139  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
140 
141  // Setup TCP/IP & AODV
142  AodvHelper aodv; // Use default parameters here
143  InternetStackHelper internetStack;
144  internetStack.SetRoutingHelper (aodv);
145  internetStack.Install (nodes);
147  address.SetBase ("10.1.1.0", "255.255.255.0");
149 
150  // Setup echos
151  Ptr<SocketFactory> socketFactory = nodes.Get (0)->GetObject<UdpSocketFactory> ();
152  m_rxSocket = socketFactory->CreateSocket ();
155 
156  m_echoSocket = socketFactory->CreateSocket ();
159 
160  m_txSocket = socketFactory->CreateSocket ();
161 
164 
165  // Run
166  Simulator::Stop (Seconds (5));
167  Simulator::Run ();
168 
169  m_txSocket->Close ();
170  m_echoSocket->Close ();
171  m_rxSocket->Close ();
172 
174 
175  // Check that 4 packets delivered
176  NS_TEST_ASSERT_MSG_EQ (m_count, 4, "Exactly 4 echo replies must be delivered.");
177 }
178 
186 {
187 public:
188  AodvLoopbackTestSuite () : TestSuite ("routing-aodv-loopback", SYSTEM)
189  {
190  SetDataDir (NS_TEST_SOURCEDIR);
191  // UDP Echo loopback test case
193  }
195 
196 
197 } // namespace aodv
198 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:43
a polymophic address class
Definition: address.h:91
Helper class that adds AODV routing to nodes.
Definition: aodv-helper.h:35
an Inet address class
Ipv4Address GetIpv4(void) const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
static Ipv4Address GetLoopback(void)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void SetPosition(const Vector &position)
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
uint32_t GetId(void) const
Definition: node.cc:109
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:984
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
virtual int Close(void)=0
Close a socket.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
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 int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
Hold variables of type string.
Definition: string.h:41
encapsulates test code
Definition: test.h:994
@ QUICK
Fast test.
Definition: test.h:999
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
Definition: test.cc:465
A suite of tests to run.
Definition: test.h:1188
@ SYSTEM
This test suite implements a System Test.
Definition: test.h:1198
API to create UDP socket instances.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:274
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
Ptr< YansWifiChannel > Create(void) const
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
AODV Loopback test suite.
Definition: loopback.cc:186
AODV loopback UDP echo test case.
Definition: loopback.cc:53
Ptr< Socket > m_rxSocket
receive socket;
Definition: loopback.cc:57
void EchoData(Ptr< Socket > socket)
Echo data function.
Definition: loopback.cc:98
void SendData(Ptr< Socket > socket)
Send data function.
Definition: loopback.cc:113
uint16_t m_echoSendPort
echo send port;
Definition: loopback.cc:58
void DoRun()
Implementation to actually run this TestCase.
Definition: loopback.cc:123
Ptr< Socket > m_txSocket
transmit socket;
Definition: loopback.cc:55
void ReceivePkt(Ptr< Socket > socket)
Receive packet function.
Definition: loopback.cc:90
uint16_t m_echoReplyPort
echo reply port;
Definition: loopback.cc:59
Ptr< Socket > m_echoSocket
echo socket;
Definition: loopback.cc:56
uint32_t m_count
number of packet received;
Definition: loopback.cc:54
#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:141
ns3::aodv::AodvLoopbackTestSuite g_aodvLoopbackTestSuite
the test suite
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
@ WIFI_STANDARD_80211a
address
Definition: first.py:44
devices
Definition: first.py:39
nodes
Definition: first.py:32
interfaces
Definition: first.py:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
wifi
Definition: third.py:96