A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
43namespace ns3
44{
45namespace aodv
46{
47
48/**
49 * \ingroup aodv
50 *
51 * \brief AODV loopback UDP echo test case
52 */
54{
55 uint32_t m_count; //!< number of packet received;
56 Ptr<Socket> m_txSocket; //!< transmit socket;
57 Ptr<Socket> m_echoSocket; //!< echo socket;
58 Ptr<Socket> m_rxSocket; //!< receive socket;
59 uint16_t m_echoSendPort; //!< echo send port;
60 uint16_t m_echoReplyPort; //!< echo reply port;
61
62 /**
63 * Send data function
64 * \param socket The socket to send data
65 */
66 void SendData(Ptr<Socket> socket);
67 /**
68 * Receive packet function
69 * \param socket The socket to receive data
70 */
71 void ReceivePkt(Ptr<Socket> socket);
72 /**
73 * Echo data function
74 * \param socket The socket to echo data
75 */
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
91void
93{
94 Ptr<Packet> receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
95
96 m_count++;
97}
98
99void
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
114void
116{
118 socket->SendTo(Create<Packet>(123), 0, realTo);
119
120 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
121 Seconds(1.0),
123 this,
124 socket);
125}
126
127void
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());
141 WifiHelper wifi;
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);
155 Ipv4AddressHelper address;
156 address.SetBase("10.1.1.0", "255.255.255.0");
157 Ipv4InterfaceContainer interfaces = address.Assign(devices);
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
180
181 m_txSocket->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
191/**
192 * \ingroup aodv-test
193 *
194 * \brief AODV Loopback test suite
195 */
197{
198 public:
200 : TestSuite("routing-aodv-loopback", Type::SYSTEM)
201 {
202 SetDataDir(NS_TEST_SOURCEDIR);
203 // UDP Echo loopback test case
205 }
206} g_aodvLoopbackTestSuite; ///< the test suite
207
208} // namespace aodv
209} // namespace ns3
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
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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 int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
Hold variables of type string.
Definition: string.h:56
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=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:1268
Type
Type of test.
Definition: test.h:1275
static constexpr auto SYSTEM
Definition: test.h:1288
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:145
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
@ WIFI_STANDARD_80211a
NodeContainer nodes
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:706