BowserFlash13

Untitled

Jun 26th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include <ns3/core-module.h>
  2. #include <ns3/network-module.h>
  3. #include <ns3/internet-module.h>
  4. #include <ns3/point-to-point-module.h>
  5. #include <ns3/applications-module.h>
  6.  
  7. using namespace ns3;
  8.  
  9. class MojaAplikacija : public Application
  10. {
  11. public:
  12.  
  13. MojaAplikacija ();
  14. virtual ~MojaAplikacija ();
  15.  
  16. void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
  17.  
  18. private:
  19. virtual void StartApplication (void);
  20. virtual void StopApplication (void);
  21.  
  22. void ScheduleTx (void);
  23. void SendPacket (void);
  24.  
  25. Ptr<Socket> m_socket;
  26. Address m_peer;
  27. uint32_t m_packetSize;
  28. uint32_t m_nPackets;
  29. DataRate m_dataRate;
  30. EventId m_sendEvent;
  31. bool m_running;
  32. uint32_t m_packetsSent;
  33. };
  34.  
  35. MojaAplikacija::MojaAplikacija ()
  36. : m_socket (0),
  37. m_peer (),
  38. m_packetSize (0),
  39. m_nPackets (0),
  40. m_dataRate (0),
  41. m_sendEvent (),
  42. m_running (false),
  43. m_packetsSent (0)
  44. {
  45. }
  46.  
  47. MojaAplikacija::~MojaAplikacija ()
  48. {
  49. m_socket = 0;
  50. }
  51.  
  52. void
  53. MojaAplikacija::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
  54. {
  55. m_socket = socket;
  56. m_peer = address;
  57. m_packetSize = packetSize;
  58. m_nPackets = nPackets;
  59. m_dataRate = dataRate;
  60. }
  61.  
  62. void
  63. MojaAplikacija::StartApplication (void)
  64. {
  65. m_running = true;
  66. m_packetsSent = 0;
  67. m_socket->Bind ();
  68. m_socket->Connect (m_peer);
  69. SendPacket ();
  70. }
  71.  
  72. void
  73. MojaAplikacija::StopApplication (void)
  74. {
  75. m_running = false;
  76.  
  77. if (m_sendEvent.IsRunning ())
  78. {
  79. Simulator::Cancel (m_sendEvent);
  80. }
  81.  
  82. if (m_socket)
  83. {
  84. m_socket->Close ();
  85. }
  86. }
  87.  
  88. void
  89. MojaAplikacija::SendPacket (void)
  90. {
  91. Ptr<Packet> packet = Create<Packet> (m_packetSize);
  92. m_socket->Send (packet);
  93.  
  94. if (++m_packetsSent < m_nPackets)
  95. {
  96. ScheduleTx ();
  97. }
  98. }
  99.  
  100. void
  101. MojaAplikacija::ScheduleTx (void)
  102. {
  103. if (m_running)
  104. {
  105. Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
  106. m_sendEvent = Simulator::Schedule (tNext, &MojaAplikacija::SendPacket, this);
  107. }
  108. }
  109.  
  110. int main ()
  111. {
  112. NodeContainer nodes;
  113. nodes.Create (2);
  114.  
  115. PointToPointHelper pointToPoint;
  116. pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  117. pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
  118.  
  119. NetDeviceContainer devices;
  120. devices = pointToPoint.Install (nodes);
  121.  
  122. InternetStackHelper stack;
  123. stack.Install (nodes);
  124.  
  125. Ipv4AddressHelper address;
  126. address.SetBase ("10.1.1.0", "255.255.255.252");
  127. Ipv4InterfaceContainer interfaces = address.Assign (devices);
  128.  
  129. uint16_t sinkPort = 8080;
  130. PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
  131. ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
  132. sinkApps.Start (Seconds (0.));
  133. sinkApps.Stop (Seconds (20.));
  134.  
  135. Ptr<Socket> ns3TcpSocket = Socket::CreateSocket (nodes.Get (0), TcpSocketFactory::GetTypeId ());
  136. Ptr<MojaAplikacija> app = CreateObject<MojaAplikacija> ();
  137. app->Setup (ns3TcpSocket, InetSocketAddress (interfaces.GetAddress (1), sinkPort), 1024, 500, DataRate ("1Mbps"));
  138. nodes.Get (0)->AddApplication (app);
  139. app->SetStartTime (Seconds (1.));
  140. app->SetStopTime (Seconds (20.));
  141.  
  142. pointToPoint.EnableAsciiAll ("vjezba-vlastita-tcp-app");
  143.  
  144. Simulator::Run ();
  145. Simulator::Destroy ();
  146.  
  147. return 0;
  148. }
Add Comment
Please, Sign In to add comment