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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Pavel Boyko <boyko@iitp.ru>
7 */
8
9#include "ns3/abort.h"
10#include "ns3/aodv-helper.h"
11#include "ns3/boolean.h"
12#include "ns3/config.h"
13#include "ns3/constant-position-mobility-model.h"
14#include "ns3/double.h"
15#include "ns3/internet-stack-helper.h"
16#include "ns3/ipv4-address-helper.h"
17#include "ns3/mobility-helper.h"
18#include "ns3/mobility-model.h"
19#include "ns3/names.h"
20#include "ns3/pcap-file.h"
21#include "ns3/simulator.h"
22#include "ns3/socket-factory.h"
23#include "ns3/string.h"
24#include "ns3/test.h"
25#include "ns3/udp-echo-helper.h"
26#include "ns3/udp-socket-factory.h"
27#include "ns3/uinteger.h"
28#include "ns3/yans-wifi-helper.h"
29
30#include <sstream>
31
32namespace ns3
33{
34namespace aodv
35{
36
37/**
38 * \ingroup aodv
39 *
40 * \brief AODV loopback UDP echo test case
41 */
43{
44 uint32_t m_count; //!< number of packet received;
45 Ptr<Socket> m_txSocket; //!< transmit socket;
46 Ptr<Socket> m_echoSocket; //!< echo socket;
47 Ptr<Socket> m_rxSocket; //!< receive socket;
48 uint16_t m_echoSendPort; //!< echo send port;
49 uint16_t m_echoReplyPort; //!< echo reply port;
50
51 /**
52 * Send data function
53 * \param socket The socket to send data
54 */
55 void SendData(Ptr<Socket> socket);
56 /**
57 * Receive packet function
58 * \param socket The socket to receive data
59 */
60 void ReceivePkt(Ptr<Socket> socket);
61 /**
62 * Echo data function
63 * \param socket The socket to echo data
64 */
65 void EchoData(Ptr<Socket> socket) const;
66
67 public:
69 void DoRun() override;
70};
71
73 : TestCase("UDP Echo 127.0.0.1 test"),
74 m_count(0)
75{
76 m_echoSendPort = 1233;
77 m_echoReplyPort = 1234;
78}
79
80void
82{
83 Ptr<Packet> receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
84
85 m_count++;
86}
87
88void
90{
91 Address from;
92 Ptr<Packet> receivedPacket = socket->RecvFrom(std::numeric_limits<uint32_t>::max(), 0, from);
93
96
97 receivedPacket->RemoveAllPacketTags();
98 receivedPacket->RemoveAllByteTags();
99
100 socket->SendTo(receivedPacket, 0, to);
101}
102
103void
105{
107 socket->SendTo(Create<Packet>(123), 0, realTo);
108
109 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
110 Seconds(1.0),
112 this,
113 socket);
114}
115
116void
118{
120 nodes.Create(1);
122 m->SetPosition(Vector(0, 0, 0));
123 nodes.Get(0)->AggregateObject(m);
124 // Setup WiFi
125 WifiMacHelper wifiMac;
126 wifiMac.SetType("ns3::AdhocWifiMac");
127 YansWifiPhyHelper wifiPhy;
129 wifiPhy.SetChannel(wifiChannel.Create());
130 WifiHelper wifi;
131 wifi.SetStandard(WIFI_STANDARD_80211a);
132 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
133 "DataMode",
134 StringValue("OfdmRate6Mbps"),
135 "RtsCtsThreshold",
136 StringValue("2200"));
137 NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);
138
139 // Setup TCP/IP & AODV
140 AodvHelper aodv; // Use default parameters here
141 InternetStackHelper internetStack;
142 internetStack.SetRoutingHelper(aodv);
143 internetStack.Install(nodes);
144 Ipv4AddressHelper address;
145 address.SetBase("10.1.1.0", "255.255.255.0");
146 Ipv4InterfaceContainer interfaces = address.Assign(devices);
147
148 // Setup echos
149 Ptr<SocketFactory> socketFactory = nodes.Get(0)->GetObject<UdpSocketFactory>();
150 m_rxSocket = socketFactory->CreateSocket();
153
154 m_echoSocket = socketFactory->CreateSocket();
157
158 m_txSocket = socketFactory->CreateSocket();
159
161 Seconds(1.0),
163 this,
164 m_txSocket);
165
166 // Run
169
170 m_txSocket->Close();
172 m_rxSocket->Close();
173
175
176 // Check that 4 packets delivered
177 NS_TEST_ASSERT_MSG_EQ(m_count, 4, "Exactly 4 echo replies must be delivered.");
178}
179
180/**
181 * \ingroup aodv-test
182 *
183 * \brief AODV Loopback test suite
184 */
186{
187 public:
189 : TestSuite("routing-aodv-loopback", Type::SYSTEM)
190 {
191 SetDataDir(NS_TEST_SOURCEDIR);
192 // UDP Echo loopback test case
194 }
195} g_aodvLoopbackTestSuite; ///< the test suite
196
197} // namespace aodv
198} // namespace ns3
a polymophic address class
Definition address.h:90
Helper class that adds AODV routing to nodes.
Definition aodv-helper.h:25
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.
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.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
uint32_t GetId() const
Definition node.cc:106
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition socket.cc:117
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:45
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
Definition test.cc:472
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto SYSTEM
Definition test.h:1293
API to create UDP socket instances.
helps to create WifiNetDevice objects
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:186
AODV loopback UDP echo test case.
Definition loopback.cc:43
Ptr< Socket > m_rxSocket
receive socket;
Definition loopback.cc:47
void SendData(Ptr< Socket > socket)
Send data function.
Definition loopback.cc:104
uint16_t m_echoSendPort
echo send port;
Definition loopback.cc:48
void DoRun() override
Implementation to actually run this TestCase.
Definition loopback.cc:117
void EchoData(Ptr< Socket > socket) const
Echo data function.
Definition loopback.cc:89
Ptr< Socket > m_txSocket
transmit socket;
Definition loopback.cc:45
void ReceivePkt(Ptr< Socket > socket)
Receive packet function.
Definition loopback.cc:81
uint16_t m_echoReplyPort
echo reply port;
Definition loopback.cc:49
Ptr< Socket > m_echoSocket
echo socket;
Definition loopback.cc:46
uint32_t m_count
number of packet received;
Definition loopback.cc:44
ns3::aodv::AodvLoopbackTestSuite g_aodvLoopbackTestSuite
the test suite
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:134
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
@ 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:684