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 {
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 
63  void SendData (Ptr<Socket> socket);
64  void ReceivePkt (Ptr<Socket> socket);
65  void EchoData (Ptr<Socket> socket);
66 
67 public:
69  void DoRun ();
70 };
71 
73  TestCase ("UDP Echo 127.0.0.1 test"), m_count (0)
74 {
75  m_echoSendPort = 1233;
76  m_echoReplyPort = 1234;
77 }
78 
80 {
81  Ptr<Packet> receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
82 
83  m_count ++;
84 }
85 
86 void
88 {
89  Address from;
90  Ptr<Packet> receivedPacket = socket->RecvFrom (std::numeric_limits<uint32_t>::max (), 0, from);
91 
94 
95  receivedPacket->RemoveAllPacketTags ();
96  receivedPacket->RemoveAllByteTags ();
97 
98  socket->SendTo (receivedPacket, 0, to);
99 }
100 
101 void
103 {
105  socket->SendTo (Create<Packet> (123), 0, realTo);
106 
107  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (1.0),
108  &LoopbackTestCase::SendData, this, socket);
109 }
110 
111 void
113 {
115  nodes.Create (1);
116  Ptr<MobilityModel> m = CreateObject<ConstantPositionMobilityModel> ();
117  m->SetPosition (Vector (0, 0, 0));
118  nodes.Get (0)->AggregateObject (m);
119  // Setup WiFi
120  WifiMacHelper wifiMac;
121  wifiMac.SetType ("ns3::AdhocWifiMac");
124  wifiPhy.SetChannel (wifiChannel.Create ());
126  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", StringValue ("2200"));
127  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
128 
129  // Setup TCP/IP & AODV
130  AodvHelper aodv; // Use default parameters here
131  InternetStackHelper internetStack;
132  internetStack.SetRoutingHelper (aodv);
133  internetStack.Install (nodes);
135  address.SetBase ("10.1.1.0", "255.255.255.0");
136  Ipv4InterfaceContainer interfaces = address.Assign (devices);
137 
138  // Setup echos
139  Ptr<SocketFactory> socketFactory = nodes.Get (0)->GetObject<UdpSocketFactory> ();
140  m_rxSocket = socketFactory->CreateSocket ();
142  m_rxSocket->SetRecvCallback (MakeCallback (&LoopbackTestCase::ReceivePkt, this));
143 
144  m_echoSocket = socketFactory->CreateSocket ();
147 
148  m_txSocket = socketFactory->CreateSocket ();
149 
152 
153  // Run
154  Simulator::Stop (Seconds (5));
155  Simulator::Run ();
156 
157  m_txSocket->Close ();
158  m_echoSocket->Close ();
159  m_rxSocket->Close ();
160 
162 
163  // Check that 4 packets delivered
164  NS_TEST_ASSERT_MSG_EQ (m_count, 4, "Exactly 4 echo replies must be delivered.");
165 }
166 
167 //-----------------------------------------------------------------------------
168 // Test suite
169 //-----------------------------------------------------------------------------
171 {
172 public:
173  AodvLoopbackTestSuite () : TestSuite ("routing-aodv-loopback", SYSTEM)
174  {
175  SetDataDir (NS_TEST_SOURCEDIR);
176  // UDP Echo loopback test case
178  }
180 
181 
182 }
183 }
an Inet address class
Ipv4Address GetIpv4(void) const
tuple devices
Definition: first.py:32
holds a vector of std::pair of Ptr and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:683
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
void DoRun()
Implementation to actually run this TestCase.
Definition: loopback.cc:112
Fast test.
Definition: test.h:1152
A suite of tests to run.
Definition: test.h:1333
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
aggregate IP/TCP/UDP functionality to existing Nodes.
AODV loopback UDP echo test case.
Definition: loopback.cc:54
Ptr< Socket > m_echoSocket
Definition: loopback.cc:58
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
encapsulates test code
Definition: test.h:1147
helps to create WifiNetDevice objects
Definition: wifi-helper.h:231
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:712
a polymophic address class
Definition: address.h:90
tuple nodes
Definition: first.py:25
void SetChannel(Ptr< YansWifiChannel > channel)
Ptr< Socket > m_txSocket
Definition: loopback.cc:57
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:852
#define max(a, b)
Definition: 80211b.c:45
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
void SendData(Ptr< Socket > socket)
Definition: loopback.cc:102
void ReceivePkt(Ptr< Socket > socket)
Definition: loopback.cc:79
#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:161
tuple interfaces
Definition: first.py:41
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
uint32_t m_count
number of packet received;
Definition: loopback.cc:56
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
manage and create wifi channel objects for the yans model.
create MAC layers for a ns3::WifiNetDevice.
static Ipv4Address GetLoopback(void)
void SetPosition(const Vector &position)
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
This test suite implements a System Test.
Definition: test.h:1344
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1319
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
uint32_t GetId(void) const
Definition: node.cc:107
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:209
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void EchoData(Ptr< Socket > socket)
Definition: loopback.cc:87
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
Definition: test.cc:464
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:349
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
tuple wifi
Definition: third.py:89
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Helper class that adds AODV routing to nodes.
Definition: aodv-helper.h:35
ns3::aodv::AodvLoopbackTestSuite g_aodvLoopbackTestSuite
tuple address
Definition: first.py:37
API to create UDP socket instances.
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 Close(void)=0
Close a socket.
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ptr< Socket > m_rxSocket
Definition: loopback.cc:59