Guest User

Untitled

a guest
Mar 6th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. void
  2. RoutingExperiment::Run (double txp, std::string CSVfileName)
  3. {
  4.     Packet::EnablePrinting ();
  5.     m_txp = txp;
  6.    
  7.     std::cout<<"Setting various parameters\n";
  8.     //Number of cars
  9.     int nWifis = 0;
  10.     int dist=24;
  11.     //Simulation time
  12.     double TotalTime = 990000.0;
  13.     //Road
  14.     uint32_t road = 4000;
  15.     uint32_t bord= (road/300)-1;
  16.    
  17.     //Other parameters
  18.     std::string rate ("2048bps");
  19.     std::string phyMode ("DsssRate11Mbps");
  20.     Config::SetDefault  ("ns3::OnOffApplication::PacketSize",StringValue ("64"));
  21.     Config::SetDefault ("ns3::OnOffApplication::DataRate",  StringValue (rate));
  22.     Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",StringValue (phyMode));
  23.  
  24.     NodeContainer adhocNodes;
  25.     MobilityHelper mobility;
  26.     Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  27.    
  28.     std::cout<<"Creating nodes\n";
  29.    
  30.     //Left side
  31.     uint32_t borders=0, cont=0;
  32.     while (cont<bord)
  33.     {
  34.         adhocNodes.Add(CreateObject<Node> ());
  35.         positionAlloc->Add (Vector (0.0, (cont+1)*300, 0.0));
  36.         cont++;
  37.         borders++;
  38.         nWifis++;
  39.     }
  40.     cont=0;
  41.     //Bottom side
  42.     while (cont<bord)
  43.     {
  44.         adhocNodes.Add(CreateObject<Node> ());
  45.         positionAlloc->Add (Vector ((cont+1)*300, road, 0.0));
  46.         cont++;
  47.         borders++;
  48.         nWifis++;
  49.     }
  50.     cont=0;
  51.     //Right side
  52.     while (cont<bord)
  53.     {
  54.         adhocNodes.Add(CreateObject<Node> ());
  55.         positionAlloc->Add (Vector (road, (cont+1)*300, 0.0));
  56.         cont++;
  57.         borders++;
  58.         nWifis++;
  59.     }
  60.     cont=0;
  61.     //Top side
  62.     while (cont<bord)
  63.     {
  64.         adhocNodes.Add(CreateObject<Node> ());
  65.         positionAlloc->Add (Vector ((cont+1)*300, 0.0, 0.0));
  66.         cont++;
  67.         borders++;
  68.         nWifis++;
  69.     }
  70.     //Other nodes
  71.     for (uint i=0; i<bord; i++)
  72.     {
  73.         int r=road;
  74.         int d=dist;
  75.         while(r>0)
  76.         {  
  77.             adhocNodes.Add(CreateObject<Node> ());
  78.             positionAlloc->Add (Vector ((i+1)*300, d, 0.0));
  79.             borders++;
  80.             d=d+dist;
  81.             r=r-dist;
  82.             nWifis++;
  83.         }
  84.     }
  85.     for (uint i=0; i<bord; i++)
  86.     {
  87.         int r=road;
  88.         int d=dist;
  89.         while(r>0)
  90.         {  
  91.             adhocNodes.Add(CreateObject<Node> ());
  92.             positionAlloc->Add (Vector (d, (i+1)*300, 0.0));
  93.             borders++;
  94.             d=d+dist;
  95.             r=r-dist;
  96.             nWifis++;
  97.         }
  98.     }
  99.    
  100.     std::cout<<"Nodes created\n";
  101.     //Nodes with constant speed
  102.     std::cout<<"Setting mobility model\n";
  103.     mobility.SetPositionAllocator (positionAlloc);
  104.     mobility.SetMobilityModel ("ns3::ConstantVelocityMobilityModel");
  105.     mobility.Install (adhocNodes);
  106.     for (uint n=0 ; n < adhocNodes.GetN() ; n++)
  107.     {
  108.         Ptr<ConstantVelocityMobilityModel> mob = adhocNodes.Get(n)->GetObject<ConstantVelocityMobilityModel>();
  109.         //mob->SetVelocity(Vector(-1, 0, 0));
  110.         mob->SetVelocity(Vector(0, 0, 0));
  111.     }  
  112.     std::cout<<"Mobility model set\n";
  113.  
  114.     //Setting up wifi phy and channel using helpers
  115.     WifiHelper wifi;
  116.     wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
  117.  
  118.     //Setting max range
  119.     YansWifiPhyHelper wifiPhy =  YansWifiPhyHelper::Default ();
  120.     YansWifiChannelHelper wifiChannel;
  121.     wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
  122.     wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (RoutingExperiment::m_range+100));
  123.     wifiPhy.SetChannel (wifiChannel.Create ());
  124.  
  125.     //Add a mac and disable rate control
  126.     WifiMacHelper wifiMac;
  127.     wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
  128.     "DataMode",StringValue (phyMode),
  129.     "ControlMode",StringValue (phyMode));
  130.     wifiPhy.Set ("TxPowerStart",DoubleValue (txp));
  131.     wifiPhy.Set ("TxPowerEnd", DoubleValue (txp));
  132.     wifiMac.SetType ("ns3::AdhocWifiMac");
  133.     NetDeviceContainer adhocDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes);
  134.    
  135.     InternetStackHelper internet;
  136.     internet.Install (adhocNodes);
  137.     NS_LOG_INFO ("Sssigning ip address");
  138.  
  139.     Ipv4AddressHelper addressAdhoc;
  140.     addressAdhoc.SetBase ("10.1.0.0", "255.255.0.0");
  141.     Ipv4InterfaceContainer adhocInterfaces;
  142.     adhocInterfaces = addressAdhoc.Assign (adhocDevices);
  143.  
  144.     OnOffHelper onoff1 ("ns3::UdpSocketFactory",Address ());
  145.     onoff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
  146.     onoff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
  147.  
  148.     std::cout<<"Setting connections\n";
  149.     //Receiver j (da sistemare un attimo)
  150.     for (int j = 0; j < nWifis; j++)
  151.     {
  152.         Ptr<Socket> sink = SetupPacketReceive (adhocInterfaces.GetAddress (j), adhocNodes.Get (j));
  153.         AddressValue remoteAddress (InetSocketAddress (ns3::Ipv4Address::GetAny (), port));
  154.         onoff1.SetAttribute ("Remote", remoteAddress);
  155.         Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable> ();    
  156.     }
  157.     //Sender i
  158.     for (int i = 0; i < nWifis; i++)
  159.     {  
  160.         //Sender i>broadcast
  161.         Ptr<Socket> sender = SetupPacketSend (ns3::Ipv4Address("10.1.255.255"),  adhocNodes.Get (i));
  162.         sender->SetAllowBroadcast (true);
  163.         //Add the socket to the node
  164.         adhocNodes.Get(i)->setBroadcast(sender);   
  165.     }
  166.     std::cout<<"Connections set\n";
  167.    
  168.        
  169.     //Generate alert message
  170.     uint start=2275;
  171.     Simulator::ScheduleWithContext (adhocNodes.Get (start)->GetId (), Seconds (45000), &RoutingExperiment::GenerateTraffic, adhocNodes.Get (start), adhocNodes);
  172.     std::cout<<"Alert scheduled\n";
  173.     std::cout<<"Run Simulation\n\n----------------------------------------\n";
  174.     NS_LOG_INFO ("Run Simulation.");
  175.  
  176.     Simulator::Stop (Seconds (TotalTime));
  177.    
  178.     Simulator::Run ();
  179.    
  180.     Simulator::Destroy ();
  181. }
Advertisement
Add Comment
Please, Sign In to add comment