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/core-module.h"
33 #include "ns3/dsdv-helper.h"
34 #include "ns3/internet-module.h"
35 #include "ns3/mobility-module.h"
36 #include "ns3/network-module.h"
37 #include "ns3/yans-wifi-helper.h"
38 
39 #include <cmath>
40 #include <iostream>
41 
42 using namespace ns3;
43 
44 uint16_t port = 9;
45 
46 NS_LOG_COMPONENT_DEFINE("DsdvManetExample");
47 
56 {
57  public:
73  void CaseRun(uint32_t nWifis,
74  uint32_t nSinks,
75  double totalTime,
76  std::string rate,
77  std::string phyMode,
78  uint32_t nodeSpeed,
79  uint32_t periodicUpdateInterval,
80  uint32_t settlingTime,
81  double dataStart,
82  bool printRoutes,
83  std::string CSVfileName);
84 
85  private:
86  uint32_t m_nWifis;
87  uint32_t m_nSinks;
88  double m_totalTime;
89  std::string m_rate;
90  std::string m_phyMode;
91  uint32_t m_nodeSpeed;
93  uint32_t m_settlingTime;
94  double m_dataStart;
95  uint32_t bytesTotal;
96  uint32_t packetsReceived;
98  std::string m_CSVfileName;
99 
103 
104  private:
106  void CreateNodes();
111  void CreateDevices(std::string tr_name);
116  void InstallInternetStack(std::string tr_name);
118  void InstallApplications();
120  void SetupMobility();
125  void ReceivePacket(Ptr<Socket> socket);
134  void CheckThroughput();
135 };
136 
137 int
138 main(int argc, char** argv)
139 {
140  DsdvManetExample test;
141  uint32_t nWifis = 30;
142  uint32_t nSinks = 10;
143  double totalTime = 100.0;
144  std::string rate("8kbps");
145  std::string phyMode("DsssRate11Mbps");
146  uint32_t nodeSpeed = 10; // in m/s
147  std::string appl = "all";
148  uint32_t periodicUpdateInterval = 15;
149  uint32_t settlingTime = 6;
150  double dataStart = 50.0;
151  bool printRoutingTable = true;
152  std::string CSVfileName = "DsdvManetExample.csv";
153 
154  CommandLine cmd(__FILE__);
155  cmd.AddValue("nWifis", "Number of wifi nodes[Default:30]", nWifis);
156  cmd.AddValue("nSinks", "Number of wifi sink nodes[Default:10]", nSinks);
157  cmd.AddValue("totalTime", "Total Simulation time[Default:100]", totalTime);
158  cmd.AddValue("phyMode", "Wifi Phy mode[Default:DsssRate11Mbps]", phyMode);
159  cmd.AddValue("rate", "CBR traffic rate[Default:8kbps]", rate);
160  cmd.AddValue("nodeSpeed", "Node speed in RandomWayPoint model[Default:10]", nodeSpeed);
161  cmd.AddValue("periodicUpdateInterval",
162  "Periodic Interval Time[Default=15]",
163  periodicUpdateInterval);
164  cmd.AddValue("settlingTime",
165  "Settling Time before sending out an update for changed metric[Default=6]",
166  settlingTime);
167  cmd.AddValue("dataStart",
168  "Time at which nodes start to transmit data[Default=50.0]",
169  dataStart);
170  cmd.AddValue("printRoutingTable",
171  "print routing table for nodes[Default:1]",
172  printRoutingTable);
173  cmd.AddValue("CSVfileName",
174  "The name of the CSV output file name[Default:DsdvManetExample.csv]",
175  CSVfileName);
176  cmd.Parse(argc, argv);
177 
178  std::ofstream out(CSVfileName);
179  out << "SimulationSecond,"
180  << "ReceiveRate,"
181  << "PacketsReceived,"
182  << "NumberOfSinks," << std::endl;
183  out.close();
184 
185  SeedManager::SetSeed(12345);
186 
187  Config::SetDefault("ns3::OnOffApplication::PacketSize", StringValue("1000"));
188  Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue(rate));
189  Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
190  Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("2000"));
191 
192  test = DsdvManetExample();
193  test.CaseRun(nWifis,
194  nSinks,
195  totalTime,
196  rate,
197  phyMode,
198  nodeSpeed,
199  periodicUpdateInterval,
200  settlingTime,
201  dataStart,
202  printRoutingTable,
203  CSVfileName);
204 
205  return 0;
206 }
207 
209  : bytesTotal(0),
210  packetsReceived(0)
211 {
212 }
213 
214 void
216 {
217  NS_LOG_UNCOND(Simulator::Now().As(Time::S) << " Received one packet!");
218  Ptr<Packet> packet;
219  while ((packet = socket->Recv()))
220  {
221  bytesTotal += packet->GetSize();
222  packetsReceived += 1;
223  }
224 }
225 
226 void
228 {
229  double kbs = (bytesTotal * 8.0) / 1000;
230  bytesTotal = 0;
231 
232  std::ofstream out(m_CSVfileName, std::ios::app);
233 
234  out << (Simulator::Now()).GetSeconds() << "," << kbs << "," << packetsReceived << ","
235  << m_nSinks << std::endl;
236 
237  out.close();
238  packetsReceived = 0;
239  Simulator::Schedule(Seconds(1.0), &DsdvManetExample::CheckThroughput, this);
240 }
241 
244 {
245  TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
246  Ptr<Socket> sink = Socket::CreateSocket(node, tid);
248  sink->Bind(local);
249  sink->SetRecvCallback(MakeCallback(&DsdvManetExample::ReceivePacket, this));
250 
251  return sink;
252 }
253 
254 void
256  uint32_t nSinks,
257  double totalTime,
258  std::string rate,
259  std::string phyMode,
260  uint32_t nodeSpeed,
261  uint32_t periodicUpdateInterval,
262  uint32_t settlingTime,
263  double dataStart,
264  bool printRoutes,
265  std::string CSVfileName)
266 {
267  m_nWifis = nWifis;
268  m_nSinks = nSinks;
269  m_totalTime = totalTime;
270  m_rate = rate;
271  m_phyMode = phyMode;
272  m_nodeSpeed = nodeSpeed;
273  m_periodicUpdateInterval = periodicUpdateInterval;
274  m_settlingTime = settlingTime;
275  m_dataStart = dataStart;
276  m_printRoutes = printRoutes;
277  m_CSVfileName = CSVfileName;
278 
279  std::stringstream ss;
280  ss << m_nWifis;
281  std::string t_nodes = ss.str();
282 
283  std::stringstream ss3;
284  ss3 << m_totalTime;
285  std::string sTotalTime = ss3.str();
286 
287  std::string tr_name = "Dsdv_Manet_" + t_nodes + "Nodes_" + sTotalTime + "SimTime";
288  std::cout << "Trace file generated is " << tr_name << ".tr\n";
289 
290  CreateNodes();
291  CreateDevices(tr_name);
292  SetupMobility();
293  InstallInternetStack(tr_name);
295 
296  std::cout << "\nStarting simulation for " << m_totalTime << " s ...\n";
297 
298  CheckThroughput();
299 
300  Simulator::Stop(Seconds(m_totalTime));
301  Simulator::Run();
302  Simulator::Destroy();
303 }
304 
305 void
307 {
308  std::cout << "Creating " << (unsigned)m_nWifis << " nodes.\n";
311  "Sinks must be less or equal to the number of nodes in network");
312 }
313 
314 void
316 {
318  ObjectFactory pos;
319  pos.SetTypeId("ns3::RandomRectanglePositionAllocator");
320  pos.Set("X", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
321  pos.Set("Y", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
322 
323  std::ostringstream speedConstantRandomVariableStream;
324  speedConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant=" << m_nodeSpeed
325  << "]";
326 
327  Ptr<PositionAllocator> taPositionAlloc = pos.Create()->GetObject<PositionAllocator>();
328  mobility.SetMobilityModel("ns3::RandomWaypointMobilityModel",
329  "Speed",
330  StringValue(speedConstantRandomVariableStream.str()),
331  "Pause",
332  StringValue("ns3::ConstantRandomVariable[Constant=2.0]"),
333  "PositionAllocator",
334  PointerValue(taPositionAlloc));
335  mobility.SetPositionAllocator(taPositionAlloc);
336  mobility.Install(nodes);
337 }
338 
339 void
341 {
342  WifiMacHelper wifiMac;
343  wifiMac.SetType("ns3::AdhocWifiMac");
344  YansWifiPhyHelper wifiPhy;
345  YansWifiChannelHelper wifiChannel;
346  wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
347  wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");
348  wifiPhy.SetChannel(wifiChannel.Create());
350  wifi.SetStandard(WIFI_STANDARD_80211b);
351  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
352  "DataMode",
354  "ControlMode",
356  devices = wifi.Install(wifiPhy, wifiMac, nodes);
357 
358  AsciiTraceHelper ascii;
359  wifiPhy.EnableAsciiAll(ascii.CreateFileStream(tr_name + ".tr"));
360  wifiPhy.EnablePcapAll(tr_name);
361 }
362 
363 void
365 {
366  DsdvHelper dsdv;
367  dsdv.Set("PeriodicUpdateInterval", TimeValue(Seconds(m_periodicUpdateInterval)));
368  dsdv.Set("SettlingTime", TimeValue(Seconds(m_settlingTime)));
370  stack.SetRoutingHelper(dsdv); // has effect on the next Install ()
371  stack.Install(nodes);
373  address.SetBase("10.1.1.0", "255.255.255.0");
374  interfaces = address.Assign(devices);
375  if (m_printRoutes)
376  {
377  Ptr<OutputStreamWrapper> routingStream =
378  Create<OutputStreamWrapper>((tr_name + ".routes"), std::ios::out);
379  Ipv4RoutingHelper::PrintRoutingTableAllAt(Seconds(m_periodicUpdateInterval), routingStream);
380  }
381 }
382 
383 void
385 {
386  for (uint32_t i = 0; i <= m_nSinks - 1; i++)
387  {
388  Ptr<Node> node = NodeList::GetNode(i);
389  Ipv4Address nodeAddress = node->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal();
390  Ptr<Socket> sink = SetupPacketReceive(nodeAddress, node);
391  }
392 
393  for (uint32_t clientNode = 0; clientNode <= m_nWifis - 1; clientNode++)
394  {
395  for (uint32_t j = 0; j <= m_nSinks - 1; j++)
396  {
397  OnOffHelper onoff1("ns3::UdpSocketFactory",
399  onoff1.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1.0]"));
400  onoff1.SetAttribute("OffTime",
401  StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
402 
403  if (j != clientNode)
404  {
405  ApplicationContainer apps1 = onoff1.Install(nodes.Get(clientNode));
406  Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable>();
407  apps1.Start(Seconds(var->GetValue(m_dataStart, m_dataStart + 1)));
408  apps1.Stop(Seconds(m_totalTime));
409  }
410  }
411  }
412 }
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Create a socket and prepare it for packet reception.
DSDV Manet example.
Definition: dsdv-manet.cc:56
uint32_t m_nSinks
number of receiver nodes
Definition: dsdv-manet.cc:87
void InstallApplications()
Create data sinks and sources.
Definition: dsdv-manet.cc:384
NodeContainer nodes
the collection of nodes
Definition: dsdv-manet.cc:100
void ReceivePacket(Ptr< Socket > socket)
Packet receive function.
Definition: dsdv-manet.cc:215
double m_dataStart
time to start data transmissions (seconds)
Definition: dsdv-manet.cc:94
std::string m_CSVfileName
CSV file name.
Definition: dsdv-manet.cc:98
uint32_t packetsReceived
total packets received by all nodes
Definition: dsdv-manet.cc:96
std::string m_rate
network bandwidth
Definition: dsdv-manet.cc:89
uint32_t bytesTotal
total bytes received by all nodes
Definition: dsdv-manet.cc:95
void CreateNodes()
Create and initialize all nodes.
Definition: dsdv-manet.cc:306
bool m_printRoutes
print routing table
Definition: dsdv-manet.cc:97
void InstallInternetStack(std::string tr_name)
Create network.
Definition: dsdv-manet.cc:364
uint32_t m_nodeSpeed
mobility speed
Definition: dsdv-manet.cc:91
void CreateDevices(std::string tr_name)
Create and initialize all devices.
Definition: dsdv-manet.cc:340
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:255
uint32_t m_settlingTime
routing setting time
Definition: dsdv-manet.cc:93
Ipv4InterfaceContainer interfaces
the collection of interfaces
Definition: dsdv-manet.cc:102
void CheckThroughput()
Check network throughput.
Definition: dsdv-manet.cc:227
void SetupMobility()
Setup mobility model.
Definition: dsdv-manet.cc:315
NetDeviceContainer devices
the collection of devices
Definition: dsdv-manet.cc:101
double m_totalTime
total simulation time (in seconds)
Definition: dsdv-manet.cc:88
std::string m_phyMode
remote station manager data mode
Definition: dsdv-manet.cc:90
Ptr< Socket > SetupPacketReceive(Ipv4Address addr, Ptr< Node > node)
Setup packet receivers.
Definition: dsdv-manet.cc:243
uint32_t m_periodicUpdateInterval
routing update interval
Definition: dsdv-manet.cc:92
uint32_t m_nWifis
total number of nodes
Definition: dsdv-manet.cc:86
a polymophic address class
Definition: address.h:101
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:174
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:42
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
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:861
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
a unique identifier for an interface.
Definition: type-id.h:59
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
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.
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:44
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:890
#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:305
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
@ WIFI_STANDARD_80211b
address
Definition: first.py:47
stack
Definition: first.py:44
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:704
cmd
Definition: second.py:40
wifi
Definition: third.py:95
mobility
Definition: third.py:105
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