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