A Discrete-Event Network Simulator
API
csma-ping.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 // Network topology
18 //
19 // n0 n1 n2 n3
20 // | | | |
21 // =====================
22 //
23 // node n0,n1,n3 pings to node n2
24 // node n0 generates protocol 2 (IGMP) to node n3
25 
26 #include <iostream>
27 #include <fstream>
28 #include <string>
29 #include <cassert>
30 
31 #include "ns3/core-module.h"
32 #include "ns3/network-module.h"
33 #include "ns3/csma-module.h"
34 #include "ns3/applications-module.h"
35 #include "ns3/internet-module.h"
36 
37 using namespace ns3;
38 
39 NS_LOG_COMPONENT_DEFINE ("CsmaPingExample");
40 
41 static void SinkRx (Ptr<const Packet> p, const Address &ad)
42 {
43  //std::cout << *p << std::endl;
44 }
45 
46 static void PingRtt (std::string context, Time rtt)
47 {
48  //std::cout << context << " " << rtt << std::endl;
49 }
50 
51 int
52 main (int argc, char *argv[])
53 {
54 
56  cmd.Parse (argc, argv);
57 
58  // Here, we will explicitly create four nodes.
59  NS_LOG_INFO ("Create nodes.");
60  NodeContainer c;
61  c.Create (4);
62 
63  // connect all our nodes to a shared channel.
64  NS_LOG_INFO ("Build Topology.");
66  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
67  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
68  csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
69  NetDeviceContainer devs = csma.Install (c);
70 
71  // add an ip stack to all nodes.
72  NS_LOG_INFO ("Add ip stack.");
73  InternetStackHelper ipStack;
74  ipStack.Install (c);
75 
76  // assign ip addresses
77  NS_LOG_INFO ("Assign ip addresses.");
79  ip.SetBase ("192.168.1.0", "255.255.255.0");
80  Ipv4InterfaceContainer addresses = ip.Assign (devs);
81 
82  NS_LOG_INFO ("Create Source");
83  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
84  InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3));
85  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
86  onoff.SetConstantRate (DataRate (15000));
87  onoff.SetAttribute ("PacketSize", UintegerValue (1200));
88 
89 
90  ApplicationContainer apps = onoff.Install (c.Get (0));
91  apps.Start (Seconds (1.0));
92  apps.Stop (Seconds (10.0));
93 
94  NS_LOG_INFO ("Create Sink.");
95  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
96  apps = sink.Install (c.Get (3));
97  apps.Start (Seconds (0.0));
98  apps.Stop (Seconds (11.0));
99 
100  NS_LOG_INFO ("Create pinger");
101  V4PingHelper ping = V4PingHelper (addresses.GetAddress (2));
102  NodeContainer pingers;
103  pingers.Add (c.Get (0));
104  pingers.Add (c.Get (1));
105  pingers.Add (c.Get (3));
106  apps = ping.Install (pingers);
107  apps.Start (Seconds (2.0));
108  apps.Stop (Seconds (5.0));
109 
110  NS_LOG_INFO ("Configure Tracing.");
111  // first, pcap tracing in non-promiscuous mode
112  csma.EnablePcapAll ("csma-ping", false);
113 
114  // then, print what the packet sink receives.
115  Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
116  MakeCallback (&SinkRx));
117  // finally, print the ping rtts.
118  Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt",
119  MakeCallback (&PingRtt));
120 
122 
123 
124  NS_LOG_INFO ("Run Simulation.");
125  Simulator::Run ();
127  NS_LOG_INFO ("Done.");
128 }
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
static void PingRtt(std::string context, Time rtt)
Definition: csma-ping.cc:46
an Inet address class
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
holds a vector of std::pair of Ptr and interface index.
Hold variables of type string.
Definition: string.h:41
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
aggregate IP/TCP/UDP functionality to existing Nodes.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
ApplicationContainer Install(NodeContainer nodes) const
Install a Ping application on each Node in the provided NodeContainer.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
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 ...
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:533
AttributeValue implementation for Time.
Definition: nstime.h:957
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Definition: csma-ping.cc:41
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1480
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:824
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:201
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:835
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:63
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
AttributeValue implementation for DataRate.
Definition: data-rate.h:241
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Create a IPv5 ping application and associate it to a node.
Definition: v4ping-helper.h:37
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
tuple csma
Definition: second.py:63
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const