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/v4ping.h"
40 #include "ns3/yans-wifi-helper.h"
41 
42 #include <sstream>
43 
44 namespace ns3
45 {
46 namespace aodv
47 {
48 
54 class LoopbackTestCase : public TestCase
55 {
56  uint32_t m_count;
60  uint16_t m_echoSendPort;
61  uint16_t m_echoReplyPort;
62 
67  void SendData(Ptr<Socket> socket);
72  void ReceivePkt(Ptr<Socket> socket);
77  void EchoData(Ptr<Socket> socket) const;
78 
79  public:
81  void DoRun() override;
82 };
83 
85  : TestCase("UDP Echo 127.0.0.1 test"),
86  m_count(0)
87 {
88  m_echoSendPort = 1233;
89  m_echoReplyPort = 1234;
90 }
91 
92 void
94 {
95  Ptr<Packet> receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
96 
97  m_count++;
98 }
99 
100 void
102 {
103  Address from;
104  Ptr<Packet> receivedPacket = socket->RecvFrom(std::numeric_limits<uint32_t>::max(), 0, from);
105 
108 
109  receivedPacket->RemoveAllPacketTags();
110  receivedPacket->RemoveAllByteTags();
111 
112  socket->SendTo(receivedPacket, 0, to);
113 }
114 
115 void
117 {
119  socket->SendTo(Create<Packet>(123), 0, realTo);
120 
122  Seconds(1.0),
124  this,
125  socket);
126 }
127 
128 void
130 {
132  nodes.Create(1);
133  Ptr<MobilityModel> m = CreateObject<ConstantPositionMobilityModel>();
134  m->SetPosition(Vector(0, 0, 0));
135  nodes.Get(0)->AggregateObject(m);
136  // Setup WiFi
137  WifiMacHelper wifiMac;
138  wifiMac.SetType("ns3::AdhocWifiMac");
139  YansWifiPhyHelper wifiPhy;
141  wifiPhy.SetChannel(wifiChannel.Create());
143  wifi.SetStandard(WIFI_STANDARD_80211a);
144  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
145  "DataMode",
146  StringValue("OfdmRate6Mbps"),
147  "RtsCtsThreshold",
148  StringValue("2200"));
149  NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);
150 
151  // Setup TCP/IP & AODV
152  AodvHelper aodv; // Use default parameters here
153  InternetStackHelper internetStack;
154  internetStack.SetRoutingHelper(aodv);
155  internetStack.Install(nodes);
157  address.SetBase("10.1.1.0", "255.255.255.0");
159 
160  // Setup echos
161  Ptr<SocketFactory> socketFactory = nodes.Get(0)->GetObject<UdpSocketFactory>();
162  m_rxSocket = socketFactory->CreateSocket();
165 
166  m_echoSocket = socketFactory->CreateSocket();
169 
170  m_txSocket = socketFactory->CreateSocket();
171 
173  Seconds(1.0),
175  this,
176  m_txSocket);
177 
178  // Run
180  Simulator::Run();
181 
182  m_txSocket->Close();
183  m_echoSocket->Close();
184  m_rxSocket->Close();
185 
187 
188  // Check that 4 packets delivered
189  NS_TEST_ASSERT_MSG_EQ(m_count, 4, "Exactly 4 echo replies must be delivered.");
190 }
191 
198 {
199  public:
201  : TestSuite("routing-aodv-loopback", SYSTEM)
202  {
203  SetDataDir(NS_TEST_SOURCEDIR);
204  // UDP Echo loopback test case
206  }
208 
209 } // namespace aodv
210 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:43
a polymophic address class
Definition: address.h:100
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:43
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:1009
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:587
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
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:305
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
Definition: test.cc:482
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:325
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:198
AODV loopback UDP echo test case.
Definition: loopback.cc:55
Ptr< Socket > m_rxSocket
receive socket;
Definition: loopback.cc:59
void SendData(Ptr< Socket > socket)
Send data function.
Definition: loopback.cc:116
uint16_t m_echoSendPort
echo send port;
Definition: loopback.cc:60
void DoRun() override
Implementation to actually run this TestCase.
Definition: loopback.cc:129
void EchoData(Ptr< Socket > socket) const
Echo data function.
Definition: loopback.cc:101
Ptr< Socket > m_txSocket
transmit socket;
Definition: loopback.cc:57
void ReceivePkt(Ptr< Socket > socket)
Receive packet function.
Definition: loopback.cc:93
uint16_t m_echoReplyPort
echo reply port;
Definition: loopback.cc:61
Ptr< Socket > m_echoSocket
echo socket;
Definition: loopback.cc:58
uint32_t m_count
number of packet received;
Definition: loopback.cc:56
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:1336
@ WIFI_STANDARD_80211a
NodeContainer nodes
address
Definition: first.py:40
devices
Definition: first.py:35
interfaces
Definition: first.py:44
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:707
wifi
Definition: third.py:88