A Discrete-Event Network Simulator
API
dsdv-manet.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Hemanth Narra
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  * Author: Hemanth Narra <hemanth@ittc.ku.com>
18  *
19  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20  * ResiliNets Research Group https://resilinets.org/
21  * Information and Telecommunication Technology Center (ITTC)
22  * and Department of Electrical Engineering and Computer Science
23  * The University of Kansas Lawrence, KS USA.
24  *
25  * Work supported in part by NSF FIND (Future Internet Design) Program
26  * under grant CNS-0626918 (Postmodern Internet Architecture),
27  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28  * US Department of Defense (DoD), and ITTC at The University of Kansas.
29  */
30 
31 #include "ns3/applications-module.h"
32 #include "ns3/config-store-module.h"
33 #include "ns3/core-module.h"
34 #include "ns3/dsdv-helper.h"
35 #include "ns3/internet-module.h"
36 #include "ns3/mobility-module.h"
37 #include "ns3/network-module.h"
38 #include "ns3/yans-wifi-helper.h"
39 
40 #include <cmath>
41 #include <iostream>
42 
43 using namespace ns3;
44 
45 uint16_t port = 9;
46 
47 NS_LOG_COMPONENT_DEFINE("DsdvManetExample");
48 
57 {
58  public:
74  void CaseRun(uint32_t nWifis,
75  uint32_t nSinks,
76  double totalTime,
77  std::string rate,
78  std::string phyMode,
79  uint32_t nodeSpeed,
80  uint32_t periodicUpdateInterval,
81  uint32_t settlingTime,
82  double dataStart,
83  bool printRoutes,
84  std::string CSVfileName);
85 
86  private:
87  uint32_t m_nWifis;
88  uint32_t m_nSinks;
89  double m_totalTime;
90  std::string m_rate;
91  std::string m_phyMode;
92  uint32_t m_nodeSpeed;
94  uint32_t m_settlingTime;
95  double m_dataStart;
96  uint32_t bytesTotal;
97  uint32_t packetsReceived;
99  std::string m_CSVfileName;
100 
104 
105  private:
107  void CreateNodes();
112  void CreateDevices(std::string tr_name);
117  void InstallInternetStack(std::string tr_name);
119  void InstallApplications();
121  void SetupMobility();
126  void ReceivePacket(Ptr<Socket> socket);
135  void CheckThroughput();
136 };
137 
138 int
139 main(int argc, char** argv)
140 {
141  DsdvManetExample test;
142  uint32_t nWifis = 30;
143  uint32_t nSinks = 10;
144  double totalTime = 100.0;
145  std::string rate("8kbps");
146  std::string phyMode("DsssRate11Mbps");
147  uint32_t nodeSpeed = 10; // in m/s
148  std::string appl = "all";
149  uint32_t periodicUpdateInterval = 15;
150  uint32_t settlingTime = 6;
151  double dataStart = 50.0;
152  bool printRoutingTable = true;
153  std::string CSVfileName = "DsdvManetExample.csv";
154 
155  CommandLine cmd(__FILE__);
156  cmd.AddValue("nWifis", "Number of wifi nodes[Default:30]", nWifis);
157  cmd.AddValue("nSinks", "Number of wifi sink nodes[Default:10]", nSinks);
158  cmd.AddValue("totalTime", "Total Simulation time[Default:100]", totalTime);
159  cmd.AddValue("phyMode", "Wifi Phy mode[Default:DsssRate11Mbps]", phyMode);
160  cmd.AddValue("rate", "CBR traffic rate[Default:8kbps]", rate);
161  cmd.AddValue("nodeSpeed", "Node speed in RandomWayPoint model[Default:10]", nodeSpeed);
162  cmd.AddValue("periodicUpdateInterval",
163  "Periodic Interval Time[Default=15]",
164  periodicUpdateInterval);
165  cmd.AddValue("settlingTime",
166  "Settling Time before sending out an update for changed metric[Default=6]",
167  settlingTime);
168  cmd.AddValue("dataStart",
169  "Time at which nodes start to transmit data[Default=50.0]",
170  dataStart);
171  cmd.AddValue("printRoutingTable",
172  "print routing table for nodes[Default:1]",
173  printRoutingTable);
174  cmd.AddValue("CSVfileName",
175  "The name of the CSV output file name[Default:DsdvManetExample.csv]",
176  CSVfileName);
177  cmd.Parse(argc, argv);
178 
179  std::ofstream out(CSVfileName);
180  out << "SimulationSecond,"
181  << "ReceiveRate,"
182  << "PacketsReceived,"
183  << "NumberOfSinks," << std::endl;
184  out.close();
185 
186  SeedManager::SetSeed(12345);
187 
188  Config::SetDefault("ns3::OnOffApplication::PacketSize", StringValue("1000"));
189  Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue(rate));
190  Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
191  Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("2000"));
192 
193  test = DsdvManetExample();
194  test.CaseRun(nWifis,
195  nSinks,
196  totalTime,
197  rate,
198  phyMode,
199  nodeSpeed,
200  periodicUpdateInterval,
201  settlingTime,
202  dataStart,
203  printRoutingTable,
204  CSVfileName);
205 
206  return 0;
207 }
208 
210  : bytesTotal(0),
211  packetsReceived(0)
212 {
213 }
214 
215 void
217 {
218  NS_LOG_UNCOND(Simulator::Now().As(Time::S) << " Received one packet!");
219  Ptr<Packet> packet;
220  while ((packet = socket->Recv()))
221  {
222  bytesTotal += packet->GetSize();
223  packetsReceived += 1;
224  }
225 }
226 
227 void
229 {
230  double kbs = (bytesTotal * 8.0) / 1000;
231  bytesTotal = 0;
232 
233  std::ofstream out(m_CSVfileName, std::ios::app);
234 
235  out << (Simulator::Now()).GetSeconds() << "," << kbs << "," << packetsReceived << ","
236  << m_nSinks << std::endl;
237 
238  out.close();
239  packetsReceived = 0;
240  Simulator::Schedule(Seconds(1.0), &DsdvManetExample::CheckThroughput, this);
241 }
242 
245 {
246  TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
247  Ptr<Socket> sink = Socket::CreateSocket(node, tid);
249  sink->Bind(local);
250  sink->SetRecvCallback(MakeCallback(&DsdvManetExample::ReceivePacket, this));
251 
252  return sink;
253 }
254 
255 void
257  uint32_t nSinks,
258  double totalTime,
259  std::string rate,
260  std::string phyMode,
261  uint32_t nodeSpeed,
262  uint32_t periodicUpdateInterval,
263  uint32_t settlingTime,
264  double dataStart,
265  bool printRoutes,
266  std::string CSVfileName)
267 {
268  m_nWifis = nWifis;
269  m_nSinks = nSinks;
270  m_totalTime = totalTime;
271  m_rate = rate;
272  m_phyMode = phyMode;
273  m_nodeSpeed = nodeSpeed;
274  m_periodicUpdateInterval = periodicUpdateInterval;
275  m_settlingTime = settlingTime;
276  m_dataStart = dataStart;
277  m_printRoutes = printRoutes;
278  m_CSVfileName = CSVfileName;
279 
280  std::stringstream ss;
281  ss << m_nWifis;
282  std::string t_nodes = ss.str();
283 
284  std::stringstream ss3;
285  ss3 << m_totalTime;
286  std::string sTotalTime = ss3.str();
287 
288  std::string tr_name = "Dsdv_Manet_" + t_nodes + "Nodes_" + sTotalTime + "SimTime";
289  std::cout << "Trace file generated is " << tr_name << ".tr\n";
290 
291  CreateNodes();
292  CreateDevices(tr_name);
293  SetupMobility();
294  InstallInternetStack(tr_name);
296 
297  std::cout << "\nStarting simulation for " << m_totalTime << " s ...\n";
298 
299  CheckThroughput();
300 
301  Simulator::Stop(Seconds(m_totalTime));
302  Simulator::Run();
303  Simulator::Destroy();
304 }
305 
306 void
308 {
309  std::cout << "Creating " << (unsigned)m_nWifis << " nodes.\n";
312  "Sinks must be less or equal to the number of nodes in network");
313 }
314 
315 void
317 {
319  ObjectFactory pos;
320  pos.SetTypeId("ns3::RandomRectanglePositionAllocator");
321  pos.Set("X", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
322  pos.Set("Y", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
323 
324  std::ostringstream speedConstantRandomVariableStream;
325  speedConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" << m_nodeSpeed
326  << "]";
327 
328  Ptr<PositionAllocator> taPositionAlloc = pos.Create()->GetObject<PositionAllocator>();
329  mobility.SetMobilityModel("ns3::RandomWaypointMobilityModel",
330  "Speed",
331  StringValue(speedConstantRandomVariableStream.str()),
332  "Pause",
333  StringValue("ns3::ConstantRandomVariable[Constant=2.0]"),
334  "PositionAllocator",
335  PointerValue(taPositionAlloc));
336  mobility.SetPositionAllocator(taPositionAlloc);
337  mobility.Install(nodes);
338 }
339 
340 void
342 {
343  WifiMacHelper wifiMac;
344  wifiMac.SetType("ns3::AdhocWifiMac");
345  YansWifiPhyHelper wifiPhy;
346  YansWifiChannelHelper wifiChannel;
347  wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
348  wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");
349  wifiPhy.SetChannel(wifiChannel.Create());
351  wifi.SetStandard(WIFI_STANDARD_80211b);
352  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
353  "DataMode",
355  "ControlMode",
357  devices = wifi.Install(wifiPhy, wifiMac, nodes);
358 
359  AsciiTraceHelper ascii;
360  wifiPhy.EnableAsciiAll(ascii.CreateFileStream(tr_name + ".tr"));
361  wifiPhy.EnablePcapAll(tr_name);
362 }
363 
364 void
366 {
367  DsdvHelper dsdv;
368  dsdv.Set("PeriodicUpdateInterval", TimeValue(Seconds(m_periodicUpdateInterval)));
369  dsdv.Set("SettlingTime", TimeValue(Seconds(m_settlingTime)));
371  stack.SetRoutingHelper(dsdv); // has effect on the next Install ()
372  stack.Install(nodes);
374  address.SetBase("10.1.1.0", "255.255.255.0");
375  interfaces = address.Assign(devices);
376  if (m_printRoutes)
377  {
378  Ptr<OutputStreamWrapper> routingStream =
379  Create<OutputStreamWrapper>((tr_name + ".routes"), std::ios::out);
380  Ipv4RoutingHelper::PrintRoutingTableAllAt(Seconds(m_periodicUpdateInterval), routingStream);
381  }
382 }
383 
384 void
386 {
387  for (uint32_t i = 0; i <= m_nSinks - 1; i++)
388  {
389  Ptr<Node> node = NodeList::GetNode(i);
390  Ipv4Address nodeAddress = node->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal();
391  Ptr<Socket> sink = SetupPacketReceive(nodeAddress, node);
392  }
393 
394  for (uint32_t clientNode = 0; clientNode <= m_nWifis - 1; clientNode++)
395  {
396  for (uint32_t j = 0; j <= m_nSinks - 1; j++)
397  {
398  OnOffHelper onoff1("ns3::UdpSocketFactory",
400  onoff1.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1.0]"));
401  onoff1.SetAttribute("OffTime",
402  StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
403 
404  if (j != clientNode)
405  {
406  ApplicationContainer apps1 = onoff1.Install(nodes.Get(clientNode));
407  Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable>();
408  apps1.Start(Seconds(var->GetValue(m_dataStart, m_dataStart + 1)));
409  apps1.Stop(Seconds(m_totalTime));
410  }
411  }
412  }
413 }
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Create a socket and prepare it for packet reception.
DSDV Manet example.
Definition: dsdv-manet.cc:57
uint32_t m_nSinks
number of receiver nodes
Definition: dsdv-manet.cc:88
void InstallApplications()
Create data sinks and sources.
Definition: dsdv-manet.cc:385
NodeContainer nodes
the collection of nodes
Definition: dsdv-manet.cc:101
void ReceivePacket(Ptr< Socket > socket)
Packet receive function.
Definition: dsdv-manet.cc:216
double m_dataStart
time to start data transmissions (seconds)
Definition: dsdv-manet.cc:95
std::string m_CSVfileName
CSV file name.
Definition: dsdv-manet.cc:99
uint32_t packetsReceived
total packets received by all nodes
Definition: dsdv-manet.cc:97
std::string m_rate
network bandwidth
Definition: dsdv-manet.cc:90
uint32_t bytesTotal
total bytes received by all nodes
Definition: dsdv-manet.cc:96
void CreateNodes()
Create and initialize all nodes.
Definition: dsdv-manet.cc:307
bool m_printRoutes
print routing table
Definition: dsdv-manet.cc:98
void InstallInternetStack(std::string tr_name)
Create network.
Definition: dsdv-manet.cc:365
uint32_t m_nodeSpeed
mobility speed
Definition: dsdv-manet.cc:92
void CreateDevices(std::string tr_name)
Create and initialize all devices.
Definition: dsdv-manet.cc:341
void CaseRun(uint32_t nWifis, uint32_t nSinks, double totalTime, std::string rate, std::string phyMode, uint32_t nodeSpeed, uint32_t periodicUpdateInterval, uint32_t settlingTime, double dataStart, bool printRoutes, std::string CSVfileName)
Run function.
Definition: dsdv-manet.cc:256
uint32_t m_settlingTime
routing setting time
Definition: dsdv-manet.cc:94
Ipv4InterfaceContainer interfaces
the collection of interfaces
Definition: dsdv-manet.cc:103
void CheckThroughput()
Check network throughput.
Definition: dsdv-manet.cc:228
void SetupMobility()
Setup mobility model.
Definition: dsdv-manet.cc:316
NetDeviceContainer devices
the collection of devices
Definition: dsdv-manet.cc:102
double m_totalTime
total simulation time (in seconds)
Definition: dsdv-manet.cc:89
std::string m_phyMode
remote station manager data mode
Definition: dsdv-manet.cc:91
Ptr< Socket > SetupPacketReceive(Ipv4Address addr, Ptr< Node > node)
Setup packet receivers.
Definition: dsdv-manet.cc:244
uint32_t m_periodicUpdateInterval
routing update interval
Definition: dsdv-manet.cc:93
uint32_t m_nWifis
total number of nodes
Definition: dsdv-manet.cc:87
a polymophic address class
Definition: address.h:100
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:173
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:232
Helper class that adds DSDV routing to nodes.
Definition: dsdv-helper.h:47
void Set(std::string name, const AttributeValue &value)
Definition: dsdv-helper.cc:65
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
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
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
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.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
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 ...
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Allocate a set of positions.
static void SetSeed(uint32_t seed)
Set the seed.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Hold variables of type string.
Definition: string.h:56
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:60
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
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.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
uint16_t port
Definition: dsdv-manet.cc:45
void ReceivePacket(Ptr< Socket > socket)
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
@ WIFI_STANDARD_80211b
address
Definition: first.py:40
stack
Definition: first.py:37
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
cmd
Definition: second.py:33
wifi
Definition: third.py:88
mobility
Definition: third.py:96
std::map< Mac48Address, uint64_t > packetsReceived
Map that stores the total packets received per STA (and addressed to that STA)
Definition: wifi-bianchi.cc:72
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55