A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-error-model.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 */
5
6// Network topology
7//
8// n0
9// \ 5 Mb/s, 2ms
10// \ 1.5Mb/s, 10ms
11// n2 -------------------------n3
12// /
13// / 5 Mb/s, 2ms
14// n1
15//
16// - all links are point-to-point links with indicated one-way BW/delay
17// - CBR/UDP flows from n0 to n3, and from n3 to n1
18// - FTP/TCP flow from n0 to n3, starting at time 1.2 to time 1.35 sec.
19// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
20// (i.e., DataRate of 448,000 bps)
21// - DropTail queues
22// - Tracing of queues and packet receptions to file
23// "simple-error-model.tr"
24
25#include "ns3/applications-module.h"
26#include "ns3/core-module.h"
27#include "ns3/internet-module.h"
28#include "ns3/ipv4-global-routing-helper.h"
29#include "ns3/network-module.h"
30#include "ns3/point-to-point-module.h"
31
32#include <fstream>
33
34using namespace ns3;
35
36NS_LOG_COMPONENT_DEFINE("SimpleErrorModelExample");
37
38int
39main(int argc, char* argv[])
40{
41 // Users may find it convenient to turn on explicit debugging
42 // for selected modules; the below lines suggest how to do this
43#if 0
44 LogComponentEnable ("SimplePointToPointExample", LOG_LEVEL_INFO);
45#endif
46
47 // Set a few attributes
48 Config::SetDefault("ns3::RateErrorModel::ErrorRate", DoubleValue(0.001));
49 Config::SetDefault("ns3::RateErrorModel::ErrorUnit", StringValue("ERROR_UNIT_PACKET"));
50
51 Config::SetDefault("ns3::BurstErrorModel::ErrorRate", DoubleValue(0.01));
52 Config::SetDefault("ns3::BurstErrorModel::BurstSize",
53 StringValue("ns3::UniformRandomVariable[Min=1|Max=3]"));
54
55 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(210));
56 Config::SetDefault("ns3::OnOffApplication::DataRate", DataRateValue(DataRate("448kb/s")));
57
58 std::string errorModelType = "ns3::RateErrorModel";
59
60 // Allow the user to override any of the defaults and the above
61 // Bind()s at run-time, via command-line arguments
62 CommandLine cmd(__FILE__);
63 cmd.AddValue("errorModelType", "TypeId of the error model to use", errorModelType);
64 cmd.Parse(argc, argv);
65
66 // Here, we will explicitly create four nodes. In more sophisticated
67 // topologies, we could configure a node factory.
68 NS_LOG_INFO("Create nodes.");
70 c.Create(4);
73 NodeContainer n3n2 = NodeContainer(c.Get(3), c.Get(2));
74
76 internet.Install(c);
77
78 // We create the channels first without any IP addressing information
79 NS_LOG_INFO("Create channels.");
81 p2p.SetDeviceAttribute("DataRate", DataRateValue(DataRate(5000000)));
82 p2p.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
83 NetDeviceContainer d0d2 = p2p.Install(n0n2);
84
85 NetDeviceContainer d1d2 = p2p.Install(n1n2);
86
87 p2p.SetDeviceAttribute("DataRate", DataRateValue(DataRate(1500000)));
88 p2p.SetChannelAttribute("Delay", TimeValue(MilliSeconds(10)));
89 NetDeviceContainer d3d2 = p2p.Install(n3n2);
90
91 // Later, we add IP addresses.
92 NS_LOG_INFO("Assign IP Addresses.");
94 ipv4.SetBase("10.1.1.0", "255.255.255.0");
95 ipv4.Assign(d0d2);
96
97 ipv4.SetBase("10.1.2.0", "255.255.255.0");
98 Ipv4InterfaceContainer i1i2 = ipv4.Assign(d1d2);
99
100 ipv4.SetBase("10.1.3.0", "255.255.255.0");
101 Ipv4InterfaceContainer i3i2 = ipv4.Assign(d3d2);
102
103 NS_LOG_INFO("Use global routing.");
105
106 // Create the OnOff application to send UDP datagrams of size
107 // 210 bytes at a rate of 448 Kb/s
108 NS_LOG_INFO("Create Applications.");
109 uint16_t port = 9; // Discard port (RFC 863)
110
111 OnOffHelper onoff("ns3::UdpSocketFactory",
113 onoff.SetConstantRate(DataRate("448kb/s"));
114 ApplicationContainer apps = onoff.Install(c.Get(0));
115 apps.Start(Seconds(1.0));
116 apps.Stop(Seconds(10.0));
117
118 // Create an optional packet sink to receive these packets
119 PacketSinkHelper sink("ns3::UdpSocketFactory",
121 apps = sink.Install(c.Get(2));
122 apps.Start(Seconds(1.0));
123 apps.Stop(Seconds(10.0));
124
125 // Create a similar flow from n3 to n1, starting at time 1.1 seconds
126 onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(i1i2.GetAddress(0), port)));
127 apps = onoff.Install(c.Get(3));
128 apps.Start(Seconds(1.1));
129 apps.Stop(Seconds(10.0));
130
131 // Create a packet sink to receive these packets
132 sink.SetAttribute("Local", AddressValue(InetSocketAddress(Ipv4Address::GetAny(), port)));
133 apps = sink.Install(c.Get(1));
134 apps.Start(Seconds(1.1));
135 apps.Stop(Seconds(10.0));
136
137 //
138 // Error model
139 //
140 // Create an ErrorModel based on the implementation (constructor)
141 // specified by the default TypeId
142
143 ObjectFactory factory;
144 factory.SetTypeId(errorModelType);
145 Ptr<ErrorModel> em = factory.Create<ErrorModel>();
146 d3d2.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue(em));
147
148 // Now, let's use the ListErrorModel and explicitly force a loss
149 // of the packets with pkt-uids = 11 and 17 on node 2, device 0
150 std::list<uint64_t> sampleList;
151 sampleList.push_back(11);
152 sampleList.push_back(17);
153 // This time, we'll explicitly create the error model we want
155 pem->SetList(sampleList);
156 d0d2.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(pem));
157
158 AsciiTraceHelper ascii;
159 p2p.EnableAsciiAll(ascii.CreateFileStream("simple-error-model.tr"));
160 p2p.EnablePcapAll("simple-error-model");
161
162 NS_LOG_INFO("Run Simulation.");
165 NS_LOG_INFO("Done.");
166
167 return 0;
168}
NodeContainer n1n2
Nodecontainer n1 + n2.
Ipv4InterfaceContainer i1i2
IPv4 interface container i1 + i2.
NodeContainer n0n2
Nodecontainer n0 + n2.
a polymophic address class
Definition address.h:90
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
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
General error model that can be used to corrupt packets.
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()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
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.
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44