Guest User

Untitled

a guest
Mar 20th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4.  
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <dirent.h>
  8.  
  9. #include "ns3/log.h"
  10.  
  11. #include "ns3/data-rate.h"
  12. #include "ns3/core-module.h"
  13. #include "ns3/mobility-module.h"
  14.  
  15. #include "ns3/internet-module.h"
  16. #include "ns3/internet-stack-helper.h"
  17. #include "ns3/ipv4-address-helper.h"
  18. #include "ns3/ipv4-interface-container.h"
  19. #include "ns3/ipv4-flow-classifier.h"
  20.  
  21. #include "ns3/wave-net-device.h"
  22. #include "ns3/wave-mac-helper.h"
  23. #include "ns3/wave-helper.h"
  24.  
  25. #include "ns3/applications-module.h"
  26. #include "ns3/flow-monitor-helper.h"
  27.  
  28. #define NODE_COUNT 2
  29. #define DEFAULT_SIM_TIME 10.0
  30.  
  31. using namespace ns3;
  32.  
  33. #define MODULE_NAME "WaveTestSuite"
  34.  
  35. //!< Specifies max data rate with which UDP stream will be send
  36. //!< Should be higher or equal WiFi data rate (m_dataRate)
  37. #define MAX_UDP_DATA_RATE "30000kb/s"
  38.  
  39. NS_LOG_COMPONENT_DEFINE (MODULE_NAME);
  40.  
  41. class WaveTestSuite
  42. {
  43. public:
  44. WaveTestSuite(std::string dataRate);
  45.  
  46. void Run ();
  47.  
  48. private:
  49. /// >>
  50. void CreateNodes ();
  51. void SetUpMobilityModel ();
  52. void CreateDevices ();
  53. void InstallInternetStack ();
  54. void InstallApplications (double simTime);
  55. void Report ();
  56. void PrepareAnimationInterface ();
  57. void PrepareReportGeneration ();
  58. /// <<
  59.  
  60. bool PrepareFolderForResults ();
  61. void FinalizeReport ();
  62. bool CheckParams ();
  63.  
  64. int32_t m_distance;
  65. std::string m_dataRate;
  66.  
  67. std::ostringstream m_prefix;
  68. std::ostringstream m_folderName;
  69. std::ofstream m_reportFile;
  70. NodeContainer m_nodes;
  71. NetDeviceContainer m_devices;
  72. Ipv4InterfaceContainer m_ipv4Interfaces;
  73.  
  74. FlowMonitorHelper m_flowMonHelper;
  75. Ptr<FlowMonitor> m_monitor;
  76.  
  77. double m_rxBt;
  78. double m_lastT;
  79.  
  80. double m_reportTime;
  81. };
  82.  
  83. WaveTestSuite::WaveTestSuite (std::string dataRate):
  84. m_distance (200),
  85. m_dataRate (dataRate),
  86. m_prefix(MODULE_NAME),
  87. m_folderName("./"),
  88. m_rxBt (0),
  89. m_lastT (0),
  90. m_reportTime (1.)
  91. {
  92. }
  93.  
  94. void
  95. WaveTestSuite::CreateNodes ()
  96. {
  97. m_nodes = NodeContainer();
  98. m_nodes.Create (NODE_COUNT);
  99. }
  100.  
  101. void
  102. WaveTestSuite::SetUpMobilityModel ()
  103. {
  104. MobilityHelper mobility;
  105.  
  106. Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  107.  
  108. positionAlloc->Add (Vector (0, 0, 0.0));
  109. positionAlloc->Add (Vector (0, m_distance, 0.0));
  110. mobility.SetPositionAllocator (positionAlloc);
  111. mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  112. mobility.Install (m_nodes);
  113. }
  114.  
  115. void
  116. WaveTestSuite::CreateDevices ()
  117. {
  118. YansWifiChannelHelper waveChannel = YansWifiChannelHelper::Default ();
  119.  
  120. YansWavePhyHelper wavePhy = YansWavePhyHelper::Default ();
  121. wavePhy.SetChannel (waveChannel.Create ());
  122. wavePhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11);
  123.  
  124. /// 44.8 dBm as defined in 802.11p for US
  125. wavePhy.Set ("TxPowerStart", DoubleValue (44.8) );
  126. wavePhy.Set ("TxPowerEnd", DoubleValue (44.8) );
  127.  
  128. QosWaveMacHelper waveMac = QosWaveMacHelper::Default ();
  129. WaveHelper waveHelper = WaveHelper::Default ();
  130. waveHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
  131. "DataMode", StringValue (m_dataRate),
  132. "ControlMode",StringValue (m_dataRate),
  133. "NonUnicastMode", StringValue (m_dataRate));
  134.  
  135. m_devices = waveHelper.Install (wavePhy, waveMac, m_nodes);
  136.  
  137. const SchInfo schInfo = SchInfo (SCH3, true, EXTENDED_CONTINUOUS);
  138. const TxProfile txProfile = TxProfile (SCH3);
  139. // >>>
  140. // Replace the previous line with the next one to check that user is able to specify Data Rate
  141. // const TxProfile txProfile = TxProfile (SCH3, true, 4, m_dataRate);
  142. // <<<
  143. for (NetDeviceContainer::Iterator i = m_devices.Begin (); i != m_devices.End (); ++i)
  144. {
  145. Ptr<NetDevice> device = *i;
  146.  
  147. WaveNetDevice * waveNetDevice = static_cast<WaveNetDevice*>(GetPointer (device));
  148.  
  149. Simulator::Schedule(Seconds (0.0), &WaveNetDevice::StartSch, waveNetDevice, schInfo);
  150. Simulator::Schedule(Seconds (0.0), &WaveNetDevice::RegisterTxProfile, waveNetDevice, txProfile);
  151. }
  152.  
  153. // wavePhy.EnablePcapAll ("waveTxProfileTest.pcap");
  154. }
  155.  
  156. void
  157. WaveTestSuite::InstallInternetStack ()
  158. {
  159. InternetStackHelper internet;
  160. internet.Install (m_nodes);
  161.  
  162. NS_LOG_INFO ("Assign IP Addresses.");
  163.  
  164. Ipv4AddressHelper ipv4;
  165. ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  166.  
  167. m_ipv4Interfaces = ipv4.Assign (m_devices);
  168. }
  169.  
  170. void
  171. WaveTestSuite::InstallApplications (double simTime)
  172. {
  173. // BulkSendHelper sourceHelper ("ns3::UdpSocketFactory", InetSocketAddress (m_ipv4Interfaces.GetAddress (m_nodes.GetN () - 1), 50001));
  174. // sourceHelper.SetAttribute ("SendSize", UintegerValue (1024));
  175. // sourceHelper.SetAttribute ("MaxBytes", UintegerValue (0));
  176. // sourceHelper.SetAttribute ("DataRate", DataRateValue (DataRate (MAX_UDP_DATA_RATE)));
  177. // ApplicationContainer sourceApp = sourceHelper.Install (m_nodes.Get (0));
  178.  
  179. OnOffHelper onOffHelper ("ns3::UdpSocketFactory", InetSocketAddress (m_ipv4Interfaces.GetAddress (m_nodes.GetN () - 1), 50001));
  180. onOffHelper.SetAttribute ("DataRate", DataRateValue (DataRate (MAX_UDP_DATA_RATE)));
  181. onOffHelper.SetAttribute ("PacketSize", UintegerValue (1024));
  182. onOffHelper.SetAttribute ("MaxBytes", UintegerValue (0));
  183. onOffHelper.SetAttribute ("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
  184. onOffHelper.SetAttribute ("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=10.0]"));
  185.  
  186. ApplicationContainer sourceApp = onOffHelper.Install (m_nodes.Get (0));
  187. PacketSinkHelper dlPacketSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 50001));
  188. sourceApp.Add(dlPacketSinkHelper.Install (m_nodes.Get (m_nodes.GetN () - 1)));
  189. sourceApp.Start (Seconds (0.));
  190. sourceApp.Stop (Seconds (simTime));
  191. }
  192.  
  193. void
  194. WaveTestSuite::Report ()
  195. {
  196. m_monitor->CheckForLostPackets ();
  197.  
  198. Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (m_flowMonHelper.GetClassifier ());
  199. m_monitor->SerializeToXmlFile(m_folderName.str () + "flowmon_" + m_prefix.str () + ".xml", true, true);
  200.  
  201. std::map<FlowId, FlowMonitor::FlowStats> stats = m_monitor->GetFlowStats ();
  202.  
  203. std::ostringstream reportStream;
  204.  
  205. for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator iter = stats.begin ();iter != stats.end ();iter++)
  206. {
  207. Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (iter->first);
  208.  
  209. if (iter->first == 1)
  210. {
  211. if(iter->second.rxPackets==0)
  212. {
  213. reportStream << "Time: " << Simulator::Now () << " Src Addr " << t.sourceAddress << " Throughput: 0 Kbps\n";
  214. }
  215. else
  216. {
  217. float th = iter->second.rxBytes * 8.0 / (iter->second.timeLastRxPacket.GetSeconds() - iter->second.timeFirstRxPacket.GetSeconds()) / 1024;
  218. float th2;
  219. if (iter->second.timeLastRxPacket.GetSeconds() - m_lastT > 0)
  220. {
  221. th2 = (iter->second.rxBytes - m_rxBt) * 8.0 / (iter->second.timeLastRxPacket.GetSeconds() - m_lastT) / 1024;
  222. }
  223. else
  224. {
  225. // No packets were received since last report
  226. th2 = 0;
  227. }
  228.  
  229. reportStream << "Time: " << Simulator::Now () << " Src Addr " << t.sourceAddress << " AveThroughput: " << th << " Kbps";
  230. reportStream << " Throughput: " << th2 << " Kbps";
  231. }
  232.  
  233. m_rxBt = iter->second.rxBytes;
  234. m_lastT = iter->second.timeLastRxPacket.GetSeconds();
  235. }
  236. }
  237.  
  238. m_reportFile << reportStream.str () << "\n";
  239. NS_LOG_INFO (reportStream.str ());
  240.  
  241. Simulator::Schedule(Seconds(m_reportTime), &WaveTestSuite::Report, this);
  242. }
  243.  
  244. void
  245. WaveTestSuite::PrepareReportGeneration ()
  246. {
  247. m_monitor = m_flowMonHelper.Install(m_nodes);
  248. m_reportFile.open (m_folderName.str () + "report_" + m_prefix.str () + ".txt");
  249.  
  250. Simulator::Schedule (Seconds (m_reportTime), &WaveTestSuite::Report, this);
  251. }
  252.  
  253. void
  254. WaveTestSuite::FinalizeReport ()
  255. {
  256. m_reportFile.close ();
  257. }
  258.  
  259. void
  260. WaveTestSuite::Run ()
  261. {
  262. CreateNodes ();
  263. SetUpMobilityModel ();
  264. CreateDevices ();
  265. InstallInternetStack ();
  266. InstallApplications (DEFAULT_SIM_TIME);
  267.  
  268. PrepareReportGeneration ();
  269.  
  270. Simulator::Stop (Seconds (DEFAULT_SIM_TIME));
  271. Simulator::Run ();
  272. Simulator::Destroy ();
  273.  
  274. FinalizeReport ();
  275. }
  276.  
  277. int main (int argc, char *argv[])
  278. {
  279. WaveTestSuite testSuite ("OfdmRate3MbpsBW10MHz");
  280.  
  281. LogComponentEnable(MODULE_NAME, LOG_LEVEL_ALL);
  282. LogComponentEnable(MODULE_NAME, LOG_PREFIX_ALL);
  283.  
  284. testSuite.Run ();
  285. }
Advertisement
Add Comment
Please, Sign In to add comment