Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.21 KB | None | 0 0
  1. /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3. * Copyright (c) 2009 The Boeing Company
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation;
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. */
  19.  
  20. // Ruiling Zhang, April 2016
  21. // Use for loop to achieve test different node amount, distance and RSS power
  22. // save the test data to .dat file
  23. // Then use GNUplot to plot figures
  24. // Based on last year student Hedi Krishna's code
  25.  
  26. #include "ns3/core-module.h"
  27. #include "ns3/network-module.h"
  28. #include "ns3/mobility-module.h"
  29. #include "ns3/config-store-module.h"
  30. #include "ns3/wifi-module.h"
  31. #include "ns3/internet-module.h"
  32. #include "ns3/flow-monitor-module.h"
  33. #include "ns3/applications-module.h"
  34.  
  35. #include "ns3/oh-buildings-propagation-loss-model.h"
  36.  
  37. #include <iostream>
  38. #include <fstream>
  39. #include <vector>
  40. #include <string>
  41. #include <limits>
  42. #include <iomanip>
  43.  
  44. using namespace ns3;
  45.  
  46. NS_LOG_COMPONENT_DEFINE ("WifiSimpleInfra");
  47.  
  48. int main (int argc, char *argv[])
  49. {
  50. double simulationTime = 10; //seconds
  51. //==========================
  52. time_t timev;
  53. time(&timev);
  54. RngSeedManager::SetSeed(timev);
  55. RngSeedManager::SetRun (7);
  56. //==========================
  57.  
  58. std::string phyMode ("DsssRate11Mbps");
  59. std::string rtsCts ("150");
  60. double ApStaDistance;
  61. for(ApStaDistance=40.0;ApStaDistance<90.0;ApStaDistance++)
  62. {
  63. //std::string DistanceString = std::to_string(ApStaDistance);
  64. std::ostringstream sstream;
  65. sstream << ApStaDistance;
  66. std::string DistanceString = sstream.str();
  67. //std::string DistanceTail ("]");
  68.  
  69. std::string Distance ="ns3::ConstantRandomVariable[Constant=" + DistanceString + "]";
  70. //std::cout << Distance << '\n';
  71. bool verbose = false;
  72. //double rss = -80; //dbm use for fixdss model
  73.  
  74. uint32_t n=40;
  75. //for(n=1;n<30;n++)
  76. //{
  77. uint32_t payloadSize = 1024;
  78.  
  79. CommandLine cmd;
  80.  
  81. cmd.AddValue ("n", "number of nodes", n);
  82. cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode);
  83. cmd.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose);
  84. cmd.AddValue ("rtsCts", "RTS/CTS threshold", rtsCts);
  85. cmd.AddValue ("Distance", "APSta distance", Distance);
  86. cmd.Parse (argc, argv);
  87.  
  88. // disable fragmentation for frames below 2200 bytes
  89. Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
  90. // turn off RTS/CTS for frames below 'rtsCts' bytes
  91. Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue (rtsCts));
  92. // Fix non-unicast data rate to be the same as that of unicast
  93. Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
  94. StringValue (phyMode));
  95.  
  96. NodeContainer apContainer;
  97. NodeContainer staContainer;
  98. apContainer.Create (1);
  99. staContainer.Create (n);
  100.  
  101. // The below set of helpers will help us to put together the wifi NICs we want
  102. WifiHelper wifi;
  103. if (verbose)
  104. {
  105. wifi.EnableLogComponents (); // Turn on all Wifi logging
  106. }
  107. wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
  108.  
  109. YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  110. // This is one parameter that matters when using FixedRssLossModel
  111. // set it to zero; otherwise, gain will be added
  112. wifiPhy.Set ("RxGain", DoubleValue (0) );
  113. // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
  114. wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
  115.  
  116. YansWifiChannelHelper wifiChannel;
  117. wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
  118. // The below FixedRssLossModel will cause the rss to be fixed regardless
  119. // of the distance between the two stations, and the transmit power
  120. wifiChannel.AddPropagationLoss ("ns3::LogDistancePropagationLossModel"); //change the different channel path loss function
  121. wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue(rss)); //change the different channel path loss function
  122.  
  123. wifiPhy.SetChannel (wifiChannel.Create ());
  124.  
  125. // Add a non-QoS upper mac, and disable rate control
  126. NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
  127. wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
  128. "DataMode",StringValue (phyMode),
  129. "ControlMode",StringValue (phyMode));
  130.  
  131. // Setup the rest of the upper mac
  132. Ssid ssid = Ssid ("wifi-default");
  133. // setup sta.
  134. wifiMac.SetType ("ns3::StaWifiMac",
  135. "Ssid", SsidValue (ssid),
  136. "ActiveProbing", BooleanValue (false));
  137. NetDeviceContainer staDevice = wifi.Install (wifiPhy, wifiMac, staContainer);
  138. //NetDeviceContainer devices = staDevice;
  139.  
  140. // setup ap.
  141. wifiMac.SetType ("ns3::ApWifiMac",
  142. "Ssid", SsidValue (ssid));
  143. NetDeviceContainer apDevice = wifi.Install (wifiPhy, wifiMac, apContainer.Get (0));
  144. //devices.Add (apDevice);
  145.  
  146.  
  147. // Note that with FixedRssLossModel, the positions below are not
  148. // used for received signal strength.
  149. MobilityHelper mobility, mobilityAp;
  150.  
  151. ObjectFactory pos;
  152. pos.SetTypeId ("ns3::RandomDiscPositionAllocator");
  153. pos.Set ("Rho",StringValue (Distance));// setting radius is easier to control distance between AP and Sta
  154. Ptr<PositionAllocator> positionAlloc = pos.Create ()->GetObject<PositionAllocator> ();
  155.  
  156. Ptr<ListPositionAllocator> positionAllocAp = CreateObject<ListPositionAllocator> ();
  157. positionAllocAp->Add (Vector (0.0, 0.0, 0.0));
  158. mobilityAp.SetPositionAllocator (positionAllocAp);
  159. mobilityAp.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  160. mobilityAp.Install (apContainer);
  161.  
  162. mobility.SetPositionAllocator (positionAlloc);
  163. mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  164. mobility.Install (staContainer);
  165.  
  166. InternetStackHelper internet;
  167. internet.Install (apContainer);
  168. internet.Install (staContainer);
  169.  
  170. Ipv4AddressHelper ipv4;
  171. NS_LOG_INFO ("Assign IP Addresses.");
  172. ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  173. Ipv4InterfaceContainer iap = ipv4.Assign (apDevice);
  174. Ipv4InterfaceContainer i = ipv4.Assign (staDevice);
  175.  
  176. //port number, given in array
  177. uint16_t port[n];
  178.  
  179. ApplicationContainer apps[n], sinkApp[n];
  180.  
  181. for( uint16_t a = 0; a < n; a = a + 1 )
  182. {
  183. port[a]=8000+a;
  184. Address apLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port[a]));
  185. PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", apLocalAddress);
  186. sinkApp[a] = packetSinkHelper.Install (apContainer.Get (0));
  187.  
  188. sinkApp[a].Start (Seconds (0.0));
  189. sinkApp[a].Stop (Seconds (simulationTime+1));
  190.  
  191.  
  192. OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
  193.  
  194. onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=10]"));
  195. onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
  196. onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
  197. onoff.SetAttribute ("DataRate", StringValue ("0.5Mbps")); //bit/s set a small datarate for sta in order to observe the relationship between channel capacity and receiver's rate capacity
  198.  
  199. AddressValue remoteAddress (InetSocketAddress (iap.GetAddress (0), port[a]));
  200. onoff.SetAttribute ("Remote", remoteAddress);
  201.  
  202. apps[a].Add (onoff.Install (staContainer.Get (a)));
  203.  
  204. apps[a].Start (Seconds (1.0));
  205. apps[a].Stop (Seconds (simulationTime+1));
  206. }
  207.  
  208.  
  209. FlowMonitorHelper flowmon;
  210. Ptr<FlowMonitor> monitor = flowmon.InstallAll();
  211.  
  212. wifiPhy.EnablePcap ("wifitcp", apDevice);
  213. Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  214.  
  215. Simulator::Stop (Seconds (simulationTime+1));
  216. Simulator::Run ();
  217.  
  218. monitor->CheckForLostPackets ();
  219. double tot=0;
  220. double totsq=0;
  221. //double variance=0;
  222. std::cout << std::fixed;
  223.  
  224. double throughput[2*n]; //for every node, there are 2 flows in TCP
  225. int psent=0;
  226. int preceived=0;
  227.  
  228. Vector pos2;
  229.  
  230. Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
  231. std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
  232. for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
  233. {
  234. Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
  235. throughput[i->first] = i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024;
  236.  
  237. if (t.destinationAddress=="10.1.1.1")
  238. {
  239. int ncount=(i->first)-1;
  240. Ptr<MobilityModel> mob = staContainer.Get(ncount)->GetObject<MobilityModel>();
  241. pos2 = mob->GetPosition ();
  242.  
  243.  
  244. tot=tot+throughput[i->first];
  245. totsq=totsq+pow(throughput[i->first],2);
  246. psent=psent+i->second.txBytes;
  247. preceived=preceived+i->second.rxBytes;
  248. }
  249. }
  250.  
  251. std::cout << ApStaDistance << "\t" << tot/n <<"\n";
  252. Simulator::Destroy ();
  253. }
  254. return 0;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement