A Discrete-Event Network Simulator
API
hwmp-simplest-regression.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 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: Kirill Andreev <andreev@iitp.ru>
19  */
20 
21 #include "ns3/mesh-helper.h"
22 #include "ns3/simulator.h"
23 #include "ns3/random-variable-stream.h"
24 #include "ns3/rng-seed-manager.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/yans-wifi-helper.h"
30 #include "ns3/internet-stack-helper.h"
31 #include "ns3/ipv4-address-helper.h"
32 #include "ns3/abort.h"
33 #include "ns3/mobility-model.h"
34 #include "ns3/pcap-test.h"
35 #include <sstream>
37 
38 using namespace ns3;
39 
41 const char * const PREFIX = "hwmp-simplest-regression-test";
42 
43 
45  m_nodes (0),
46  m_time (Seconds (15)),
47  m_sentPktsCounter (0)
48 {
49 }
50 
52 {
53  delete m_nodes;
54 }
55 void
57 {
58  RngSeedManager::SetSeed (12345);
59  RngSeedManager::SetRun (7);
60  CreateNodes ();
61  CreateDevices ();
63 
64  Simulator::Stop (m_time);
65  Simulator::Run ();
66  Simulator::Destroy ();
67 
68  CheckResults ();
69 
70  delete m_nodes, m_nodes = 0;
71 }
72 void
74 {
75  m_nodes = new NodeContainer;
76  m_nodes->Create (2);
78  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
79  "MinX", DoubleValue (0.0),
80  "MinY", DoubleValue (0.0),
81  "DeltaX", DoubleValue (1 /*meter*/),
82  "DeltaY", DoubleValue (0),
83  "GridWidth", UintegerValue (2),
84  "LayoutType", StringValue ("RowFirst"));
85  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
86  mobility.Install (*m_nodes);
87  Simulator::Schedule (Seconds (10.0), &HwmpSimplestRegressionTest::ResetPosition, this);
88 }
89 void
91 {
92  Ptr<Object> object = m_nodes->Get (1);
93  Ptr<MobilityModel> model = object->GetObject<MobilityModel> ();
94  if (model == 0)
95  {
96  return;
97  }
98  model->SetPosition (Vector (9000, 0, 0));
99 }
100 void
102 {
103  // client socket
104  m_clientSocket = Socket::CreateSocket (m_nodes->Get (1), TypeId::LookupByName ("ns3::UdpSocketFactory"));
105  m_clientSocket->Bind ();
108  Simulator::ScheduleWithContext (m_clientSocket->GetNode ()->GetId (), Seconds (2.0),
110 
111  // server socket
112  m_serverSocket = Socket::CreateSocket (m_nodes->Get (0), TypeId::LookupByName ("ns3::UdpSocketFactory"));
113  m_serverSocket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 9));
114  m_serverSocket->SetRecvCallback (MakeCallback (&HwmpSimplestRegressionTest::HandleReadServer, this));
115 }
116 void
118 {
119  int64_t streamsUsed = 0;
120  // 1. setup WiFi
121  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
122  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
123  Ptr<YansWifiChannel> chan = wifiChannel.Create ();
124  wifiPhy.SetChannel (chan);
125  // 2. setup mesh
126  MeshHelper mesh = MeshHelper::Default ();
127  mesh.SetStackInstaller ("ns3::Dot11sStack");
128  mesh.SetMacType ("RandomStart", TimeValue (Seconds (0.1)));
129  mesh.SetNumberOfInterfaces (1);
130  NetDeviceContainer meshDevices = mesh.Install (wifiPhy, *m_nodes);
131  // Two devices, nine streams per mesh device
132  streamsUsed += mesh.AssignStreams (meshDevices, streamsUsed);
133  NS_TEST_ASSERT_MSG_EQ (streamsUsed, (meshDevices.GetN () * 9), "Stream assignment mismatch");
134  streamsUsed += wifiChannel.AssignStreams (chan, streamsUsed);
135  NS_TEST_ASSERT_MSG_EQ (streamsUsed, (meshDevices.GetN () * 9), "Stream assignment mismatch");
136 
137  // 3. setup TCP/IP
138  InternetStackHelper internetStack;
139  internetStack.Install (*m_nodes);
140  streamsUsed += internetStack.AssignStreams (*m_nodes, streamsUsed);
142  address.SetBase ("10.1.1.0", "255.255.255.0");
143  m_interfaces = address.Assign (meshDevices);
144  // 4. write PCAP if needed
146 }
147 
148 void
150 {
151  for (int i = 0; i < 2; ++i)
152  {
153  NS_PCAP_TEST_EXPECT_EQ (PREFIX << "-" << i << "-1.pcap");
154  }
155 }
156 
157 void
159 {
160  if ((Simulator::Now () < m_time) && (m_sentPktsCounter < 300))
161  {
162  socket->Send (Create<Packet> (100));
164  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0.05),
165  &HwmpSimplestRegressionTest::SendData, this, socket);
166  }
167 }
168 
169 void
171 {
172  Ptr<Packet> packet;
173  Address from;
174  while ((packet = socket->RecvFrom (from)))
175  {
176  packet->RemoveAllPacketTags ();
177  packet->RemoveAllByteTags ();
178 
179  socket->SendTo (packet, 0, from);
180  }
181 }
182 
183 void
185 {
186  Ptr<Packet> packet;
187  Address from;
188  while ((packet = socket->RecvFrom (from)))
189  {
190  }
191 }
an Inet address class
const char *const PREFIX
Unique PCAP file name prefix.
Ptr< YansWifiChannel > Create(void) const
Ipv4InterfaceContainer m_interfaces
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.
aggregate IP/TCP/UDP functionality to existing Nodes.
Ptr< Socket > m_clientSocket
Client-side socket.
encapsulates test code
Definition: test.h:1147
#define NS_PCAP_TEST_EXPECT_EQ(filename)
Test that a pair of reference/new pcap files are equal.
Definition: pcap-test.h:38
void SetNumberOfInterfaces(uint32_t nInterfaces)
Set a number of interfaces in a mesh network.
Definition: mesh-helper.cc:77
a polymophic address class
Definition: address.h:90
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:852
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
AttributeValue implementation for Time.
Definition: nstime.h:957
void HandleReadClient(Ptr< Socket > socket)
Handle a packet reception.
Hold an unsigned integer type.
Definition: uinteger.h:44
#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
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
Ptr< Socket > m_serverSocket
Server-side socket.
void SetMacType(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: mesh-helper.cc:126
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
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.
keep track of a set of node pointers.
void SetMobilityModel(std::string type, 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())
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.
void SendData(Ptr< Socket > socket)
Send data.
int64_t AssignStreams(NetDeviceContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: mesh-helper.cc:215
void SetStackInstaller(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: mesh-helper.cc:49
void SetPosition(const Vector &position)
virtual void DoRun()
Implementation to actually run this TestCase.
Helper class used to assign positions and mobility models to nodes.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
int64_t AssignStreams(NodeContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
NetDeviceContainer Install(const WifiPhyHelper &phyHelper, NodeContainer c) const
Install 802.11s mesh device & protocols on given node list.
Definition: mesh-helper.cc:82
uint32_t GetId(void) const
Definition: node.cc:107
void HandleReadServer(Ptr< Socket > socket)
Handle a packet reception.
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
Definition: test.cc:429
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
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.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:340
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Helper to create IEEE 802.11s mesh networks.
Definition: mesh-helper.h:38
tuple address
Definition: first.py:37
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 Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
int64_t AssignStreams(Ptr< YansWifiChannel > c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by the channel.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const