A Discrete-Event Network Simulator
API
csma-broadcast.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 //
17 // Example of the sending of a datagram to a broadcast address
18 //
19 // Network topology
20 // ==============
21 // | |
22 // n0 n1 n2
23 // | |
24 // ==========
25 //
26 // n0 originates UDP broadcast to 255.255.255.255/discard port, which
27 // is replicated and received on both n1 and n2
28 
29 #include "ns3/applications-module.h"
30 #include "ns3/core-module.h"
31 #include "ns3/csma-module.h"
32 #include "ns3/internet-module.h"
33 #include "ns3/network-module.h"
34 
35 #include <cassert>
36 #include <fstream>
37 #include <iostream>
38 #include <string>
39 
40 using namespace ns3;
41 
42 NS_LOG_COMPONENT_DEFINE("CsmaBroadcastExample");
43 
44 int
45 main(int argc, char* argv[])
46 {
47  // Users may find it convenient to turn on explicit debugging
48  // for selected modules; the below lines suggest how to do this
49 #if 0
50  LogComponentEnable ("CsmaBroadcastExample", LOG_LEVEL_INFO);
51 #endif
52  LogComponentEnable("CsmaBroadcastExample", LOG_PREFIX_TIME);
53 
54  // Allow the user to override any of the defaults and the above
55  // Bind()s at run-time, via command-line arguments
56  CommandLine cmd(__FILE__);
57  cmd.Parse(argc, argv);
58 
59  NS_LOG_INFO("Create nodes.");
60  NodeContainer c;
61  c.Create(3);
62  NodeContainer c0 = NodeContainer(c.Get(0), c.Get(1));
63  NodeContainer c1 = NodeContainer(c.Get(0), c.Get(2));
64 
65  NS_LOG_INFO("Build Topology.");
67  csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
68  csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
69 
70  NetDeviceContainer n0 = csma.Install(c0);
71  NetDeviceContainer n1 = csma.Install(c1);
72 
74  internet.Install(c);
75 
76  NS_LOG_INFO("Assign IP Addresses.");
78  ipv4.SetBase("10.1.0.0", "255.255.255.0");
79  ipv4.Assign(n0);
80  ipv4.SetBase("192.168.1.0", "255.255.255.0");
81  ipv4.Assign(n1);
82 
83  // RFC 863 discard port ("9") indicates packet should be thrown away
84  // by the system. We allow this silent discard to be overridden
85  // by the PacketSink application.
86  uint16_t port = 9;
87 
88  // Create the OnOff application to send UDP datagrams of size
89  // 512 bytes (default) at a rate of 500 Kb/s (default) from n0
90  NS_LOG_INFO("Create Applications.");
91  OnOffHelper onoff("ns3::UdpSocketFactory",
92  Address(InetSocketAddress(Ipv4Address("255.255.255.255"), port)));
93  onoff.SetConstantRate(DataRate("500kb/s"));
94 
95  ApplicationContainer app = onoff.Install(c0.Get(0));
96  // Start the application
97  app.Start(Seconds(1.0));
98  app.Stop(Seconds(10.0));
99 
100  // Create an optional packet sink to receive these packets
101  PacketSinkHelper sink("ns3::UdpSocketFactory",
103  app = sink.Install(c0.Get(1));
104  app.Add(sink.Install(c1.Get(1)));
105  app.Start(Seconds(1.0));
106  app.Stop(Seconds(10.0));
107 
108  // Configure ascii tracing of all enqueue, dequeue, and NetDevice receive
109  // events on all devices. Trace output will be sent to the file
110  // "csma-one-subnet.tr"
111  AsciiTraceHelper ascii;
112  csma.EnableAsciiAll(ascii.CreateFileStream("csma-broadcast.tr"));
113 
114  // Also configure some tcpdump traces; each interface will be traced
115  // The output files will be named
116  // csma-broadcast-<nodeId>-<interfaceId>.pcap
117  // and can be read by the "tcpdump -tt -r" command
118  csma.EnablePcapAll("csma-broadcast", false);
119 
120  NS_LOG_INFO("Run Simulation.");
121  Simulator::Run();
123  NS_LOG_INFO("Done.");
124 
125  return 0;
126 }
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
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.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
static Ipv4Address GetAny()
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.
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
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
uint16_t port
Definition: dsdv-manet.cc:44
#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:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
csma
Definition: second.py:63
cmd
Definition: second.py:40
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55