A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
csma-ping.cc
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 
55  CommandLine cmd;
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.");
65  CsmaHelper csma;
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.
keep track of time unit.
Definition: nstime.h:149
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<Ipv4> and interface index.
hold variables of type string
Definition: string.h:19
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
aggregate IP/TCP/UDP functionality to existing Nodes.
NetDeviceContainer Install(Ptr< Node > node) const
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Definition: log.h:264
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
ApplicationContainer Install(NodeContainer nodes) const
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:41
a polymophic address class
Definition: address.h:86
Class for representing data rates.
Definition: data-rate.h:71
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)
Definition: packet.cc:575
hold objects of type ns3::Time
Definition: nstime.h:700
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
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 argumentsInstances of this class can be used to parse command-line arguments: user...
Definition: command-line.h:50
static void Destroy(void)
Definition: simulator.cc:121
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
keep track of a set of node pointers.
void Install(std::string nodeName) const
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...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
hold objects of type ns3::DataRate
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84
ApplicationContainer Install(NodeContainer c) const
Time MilliSeconds(uint64_t ms)
create ns3::Time instances in units of milliseconds.
Definition: nstime.h:601
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 pinger application and associate it to a node
Definition: v4ping-helper.h:16
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:717
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const