Advertisement
Guest User

AODV scratch

a guest
Dec 11th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. #include "ns3/core-module.h"
  2. #include "ns3/aodv-module.h"
  3. #include "ns3/netanim-module.h"
  4. #include "ns3/network-module.h"
  5. #include "ns3/internet-module.h"
  6. #include "ns3/applications-module.h"
  7. #include "ns3/mobility-module.h"
  8. #include "ns3/wifi-module.h"
  9. #include "ns3/flow-monitor-module.h"
  10.  
  11. using namespace ns3;
  12.  
  13. class MyApp : public Application
  14. {
  15. public:
  16.  
  17. MyApp ();
  18. virtual ~MyApp();
  19.  
  20. void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
  21.  
  22. private:
  23. virtual void StartApplication (void);
  24. virtual void StopApplication (void);
  25.  
  26. void ScheduleTx (void);
  27. void SendPacket (void);
  28.  
  29. Ptr<Socket> m_socket;
  30. Address m_peer;
  31. uint32_t m_packetSize;
  32. uint32_t m_nPackets;
  33. DataRate m_dataRate;
  34. EventId m_sendEvent;
  35. bool m_running;
  36. uint32_t m_packetsSent;
  37. };
  38.  
  39. MyApp::MyApp ()
  40. : m_socket (0),
  41. m_peer (),
  42. m_packetSize (0),
  43. m_nPackets (0),
  44. m_dataRate (0),
  45. m_sendEvent (),
  46. m_running (false),
  47. m_packetsSent (0)
  48. {
  49. }
  50.  
  51. MyApp::~MyApp()
  52. {
  53. m_socket = 0;
  54. }
  55.  
  56. void
  57. MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
  58. {
  59. m_socket = socket;
  60. m_peer = address;
  61. m_packetSize = packetSize;
  62. m_nPackets = nPackets;
  63. m_dataRate = dataRate;
  64. }
  65.  
  66. void
  67. MyApp::StartApplication (void)
  68. {
  69. m_running = true;
  70. m_packetsSent = 0;
  71. m_socket->Bind ();
  72. m_socket->Connect (m_peer);
  73. SendPacket ();
  74. }
  75.  
  76. void
  77. MyApp::StopApplication (void)
  78. {
  79. m_running = false;
  80.  
  81. if (m_sendEvent.IsRunning ())
  82. {
  83. Simulator::Cancel (m_sendEvent);
  84. }
  85.  
  86. if (m_socket)
  87. {
  88. m_socket->Close ();
  89. }
  90. }
  91.  
  92. void
  93. MyApp::SendPacket (void)
  94. {
  95. Ptr<Packet> packet = Create<Packet> (m_packetSize);
  96. m_socket->Send (packet);
  97.  
  98. if (++m_packetsSent < m_nPackets)
  99. {
  100. ScheduleTx ();
  101. }
  102. }
  103.  
  104. void
  105. MyApp::ScheduleTx (void)
  106. {
  107. if (m_running)
  108. {
  109. Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
  110. m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
  111. }
  112. }
  113.  
  114. int main (int argc, char* argv[])
  115. {
  116. bool enableFlowMonitor = false;
  117. CommandLine cmd;
  118. cmd.AddValue ("EnableMonitor", "Enable Flow Monitor", enableFlowMonitor);
  119. cmd.Parse (argc, argv);
  120. uint32_t jumlah_node = 10; // jumlah node
  121. double waktu_simulasi = 100; // waktu simulasi
  122. std::string phyMode ("DsssRate1Mbps");
  123.  
  124. Config::SetDefault("ns3::OnOffApplication::PacketSize", StringValue("512")); // ukuran paket dalam bentuk byte
  125.  
  126. // membuat device
  127. // node
  128. NodeContainer node; // semua node
  129. NodeContainer node_aodv;
  130. NodeContainer node_blackhole;
  131. node.Create(jumlah_node);
  132.  
  133. for (int jml = 1; jml <=9; jml++)
  134. {
  135. node_aodv.Add(node.Get(jml));
  136. }
  137.  
  138. node_blackhole.Add(node.Get(0));
  139.  
  140. YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  141.  
  142. // wifi
  143. WifiHelper wifi;
  144.  
  145.  
  146. YansWifiChannelHelper wifiChan = YansWifiChannelHelper::Default ();
  147. wifiPhy.SetChannel(wifiChan.Create());
  148.  
  149. NqosWifiMacHelper mac = NqosWifiMacHelper::Default();
  150. mac.SetType("ns3::AdhocWifiMac");
  151.  
  152. wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
  153.  
  154. wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode",StringValue(phyMode), "ControlMode",StringValue(phyMode));
  155.  
  156. NetDeviceContainer device;
  157. device = wifi.Install(wifiPhy,mac,node);
  158.  
  159. // set protokol routing
  160. AodvHelper aodv;
  161. AodvHelper malicious_aodv;
  162. InternetStackHelper internet;
  163. internet.SetRoutingHelper (aodv);
  164. internet.Install (node_aodv);
  165.  
  166. malicious_aodv.Set("IsMalicious",BooleanValue(true)); // mengaktifkan fungsi black hole
  167. internet.SetRoutingHelper (malicious_aodv);
  168. internet.Install (node_blackhole);
  169.  
  170. // set ip ke semua device
  171. Ipv4AddressHelper ipadd;
  172. ipadd.SetBase("10.1.0.0", "255.255.255.240");
  173. Ipv4InterfaceContainer adhocInterface;
  174. adhocInterface = ipadd.Assign(device);
  175.  
  176. // set mobilitas
  177. MobilityHelper mobilitas;
  178. ObjectFactory pos;
  179. pos.SetTypeId("ns3::RandomRectanglePositionAllocator");
  180. pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=500.0]"));
  181. pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=500.0]"));
  182. Ptr<PositionAllocator> posAlloc = pos.Create()->GetObject<PositionAllocator>();
  183. mobilitas.SetMobilityModel("ns3::RandomWaypointMobilityModel",
  184. "Speed", StringValue ("ns3::UniformRandomVariable[Min=0|Max=60]"),
  185. "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
  186. "PositionAllocator", PointerValue(posAlloc));
  187. mobilitas.SetPositionAllocator (posAlloc);
  188. mobilitas.Install(node_aodv);
  189.  
  190. // set posisi node blackhole
  191. Ptr<ListPositionAllocator> posisiBlackhole = CreateObject <ListPositionAllocator>();
  192. posisiBlackhole ->Add(Vector(250, 250, 0)); // posisi node blackhole
  193. mobilitas.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  194. mobilitas.SetPositionAllocator(posisiBlackhole);
  195. mobilitas.Install(node_blackhole);
  196.  
  197. // set node sumber dan node tujuan
  198. uint32_t port = 5000;
  199. Address sinkAddress (InetSocketAddress (adhocInterface.GetAddress (5), port)); // interface dari node tujuan
  200. PacketSinkHelper packetSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
  201. ApplicationContainer sinkApps = packetSinkHelper.Install (node.Get (5)); //set node tujuan
  202. sinkApps.Start (Seconds (0.0));
  203. sinkApps.Stop (Seconds (waktu_simulasi-10.0));
  204.  
  205. Ptr<Socket> ns3UdpSocket = Socket::CreateSocket (node.Get (3), UdpSocketFactory::GetTypeId ()); //set node sumber
  206. // membuat app udp pada node sumber
  207. Ptr<MyApp> app = CreateObject<MyApp> ();
  208. app->Setup (ns3UdpSocket, sinkAddress, 1040, 5, DataRate ("1024Kbps"));
  209. node.Get (3)->AddApplication (app);
  210. app->SetStartTime (Seconds (10.0));
  211. app->SetStopTime (Seconds (waktu_simulasi-10.0));
  212.  
  213.  
  214. // perhitungan pdr dengan flow monitor
  215. FlowMonitorHelper flowmon;
  216. Ptr<FlowMonitor> monitor = flowmon.InstallAll();
  217.  
  218. Simulator::Stop(Seconds(waktu_simulasi));
  219. wifiPhy.EnablePcap("test", device.Get(0),true);
  220. AnimationInterface anim ("blackhole_rwp_10node.xml");
  221. Simulator::Run();
  222.  
  223. monitor->CheckForLostPackets ();
  224.  
  225. Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
  226. std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
  227. for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
  228. {
  229. Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
  230. if ((t.sourceAddress=="10.1.0.4" && t.destinationAddress == "10.1.0.6"))
  231. {
  232. std::cout << "\n\n=====Output=====\n";
  233. std::cout << "Blackhole 10 node Random Waypoint\n";
  234. std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";
  235. std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
  236. std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
  237. std::cout << " Packet Delivery Ratio: " << ((i->second.rxPackets * 100) / i->second.txPackets) << "%" << "\n";
  238. }
  239. }
  240. Simulator::Destroy();
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement