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