Advertisement
abrar1

Untitled

Feb 19th, 2022
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.20 KB | None | 0 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. #include "ns3/command-line.h"
  18. #include "ns3/config.h"
  19. #include "ns3/string.h"
  20. #include "ns3/log.h"
  21. #include "ns3/yans-wifi-helper.h"
  22. #include "ns3/ssid.h"
  23. #include "ns3/mobility-helper.h"
  24. #include "ns3/on-off-helper.h"
  25. #include "ns3/yans-wifi-channel.h"
  26. #include "ns3/mobility-model.h"
  27. #include "ns3/packet-sink.h"
  28. #include "ns3/packet-sink-helper.h"
  29. #include "ns3/tcp-westwood.h"
  30. #include "ns3/internet-stack-helper.h"
  31. #include "ns3/ipv4-address-helper.h"
  32. #include "ns3/ipv4-global-routing-helper.h"
  33.  
  34. #include "ns3/core-module.h"
  35. #include "ns3/network-module.h"
  36. #include "ns3/csma-module.h"
  37. #include "ns3/internet-module.h"
  38. #include "ns3/point-to-point-module.h"
  39. #include "ns3/applications-module.h"
  40. #include "ns3/ipv4-global-routing-helper.h"
  41. #include "ns3/flow-monitor-module.h"
  42. #include "ns3/flow-monitor-helper.h"
  43.  
  44.  
  45.  
  46.  
  47. // Default Network Topology
  48. //
  49. // 10.3.0.0
  50. // n5 n4 n3 n2 n0 ---------------- n1 n6 n7 n8 n9-> server
  51. // | | | | | point-to-point | | | | |
  52. // ===================== ====================
  53. // senders 10.1.0.0 receivers 10.2.0.0
  54. //n0 is client
  55.  
  56. using namespace ns3;
  57.  
  58. NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
  59.  
  60. Ptr<PacketSink> sink; /* Pointer to the packet sink application */
  61. uint64_t lastTotalRx = 0; /* The value of the last total received bytes */
  62. double total = 0;
  63. int n = 0;
  64.  
  65. class MyApp : public Application
  66. {
  67. public:
  68. MyApp ();
  69. virtual ~MyApp ();
  70.  
  71. /**
  72. * Register this type.
  73. * \return The TypeId.
  74. */
  75. static TypeId GetTypeId (void);
  76. void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
  77.  
  78. private:
  79. virtual void StartApplication (void);
  80. virtual void StopApplication (void);
  81.  
  82. void ScheduleTx (void);
  83. void SendPacket (void);
  84.  
  85. Ptr<Socket> m_socket;
  86. Address m_peer;
  87. uint32_t m_packetSize;
  88. uint32_t m_nPackets;
  89. DataRate m_dataRate;
  90. EventId m_sendEvent;
  91. bool m_running;
  92. uint32_t m_packetsSent;
  93. };
  94.  
  95. MyApp::MyApp ()
  96. : m_socket (0),
  97. m_peer (),
  98. m_packetSize (0),
  99. m_nPackets (0),
  100. m_dataRate (0),
  101. m_sendEvent (),
  102. m_running (false),
  103. m_packetsSent (0)
  104. {
  105. }
  106.  
  107. MyApp::~MyApp ()
  108. {
  109. m_socket = 0;
  110. }
  111.  
  112. /* static */
  113. TypeId MyApp::GetTypeId (void)
  114. {
  115. static TypeId tid = TypeId ("MyApp")
  116. .SetParent<Application> ()
  117. .SetGroupName ("Tutorial")
  118. .AddConstructor<MyApp> ()
  119. ;
  120. return tid;
  121. }
  122.  
  123. void
  124. MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
  125. {
  126. m_socket = socket;
  127. m_peer = address;
  128. m_packetSize = packetSize;
  129. m_nPackets = nPackets;
  130. m_dataRate = dataRate;
  131. }
  132.  
  133. void
  134. MyApp::StartApplication (void)
  135. {
  136. m_running = true;
  137. m_packetsSent = 0;
  138. if (InetSocketAddress::IsMatchingType (m_peer))
  139. {
  140. m_socket->Bind ();
  141. }
  142. else
  143. {
  144. m_socket->Bind6 ();
  145. }
  146. m_socket->Connect (m_peer);
  147. SendPacket ();
  148. }
  149.  
  150. void
  151. MyApp::StopApplication (void)
  152. {
  153. m_running = false;
  154.  
  155. if (m_sendEvent.IsRunning ())
  156. {
  157. Simulator::Cancel (m_sendEvent);
  158. }
  159.  
  160. if (m_socket)
  161. {
  162. m_socket->Close ();
  163. }
  164. }
  165.  
  166. void
  167. MyApp::SendPacket (void)
  168. {
  169. Ptr<Packet> packet = Create<Packet> (m_packetSize);
  170. m_socket->Send (packet);
  171.  
  172. if (++m_packetsSent < m_nPackets)
  173. {
  174. ScheduleTx ();
  175. }
  176. }
  177.  
  178. void
  179. MyApp::ScheduleTx (void)
  180. {
  181. if (m_running)
  182. {
  183. Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
  184. m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
  185. }
  186. }
  187.  
  188. // static void
  189. // CwndChange (Ptr<OutputStreamWrapper> stream, uint32_t oldCwnd, uint32_t newCwnd)
  190. // {
  191. // NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
  192. // *stream->GetStream () << Simulator::Now ().GetSeconds () << "\t" << oldCwnd << "\t" << newCwnd << std::endl;
  193. // // }
  194.  
  195. // static void
  196. // RxDrop (Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
  197. // {
  198. // NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());
  199. // file->Write (Simulator::Now (), p);
  200. // }
  201.  
  202.  
  203. void
  204. CalculateThroughput ()
  205. {
  206. Time now = Simulator::Now (); /* Return the simulator's virtual time. */
  207. double cur = (sink->GetTotalRx () - lastTotalRx) * (double) 8 / 1e5; /* Convert Application RX Packets to MBits. */
  208. std::cout << now.GetSeconds () << "s: \t" << cur << " Mbit/s" << std::endl;
  209. total += cur ;
  210. n++;
  211. lastTotalRx = sink->GetTotalRx ();
  212. Simulator::Schedule (MilliSeconds (100), &CalculateThroughput);
  213. }
  214.  
  215. int
  216. main (int argc, char *argv[])
  217. {
  218. Config::SetDefault ("ns3::TcpL4Protocol::SocketType", StringValue ("ns3::TcpNewReno"));
  219. Time::SetResolution (Time::NS);
  220. LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  221. LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
  222.  
  223. bool useV6 = false ;
  224. uint nosenders = 4;
  225. uint noreceivers = 4;
  226. //empty node containers
  227. NodeContainer routers, senders, receivers;
  228. routers.Create(2);
  229. senders.Create(nosenders);
  230. receivers.Create(noreceivers);
  231.  
  232. PointToPointHelper p2pHR, p2pRR ;
  233.  
  234. p2pHR.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  235. p2pHR.SetChannelAttribute ("Delay", StringValue ("2ms"));
  236.  
  237. p2pRR.SetDeviceAttribute ("DataRate", StringValue ("1.5Mbps"));
  238. p2pRR.SetChannelAttribute ("Delay", StringValue ("50ms"));
  239.  
  240. //Empty containers
  241. NetDeviceContainer routerDevices = p2pRR.Install(routers);
  242. NetDeviceContainer leftRouterDevices, rightRouterDevices, senderDevices, receiverDevices;
  243.  
  244. //Adding links
  245. for(uint i = 0 ; i<nosenders ; i++){
  246. //left side links
  247. NetDeviceContainer cleft = p2pHR.Install(routers.Get(0),senders.Get(i));
  248. leftRouterDevices.Add(cleft.Get(0));
  249. senderDevices.Add(cleft.Get(1));
  250.  
  251. //right side links
  252. NetDeviceContainer cright = p2pHR.Install(routers.Get(1), receivers.Get(i));
  253. rightRouterDevices.Add(cright.Get(0));
  254. receiverDevices.Add(cright.Get(1));
  255. }
  256.  
  257.  
  258. //Install Internet Stack
  259. InternetStackHelper stack;
  260. stack.Install(routers);
  261. stack.Install(senders);
  262. stack.Install(receivers);
  263.  
  264. //Adding IP address
  265. Ipv4AddressHelper routerIP = Ipv4AddressHelper("10.3.0.0","255.255.255.0");
  266. Ipv4AddressHelper senderIP = Ipv4AddressHelper("10.1.0.0","255.255.255.0");
  267. Ipv4AddressHelper receiverIP = Ipv4AddressHelper("10.2.0.0","255.255.255.0");
  268.  
  269. Ipv4InterfaceContainer routerIFC, senderIFCs, receiverIFCs, leftRouterIFCs, rightRouterIFCs;
  270.  
  271. //assign ipv4
  272.  
  273. routerIFC = routerIP.Assign(routerDevices);
  274.  
  275.  
  276. for(uint i = 0; i < nosenders ; ++i) {
  277. NetDeviceContainer senderDevice;
  278. senderDevice.Add(senderDevices.Get(i));
  279. senderDevice.Add(leftRouterDevices.Get(i));
  280. Ipv4InterfaceContainer senderIFC = senderIP.Assign(senderDevice);
  281. senderIFCs.Add(senderIFC.Get(0));
  282. leftRouterIFCs.Add(senderIFC.Get(1));
  283. //Increment the network number and reset the IP address counter
  284. //to the base value provided in the SetBase method.
  285. senderIP.NewNetwork();
  286.  
  287. NetDeviceContainer receiverDevice;
  288. receiverDevice.Add(receiverDevices.Get(i));
  289. receiverDevice.Add(rightRouterDevices.Get(i));
  290. Ipv4InterfaceContainer receiverIFC = receiverIP.Assign(receiverDevice);
  291. receiverIFCs.Add(receiverIFC.Get(0));
  292. rightRouterIFCs.Add(receiverIFC.Get(1));
  293. receiverIP.NewNetwork();
  294. }
  295.  
  296.  
  297. uint16_t sinkPort = 8080;
  298. Address sinkAddress;
  299. Address anyAddress;
  300. std::string probeType;
  301. std::string tracePath;
  302. if (useV6 == false)
  303. {
  304. sinkAddress = InetSocketAddress (receiverIFCs.GetAddress(0), sinkPort);
  305. anyAddress = InetSocketAddress (Ipv4Address::GetAny (), sinkPort);
  306. probeType = "ns3::Ipv4PacketProbe";
  307. tracePath = "/NodeList/*/$ns3::Ipv4L3Protocol/Tx";
  308. }
  309. else
  310. {
  311. //sinkAddress = Inet6SocketAddress (interfaces.GetAddress (1,1), sinkPort);
  312. anyAddress = Inet6SocketAddress (Ipv6Address::GetAny (), sinkPort);
  313. probeType = "ns3::Ipv6PacketProbe";
  314. tracePath = "/NodeList/*/$ns3::Ipv6L3Protocol/Tx";
  315. }
  316.  
  317. // topology done
  318.  
  319. // UdpEchoServerHelper echoServer(1234);
  320.  
  321. // ApplicationContainer serverApps = echoServer.Install (receivers.Get (nosenders-1));
  322. // serverApps.Start (Seconds (1.0));
  323. // serverApps.Stop (Seconds (10.0));
  324.  
  325. // UdpEchoClientHelper echoClient (senderIFCs.GetAddress (0), 1234);
  326. // echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
  327. // echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  328. // echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
  329.  
  330. // ApplicationContainer clientApps = echoClient.Install (senders.Get (nosenders-1));
  331. // clientApps.Start (Seconds (2.0));
  332. // clientApps.Stop (Seconds (10.0));
  333.  
  334. //PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", anyAddress);
  335. //ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
  336. //sinkApps.Start (Seconds (0.));
  337. //sinkApps.Stop (Seconds (20.));
  338. Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  339.  
  340. for(int i=0;i<3;i++)
  341. {
  342. sinkAddress = InetSocketAddress (receiverIFCs.GetAddress(i), sinkPort);
  343. PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", anyAddress);
  344. ApplicationContainer sinkApps = packetSinkHelper.Install (receivers.Get (i));
  345. sink = StaticCast<PacketSink>(sinkApps.Get(0));
  346.  
  347. OnOffHelper server("ns3::TcpSocketFactory",(InetSocketAddress(receiverIFCs.GetAddress(i),sinkPort)));
  348. server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
  349. server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
  350. server.SetAttribute ("PacketSize", UintegerValue (1000));
  351. server.SetAttribute ("DataRate", StringValue ("100Mbps"));
  352.  
  353. ApplicationContainer serverApp = server.Install(senders.Get(i));
  354. sinkApps.Start (Seconds (0.));
  355. serverApp.Start(Seconds(1.0));
  356. }
  357.  
  358. // // Ptr<MyApp> app = CreateObject<MyApp>()PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", anyAddress);
  359. // ApplicationContainer sinkApps = packetSinkHelper.Install (routers.Get (0));
  360. // sink = StaticCast<PacketSink>(sinkApps.Get(0));
  361.  
  362. // OnOffHelper server("ns3::TcpSocketFactory",(InetSocketAddress(routerIFC.GetAddress(0),sinkPort)));
  363. // server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
  364. // server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
  365. // server.SetAttribute ("PacketSize", UintegerValue (1000));
  366. // server.SetAttribute ("DataRate", StringValue ("100Mbps"));
  367.  
  368. // ApplicationContainer serverApp = server.Install(routers.Get(1));
  369. // sinkApps.Start (Seconds (0.));
  370. // serverApp.Start(Seconds(1.0));;
  371. // app->Setup (ns3TcpSocket, sinkAddress, 1040, 1000, DataRate ("100Mbps"));
  372. // app->SetStartTime (Seconds(1.));
  373. // app->SetStopTime (Seconds (20.));
  374.  
  375.  
  376.  
  377. Simulator::Schedule(Seconds(1.1),&CalculateThroughput);
  378.  
  379. Ptr<FlowMonitor> flowMonitor;
  380. FlowMonitorHelper flowHelper;
  381. flowMonitor = flowHelper.InstallAll();
  382. flowMonitor->CheckForLostPackets();
  383.  
  384.  
  385.  
  386. Simulator::Stop (Seconds (2));
  387. Simulator::Run ();
  388. flowMonitor->SerializeToXmlFile("flowMonitor.xml", false, true);
  389. Simulator::Destroy ();
  390.  
  391. std::cout<<"Average is : "<<(total)/(n)<<std::endl;
  392. return 0;
  393.  
  394. }
  395.  
  396.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement