NS3 Smiulation === comment >this is comment [name=林曉君] ## 用來比較的ad-hoc: olsr、aodv、dsdv ### olsr 一種主動鏈路狀態路由協議,使用hello和拓撲控制消息來發現並在整個移動自組織網絡中傳播鏈路狀態信息 ![](https://i.imgur.com/mqbhNNy.jpg) ### aodv 是應用於無線隨意網路(也稱作無線Ad hoc網路)中進行路由選擇的路由協議,它能夠實現單播和多播路由 ### dsdv 一種主動的、表驅動的 MANET 路由協議。它使用跳數作為路由選擇的度量標準。 DSDV 更新消息由三個字段組成,目的地址、序列號和跳數。 有定期更新(15s)跟觸發更新 ### Code * Create container * Create the backbone wifi * Add the IPv4 protocol * UDP datagrams of size 210 bytes * rate of 10 Kb/s ``` #include "ns3/command-line.h" #include "ns3/string.h" #include "ns3/yans-wifi-helper.h" #include "ns3/ssid.h" #include "ns3/mobility-helper.h" #include "ns3/internet-stack-helper.h" #include "ns3/ipv4-address-helper.h" #include "ns3/on-off-helper.h" #include "ns3/yans-wifi-channel.h" #include "ns3/qos-txop.h" #include "ns3/packet-sink-helper.h" #include "ns3/olsr-helper.h" #include "ns3/csma-helper.h" #include "ns3/animation-interface.h" #include "ns3/aodv-helper.h"//Alex #include "ns3/dsdv-helper.h"//Alex #include "ns3/random-variable-stream.h" #include "ns3/random-waypoint-mobility-model.h" using namespace ns3; using namespace std; string AdHocRoutingProtocol="aodv";//aodv,olsr,dsdv // // Define logging keyword for this file // NS_LOG_COMPONENT_DEFINE ("MixedWireless"); // // This function will be used below as a trace sink, if the command-line // argument or default value "useCourseChangeCallback" is set to true // static void CourseChangeCallback (std::string path, Ptr<const MobilityModel> model) { Vector position = model->GetPosition (); std::cout << "CourseChange " << path << " x=" << position.x << ", y=" << position.y << ", z=" << position.z << std::endl; } int main (int argc, char *argv[]) { // // First, we declare and initialize a few local variables that control some // simulation parameters. // uint32_t backboneNodes = 10; uint32_t infraNodes = 2 ; uint32_t lanNodes = 2; uint32_t stopTime = 20; bool useCourseChangeCallback = false; // // Simulation defaults are typically set next, before command line // arguments are parsed. // Config::SetDefault ("ns3::OnOffApplication::PacketSize", StringValue ("1472")); Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("100kb/s")); // // For convenience, we add the local variables to the command line argument // system so that they can be overridden with flags such as // "--backboneNodes=20" // CommandLine cmd (__FILE__); cmd.AddValue ("backboneNodes", "number of backbone nodes", backboneNodes); cmd.AddValue ("infraNodes", "number of leaf nodes", infraNodes); cmd.AddValue ("lanNodes", "number of LAN nodes", lanNodes); cmd.AddValue ("stopTime", "simulation stop time (seconds)", stopTime); cmd.AddValue ("useCourseChangeCallback", "whether to enable course change tracing", useCourseChangeCallback); // // The system global variables and the local values added to the argument // system can be overridden by command line arguments by using this call. // cmd.Parse (argc, argv); if (stopTime < 10) { std::cout << "Use a simulation stop time >= 10 seconds" << std::endl; exit (1); } // Construct the backbone NodeContainer backbone; backbone.Create (backboneNodes); WifiHelper wifi; WifiMacHelper mac; mac.SetType ("ns3::AdhocWifiMac"); wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate54Mbps")); YansWifiPhyHelper wifiPhy; wifiPhy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO); YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); wifiPhy.SetChannel (wifiChannel.Create ()); NetDeviceContainer backboneDevices = wifi.Install (wifiPhy, mac, backbone); InternetStackHelper internet; if(AdHocRoutingProtocol=="olsr") { // We enable OLSR (which will be consulted at a higher priority than // the global routing) on the backbone ad hoc nodes NS_LOG_INFO ("Enabling OLSR routing on all backbone nodes"); OlsrHelper olsr; internet.SetRoutingHelper (olsr); }else if(AdHocRoutingProtocol=="aodv") { NS_LOG_INFO ("Enabling aodv routing on all backbone nodes"); AodvHelper aodv; Internet. SetRoutingHelper (aodv); }else if(AdHocRoutingProtocol=="dsdv") { NS_LOG_INFO ("Enabling dsdv routing on all backbone nodes"); DsdvHelper dsdv; internet.SetRoutingHelper (dsdv); } // Add the IPv4 protocol stack to the nodes in our container internet.Install (backbone); Ipv4AddressHelper ipAddrs; ipAddrs.SetBase ("192.168.0.0", "255.255.255.0"); ipAddrs.Assign (backboneDevices); MobilityHelper mobility; mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "MinX", DoubleValue (20.0), "MinY", DoubleValue (20.0), "DeltaX", DoubleValue (20.0), "DeltaY", DoubleValue (20.0), "GridWidth", UintegerValue (5), "LayoutType", StringValue ("RowFirst")); mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel", "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)), "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.1|Max=100]"), // "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=10]"), "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); mobility.Install (backbone); // Application configuration NS_LOG_INFO ("Create Applications."); uint16_t port = 9; // Discard port (RFC 863) backboneNodes = 9; Ptr<Node> appSource = NodeList::GetNode (backboneNodes); uint32_t lastNodeIndex = backboneNodes + backboneNodes * (lanNodes - 1 ) + backboneNodes * (infraNodes - 1 ) - 1 ; lastNodeIndex = 9 ; Ptr<Node> appSink = NodeList::GetNode (lastNodeIndex); Ipv4Address remoteAddr = appSink->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal (); OnOffHelper onoff ("ns3::UdpSocketFactory", Address (InetSocketAddress (remoteAddr, port))); ApplicationContainer apps = onoff.Install (appSource); apps.Start (Seconds (3)); apps.Stop (Seconds (stopTime - 1)); PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port)); apps = sink.Install (appSink); apps.Start (Seconds (3)); // Tracing configuration NS_LOG_INFO ("Configure Tracing."); CsmaHelper csma; // Let's set up some ns-2-like ascii traces, using another helper class AsciiTraceHelper ascii; Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("trace.tr"); wifiPhy.EnableAsciiAll (stream); csma.EnableAsciiAll (stream); internet.EnableAsciiIpv4All (stream); // Csma captures in non-promiscuous mode csma.EnablePcapAll ("mixed-wireless", false); // pcap captures on the backbone wifi devices wifiPhy.EnablePcap ("mixed-wireless", backboneDevices, false); // pcap trace on the application data sink wifiPhy.EnablePcap ("mixed-wireless", appSink->GetId (), 0); if (useCourseChangeCallback == true) { Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange", MakeCallback (&CourseChangeCallback)); } AnimationInterface anim ("mixed-wireless.xml"); // Run simulation NS_LOG_INFO ("Run Simulation."); Simulator::Stop (Seconds (stopTime)); Simulator::Run (); Simulator::Destroy (); } ```