Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ns3/core-module.h"
- #include "ns3/aodv-module.h"
- #include "ns3/netanim-module.h"
- #include "ns3/network-module.h"
- #include "ns3/internet-module.h"
- #include "ns3/applications-module.h"
- #include "ns3/mobility-module.h"
- #include "ns3/wifi-module.h"
- #include "ns3/flow-monitor-module.h"
- using namespace ns3;
- class MyApp : public Application
- {
- public:
- MyApp ();
- virtual ~MyApp();
- void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
- private:
- virtual void StartApplication (void);
- virtual void StopApplication (void);
- void ScheduleTx (void);
- void SendPacket (void);
- Ptr<Socket> m_socket;
- Address m_peer;
- uint32_t m_packetSize;
- uint32_t m_nPackets;
- DataRate m_dataRate;
- EventId m_sendEvent;
- bool m_running;
- uint32_t m_packetsSent;
- };
- MyApp::MyApp ()
- : m_socket (0),
- m_peer (),
- m_packetSize (0),
- m_nPackets (0),
- m_dataRate (0),
- m_sendEvent (),
- m_running (false),
- m_packetsSent (0)
- {
- }
- MyApp::~MyApp()
- {
- m_socket = 0;
- }
- void
- MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
- {
- m_socket = socket;
- m_peer = address;
- m_packetSize = packetSize;
- m_nPackets = nPackets;
- m_dataRate = dataRate;
- }
- void
- MyApp::StartApplication (void)
- {
- m_running = true;
- m_packetsSent = 0;
- m_socket->Bind ();
- m_socket->Connect (m_peer);
- SendPacket ();
- }
- void
- MyApp::StopApplication (void)
- {
- m_running = false;
- if (m_sendEvent.IsRunning ())
- {
- Simulator::Cancel (m_sendEvent);
- }
- if (m_socket)
- {
- m_socket->Close ();
- }
- }
- void
- MyApp::SendPacket (void)
- {
- Ptr<Packet> packet = Create<Packet> (m_packetSize);
- m_socket->Send (packet);
- if (++m_packetsSent < m_nPackets)
- {
- ScheduleTx ();
- }
- }
- void
- MyApp::ScheduleTx (void)
- {
- if (m_running)
- {
- Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
- m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
- }
- }
- int main (int argc, char* argv[])
- {
- bool enableFlowMonitor = false;
- CommandLine cmd;
- cmd.AddValue ("EnableMonitor", "Enable Flow Monitor", enableFlowMonitor);
- cmd.Parse (argc, argv);
- uint32_t jumlah_node = 10; // jumlah node
- double waktu_simulasi = 100; // waktu simulasi
- std::string phyMode ("DsssRate1Mbps");
- Config::SetDefault("ns3::OnOffApplication::PacketSize", StringValue("512")); // ukuran paket dalam bentuk byte
- // membuat device
- // node
- NodeContainer node; // semua node
- NodeContainer node_aodv;
- NodeContainer node_blackhole;
- node.Create(jumlah_node);
- for (int jml = 1; jml <=9; jml++)
- {
- node_aodv.Add(node.Get(jml));
- }
- node_blackhole.Add(node.Get(0));
- YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
- // wifi
- WifiHelper wifi;
- YansWifiChannelHelper wifiChan = YansWifiChannelHelper::Default ();
- wifiPhy.SetChannel(wifiChan.Create());
- NqosWifiMacHelper mac = NqosWifiMacHelper::Default();
- mac.SetType("ns3::AdhocWifiMac");
- wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
- wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode",StringValue(phyMode), "ControlMode",StringValue(phyMode));
- NetDeviceContainer device;
- device = wifi.Install(wifiPhy,mac,node);
- // set protokol routing
- AodvHelper aodv;
- AodvHelper malicious_aodv;
- InternetStackHelper internet;
- internet.SetRoutingHelper (aodv);
- internet.Install (node_aodv);
- malicious_aodv.Set("IsMalicious",BooleanValue(true)); // mengaktifkan fungsi black hole
- internet.SetRoutingHelper (malicious_aodv);
- internet.Install (node_blackhole);
- // set ip ke semua device
- Ipv4AddressHelper ipadd;
- ipadd.SetBase("10.1.0.0", "255.255.255.240");
- Ipv4InterfaceContainer adhocInterface;
- adhocInterface = ipadd.Assign(device);
- // set mobilitas
- MobilityHelper mobilitas;
- ObjectFactory pos;
- pos.SetTypeId("ns3::RandomRectanglePositionAllocator");
- pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=500.0]"));
- pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=500.0]"));
- Ptr<PositionAllocator> posAlloc = pos.Create()->GetObject<PositionAllocator>();
- mobilitas.SetMobilityModel("ns3::RandomWaypointMobilityModel",
- "Speed", StringValue ("ns3::UniformRandomVariable[Min=0|Max=60]"),
- "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
- "PositionAllocator", PointerValue(posAlloc));
- mobilitas.SetPositionAllocator (posAlloc);
- mobilitas.Install(node_aodv);
- // set posisi node blackhole
- Ptr<ListPositionAllocator> posisiBlackhole = CreateObject <ListPositionAllocator>();
- posisiBlackhole ->Add(Vector(250, 250, 0)); // posisi node blackhole
- mobilitas.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
- mobilitas.SetPositionAllocator(posisiBlackhole);
- mobilitas.Install(node_blackhole);
- // set node sumber dan node tujuan
- uint32_t port = 5000;
- Address sinkAddress (InetSocketAddress (adhocInterface.GetAddress (5), port)); // interface dari node tujuan
- PacketSinkHelper packetSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
- ApplicationContainer sinkApps = packetSinkHelper.Install (node.Get (5)); //set node tujuan
- sinkApps.Start (Seconds (0.0));
- sinkApps.Stop (Seconds (waktu_simulasi-10.0));
- Ptr<Socket> ns3UdpSocket = Socket::CreateSocket (node.Get (3), UdpSocketFactory::GetTypeId ()); //set node sumber
- // membuat app udp pada node sumber
- Ptr<MyApp> app = CreateObject<MyApp> ();
- app->Setup (ns3UdpSocket, sinkAddress, 1040, 5, DataRate ("1024Kbps"));
- node.Get (3)->AddApplication (app);
- app->SetStartTime (Seconds (10.0));
- app->SetStopTime (Seconds (waktu_simulasi-10.0));
- // perhitungan pdr dengan flow monitor
- FlowMonitorHelper flowmon;
- Ptr<FlowMonitor> monitor = flowmon.InstallAll();
- Simulator::Stop(Seconds(waktu_simulasi));
- wifiPhy.EnablePcap("test", device.Get(0),true);
- AnimationInterface anim ("blackhole_rwp_10node.xml");
- Simulator::Run();
- monitor->CheckForLostPackets ();
- Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
- std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
- for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
- {
- Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
- if ((t.sourceAddress=="10.1.0.4" && t.destinationAddress == "10.1.0.6"))
- {
- std::cout << "\n\n=====Output=====\n";
- std::cout << "Blackhole 10 node Random Waypoint\n";
- std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";
- std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
- std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
- std::cout << " Packet Delivery Ratio: " << ((i->second.rxPackets * 100) / i->second.txPackets) << "%" << "\n";
- }
- }
- Simulator::Destroy();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement