A Discrete-Event Network Simulator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
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  */
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 port, which
28 // is replicated and received on both n1 and n2
30 #include <iostream>
31 #include <fstream>
32 #include <string>
33 #include <cassert>
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"
41 using namespace ns3;
43 NS_LOG_COMPONENT_DEFINE ("CsmaBroadcastExample");
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);
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;
58  cmd.Parse (argc, argv);
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));
66  NS_LOG_INFO ("Build Topology.");
67  CsmaHelper csma;
68  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
69  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
71  NetDeviceContainer n0 = csma.Install (c0);
72  NetDeviceContainer n1 = csma.Install (c1);
74  InternetStackHelper internet;
75  internet.Install (c);
77  NS_LOG_INFO ("Assign IP Addresses.");
78  Ipv4AddressHelper ipv4;
79  ipv4.SetBase ("", "");
80  ipv4.Assign (n0);
81  ipv4.SetBase ("", "");
82  ipv4.Assign (n1);
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;
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 (""), port)));
95  onoff.SetConstantRate (DataRate ("500kb/s"));
97  ApplicationContainer app = onoff.Install (c0.Get (0));
98  // Start the application
99  app.Start (Seconds (1.0));
100  app.Stop (Seconds (10.0));
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));
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"));
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);
122  NS_LOG_INFO ("Run Simulation.");
123  Simulator::Run ();
125  NS_LOG_INFO ("Done.");
126 }
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:128
an Inet address class
static Ipv4Address GetAny(void)
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Definition: log.h:298
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
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. ...
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:44
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 ...
hold objects of type ns3::Time
Definition: nstime.h:961
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...
Parse command-line arguments.
Definition: command-line.h:152
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
keep track of a set of node pointers.
int main(int argc, char *argv[])
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
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...
hold objects of type ns3::DataRate
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="")
Set the base network number, network mask and base address.
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311