PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/csma/examples/csma-packet-socket.cc

https://github.com/scarletttu/ns-3-codel-dev
C++ | 134 lines | 74 code | 23 blank | 37 comment | 0 complexity | 511a0436885e6f25a81a1487c313e31c MD5 | raw file
Possible License(s): GPL-2.0
  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 n1 n2 n3
  20. // | | | |
  21. // =====================
  22. //
  23. // - Two packet socket flows: from n0 to n1 and from n3 to n0
  24. // - Default 512 byte packets generated by traffic generator
  25. // - Output from the PacketSink trace source will be sent to the
  26. // csma-packet-socket-sink.tr file
  27. // ASCII trace output will be sent to the csma-packet-socket.tr file
  28. #include <iostream>
  29. #include <fstream>
  30. #include <string>
  31. #include <cassert>
  32. #include "ns3/core-module.h"
  33. #include "ns3/network-module.h"
  34. #include "ns3/csma-module.h"
  35. #include "ns3/applications-module.h"
  36. using namespace ns3;
  37. NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample");
  38. std::ofstream g_os;
  39. static void
  40. SinkRx (std::string path, Ptr<const Packet> p, const Address &address)
  41. {
  42. g_os << p->GetSize () << std::endl;
  43. }
  44. int
  45. main (int argc, char *argv[])
  46. {
  47. #if 0
  48. LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO);
  49. #endif
  50. CommandLine cmd;
  51. cmd.Parse (argc, argv);
  52. g_os.open ("csma-packet-socket-sink.tr",std::ios_base::binary | std::ios_base::out);
  53. // Here, we will explicitly create four nodes.
  54. NS_LOG_INFO ("Create nodes.");
  55. NodeContainer nodes;
  56. nodes.Create (4);
  57. PacketSocketHelper packetSocket;
  58. packetSocket.Install (nodes);
  59. // create the shared medium used by all csma devices.
  60. NS_LOG_INFO ("Create channels.");
  61. Ptr<CsmaChannel> channel = CreateObjectWithAttributes<CsmaChannel> (
  62. "DataRate", DataRateValue (DataRate (5000000)),
  63. "Delay", TimeValue (MilliSeconds (2)));
  64. // use a helper function to connect our nodes to the shared channel.
  65. NS_LOG_INFO ("Build Topology.");
  66. CsmaHelper csma;
  67. csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
  68. NetDeviceContainer devs = csma.Install (nodes, channel);
  69. NS_LOG_INFO ("Create Applications.");
  70. // Create the OnOff application to send raw datagrams
  71. PacketSocketAddress socket;
  72. socket.SetSingleDevice (devs.Get (0)->GetIfIndex ());
  73. socket.SetPhysicalAddress (devs.Get (1)->GetAddress ());
  74. socket.SetProtocol (2);
  75. OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
  76. onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1.0)));
  77. onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0)));
  78. ApplicationContainer apps = onoff.Install (nodes.Get (0));
  79. apps.Start (Seconds (1.0));
  80. apps.Stop (Seconds (10.0));
  81. socket.SetSingleDevice (devs.Get (3)->GetIfIndex ());
  82. socket.SetPhysicalAddress (devs.Get (0)->GetAddress ());
  83. socket.SetProtocol (3);
  84. onoff.SetAttribute ("Remote", AddressValue (socket));
  85. onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0)));
  86. apps = onoff.Install (nodes.Get (3));
  87. apps.Start (Seconds (1.0));
  88. apps.Stop (Seconds (10.0));
  89. // Install packet sink on node 0 to receive packets from node 1
  90. PacketSinkHelper sink = PacketSinkHelper ("ns3::PacketSocketFactory",
  91. socket);
  92. apps = sink.Install (nodes.Get (0));
  93. apps.Start (Seconds (0.0));
  94. apps.Stop (Seconds (20.0));
  95. // While the below trace sink is hooked to all nodes (the wildcard "*")
  96. // only one packet sink (on node 0) is actually added above, so
  97. // only the receive events on node 0 will be traced
  98. Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
  99. MakeCallback (&SinkRx));
  100. // Configure tracing of all enqueue, dequeue, and NetDevice receive events
  101. // Trace output will be sent to the csma-packet-socket.tr file
  102. NS_LOG_INFO ("Configure Tracing.");
  103. AsciiTraceHelper ascii;
  104. csma.EnableAsciiAll (ascii.CreateFileStream ("csma-packet-socket.tr"));
  105. NS_LOG_INFO ("Run Simulation.");
  106. Simulator::Run ();
  107. Simulator::Destroy ();
  108. NS_LOG_INFO ("Done.");
  109. g_os.close ();
  110. return 0;
  111. }