A Discrete-Event Network Simulator
API
dsdv-manet.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Hemanth Narra
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  * Author: Hemanth Narra <hemanth@ittc.ku.com>
19  *
20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * Information and Telecommunication Technology Center (ITTC)
23  * and Department of Electrical Engineering and Computer Science
24  * The University of Kansas Lawrence, KS USA.
25  *
26  * Work supported in part by NSF FIND (Future Internet Design) Program
27  * under grant CNS-0626918 (Postmodern Internet Architecture),
28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
30  */
31 
32 #include <iostream>
33 #include <cmath>
34 #include "ns3/core-module.h"
35 #include "ns3/network-module.h"
36 #include "ns3/applications-module.h"
37 #include "ns3/mobility-module.h"
38 #include "ns3/config-store-module.h"
39 #include "ns3/internet-module.h"
40 #include "ns3/dsdv-helper.h"
41 #include "ns3/yans-wifi-helper.h"
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 
139 int 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 
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", "Periodic Interval Time[Default=15]", periodicUpdateInterval);
163  cmd.AddValue ("settlingTime", "Settling Time before sending out an update for changed metric[Default=6]", settlingTime);
164  cmd.AddValue ("dataStart", "Time at which nodes start to transmit data[Default=50.0]", dataStart);
165  cmd.AddValue ("printRoutingTable", "print routing table for nodes[Default:1]", printRoutingTable);
166  cmd.AddValue ("CSVfileName", "The name of the CSV output file name[Default:DsdvManetExample.csv]", CSVfileName);
167  cmd.Parse (argc, argv);
168 
169  std::ofstream out (CSVfileName.c_str ());
170  out << "SimulationSecond," <<
171  "ReceiveRate," <<
172  "PacketsReceived," <<
173  "NumberOfSinks," <<
174  std::endl;
175  out.close ();
176 
177  SeedManager::SetSeed (12345);
178 
179  Config::SetDefault ("ns3::OnOffApplication::PacketSize", StringValue ("1000"));
180  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (rate));
181  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode));
182  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2000"));
183 
184  test = DsdvManetExample ();
185  test.CaseRun (nWifis, nSinks, totalTime, rate, phyMode, nodeSpeed, periodicUpdateInterval,
186  settlingTime, dataStart, printRoutingTable, CSVfileName);
187 
188  return 0;
189 }
190 
192  : bytesTotal (0),
193  packetsReceived (0)
194 {
195 }
196 
197 void
199 {
200  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " Received one packet!");
201  Ptr <Packet> packet;
202  while ((packet = socket->Recv ()))
203  {
204  bytesTotal += packet->GetSize ();
205  packetsReceived += 1;
206  }
207 }
208 
209 void
211 {
212  double kbs = (bytesTotal * 8.0) / 1000;
213  bytesTotal = 0;
214 
215  std::ofstream out (m_CSVfileName.c_str (), std::ios::app);
216 
217  out << (Simulator::Now ()).GetSeconds () << "," << kbs << "," << packetsReceived << "," << m_nSinks << std::endl;
218 
219  out.close ();
220  packetsReceived = 0;
221  Simulator::Schedule (Seconds (1.0), &DsdvManetExample::CheckThroughput, this);
222 }
223 
226 {
227 
228  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
229  Ptr <Socket> sink = Socket::CreateSocket (node, tid);
230  InetSocketAddress local = InetSocketAddress (addr, port);
231  sink->Bind (local);
232  sink->SetRecvCallback (MakeCallback ( &DsdvManetExample::ReceivePacket, this));
233 
234  return sink;
235 }
236 
237 void
238 DsdvManetExample::CaseRun (uint32_t nWifis, uint32_t nSinks, double totalTime, std::string rate,
239  std::string phyMode, uint32_t nodeSpeed, uint32_t periodicUpdateInterval, uint32_t settlingTime,
240  double dataStart, bool printRoutes, std::string CSVfileName)
241 {
242  m_nWifis = nWifis;
243  m_nSinks = nSinks;
244  m_totalTime = totalTime;
245  m_rate = rate;
246  m_phyMode = phyMode;
247  m_nodeSpeed = nodeSpeed;
248  m_periodicUpdateInterval = periodicUpdateInterval;
249  m_settlingTime = settlingTime;
250  m_dataStart = dataStart;
251  m_printRoutes = printRoutes;
252  m_CSVfileName = CSVfileName;
253 
254  std::stringstream ss;
255  ss << m_nWifis;
256  std::string t_nodes = ss.str ();
257 
258  std::stringstream ss3;
259  ss3 << m_totalTime;
260  std::string sTotalTime = ss3.str ();
261 
262  std::string tr_name = "Dsdv_Manet_" + t_nodes + "Nodes_" + sTotalTime + "SimTime";
263  std::cout << "Trace file generated is " << tr_name << ".tr\n";
264 
265  CreateNodes ();
266  CreateDevices (tr_name);
267  SetupMobility ();
268  InstallInternetStack (tr_name);
270 
271  std::cout << "\nStarting simulation for " << m_totalTime << " s ...\n";
272 
273  CheckThroughput ();
274 
275  Simulator::Stop (Seconds (m_totalTime));
276  Simulator::Run ();
277  Simulator::Destroy ();
278 }
279 
280 void
282 {
283  std::cout << "Creating " << (unsigned) m_nWifis << " nodes.\n";
285  NS_ASSERT_MSG (m_nWifis > m_nSinks, "Sinks must be less or equal to the number of nodes in network");
286 }
287 
288 void
290 {
292  ObjectFactory pos;
293  pos.SetTypeId ("ns3::RandomRectanglePositionAllocator");
294  pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
295  pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1000.0]"));
296 
297  std::ostringstream speedConstantRandomVariableStream;
298  speedConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant="
299  << m_nodeSpeed
300  << "]";
301 
302  Ptr <PositionAllocator> taPositionAlloc = pos.Create ()->GetObject <PositionAllocator> ();
303  mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", "Speed", StringValue (speedConstantRandomVariableStream.str ()),
304  "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=2.0]"), "PositionAllocator", PointerValue (taPositionAlloc));
305  mobility.SetPositionAllocator (taPositionAlloc);
306  mobility.Install (nodes);
307 }
308 
309 void
310 DsdvManetExample::CreateDevices (std::string tr_name)
311 {
312  WifiMacHelper wifiMac;
313  wifiMac.SetType ("ns3::AdhocWifiMac");
314  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
315  YansWifiChannelHelper wifiChannel;
316  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
317  wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");
318  wifiPhy.SetChannel (wifiChannel.Create ());
320  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
321  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue (m_phyMode), "ControlMode",
323  devices = wifi.Install (wifiPhy, wifiMac, nodes);
324 
325  AsciiTraceHelper ascii;
326  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (tr_name + ".tr"));
327  wifiPhy.EnablePcapAll (tr_name);
328 }
329 
330 void
332 {
333  DsdvHelper dsdv;
334  dsdv.Set ("PeriodicUpdateInterval", TimeValue (Seconds (m_periodicUpdateInterval)));
335  dsdv.Set ("SettlingTime", TimeValue (Seconds (m_settlingTime)));
337  stack.SetRoutingHelper (dsdv); // has effect on the next Install ()
338  stack.Install (nodes);
340  address.SetBase ("10.1.1.0", "255.255.255.0");
341  interfaces = address.Assign (devices);
342  if (m_printRoutes)
343  {
344  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ((tr_name + ".routes"), std::ios::out);
346  }
347 }
348 
349 void
351 {
352  for (uint32_t i = 0; i <= m_nSinks - 1; i++ )
353  {
354  Ptr<Node> node = NodeList::GetNode (i);
355  Ipv4Address nodeAddress = node->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();
356  Ptr<Socket> sink = SetupPacketReceive (nodeAddress, node);
357  }
358 
359  for (uint32_t clientNode = 0; clientNode <= m_nWifis - 1; clientNode++ )
360  {
361  for (uint32_t j = 0; j <= m_nSinks - 1; j++ )
362  {
363  OnOffHelper onoff1 ("ns3::UdpSocketFactory", Address (InetSocketAddress (interfaces.GetAddress (j), port)));
364  onoff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
365  onoff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
366 
367  if (j != clientNode)
368  {
369  ApplicationContainer apps1 = onoff1.Install (nodes.Get (clientNode));
370  Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable> ();
371  apps1.Start (Seconds (var->GetValue (m_dataStart, m_dataStart + 1)));
372  apps1.Stop (Seconds (m_totalTime));
373  }
374  }
375  }
376 }
377 
void AddPropagationLoss(std::string name, 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())
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
Ptr< Socket > SetupPacketReceive(Ipv4Address addr, Ptr< Node > node)
Setup packet receivers.
Definition: dsdv-manet.cc:225
holds a vector of ns3::Application pointers.
Ipv4InterfaceContainer interfaces
the collection of interfaces
Definition: dsdv-manet.cc:103
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
DSDV Manet example.
Definition: dsdv-manet.cc:56
holds a vector of std::pair of Ptr<Ipv4> and interface index.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
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:238
uint32_t packetsReceived
total packets received by all nodes
Definition: dsdv-manet.cc:97
void ReceivePacket(Ptr< Socket > socket)
void SetupMobility()
Setup mobility model.
Definition: dsdv-manet.cc:289
std::string m_CSVfileName
CSV file name.
Definition: dsdv-manet.cc:99
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
double m_totalTime
total simulation time (in seconds)
Definition: dsdv-manet.cc:89
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
aggregate IP/TCP/UDP functionality to existing Nodes.
cmd
Definition: second.py:35
void InstallApplications()
Create data sinks and sources.
Definition: dsdv-manet.cc:350
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
helps to create WifiNetDevice objects
Definition: wifi-helper.h:230
double m_dataStart
time to start data transmissions (seconds)
Definition: dsdv-manet.cc:95
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
stack
Definition: first.py:34
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
Ptr< YansWifiChannel > Create(void) const
mobility
Definition: third.py:101
void CreateDevices(std::string tr_name)
Create and initialize all devices.
Definition: dsdv-manet.cc:310
void ReceivePacket(Ptr< Socket > socket)
Packet receive function.
Definition: dsdv-manet.cc:198
void SetChannel(Ptr< YansWifiChannel > channel)
uint32_t m_nodeSpeed
mobility speed
Definition: dsdv-manet.cc:92
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:1076
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
NodeContainer nodes
the collection of nodes
Definition: dsdv-manet.cc:101
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
std::string m_rate
network bandwidth
Definition: dsdv-manet.cc:90
uint32_t m_settlingTime
routing setting time
Definition: dsdv-manet.cc:94
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
NetDeviceContainer devices
the collection of devices
Definition: dsdv-manet.cc:102
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:213
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
uint32_t m_nSinks
number of receiver nodes
Definition: dsdv-manet.cc:88
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
address
Definition: first.py:37
uint32_t m_nWifis
total number of nodes
Definition: dsdv-manet.cc:87
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
bool m_printRoutes
print routing table
Definition: dsdv-manet.cc:98
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
uint32_t bytesTotal
total bytes received by all nodes
Definition: dsdv-manet.cc:96
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
manage and create wifi channel objects for the yans model.
void InstallInternetStack(std::string tr_name)
Create network.
Definition: dsdv-manet.cc:331
create MAC layers for a ns3::WifiNetDevice.
std::string m_phyMode
remote station manager data mode
Definition: dsdv-manet.cc:91
void CreateNodes()
Create and initialize all nodes.
Definition: dsdv-manet.cc:281
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())
static void SetSeed(uint32_t seed)
Set the seed.
wifi
Definition: third.py:89
Helper class used to assign positions and mobility models to nodes.
Instantiate subclasses of ns3::Object.
void Set(std::string name, const AttributeValue &value)
Definition: dsdv-helper.cc:62
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
uint32_t m_periodicUpdateInterval
routing update interval
Definition: dsdv-manet.cc:93
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
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:365
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...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPropagationDelay(std::string name, 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())
Helper class that adds DSDV routing to nodes.
Definition: dsdv-helper.h:45
a unique identifier for an interface.
Definition: type-id.h:58
static void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of all nodes at a particular time.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
Allocate a set of positions.
void CheckThroughput()
Check network throughput.
Definition: dsdv-manet.cc:210