A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-ping.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16// Network topology
17//
18// n0 n1 n2 n3
19// | | | |
20// =====================
21//
22// node n0,n1,n3 pings to node n2
23// node n0 generates protocol 2 (IGMP) to node n3
24
25#include "ns3/applications-module.h"
26#include "ns3/core-module.h"
27#include "ns3/csma-module.h"
28#include "ns3/internet-apps-module.h"
29#include "ns3/internet-module.h"
30#include "ns3/network-module.h"
31
32#include <cassert>
33#include <fstream>
34#include <iostream>
35#include <string>
36
37using namespace ns3;
38
39NS_LOG_COMPONENT_DEFINE("CsmaPingExample");
40
41/**
42 * Rx sink
43 *
44 * \param p The packer.
45 * \param ad The sender address.
46 */
47static void
49{
50 std::cout << *p << std::endl;
51}
52
53/**
54 * Ping RTT trace sink
55 *
56 * \param context The context.
57 * \param seqNo The Sequence Number.
58 * \param rtt The RTT.
59 */
60static void
61PingRtt(std::string context, uint16_t seqNo, Time rtt)
62{
63 std::cout << context << " " << seqNo << " " << rtt << std::endl;
64}
65
66int
67main(int argc, char* argv[])
68{
69 bool verbose{false};
70
71 CommandLine cmd(__FILE__);
72 cmd.AddValue("verbose",
73 "print received background traffic packets and ping RTT values",
74 verbose);
75 cmd.Parse(argc, argv);
76
77 // Here, we will explicitly create four nodes.
78 NS_LOG_INFO("Create nodes.");
80 c.Create(4);
81
82 // connect all our nodes to a shared channel.
83 NS_LOG_INFO("Build Topology.");
85 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
86 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
87 csma.SetDeviceAttribute("EncapsulationMode", StringValue("Llc"));
88 NetDeviceContainer devs = csma.Install(c);
89
90 // add an ip stack to all nodes.
91 NS_LOG_INFO("Add ip stack.");
92 InternetStackHelper ipStack;
93 ipStack.Install(c);
94
95 // assign ip addresses
96 NS_LOG_INFO("Assign ip addresses.");
98 ip.SetBase("192.168.1.0", "255.255.255.0");
99 Ipv4InterfaceContainer addresses = ip.Assign(devs);
100
101 NS_LOG_INFO("Create Source");
102 Config::SetDefault("ns3::Ipv4RawSocketImpl::Protocol", StringValue("2"));
103 InetSocketAddress dst(addresses.GetAddress(3));
104 OnOffHelper onoff = OnOffHelper("ns3::Ipv4RawSocketFactory", dst);
105 onoff.SetConstantRate(DataRate(15000));
106 onoff.SetAttribute("PacketSize", UintegerValue(1200));
107
108 ApplicationContainer apps = onoff.Install(c.Get(0));
109 apps.Start(Seconds(1.0));
110 apps.Stop(Seconds(10.0));
111
112 NS_LOG_INFO("Create Sink.");
113 PacketSinkHelper sink = PacketSinkHelper("ns3::Ipv4RawSocketFactory", dst);
114 apps = sink.Install(c.Get(3));
115 apps.Start(Seconds(0.0));
116 apps.Stop(Seconds(11.0));
117
118 NS_LOG_INFO("Create pinger");
119 PingHelper ping(addresses.GetAddress(2));
120 NodeContainer pingers;
121 pingers.Add(c.Get(0));
122 pingers.Add(c.Get(1));
123 pingers.Add(c.Get(3));
124 apps = ping.Install(pingers);
125 apps.Start(Seconds(2.0));
126 apps.Stop(Seconds(5.0));
127
128 NS_LOG_INFO("Configure Tracing.");
129 // first, pcap tracing in non-promiscuous mode
130 csma.EnablePcapAll("csma-ping", false);
131
132 if (verbose)
133 {
134 // then, print what the packet sink receives.
135 Config::ConnectWithoutContext("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
137 // finally, print the ping rtts.
138 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::Ping/Rtt", MakeCallback(&PingRtt));
139 }
140
142
143 NS_LOG_INFO("Run Simulation.");
146 NS_LOG_INFO("Done.");
147
148 return 0;
149}
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.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
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(const NodeContainer &nc)
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:37
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Create a ping application and associate it to a node.
Definition: ping-helper.h:42
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1406
Hold an unsigned integer type.
Definition: uinteger.h:45
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Rx sink.
Definition: csma-ping.cc:48
static void PingRtt(std::string context, uint16_t seqNo, Time rtt)
Ping RTT trace sink.
Definition: csma-ping.cc:61
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:954
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
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:706
ns cmd
Definition: second.py:40
ns csma
Definition: second.py:63
bool verbose
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55