A Discrete-Event Network Simulator
API
simple-error-model.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 // Network topology
19 //
20 // n0
21 // \ 5 Mb/s, 2ms
22 // \ 1.5Mb/s, 10ms
23 // n2 -------------------------n3
24 // /
25 // / 5 Mb/s, 2ms
26 // n1
27 //
28 // - all links are point-to-point links with indicated one-way BW/delay
29 // - CBR/UDP flows from n0 to n3, and from n3 to n1
30 // - FTP/TCP flow from n0 to n3, starting at time 1.2 to time 1.35 sec.
31 // - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
32 // (i.e., DataRate of 448,000 bps)
33 // - DropTail queues
34 // - Tracing of queues and packet receptions to file
35 // "simple-error-model.tr"
36 
37 #include <fstream>
38 #include "ns3/core-module.h"
39 #include "ns3/network-module.h"
40 #include "ns3/internet-module.h"
41 #include "ns3/point-to-point-module.h"
42 #include "ns3/applications-module.h"
43 #include "ns3/ipv4-global-routing-helper.h"
44 
45 using namespace ns3;
46 
47 NS_LOG_COMPONENT_DEFINE ("SimpleErrorModelExample");
48 
49 int
50 main (int argc, char *argv[])
51 {
52  // Users may find it convenient to turn on explicit debugging
53  // for selected modules; the below lines suggest how to do this
54 #if 0
55  LogComponentEnable ("SimplePointToPointExample", LOG_LEVEL_INFO);
56 #endif
57 
58 
59  // Set a few attributes
60  Config::SetDefault ("ns3::RateErrorModel::ErrorRate", DoubleValue (0.001));
61  Config::SetDefault ("ns3::RateErrorModel::ErrorUnit", StringValue ("ERROR_UNIT_PACKET"));
62 
63  Config::SetDefault ("ns3::BurstErrorModel::ErrorRate", DoubleValue (0.01));
64  Config::SetDefault ("ns3::BurstErrorModel::BurstSize", StringValue ("ns3::UniformRandomVariable[Min=1|Max=3]"));
65 
66  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210));
67  Config::SetDefault ("ns3::OnOffApplication::DataRate", DataRateValue (DataRate ("448kb/s")));
68 
69  std::string errorModelType = "ns3::RateErrorModel";
70 
71  // Allow the user to override any of the defaults and the above
72  // Bind()s at run-time, via command-line arguments
74  cmd.AddValue("errorModelType", "TypeId of the error model to use", errorModelType);
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.");
80  NodeContainer c;
81  c.Create (4);
82  NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
83  NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
84  NodeContainer n3n2 = NodeContainer (c.Get (3), c.Get (2));
85 
86  InternetStackHelper internet;
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", DataRateValue (DataRate (5000000)));
93  p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
94  NetDeviceContainer d0d2 = p2p.Install (n0n2);
95 
96  NetDeviceContainer d1d2 = p2p.Install (n1n2);
97 
98  p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (1500000)));
99  p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10)));
100  NetDeviceContainer d3d2 = p2p.Install (n3n2);
101 
102  // Later, we add IP addresses.
103  NS_LOG_INFO ("Assign IP Addresses.");
104  Ipv4AddressHelper ipv4;
105  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
106  ipv4.Assign (d0d2);
107 
108  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
109  Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
110 
111  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
112  Ipv4InterfaceContainer i3i2 = ipv4.Assign (d3d2);
113 
114  NS_LOG_INFO ("Use global routing.");
116 
117  // Create the OnOff application to send UDP datagrams of size
118  // 210 bytes at a rate of 448 Kb/s
119  NS_LOG_INFO ("Create Applications.");
120  uint16_t port = 9; // Discard port (RFC 863)
121 
122  OnOffHelper onoff ("ns3::UdpSocketFactory",
123  Address (InetSocketAddress (i3i2.GetAddress (1), port)));
124  onoff.SetConstantRate (DataRate ("448kb/s"));
125  ApplicationContainer apps = onoff.Install (c.Get (0));
126  apps.Start (Seconds (1.0));
127  apps.Stop (Seconds (10.0));
128 
129  // Create an optional packet sink to receive these packets
130  PacketSinkHelper sink ("ns3::UdpSocketFactory",
132  apps = sink.Install (c.Get (2));
133  apps.Start (Seconds (1.0));
134  apps.Stop (Seconds (10.0));
135 
136  // Create a similar flow from n3 to n1, starting at time 1.1 seconds
137  onoff.SetAttribute ("Remote",
139  apps = onoff.Install (c.Get (3));
140  apps.Start (Seconds (1.1));
141  apps.Stop (Seconds (10.0));
142 
143  // Create a packet sink to receive these packets
144  sink.SetAttribute ("Local",
146  apps = sink.Install (c.Get (1));
147  apps.Start (Seconds (1.1));
148  apps.Stop (Seconds (10.0));
149 
150  //
151  // Error model
152  //
153  // Create an ErrorModel based on the implementation (constructor)
154  // specified by the default TypeId
155 
156  ObjectFactory factory;
157  factory.SetTypeId (errorModelType);
158  Ptr<ErrorModel> em = factory.Create<ErrorModel> ();
159  d3d2.Get (0)->SetAttribute ("ReceiveErrorModel", PointerValue (em));
160 
161  // Now, let's use the ListErrorModel and explicitly force a loss
162  // of the packets with pkt-uids = 11 and 17 on node 2, device 0
163  std::list<uint32_t> sampleList;
164  sampleList.push_back (11);
165  sampleList.push_back (17);
166  // This time, we'll explicitly create the error model we want
167  Ptr<ListErrorModel> pem = CreateObject<ListErrorModel> ();
168  pem->SetList (sampleList);
169  d0d2.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (pem));
170 
171  AsciiTraceHelper ascii;
172  p2p.EnableAsciiAll (ascii.CreateFileStream ("simple-error-model.tr"));
173  p2p.EnablePcapAll ("simple-error-model");
174 
175  NS_LOG_INFO ("Run Simulation.");
176  Simulator::Run ();
178  NS_LOG_INFO ("Done.");
179 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
holds a vector of std::pair of Ptr<Ipv4> and interface index.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
NetDeviceContainer Install(NodeContainer c)
Ipv4InterfaceContainer i1i2
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
General error model that can be used to corrupt packets.
Definition: error-model.h:115
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
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 ...
AttributeValue implementation for Time.
Definition: nstime.h:1076
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Hold an unsigned integer type.
Definition: uinteger.h:44
LOG_INFO and above.
Definition: log.h:104
holds a vector of ns3::NetDevice pointers
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
keep track of a set of node pointers.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Instantiate subclasses of ns3::Object.
AttributeValue implementation for Address.
Definition: address.h:278
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
void SetList(const std::list< uint32_t > &packetlist)
Definition: error-model.cc:448
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
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...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
NodeContainer n0n2
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
NodeContainer n1n2
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.