A Discrete-Event Network Simulator
API
wsn-ping6.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Universita' di Firenze
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 // Network topology
22 //
23 // n0 n1
24 // | |
25 // =================
26 // WSN (802.15.4)
27 //
28 // - ICMPv6 echo request flows from n0 to n1 and back with ICMPv6 echo reply
29 // - DropTail queues
30 // - Tracing of queues and packet receptions to file "wsn-ping6.tr"
31 //
32 // This example is based on the "ping6.cc" example.
33 
34 #include <fstream>
35 #include "ns3/core-module.h"
36 #include "ns3/internet-module.h"
37 #include "ns3/sixlowpan-module.h"
38 #include "ns3/lr-wpan-module.h"
39 #include "ns3/internet-apps-module.h"
40 #include "ns3/mobility-module.h"
41 
42 using namespace ns3;
43 
44 NS_LOG_COMPONENT_DEFINE ("Ping6WsnExample");
45 
46 int main (int argc, char **argv)
47 {
48  bool verbose = false;
49 
50  CommandLine cmd (__FILE__);
51  cmd.AddValue ("verbose", "turn on log components", verbose);
52  cmd.Parse (argc, argv);
53 
54  if (verbose)
55  {
56  LogComponentEnable ("Ping6WsnExample", LOG_LEVEL_INFO);
57  LogComponentEnable ("Ipv6EndPointDemux", LOG_LEVEL_ALL);
58  LogComponentEnable ("Ipv6L3Protocol", LOG_LEVEL_ALL);
59  LogComponentEnable ("Ipv6StaticRouting", LOG_LEVEL_ALL);
60  LogComponentEnable ("Ipv6ListRouting", LOG_LEVEL_ALL);
61  LogComponentEnable ("Ipv6Interface", LOG_LEVEL_ALL);
62  LogComponentEnable ("Icmpv6L4Protocol", LOG_LEVEL_ALL);
63  LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL);
64  LogComponentEnable ("NdiscCache", LOG_LEVEL_ALL);
65  LogComponentEnable ("SixLowPanNetDevice", LOG_LEVEL_ALL);
66  }
67 
68  NS_LOG_INFO ("Create nodes.");
70  nodes.Create (2);
71 
72  // Set seed for random numbers
74 
75  // Install mobility
77  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
78 
79  Ptr<ListPositionAllocator> nodesPositionAlloc = CreateObject<ListPositionAllocator> ();
80  nodesPositionAlloc->Add (Vector (0.0, 0.0, 0.0));
81  nodesPositionAlloc->Add (Vector (50.0, 0.0, 0.0));
82  mobility.SetPositionAllocator (nodesPositionAlloc);
83  mobility.Install (nodes);
84 
85  NS_LOG_INFO ("Create channels.");
86  LrWpanHelper lrWpanHelper;
87  // Add and install the LrWpanNetDevice for each node
88  // lrWpanHelper.EnableLogComponents();
89  NetDeviceContainer devContainer = lrWpanHelper.Install(nodes);
90  lrWpanHelper.AssociateToPan (devContainer, 10);
91 
92  std::cout << "Created " << devContainer.GetN() << " devices" << std::endl;
93  std::cout << "There are " << nodes.GetN() << " nodes" << std::endl;
94 
95  /* Install IPv4/IPv6 stack */
96  NS_LOG_INFO ("Install Internet stack.");
97  InternetStackHelper internetv6;
98  internetv6.SetIpv4StackInstall (false);
99  internetv6.Install (nodes);
100 
101  // Install 6LowPan layer
102  NS_LOG_INFO ("Install 6LoWPAN.");
103  SixLowPanHelper sixlowpan;
104  NetDeviceContainer six1 = sixlowpan.Install (devContainer);
105 
106  NS_LOG_INFO ("Assign addresses.");
107  Ipv6AddressHelper ipv6;
108  ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64));
109  Ipv6InterfaceContainer i = ipv6.Assign (six1);
110 
111  NS_LOG_INFO ("Create Applications.");
112 
113  /* Create a Ping6 application to send ICMPv6 echo request from node zero to
114  * all-nodes (ff02::1).
115  */
116  uint32_t packetSize = 10;
117  uint32_t maxPacketCount = 5;
118  Time interPacketInterval = Seconds (1.);
119  Ping6Helper ping6;
120 
121  ping6.SetLocal (i.GetAddress (0, 1));
122  ping6.SetRemote (i.GetAddress (1, 1));
123  // ping6.SetRemote (Ipv6Address::GetAllNodesMulticast ());
124 
125  ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
126  ping6.SetAttribute ("Interval", TimeValue (interPacketInterval));
127  ping6.SetAttribute ("PacketSize", UintegerValue (packetSize));
128  ApplicationContainer apps = ping6.Install (nodes.Get (0));
129  apps.Start (Seconds (2.0));
130  apps.Stop (Seconds (10.0));
131 
132  AsciiTraceHelper ascii;
133  lrWpanHelper.EnableAsciiAll (ascii.CreateFileStream ("ping6wsn.tr"));
134  lrWpanHelper.EnablePcapAll (std::string ("ping6wsn"), true);
135 
136  NS_LOG_INFO ("Run Simulation.");
137  Simulator::Run ();
139  NS_LOG_INFO ("Done.");
140 }
141 
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::LrWpanHelper
helps to manage and create IEEE 802.15.4 NetDevice objects
Definition: lr-wpan-helper.h:51
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::AsciiTraceHelperForDevice::EnableAsciiAll
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...
Definition: trace-helper.cc:586
ns3::ListPositionAllocator::Add
void Add(Vector v)
Add a position to the list of positions.
Definition: position-allocator.cc:70
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::PcapHelperForDevice::EnablePcapAll
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 ...
Definition: trace-helper.cc:439
ns3::Ping6Helper::SetLocal
void SetLocal(Ipv6Address ip)
Set the local IPv6 address.
Definition: ping6-helper.cc:35
ns3::LOG_LEVEL_ALL
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
ns3::Ipv6AddressHelper::SetBase
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Definition: ipv6-address-helper.cc:69
ns3::Ipv6AddressHelper
Helper class to auto-assign global IPv6 unicast addresses.
Definition: ipv6-address-helper.h:83
ns3::Ping6Helper::Install
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
first.nodes
nodes
Definition: first.py:32
ns3::Ipv6AddressHelper::Assign
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Definition: ipv6-address-helper.cc:210
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::Ping6Helper::SetRemote
void SetRemote(Ipv6Address ip)
Set the remote IPv6 address.
Definition: ping6-helper.cc:40
ns3::LrWpanHelper::Install
NetDeviceContainer Install(NodeContainer c)
Install a LrWpanNetDevice and the associated structures (e.g., channel) in the nodes.
Definition: lr-wpan-helper.cc:176
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::AsciiTraceHelper::CreateFileStream
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.
Definition: trace-helper.cc:191
ns3::Ipv6InterfaceContainer::GetAddress
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Definition: ipv6-interface-container.cc:57
ns3::Ipv6InterfaceContainer
Keep track of a set of IPv6 interfaces.
Definition: ipv6-interface-container.h:42
ns3::SixLowPanHelper::Install
NetDeviceContainer Install(NetDeviceContainer c)
Install the SixLoWPAN stack on top of an existing NetDevice.
Definition: sixlowpan-helper.cc:47
ns3::Ping6Helper
Ping6 application helper.
Definition: ping6-helper.h:39
verbose
bool verbose
Definition: openflow-switch.cc:50
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
second.cmd
cmd
Definition: second.py:35
ns3::RngSeedManager::SetSeed
static void SetSeed(uint32_t seed)
Set the seed.
Definition: rng-seed-manager.cc:81
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
ns3::LrWpanHelper::AssociateToPan
void AssociateToPan(NetDeviceContainer c, uint16_t panId)
Associate the nodes to the same PAN.
Definition: lr-wpan-helper.cc:233
packetSize
static const uint32_t packetSize
Definition: wifi-power-adaptation-distance.cc:113
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ping6Helper::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::SixLowPanHelper
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
Definition: sixlowpan-helper.h:40
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::InternetStackHelper::SetIpv4StackInstall
void SetIpv4StackInstall(bool enable)
Enable/disable IPv4 stack install.
Definition: internet-stack-helper.cc:189
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::NetDeviceContainer::GetN
uint32_t GetN(void) const
Get the number of Ptr<NetDevice> stored in this container.
Definition: net-device-container.cc:57
ns3::Ipv6Prefix
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition: mobility-helper.h:43
third.mobility
mobility
Definition: third.py:108