A Discrete-Event Network Simulator
API
csma-broadcast.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 //
18 // Example of the sending of a datagram to a broadcast address
19 //
20 // Network topology
21 // ==============
22 // | |
23 // n0 n1 n2
24 // | |
25 // ==========
26 //
27 // n0 originates UDP broadcast to 255.255.255.255/discard port, which
28 // is replicated and received on both n1 and n2
29 
30 #include <iostream>
31 #include <fstream>
32 #include <string>
33 #include <cassert>
34 
35 #include "ns3/core-module.h"
36 #include "ns3/network-module.h"
37 #include "ns3/csma-module.h"
38 #include "ns3/applications-module.h"
39 #include "ns3/internet-module.h"
40 
41 using namespace ns3;
42 
43 NS_LOG_COMPONENT_DEFINE ("CsmaBroadcastExample");
44 
45 int
46 main (int argc, char *argv[])
47 {
48  // Users may find it convenient to turn on explicit debugging
49  // for selected modules; the below lines suggest how to do this
50 #if 0
51  LogComponentEnable ("CsmaBroadcastExample", LOG_LEVEL_INFO);
52 #endif
53  LogComponentEnable ("CsmaBroadcastExample", LOG_PREFIX_TIME);
54 
55  // Allow the user to override any of the defaults and the above
56  // Bind()s at run-time, via command-line arguments
57  CommandLine cmd (__FILE__);
58  cmd.Parse (argc, argv);
59 
60  NS_LOG_INFO ("Create nodes.");
61  NodeContainer c;
62  c.Create (3);
63  NodeContainer c0 = NodeContainer (c.Get (0), c.Get (1));
64  NodeContainer c1 = NodeContainer (c.Get (0), c.Get (2));
65 
66  NS_LOG_INFO ("Build Topology.");
68  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
69  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
70 
71  NetDeviceContainer n0 = csma.Install (c0);
72  NetDeviceContainer n1 = csma.Install (c1);
73 
74  InternetStackHelper internet;
75  internet.Install (c);
76 
77  NS_LOG_INFO ("Assign IP Addresses.");
78  Ipv4AddressHelper ipv4;
79  ipv4.SetBase ("10.1.0.0", "255.255.255.0");
80  ipv4.Assign (n0);
81  ipv4.SetBase ("192.168.1.0", "255.255.255.0");
82  ipv4.Assign (n1);
83 
84 
85  // RFC 863 discard port ("9") indicates packet should be thrown away
86  // by the system. We allow this silent discard to be overridden
87  // by the PacketSink application.
88  uint16_t port = 9;
89 
90  // Create the OnOff application to send UDP datagrams of size
91  // 512 bytes (default) at a rate of 500 Kb/s (default) from n0
92  NS_LOG_INFO ("Create Applications.");
93  OnOffHelper onoff ("ns3::UdpSocketFactory",
94  Address (InetSocketAddress (Ipv4Address ("255.255.255.255"), port)));
95  onoff.SetConstantRate (DataRate ("500kb/s"));
96 
97  ApplicationContainer app = onoff.Install (c0.Get (0));
98  // Start the application
99  app.Start (Seconds (1.0));
100  app.Stop (Seconds (10.0));
101 
102  // Create an optional packet sink to receive these packets
103  PacketSinkHelper sink ("ns3::UdpSocketFactory",
105  app = sink.Install (c0.Get (1));
106  app.Add (sink.Install (c1.Get (1)));
107  app.Start (Seconds (1.0));
108  app.Stop (Seconds (10.0));
109 
110  // Configure ascii tracing of all enqueue, dequeue, and NetDevice receive
111  // events on all devices. Trace output will be sent to the file
112  // "csma-one-subnet.tr"
113  AsciiTraceHelper ascii;
114  csma.EnableAsciiAll (ascii.CreateFileStream ("csma-broadcast.tr"));
115 
116  // Also configure some tcpdump traces; each interface will be traced
117  // The output files will be named
118  // csma-broadcast-<nodeId>-<interfaceId>.pcap
119  // and can be read by the "tcpdump -tt -r" command
120  csma.EnablePcapAll ("csma-broadcast", false);
121 
122  NS_LOG_INFO ("Run Simulation.");
123  Simulator::Run ();
125  NS_LOG_INFO ("Done.");
126 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
ns3::DataRateValue
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
ns3::CsmaHelper
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
ns3::ApplicationContainer::Add
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
Definition: application-container.cc:67
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
ns3::DataRate
Class for representing data rates.
Definition: data-rate.h:89
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
ns3::OnOffHelper
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::AsciiTraceHelper::CreateFileStream
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Definition: trace-helper.cc:191
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1297
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition: node-container.cc:93
second.cmd
cmd
Definition: second.py:35
sink
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::PacketSinkHelper
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Definition: packet-sink-helper.h:36
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
ns3::LOG_PREFIX_TIME
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny(void)
Definition: ipv4-address.cc:395
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
second.csma
csma
Definition: second.py:63
port
uint16_t port
Definition: dsdv-manet.cc:45