A Discrete-Event Network Simulator
API
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
47static void
49{
50 // std::cout << *p << std::endl;
51}
52
59static void
60PingRtt(std::string context, Time rtt)
61{
62 // std::cout << context << " " << rtt << std::endl;
63}
64
65int
66main(int argc, char* argv[])
67{
68 CommandLine cmd(__FILE__);
69 cmd.Parse(argc, argv);
70
71 // Here, we will explicitly create four nodes.
72 NS_LOG_INFO("Create nodes.");
74 c.Create(4);
75
76 // connect all our nodes to a shared channel.
77 NS_LOG_INFO("Build Topology.");
79 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
80 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
81 csma.SetDeviceAttribute("EncapsulationMode", StringValue("Llc"));
82 NetDeviceContainer devs = csma.Install(c);
83
84 // add an ip stack to all nodes.
85 NS_LOG_INFO("Add ip stack.");
86 InternetStackHelper ipStack;
87 ipStack.Install(c);
88
89 // assign ip addresses
90 NS_LOG_INFO("Assign ip addresses.");
92 ip.SetBase("192.168.1.0", "255.255.255.0");
93 Ipv4InterfaceContainer addresses = ip.Assign(devs);
94
95 NS_LOG_INFO("Create Source");
96 Config::SetDefault("ns3::Ipv4RawSocketImpl::Protocol", StringValue("2"));
98 OnOffHelper onoff = OnOffHelper("ns3::Ipv4RawSocketFactory", dst);
99 onoff.SetConstantRate(DataRate(15000));
100 onoff.SetAttribute("PacketSize", UintegerValue(1200));
101
102 ApplicationContainer apps = onoff.Install(c.Get(0));
103 apps.Start(Seconds(1.0));
104 apps.Stop(Seconds(10.0));
105
106 NS_LOG_INFO("Create Sink.");
107 PacketSinkHelper sink = PacketSinkHelper("ns3::Ipv4RawSocketFactory", dst);
108 apps = sink.Install(c.Get(3));
109 apps.Start(Seconds(0.0));
110 apps.Stop(Seconds(11.0));
111
112 NS_LOG_INFO("Create pinger");
113 V4PingHelper ping = V4PingHelper(addresses.GetAddress(2));
114 NodeContainer pingers;
115 pingers.Add(c.Get(0));
116 pingers.Add(c.Get(1));
117 pingers.Add(c.Get(3));
118 apps = ping.Install(pingers);
119 apps.Start(Seconds(2.0));
120 apps.Stop(Seconds(5.0));
121
122 NS_LOG_INFO("Configure Tracing.");
123 // first, pcap tracing in non-promiscuous mode
124 csma.EnablePcapAll("csma-ping", false);
125
126 // then, print what the packet sink receives.
127 Config::ConnectWithoutContext("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
129 // finally, print the ping rtts.
130 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt", MakeCallback(&PingRtt));
131
132 Packet::EnablePrinting();
133
134 NS_LOG_INFO("Run Simulation.");
135 Simulator::Run();
136 Simulator::Destroy();
137 NS_LOG_INFO("Done.");
138}
a polymophic address class
Definition: address.h:92
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:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
AttributeValue implementation for DataRate.
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:44
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.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Hold variables of type string.
Definition: string.h:42
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1425
Hold an unsigned integer type.
Definition: uinteger.h:45
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)
Ping RTT trace sink.
Definition: csma-ping.cc:60
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Rx sink.
Definition: csma-ping.cc:48
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:975
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:951
#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
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
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:691
csma
Definition: second.py:56
cmd
Definition: second.py:33
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55