A Discrete-Event Network Simulator
API
simple-point-to-point-olsr.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 
17 //
18 // Simple example of OLSR routing over some point-to-point links
19 //
20 // Network topology
21 //
22 // n0
23 // \ 5 Mb/s, 2ms
24 // \ 1.5Mb/s, 10ms
25 // n2 -------------------------n3---------n4
26 // /
27 // / 5 Mb/s, 2ms
28 // n1
29 //
30 // - all links are point-to-point links with indicated one-way BW/delay
31 // - CBR/UDP flows from n0 to n4, and from n3 to n1
32 // - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
33 // (i.e., DataRate of 448,000 bps)
34 // - DropTail queues
35 // - Tracing of queues and packet receptions to file "simple-point-to-point-olsr.tr"
36 
37 #include "ns3/applications-module.h"
38 #include "ns3/core-module.h"
39 #include "ns3/internet-module.h"
40 #include "ns3/ipv4-list-routing-helper.h"
41 #include "ns3/ipv4-static-routing-helper.h"
42 #include "ns3/network-module.h"
43 #include "ns3/olsr-helper.h"
44 #include "ns3/point-to-point-module.h"
45 
46 #include <cassert>
47 #include <fstream>
48 #include <iostream>
49 #include <string>
50 
51 using namespace ns3;
52 
53 NS_LOG_COMPONENT_DEFINE("SimplePointToPointOlsrExample");
54 
55 int
56 main(int argc, char* argv[])
57 {
58  // Users may find it convenient to turn on explicit debugging
59  // for selected modules; the below lines suggest how to do this
60 #if 0
61  LogComponentEnable ("SimpleGlobalRoutingExample", LOG_LEVEL_INFO);
62 #endif
63 
64  // Set up some default values for the simulation. Use the
65 
66  Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(210));
67  Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("448kb/s"));
68 
69  // DefaultValue::Bind ("DropTailQueue::m_maxPackets", 30);
70 
71  // Allow the user to override any of the defaults and the above
72  // DefaultValue::Bind ()s at run-time, via command-line arguments
73  CommandLine cmd(__FILE__);
74  cmd.Parse(argc, argv);
75 
76  // Here, we will explicitly create four nodes. In more sophisticated
77  // topologies, we could configure a node factory.
78  NS_LOG_INFO("Create nodes.");
79  NodeContainer c;
80  c.Create(5);
81  NodeContainer n02 = NodeContainer(c.Get(0), c.Get(2));
82  NodeContainer n12 = NodeContainer(c.Get(1), c.Get(2));
83  NodeContainer n32 = NodeContainer(c.Get(3), c.Get(2));
84  NodeContainer n34 = NodeContainer(c.Get(3), c.Get(4));
85 
86  // Enable OLSR
87  NS_LOG_INFO("Enabling OLSR Routing.");
89 
90  Ipv4StaticRoutingHelper staticRouting;
91 
93  list.Add(staticRouting, 0);
94  list.Add(olsr, 10);
95 
97  internet.SetRoutingHelper(list); // has effect on the next Install ()
98  internet.Install(c);
99 
100  // We create the channels first without any IP addressing information
101  NS_LOG_INFO("Create channels.");
103  p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
104  p2p.SetChannelAttribute("Delay", StringValue("2ms"));
105  NetDeviceContainer nd02 = p2p.Install(n02);
106  NetDeviceContainer nd12 = p2p.Install(n12);
107  p2p.SetDeviceAttribute("DataRate", StringValue("1500kbps"));
108  p2p.SetChannelAttribute("Delay", StringValue("10ms"));
109  NetDeviceContainer nd32 = p2p.Install(n32);
110  NetDeviceContainer nd34 = p2p.Install(n34);
111 
112  // Later, we add IP addresses.
113  NS_LOG_INFO("Assign IP Addresses.");
115  ipv4.SetBase("10.1.1.0", "255.255.255.0");
116  Ipv4InterfaceContainer i02 = ipv4.Assign(nd02);
117 
118  ipv4.SetBase("10.1.2.0", "255.255.255.0");
119  Ipv4InterfaceContainer i12 = ipv4.Assign(nd12);
120 
121  ipv4.SetBase("10.1.3.0", "255.255.255.0");
122  Ipv4InterfaceContainer i32 = ipv4.Assign(nd32);
123 
124  ipv4.SetBase("10.1.4.0", "255.255.255.0");
125  Ipv4InterfaceContainer i34 = ipv4.Assign(nd34);
126 
127  // Create the OnOff application to send UDP datagrams of size
128  // 210 bytes at a rate of 448 Kb/s from n0 to n4
129  NS_LOG_INFO("Create Applications.");
130  uint16_t port = 9; // Discard port (RFC 863)
131 
132  OnOffHelper onoff1("ns3::UdpSocketFactory", InetSocketAddress(i34.GetAddress(1), port));
133  onoff1.SetConstantRate(DataRate("448kb/s"));
134 
135  ApplicationContainer onOffApp1 = onoff1.Install(c.Get(0));
136  onOffApp1.Start(Seconds(10.0));
137  onOffApp1.Stop(Seconds(20.0));
138 
139  // Create a similar flow from n3 to n1, starting at time 1.1 seconds
140  OnOffHelper onoff2("ns3::UdpSocketFactory", InetSocketAddress(i12.GetAddress(0), port));
141  onoff2.SetConstantRate(DataRate("448kb/s"));
142 
143  ApplicationContainer onOffApp2 = onoff2.Install(c.Get(3));
144  onOffApp2.Start(Seconds(10.1));
145  onOffApp2.Stop(Seconds(20.0));
146 
147  // Create packet sinks to receive these packets
148  PacketSinkHelper sink("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
149  NodeContainer sinks = NodeContainer(c.Get(4), c.Get(1));
150  ApplicationContainer sinkApps = sink.Install(sinks);
151  sinkApps.Start(Seconds(0.0));
152  sinkApps.Stop(Seconds(21.0));
153 
154  AsciiTraceHelper ascii;
155  p2p.EnableAsciiAll(ascii.CreateFileStream("simple-point-to-point-olsr.tr"));
156  p2p.EnablePcapAll("simple-point-to-point-olsr");
157 
159 
160  NS_LOG_INFO("Run Simulation.");
161  Simulator::Run();
163  NS_LOG_INFO("Done.");
164 
165  return 0;
166 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
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
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.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class that adds ns3::Ipv4ListRouting objects.
Helper class that adds ns3::Ipv4StaticRouting objects.
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.
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:42
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.
Build a set of PointToPointNetDevice objects.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#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
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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
Definition: olsr.py:1
cmd
Definition: second.py:40
#define list
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55