From Mr Smith, 1 Month ago, written in C++.
Embed
  1.  
  2. #include "ns3/applications-module.h"
  3. #include "ns3/command-line.h"
  4. #include "ns3/config.h"
  5. #include "ns3/core-module.h"
  6. #include "ns3/internet-module.h"
  7. #include "ns3/internet-stack-helper.h"
  8. #include "ns3/ipv4-address-helper.h"
  9. #include "ns3/ipv4-global-routing-helper.h"
  10. #include "ns3/log.h"
  11. #include "ns3/mobility-helper.h"
  12. #include "ns3/mobility-model.h"
  13. #include "ns3/network-module.h"
  14. #include "ns3/on-off-helper.h"
  15. #include "ns3/packet-sink-helper.h"
  16. #include "ns3/packet-sink.h"
  17. #include "ns3/point-to-point-module.h"
  18. #include "ns3/ssid.h"
  19. #include "ns3/string.h"
  20. #include "ns3/tcp-westwood.h"
  21. #include "ns3/yans-wifi-channel.h"
  22. #include "ns3/yans-wifi-helper.h"
  23. #include "ns3/flow-monitor-module.h"
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. // cd /opt/ns-allinone-3.30.1/ns-3.30.1
  41. // ./waf build --build-profile=debug --enable-examples --enable-tests
  42. // ./waf configure --build-profile=optimized --enable-examples --enable-tests
  43. // ./waf --run '<ns3-program> --arg1=value1 --arg2=value2 ...'
  44. // ./waf --run 'scratch/my-wifi-udp'
  45. // ./waf --run 'my-wifi-udp'
  46.  
  47. NS_LOG_COMPONENT_DEFINE("my-wifi-udp");
  48.  
  49. using namespace ns3;
  50.  
  51. int main(int argc, char* argv[]){
  52.  
  53.         // ##### Set default attribute values #####
  54.         double udp_client_start_time = 0;
  55.         double udp_client_stop_time = 20;
  56.         double udp_server_start_time = 1;
  57.         double udp_server_stop_time = 20;
  58.  
  59.         double simulation_end_time = 1472;
  60.         uint32_t packet_size = 1000; // bytes
  61.  
  62.         double distance = 10;
  63.  
  64.         uint32_t prng_seed = 1;
  65.         uint32_t prng_run = 1;
  66.  
  67.  
  68.         //double dbm_eirp = 1;
  69.  
  70.         StringValue data_mode_mcs_index = StringValue("HtMcs7");
  71.         StringValue control_mode_mcs_index = StringValue("HtMcs0");
  72.        
  73.        
  74.  
  75.         // ##### Configure logging #####
  76.         //LogComponentEnable("UdpEchoClientApplication",LOG_LEVEL_INFO);
  77.         LogComponentEnable("UdpEchoServerApplication",LOG_LEVEL_INFO);
  78.  
  79.  
  80.  
  81.         // ##### Parse command-line arguments #####
  82.         CommandLine cmd;
  83.         cmd.Parse(argc,argv);
  84.  
  85.  
  86.  
  87.         // ##### Configure MRG32k3a PRNG #####
  88.         RngSeedManager::SetSeed (prng_seed);
  89.         RngSeedManager::SetRun (prng_run);
  90.  
  91.  
  92.  
  93.         // ##### Create nodes #####
  94.         NodeContainer nodes;
  95.         nodes.Create(2);
  96.         Ptr<ns3::Node> wifiNodeA = nodes.Get(0);
  97.         Ptr<ns3::Node> wifiNodeB = nodes.Get(1);
  98.  
  99.  
  100.  
  101.  
  102.         // ##### Create a channel which will connect the WifiNetDevices #####
  103.         //PointToPointHelper pointToPoint;
  104.         //pointToPoint.SetDeviceAttribute("DataRate",StringValue("50Mbps"));
  105.         //pointToPoint.SetChannelAttribute("Delay",StringValue("2ms"));
  106.         //pointToPoint.SetEnbAntennaModelType ("ns3::CosineAntennaModel");
  107.         //pointToPoint.SetEnbAntennaModelAttribute ("Orientation", DoubleValue (0));
  108.         //pointToPoint.SetEnbAntennaModelAttribute ("Beamwidth",   DoubleValue (60));
  109.         //pointToPoint.SetEnbAntennaModelAttribute ("MaxGain",     DoubleValue (0.0));
  110.         //NetDeviceContainer devices;
  111.         //devices = pointToPoint.Install(nodes);
  112.         // Create a channel helper and phy helper, and then create the channel
  113.         YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
  114.         YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
  115.         phy.SetChannel (channel.Create ());
  116.  
  117.  
  118.  
  119.  
  120.         // ##### Create WifiNetDevices for nodes #####
  121.         // Create a WifiHelper, which will use the above helpers to create
  122.         // and install Wifi devices.  Configure a Wifi standard to use, which
  123.         // will align various parameters in the Phy and Mac to standard defaults.
  124.         WifiHelper wifi;
  125.         wifi.SetStandard (WIFI_PHY_STANDARD_80211n_5GHZ);
  126.         wifi.SetRemoteStationManager (
  127.                 "ns3::ConstantRateWifiManager",
  128.                 "DataMode",
  129.                 data_mode_mcs_index,
  130.                 "ControlMode",
  131.                 control_mode_mcs_index
  132.         );
  133.         // Declare NetDeviceContainers to hold the container returned by the helper
  134.         NetDeviceContainer wifiNodeADevice;
  135.         NetDeviceContainer wifiNodeBDevice;
  136.  
  137.  
  138.  
  139.         // Perform the installation
  140.         WifiMacHelper mac;
  141.         mac.SetType ("ns3::AdhocWifiMac");
  142.         wifiNodeADevice = wifi.Install (phy, mac, wifiNodeA);
  143.         mac.SetType ("ns3::AdhocWifiMac");
  144.         wifiNodeBDevice = wifi.Install (phy, mac, wifiNodeB);
  145.  
  146.         NetDeviceContainer devices;
  147.         devices.Add(wifiNodeADevice);
  148.         devices.Add(wifiNodeBDevice);
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.         // ##### Set fixed not changing distance between both nodes #####
  156.         MobilityHelper mobility;
  157.         Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  158.         positionAlloc->Add(Vector (0.0, 0.0, 0.0));
  159.         positionAlloc->Add(Vector (distance, 0.0, 0.0));
  160.  
  161.         mobility.SetPositionAllocator (positionAlloc);
  162.         mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  163.         mobility.Install(wifiNodeA);
  164.         mobility.Install(wifiNodeB);
  165.  
  166.  
  167.  
  168.         // ##### Add internet stack to nodes #####
  169.         InternetStackHelper stack;
  170.         stack.Install(nodes);
  171.        
  172.  
  173.  
  174.         // ##### Set arbitrary IP Addresses #####
  175.         Ipv4AddressHelper address;
  176.         address.SetBase("10.42.42.0","255.255.255.0");
  177.         Ipv4InterfaceContainer interfaces = address.Assign(devices);
  178.  
  179.  
  180.  
  181.  
  182.         // ##### Add application for sending UDP Traffic #####
  183.         // server
  184.         UdpEchoServerHelper echoServer(9);
  185.         ApplicationContainer serverApps = echoServer.Install(nodes.Get(1));
  186.         serverApps.Start(Seconds(udp_server_start_time));
  187.         serverApps.Stop(Seconds(udp_server_stop_time));
  188.        
  189.         // client
  190.         UdpEchoClientHelper echoClient(interfaces.GetAddress(1),9);
  191.         echoClient.SetAttribute("MaxPackets",UintegerValue(1000));
  192.         echoClient.SetAttribute("Interval",TimeValue(Seconds(1.0)));
  193.         echoClient.SetAttribute("PacketSize",UintegerValue(packet_size));
  194.         ApplicationContainer clientApps=echoClient.Install(nodes.Get(0));
  195.         clientApps.Start(Seconds(udp_client_start_time));
  196.         clientApps.Stop(Seconds(udp_client_stop_time));
  197.        
  198.  
  199.         // ##### Configure Tracing #####
  200.         // Flow monitor
  201.         Ptr<FlowMonitor> flowMonitor;
  202.         FlowMonitorHelper flowHelper;
  203.         flowMonitor = flowHelper.InstallAll();
  204.  
  205.  
  206.         // ##### Run Simulation #####
  207.         Simulator::Stop(Seconds(simulation_end_time));
  208.         Simulator::Run();
  209.         //Simulator::Stop(Seconds(10));
  210.         //Simulator::Destroy();
  211.  
  212.         flowMonitor->SerializeToXmlFile("/mnt/zwei/dl/flow_out.xml", true, true);
  213.  
  214.  
  215.         return 0;
  216. }
captcha