A Discrete-Event Network Simulator
Go to the documentation of this file.
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
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 */
18// Network topology
20// n0
21// \ 5 Mb/s, 2ms
22// \ 1.5Mb/s, 10ms
23// n2 ------------------------n3
24// / /
25// / 5 Mb/s, 2ms /
26// n1--------------------------
27// 1.5 Mb/s, 100ms
29// this is a modification of simple-global-routing to allow for
30// a single hop but higher-cost path between n1 and n3
32// - Tracing of queues and packet receptions to file "simple-rerouting.tr"
34#include "ns3/applications-module.h"
35#include "ns3/core-module.h"
36#include "ns3/internet-module.h"
37#include "ns3/ipv4-global-routing-helper.h"
38#include "ns3/network-module.h"
39#include "ns3/point-to-point-module.h"
41#include <cassert>
42#include <fstream>
43#include <iostream>
44#include <string>
46using namespace ns3;
51main(int argc, char* argv[])
53 // Users may find it convenient to turn on explicit debugging
54 // for selected modules; the below lines suggest how to do this
55#if 0
56 LogComponentEnable ("GlobalRoutingHelper", LOG_LOGIC);
57 LogComponentEnable ("GlobalRouter", LOG_LOGIC);
60 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(210));
61 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("300b/s"));
63 // The below metric, if set to 3 or higher, will cause packets between
64 // n1 and n3 to take the 2-hop route through n2
66 CommandLine cmd(__FILE__);
67 //
68 // Additionally, we plumb this metric into the default value / command
69 // line argument system as well, for exemplary purposes. This means
70 // that it can be resettable at the command-line to the program,
71 // rather than recompiling
72 // e.g. ns3 --run "simple-alternate-routing --AlternateCost=5"
73 uint16_t sampleMetric = 1;
74 cmd.AddValue("AlternateCost",
75 "This metric is used in the example script between n3 and n1 ",
76 sampleMetric);
78 // Allow the user to override any of the defaults and the above
79 // DefaultValue::Bind ()s at run-time, via command-line arguments
80 cmd.Parse(argc, argv);
82 // Here, we will explicitly create four nodes. In more sophisticated
83 // topologies, we could configure a node factory.
84 NS_LOG_INFO("Create nodes.");
86 c.Create(4);
89 NodeContainer n3n2 = NodeContainer(c.Get(3), c.Get(2));
90 NodeContainer n1n3 = NodeContainer(c.Get(1), c.Get(3));
92 // We create the channels first without any IP addressing information
93 NS_LOG_INFO("Create channels.");
95 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
96 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
101 p2p.SetDeviceAttribute("DataRate", StringValue("1500kbps"));
102 p2p.SetChannelAttribute("Delay", StringValue("10ms"));
103 NetDeviceContainer d3d2 = p2p.Install(n3n2);
105 p2p.SetChannelAttribute("Delay", StringValue("100ms"));
106 NetDeviceContainer d1d3 = p2p.Install(n1n3);
108 InternetStackHelper internet;
109 internet.Install(c);
111 // Later, we add IP addresses. The middle two octets correspond to
112 // the channel number.
113 NS_LOG_INFO("Assign IP Addresses.");
115 ipv4.SetBase("", "");
116 ipv4.Assign(d0d2);
118 ipv4.SetBase("", "");
121 ipv4.SetBase("", "");
122 ipv4.Assign(d3d2);
124 ipv4.SetBase("", "");
125 Ipv4InterfaceContainer i1i3 = ipv4.Assign(d1d3);
127 i1i3.SetMetric(0, sampleMetric);
128 i1i3.SetMetric(1, sampleMetric);
130 // Create router nodes, initialize routing database and set up the routing
131 // tables in the nodes.
132 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
134 // Create the OnOff application to send UDP datagrams
135 NS_LOG_INFO("Create Application.");
136 uint16_t port = 9; // Discard port (RFC 863)
138 // Create a flow from n3 to n1, starting at time 1.1 seconds
139 OnOffHelper onoff("ns3::UdpSocketFactory",
141 onoff.SetConstantRate(DataRate("300b/s"));
143 ApplicationContainer apps = onoff.Install(c.Get(3));
144 apps.Start(Seconds(1.1));
145 apps.Stop(Seconds(10.0));
147 // Create a packet sink to receive these packets
148 PacketSinkHelper sink("ns3::UdpSocketFactory",
149 Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
150 apps = sink.Install(c.Get(1));
151 apps.Start(Seconds(1.1));
152 apps.Stop(Seconds(10.0));
154 AsciiTraceHelper ascii;
155 p2p.EnableAsciiAll(ascii.CreateFileStream("simple-alternate-routing.tr"));
156 p2p.EnablePcapAll("simple-alternate-routing");
158 NS_LOG_INFO("Run Simulation.");
159 Simulator::Run();
160 Simulator::Destroy();
161 NS_LOG_INFO("Done.");
163 return 0;
NodeContainer n1n2
Nodecontainer n1 + n2.
Ipv4InterfaceContainer i1i2
IPv4 interface container i1 + i2.
NodeContainer n0n2
Nodecontainer n0 + n2.
a polymophic address class
Definition: address.h:100
holds a vector of ns3::Application pointers.
void Start(Time start)
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
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...
Manage ASCII trace files for device models.
Definition: trace-helper.h:173
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
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void SetMetric(uint32_t i, uint16_t metric)
Set a metric for the given interface.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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.
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 ...
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
NetDeviceContainer Install(NodeContainer c)
Hold variables of type string.
Definition: string.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
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:328
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const char *name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:296
Control flow tracing within functions.
Definition: log.h:112
Definition: second.py:33
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55