A Discrete-Event Network Simulator
API
loopback.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 IITP RAS
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Pavel Boyko <boyko@iitp.ru>
18  */
19 
20 #include "ns3/abort.h"
21 #include "ns3/aodv-helper.h"
22 #include "ns3/boolean.h"
23 #include "ns3/config.h"
24 #include "ns3/constant-position-mobility-model.h"
25 #include "ns3/double.h"
26 #include "ns3/internet-stack-helper.h"
27 #include "ns3/ipv4-address-helper.h"
28 #include "ns3/mobility-helper.h"
29 #include "ns3/mobility-model.h"
30 #include "ns3/names.h"
31 #include "ns3/pcap-file.h"
32 #include "ns3/simulator.h"
33 #include "ns3/socket-factory.h"
34 #include "ns3/string.h"
35 #include "ns3/test.h"
36 #include "ns3/udp-echo-helper.h"
37 #include "ns3/udp-socket-factory.h"
38 #include "ns3/uinteger.h"
39 #include "ns3/yans-wifi-helper.h"
40 
41 #include <sstream>
42 
43 namespace ns3
44 {
45 namespace aodv
46 {
47 
53 class LoopbackTestCase : public TestCase
54 {
55  uint32_t m_count;
59  uint16_t m_echoSendPort;
60  uint16_t m_echoReplyPort;
61 
66  void SendData(Ptr<Socket> socket);
71  void ReceivePkt(Ptr<Socket> socket);
76  void EchoData(Ptr<Socket> socket) const;
77 
78  public:
80  void DoRun() override;
81 };
82 
84  : TestCase("UDP Echo 127.0.0.1 test"),
85  m_count(0)
86 {
87  m_echoSendPort = 1233;
88  m_echoReplyPort = 1234;
89 }
90 
91 void
93 {
94  Ptr<Packet> receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
95 
96  m_count++;
97 }
98 
99 void
101 {
102  Address from;
103  Ptr<Packet> receivedPacket = socket->RecvFrom(std::numeric_limits<uint32_t>::max(), 0, from);
104 
107 
108  receivedPacket->RemoveAllPacketTags();
109  receivedPacket->RemoveAllByteTags();
110 
111  socket->SendTo(receivedPacket, 0, to);
112 }
113 
114 void
116 {
118  socket->SendTo(Create<Packet>(123), 0, realTo);
119 
121  Seconds(1.0),
123  this,
124  socket);
125 }
126 
127 void
129 {
131  nodes.Create(1);
132  Ptr<MobilityModel> m = CreateObject<ConstantPositionMobilityModel>();
133  m->SetPosition(Vector(0, 0, 0));
134  nodes.Get(0)->AggregateObject(m);
135  // Setup WiFi
136  WifiMacHelper wifiMac;
137  wifiMac.SetType("ns3::AdhocWifiMac");
138  YansWifiPhyHelper wifiPhy;
140  wifiPhy.SetChannel(wifiChannel.Create());
142  wifi.SetStandard(WIFI_STANDARD_80211a);
143  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
144  "DataMode",
145  StringValue("OfdmRate6Mbps"),
146  "RtsCtsThreshold",
147  StringValue("2200"));
148  NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);
149 
150  // Setup TCP/IP & AODV
151  AodvHelper aodv; // Use default parameters here
152  InternetStackHelper internetStack;
153  internetStack.SetRoutingHelper(aodv);
154  internetStack.Install(nodes);
156  address.SetBase("10.1.1.0", "255.255.255.0");
158 
159  // Setup echos
160  Ptr<SocketFactory> socketFactory = nodes.Get(0)->GetObject<UdpSocketFactory>();
161  m_rxSocket = socketFactory->CreateSocket();
164 
165  m_echoSocket = socketFactory->CreateSocket();
168 
169  m_txSocket = socketFactory->CreateSocket();
170 
172  Seconds(1.0),
174  this,
175  m_txSocket);
176 
177  // Run
179  Simulator::Run();
180 
181  m_txSocket->Close();
182  m_echoSocket->Close();
183  m_rxSocket->Close();
184 
186 
187  // Check that 4 packets delivered
188  NS_TEST_ASSERT_MSG_EQ(m_count, 4, "Exactly 4 echo replies must be delivered.");
189 }
190 
197 {
198  public:
200  : TestSuite("routing-aodv-loopback", SYSTEM)
201  {
202  SetDataDir(NS_TEST_SOURCEDIR);
203  // UDP Echo loopback test case
205  }
207 
208 } // namespace aodv
209 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:42
a polymophic address class
Definition: address.h:101
Helper class that adds AODV routing to nodes.
Definition: aodv-helper.h:36
an Inet address class
Ipv4Address GetIpv4() 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:42
static Ipv4Address GetLoopback()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
uint32_t GetId() const
Definition: node.cc:117
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
void RemoveAllPacketTags()
Remove all packet tags.
Definition: packet.cc:990
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
virtual int Close()=0
Close a socket.
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:56
encapsulates test code
Definition: test.h:1060
@ QUICK
Fast test.
Definition: test.h:1065
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
Definition: test.cc:478
A suite of tests to run.
Definition: test.h:1256
@ SYSTEM
This test suite implements a System Test.
Definition: test.h:1266
API to create UDP socket instances.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Ptr< YansWifiChannel > Create() const
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:197
AODV loopback UDP echo test case.
Definition: loopback.cc:54
Ptr< Socket > m_rxSocket
receive socket;
Definition: loopback.cc:58
void SendData(Ptr< Socket > socket)
Send data function.
Definition: loopback.cc:115
uint16_t m_echoSendPort
echo send port;
Definition: loopback.cc:59
void DoRun() override
Implementation to actually run this TestCase.
Definition: loopback.cc:128
void EchoData(Ptr< Socket > socket) const
Echo data function.
Definition: loopback.cc:100
Ptr< Socket > m_txSocket
transmit socket;
Definition: loopback.cc:56
void ReceivePkt(Ptr< Socket > socket)
Receive packet function.
Definition: loopback.cc:92
uint16_t m_echoReplyPort
echo reply port;
Definition: loopback.cc:60
Ptr< Socket > m_echoSocket
echo socket;
Definition: loopback.cc:57
uint32_t m_count
number of packet received;
Definition: loopback.cc:55
ns3::aodv::AodvLoopbackTestSuite g_aodvLoopbackTestSuite
the test suite
#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:144
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
@ WIFI_STANDARD_80211a
NodeContainer nodes
address
Definition: first.py:47
devices
Definition: first.py:42
interfaces
Definition: first.py:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
wifi
Definition: third.py:95