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
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 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
AttributeValue implementation for Time.
Definition: nstime.h:1076
LOG_INFO and above.
Definition: log.h:104
holds a vector of ns3::NetDevice pointers
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
csma
Definition: second.py:63
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Prefix all trace prints with simulation time.
Definition: log.h:116
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
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...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
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.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.