PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/s2016/ns3-routing/ns-3/examples/error-model/simple-error-model.cc

https://gitlab.com/pmaddi/cs433
C++ | 173 lines | 88 code | 29 blank | 56 comment | 0 complexity | 9f4f9b98623118e5fc0d29c4c3caf659 MD5 | raw 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. // Network topology
  18. //
  19. // n0
  20. // \ 5 Mb/s, 2ms
  21. // \ 1.5Mb/s, 10ms
  22. // n2 -------------------------n3
  23. // /
  24. // / 5 Mb/s, 2ms
  25. // n1
  26. //
  27. // - all links are point-to-point links with indicated one-way BW/delay
  28. // - CBR/UDP flows from n0 to n3, and from n3 to n1
  29. // - FTP/TCP flow from n0 to n3, starting at time 1.2 to time 1.35 sec.
  30. // - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
  31. // (i.e., DataRate of 448,000 bps)
  32. // - DropTail queues
  33. // - Tracing of queues and packet receptions to file
  34. // "simple-error-model.tr"
  35. #include <fstream>
  36. #include "ns3/core-module.h"
  37. #include "ns3/common-module.h"
  38. #include "ns3/simulator-module.h"
  39. #include "ns3/node-module.h"
  40. #include "ns3/helper-module.h"
  41. using namespace ns3;
  42. NS_LOG_COMPONENT_DEFINE ("SimpleErrorModelExample");
  43. int
  44. main (int argc, char *argv[])
  45. {
  46. // Users may find it convenient to turn on explicit debugging
  47. // for selected modules; the below lines suggest how to do this
  48. #if 0
  49. LogComponentEnable ("SimplePointToPointExample", LOG_LEVEL_INFO);
  50. #endif
  51. // Set a few attributes
  52. Config::SetDefault ("ns3::RateErrorModel::ErrorRate", DoubleValue (0.01));
  53. Config::SetDefault ("ns3::RateErrorModel::ErrorUnit", StringValue ("EU_PKT"));
  54. Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210));
  55. Config::SetDefault ("ns3::OnOffApplication::DataRate", DataRateValue (DataRate ("448kb/s")));
  56. // Allow the user to override any of the defaults and the above
  57. // Bind()s at run-time, via command-line arguments
  58. CommandLine cmd;
  59. cmd.Parse (argc, argv);
  60. // Here, we will explicitly create four nodes. In more sophisticated
  61. // topologies, we could configure a node factory.
  62. NS_LOG_INFO ("Create nodes.");
  63. NodeContainer c;
  64. c.Create (4);
  65. NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
  66. NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
  67. NodeContainer n3n2 = NodeContainer (c.Get (3), c.Get (2));
  68. InternetStackHelper internet;
  69. internet.Install (c);
  70. // We create the channels first without any IP addressing information
  71. NS_LOG_INFO ("Create channels.");
  72. PointToPointHelper p2p;
  73. p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (5000000)));
  74. p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
  75. NetDeviceContainer d0d2 = p2p.Install (n0n2);
  76. NetDeviceContainer d1d2 = p2p.Install (n1n2);
  77. p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (1500000)));
  78. p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10)));
  79. NetDeviceContainer d3d2 = p2p.Install (n3n2);
  80. // Later, we add IP addresses.
  81. NS_LOG_INFO ("Assign IP Addresses.");
  82. Ipv4AddressHelper ipv4;
  83. ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  84. ipv4.Assign (d0d2);
  85. ipv4.SetBase ("10.1.2.0", "255.255.255.0");
  86. Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
  87. ipv4.SetBase ("10.1.3.0", "255.255.255.0");
  88. Ipv4InterfaceContainer i3i2 = ipv4.Assign (d3d2);
  89. NS_LOG_INFO ("Use global routing.");
  90. Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  91. // Create the OnOff application to send UDP datagrams of size
  92. // 210 bytes at a rate of 448 Kb/s
  93. NS_LOG_INFO ("Create Applications.");
  94. uint16_t port = 9; // Discard port (RFC 863)
  95. OnOffHelper onoff ("ns3::UdpSocketFactory",
  96. Address (InetSocketAddress (i3i2.GetAddress (1), port)));
  97. onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable(1)));
  98. onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable(0)));
  99. ApplicationContainer apps = onoff.Install (c.Get (0));
  100. apps.Start(Seconds(1.0));
  101. apps.Stop (Seconds(10.0));
  102. // Create an optional packet sink to receive these packets
  103. PacketSinkHelper sink ("ns3::UdpSocketFactory",
  104. Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
  105. apps = sink.Install (c.Get (2));
  106. apps.Start (Seconds (1.0));
  107. apps.Stop (Seconds (10.0));
  108. // Create a similar flow from n3 to n1, starting at time 1.1 seconds
  109. onoff.SetAttribute ("Remote",
  110. AddressValue (InetSocketAddress (i1i2.GetAddress (0), port)));
  111. apps = onoff.Install (c.Get (3));
  112. apps.Start(Seconds(1.1));
  113. apps.Stop (Seconds(10.0));
  114. // Create a packet sink to receive these packets
  115. sink.SetAttribute ("Local",
  116. AddressValue (InetSocketAddress (Ipv4Address::GetAny (), port)));
  117. apps = sink.Install (c.Get (1));
  118. apps.Start (Seconds (1.1));
  119. apps.Stop (Seconds (10.0));
  120. //
  121. // Error model
  122. //
  123. // Create an ErrorModel based on the implementation (constructor)
  124. // specified by the default classId
  125. Ptr<RateErrorModel> em = CreateObjectWithAttributes<RateErrorModel> ("RanVar", RandomVariableValue (UniformVariable (0.0, 1.0)),
  126. "ErrorRate", DoubleValue (0.001));
  127. d3d2.Get (0)->SetAttribute ("ReceiveErrorModel", PointerValue (em));
  128. // Now, let's use the ListErrorModel and explicitly force a loss
  129. // of the packets with pkt-uids = 11 and 17 on node 2, device 0
  130. std::list<uint32_t> sampleList;
  131. sampleList.push_back (11);
  132. sampleList.push_back (17);
  133. // This time, we'll explicitly create the error model we want
  134. Ptr<ListErrorModel> pem = CreateObject<ListErrorModel> ();
  135. pem->SetList (sampleList);
  136. d0d2.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (pem));
  137. AsciiTraceHelper ascii;
  138. p2p.EnableAsciiAll (ascii.CreateFileStream ("simple-error-model.tr"));
  139. p2p.EnablePcapAll ("simple-error-model");
  140. NS_LOG_INFO ("Run Simulation.");
  141. Simulator::Run ();
  142. Simulator::Destroy ();
  143. NS_LOG_INFO ("Done.");
  144. }