A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-one-subnet.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// Network topology
17//
18// n0 n1 n2 n3
19// | | | |
20// =================
21// LAN
22//
23// - CBR/UDP flows from n0 to n1 and from n3 to n0
24// - DropTail queues
25// - Tracing of queues and packet receptions to file "csma-one-subnet.tr"
26
27#include "ns3/applications-module.h"
28#include "ns3/core-module.h"
29#include "ns3/csma-module.h"
30#include "ns3/internet-module.h"
31#include "ns3/network-module.h"
32
33#include <fstream>
34#include <iostream>
35
36using namespace ns3;
37
38NS_LOG_COMPONENT_DEFINE("CsmaOneSubnetExample");
39
40int
41main(int argc, char* argv[])
42{
43//
44// Users may find it convenient to turn on explicit debugging
45// for selected modules; the below lines suggest how to do this
46//
47#if 0
48 LogComponentEnable ("CsmaOneSubnetExample", LOG_LEVEL_INFO);
49#endif
50 //
51 // Allow the user to override any of the defaults and the above Bind() at
52 // run-time, via command-line arguments
53 //
54 CommandLine cmd(__FILE__);
55 cmd.Parse(argc, argv);
56 //
57 // Explicitly create the nodes required by the topology (shown above).
58 //
59 NS_LOG_INFO("Create nodes.");
61 nodes.Create(4);
62
63 NS_LOG_INFO("Build Topology");
65 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
66 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
67 //
68 // Now fill out the topology by creating the net devices required to connect
69 // the nodes to the channels and hooking them up.
70 //
72
74 internet.Install(nodes);
75
76 // We've got the "hardware" in place. Now we need to add IP addresses.
77 //
78 NS_LOG_INFO("Assign IP Addresses.");
80 ipv4.SetBase("10.1.1.0", "255.255.255.0");
81 Ipv4InterfaceContainer interfaces = ipv4.Assign(devices);
82
83 //
84 // Create an OnOff application to send UDP datagrams from node zero to node 1.
85 //
86 NS_LOG_INFO("Create Applications.");
87 uint16_t port = 9; // Discard port (RFC 863)
88
89 OnOffHelper onoff("ns3::UdpSocketFactory",
90 Address(InetSocketAddress(interfaces.GetAddress(1), port)));
91 onoff.SetConstantRate(DataRate("500kb/s"));
92
94 // Start the application
95 app.Start(Seconds(1.0));
96 app.Stop(Seconds(10.0));
97
98 // Create an optional packet sink to receive these packets
99 PacketSinkHelper sink("ns3::UdpSocketFactory",
101 app = sink.Install(nodes.Get(1));
102 app.Start(Seconds(0.0));
103
104 //
105 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
106 //
107 onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(interfaces.GetAddress(0), port)));
108 app = onoff.Install(nodes.Get(3));
109 app.Start(Seconds(1.1));
110 app.Stop(Seconds(10.0));
111
112 app = sink.Install(nodes.Get(0));
113 app.Start(Seconds(0.0));
114
115 NS_LOG_INFO("Configure Tracing.");
116 //
117 // Configure ascii tracing of all enqueue, dequeue, and NetDevice receive
118 // events on all devices. Trace output will be sent to the file
119 // "csma-one-subnet.tr"
120 //
121 AsciiTraceHelper ascii;
122 csma.EnableAsciiAll(ascii.CreateFileStream("csma-one-subnet.tr"));
123
124 //
125 // Also configure some tcpdump traces; each interface will be traced.
126 // The output files will be named:
127 //
128 // csma-one-subnet-<node ID>-<device's interface index>.pcap
129 //
130 // and can be read by the "tcpdump -r" command (use "-tt" option to
131 // display timestamps correctly)
132 //
133 csma.EnablePcapAll("csma-one-subnet", false);
134 //
135 // Now, do the actual simulation.
136 //
137 NS_LOG_INFO("Run Simulation.");
140 NS_LOG_INFO("Done.");
141
142 return 0;
143}
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Address.
Definition: address.h:286
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
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
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.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
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:37
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
AttributeValue implementation for Time.
Definition: nstime.h:1406
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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
NodeContainer nodes
ns devices
Definition: first.py:42
ns interfaces
Definition: first.py:50
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_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
ns cmd
Definition: second.py:40
ns csma
Definition: second.py:63
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55