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-apps-module.h"
36 #include "ns3/internet-module.h"
37 
38 using namespace ns3;
39 
40 NS_LOG_COMPONENT_DEFINE ("CsmaPingExample");
41 
42 static void SinkRx (Ptr<const Packet> p, const Address &ad)
43 {
44  //std::cout << *p << std::endl;
45 }
46 
47 static void PingRtt (std::string context, Time rtt)
48 {
49  //std::cout << context << " " << rtt << std::endl;
50 }
51 
52 int
53 main (int argc, char *argv[])
54 {
55 
56  CommandLine cmd (__FILE__);
57  cmd.Parse (argc, argv);
58 
59  // Here, we will explicitly create four nodes.
60  NS_LOG_INFO ("Create nodes.");
61  NodeContainer c;
62  c.Create (4);
63 
64  // connect all our nodes to a shared channel.
65  NS_LOG_INFO ("Build Topology.");
67  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
68  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
69  csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
70  NetDeviceContainer devs = csma.Install (c);
71 
72  // add an ip stack to all nodes.
73  NS_LOG_INFO ("Add ip stack.");
74  InternetStackHelper ipStack;
75  ipStack.Install (c);
76 
77  // assign ip addresses
78  NS_LOG_INFO ("Assign ip addresses.");
80  ip.SetBase ("192.168.1.0", "255.255.255.0");
81  Ipv4InterfaceContainer addresses = ip.Assign (devs);
82 
83  NS_LOG_INFO ("Create Source");
84  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
85  InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3));
86  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
87  onoff.SetConstantRate (DataRate (15000));
88  onoff.SetAttribute ("PacketSize", UintegerValue (1200));
89 
90 
91  ApplicationContainer apps = onoff.Install (c.Get (0));
92  apps.Start (Seconds (1.0));
93  apps.Stop (Seconds (10.0));
94 
95  NS_LOG_INFO ("Create Sink.");
96  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
97  apps = sink.Install (c.Get (3));
98  apps.Start (Seconds (0.0));
99  apps.Stop (Seconds (11.0));
100 
101  NS_LOG_INFO ("Create pinger");
102  V4PingHelper ping = V4PingHelper (addresses.GetAddress (2));
103  NodeContainer pingers;
104  pingers.Add (c.Get (0));
105  pingers.Add (c.Get (1));
106  pingers.Add (c.Get (3));
107  apps = ping.Install (pingers);
108  apps.Start (Seconds (2.0));
109  apps.Stop (Seconds (5.0));
110 
111  NS_LOG_INFO ("Configure Tracing.");
112  // first, pcap tracing in non-promiscuous mode
113  csma.EnablePcapAll ("csma-ping", false);
114 
115  // then, print what the packet sink receives.
116  Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
117  MakeCallback (&SinkRx));
118  // finally, print the ping rtts.
119  Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt",
120  MakeCallback (&PingRtt));
121 
123 
124 
125  NS_LOG_INFO ("Run Simulation.");
126  Simulator::Run ();
128  NS_LOG_INFO ("Done.");
129 }
a polymophic address class
Definition: address.h:91
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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.
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
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.
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
Hold an unsigned integer type.
Definition: uinteger.h:44
Create a IPv4 ping application and associate it to a node.
Definition: v4ping-helper.h:38
ApplicationContainer Install(NodeContainer nodes) const
Install a Ping application on each Node in the provided NodeContainer.
static void PingRtt(std::string context, Time rtt)
Definition: csma-ping.cc:47
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Definition: csma-ping.cc:42
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:901
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
csma
Definition: second.py:63
cmd
Definition: second.py:35
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56