A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-point-to-point-olsr.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 */
5
6//
7// Simple example of OLSR routing over some point-to-point links
8//
9// Network topology
10//
11// n0
12// \ 5 Mb/s, 2ms
13// \ 1.5Mb/s, 10ms
14// n2 -------------------------n3---------n4
15// /
16// / 5 Mb/s, 2ms
17// n1
18//
19// - all links are point-to-point links with indicated one-way BW/delay
20// - CBR/UDP flows from n0 to n4, and from n3 to n1
21// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
22// (i.e., DataRate of 448,000 bps)
23// - DropTail queues
24// - Tracing of queues and packet receptions to file "simple-point-to-point-olsr.tr"
25
26#include "ns3/applications-module.h"
27#include "ns3/core-module.h"
28#include "ns3/internet-module.h"
29#include "ns3/ipv4-list-routing-helper.h"
30#include "ns3/ipv4-static-routing-helper.h"
31#include "ns3/network-module.h"
32#include "ns3/olsr-helper.h"
33#include "ns3/point-to-point-module.h"
34
35#include <cassert>
36#include <fstream>
37#include <iostream>
38#include <string>
39
40using namespace ns3;
41
42NS_LOG_COMPONENT_DEFINE("SimplePointToPointOlsrExample");
43
44int
45main(int argc, char* argv[])
46{
47 // Users may find it convenient to turn on explicit debugging
48 // for selected modules; the below lines suggest how to do this
49#if 0
50 LogComponentEnable ("SimpleGlobalRoutingExample", LOG_LEVEL_INFO);
51#endif
52
53 // Set up some default values for the simulation. Use the
54
55 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(210));
56 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("448kb/s"));
57
58 // DefaultValue::Bind ("DropTailQueue::m_maxPackets", 30);
59
60 // Allow the user to override any of the defaults and the above
61 // DefaultValue::Bind ()s at run-time, via command-line arguments
62 CommandLine cmd(__FILE__);
63 cmd.Parse(argc, argv);
64
65 // Here, we will explicitly create four nodes. In more sophisticated
66 // topologies, we could configure a node factory.
67 NS_LOG_INFO("Create nodes.");
69 c.Create(5);
70 NodeContainer n02 = NodeContainer(c.Get(0), c.Get(2));
71 NodeContainer n12 = NodeContainer(c.Get(1), c.Get(2));
72 NodeContainer n32 = NodeContainer(c.Get(3), c.Get(2));
73 NodeContainer n34 = NodeContainer(c.Get(3), c.Get(4));
74
75 // Enable OLSR
76 NS_LOG_INFO("Enabling OLSR Routing.");
78
79 Ipv4StaticRoutingHelper staticRouting;
80
82 list.Add(staticRouting, 0);
83 list.Add(olsr, 10);
84
86 internet.SetRoutingHelper(list); // has effect on the next Install ()
87 internet.Install(c);
88
89 // We create the channels first without any IP addressing information
90 NS_LOG_INFO("Create channels.");
92 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
93 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
94 NetDeviceContainer nd02 = p2p.Install(n02);
95 NetDeviceContainer nd12 = p2p.Install(n12);
96 p2p.SetDeviceAttribute("DataRate", StringValue("1500kbps"));
97 p2p.SetChannelAttribute("Delay", StringValue("10ms"));
98 NetDeviceContainer nd32 = p2p.Install(n32);
99 NetDeviceContainer nd34 = p2p.Install(n34);
100
101 // Later, we add IP addresses.
102 NS_LOG_INFO("Assign IP Addresses.");
104 ipv4.SetBase("10.1.1.0", "255.255.255.0");
105 Ipv4InterfaceContainer i02 = ipv4.Assign(nd02);
106
107 ipv4.SetBase("10.1.2.0", "255.255.255.0");
108 Ipv4InterfaceContainer i12 = ipv4.Assign(nd12);
109
110 ipv4.SetBase("10.1.3.0", "255.255.255.0");
111 Ipv4InterfaceContainer i32 = ipv4.Assign(nd32);
112
113 ipv4.SetBase("10.1.4.0", "255.255.255.0");
114 Ipv4InterfaceContainer i34 = ipv4.Assign(nd34);
115
116 // Create the OnOff application to send UDP datagrams of size
117 // 210 bytes at a rate of 448 Kb/s from n0 to n4
118 NS_LOG_INFO("Create Applications.");
119 uint16_t port = 9; // Discard port (RFC 863)
120
121 OnOffHelper onoff1("ns3::UdpSocketFactory", InetSocketAddress(i34.GetAddress(1), port));
122 onoff1.SetConstantRate(DataRate("448kb/s"));
123
124 ApplicationContainer onOffApp1 = onoff1.Install(c.Get(0));
125 onOffApp1.Start(Seconds(10.0));
126 onOffApp1.Stop(Seconds(20.0));
127
128 // Create a similar flow from n3 to n1, starting at time 1.1 seconds
129 OnOffHelper onoff2("ns3::UdpSocketFactory", InetSocketAddress(i12.GetAddress(0), port));
130 onoff2.SetConstantRate(DataRate("448kb/s"));
131
132 ApplicationContainer onOffApp2 = onoff2.Install(c.Get(3));
133 onOffApp2.Start(Seconds(10.1));
134 onOffApp2.Stop(Seconds(20.0));
135
136 // Create packet sinks to receive these packets
137 PacketSinkHelper sink("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
138 NodeContainer sinks = NodeContainer(c.Get(4), c.Get(1));
139 ApplicationContainer sinkApps = sink.Install(sinks);
140 sinkApps.Start(Seconds(0.0));
141 sinkApps.Stop(Seconds(21.0));
142
143 AsciiTraceHelper ascii;
144 p2p.EnableAsciiAll(ascii.CreateFileStream("simple-point-to-point-olsr.tr"));
145 p2p.EnablePcapAll("simple-point-to-point-olsr");
146
148
149 NS_LOG_INFO("Run Simulation.");
152 NS_LOG_INFO("Done.");
153
154 return 0;
155}
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.
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.
Class for representing data rates.
Definition data-rate.h:78
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.
void Add(const Ipv4RoutingHelper &routing, int16_t priority)
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:31
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
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:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:291
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
Definition olsr.py:1
#define list
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44