Advertisement
Guest User

changinc CWmin in ns-3

a guest
Jun 4th, 2010
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.81 KB | None | 0 0
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3.  * Copyright (c) 2010 IITP RAS
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License version 2 as
  7.  * published by the Free Software Foundation;
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  *
  18.  * Authors: Pavel Boyko <boyko@iitp.ru>
  19.  */
  20.  
  21. /*
  22.  * Classical hidden terminal problem and its RTS/CTS solution.
  23.  *
  24.  * Topology: [node 0] <-- -50 dB --> [node 1] <-- -50 dB --> [node 2]
  25.  *
  26.  * This example illustrates the use of
  27.  *  - Wifi in ad-hoc mode
  28.  *  - Matrix propagation loss model
  29.  *  - Use of OnOffApplication to generate CBR stream
  30.  *  - IP flow monitor
  31.  */
  32.  
  33. /* THIS IS NOT THE ORIGINAL EXAMPLE. I AM MAKING MODIFICATIONS TO LEARN NS-3. jaume.barcelo@uc3m.es */
  34. #include "ns3/core-module.h"
  35. #include "ns3/common-module.h"
  36. #include "ns3/node-module.h"
  37. #include "ns3/helper-module.h"
  38. #include "ns3/mobility-module.h"
  39. #include "ns3/flow-monitor-module.h"
  40. #include "ns3/contrib-module.h"
  41.  
  42.  
  43. using namespace ns3;
  44.  
  45. /// Run single 10 seconds experiment with enabled or disabled RTS/CTS mechanism
  46. void experiment (bool enableCtsRts)
  47. {
  48.   // 0. Enable or disable CTS/RTS
  49.   UintegerValue ctsThr = (enableCtsRts ? UintegerValue (100) : UintegerValue (2200));
  50.   Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", ctsThr);
  51.  
  52.   // 1. Create 3 nodes
  53.   NodeContainer nodes;
  54.   nodes.Create (3);
  55.  
  56.   // 2. Place nodes somehow, this is required by every wireless simulation
  57.   for (size_t i = 0; i < 3; ++i)
  58.     {
  59.       nodes.Get(i)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
  60.     }
  61.  
  62.   // 3. Create propagation loss matrix
  63.   Ptr<MatrixPropagationLossModel> lossModel = CreateObject<MatrixPropagationLossModel> ();
  64.   lossModel->SetDefaultLoss (200); // set default loss to 200 dB (no link)
  65.   lossModel->SetLoss (nodes.Get (0), nodes.Get (1), 50); // set symmetric loss 0 <-> 1 to 50 dB
  66.   lossModel->SetLoss (nodes.Get (2), nodes.Get (1), 50); // set symmetric loss 2 <-> 1 to 50 dB
  67.  
  68.   // 4. Create & setup wifi channel
  69.   Ptr<YansWifiChannel> wifiChannel = CreateObject <YansWifiChannel> ();
  70.   wifiChannel->SetPropagationLossModel (lossModel);
  71.   wifiChannel->SetPropagationDelayModel (CreateObject <ConstantSpeedPropagationDelayModel> ());
  72.    
  73.   // 5. Install wireless devices
  74.   WifiHelper wifi;
  75.   wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
  76.                                 "DataMode",StringValue ("wifib-2mbs"),
  77.                                 "ControlMode",StringValue ("wifib-1mbs"));
  78.   YansWifiPhyHelper wifiPhy =  YansWifiPhyHelper::Default ();
  79.   wifiPhy.SetChannel (wifiChannel);
  80.   NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
  81.   wifiMac.SetType ("ns3::AdhocWifiMac"); // use ad-hoc MAC
  82.   NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
  83.   Config::Set ("/NodeList/0/DeviceList/0/Mac/Dcf/MinCw", UintegerValue (15));
  84.  
  85.  
  86.   // 6. Install TCP/IP stack & assign IP addresses
  87.   InternetStackHelper internet;
  88.   internet.Install (nodes);
  89.   Ipv4AddressHelper ipv4;
  90.   ipv4.SetBase ("10.0.0.0", "255.0.0.0");
  91.   ipv4.Assign (devices);
  92.  
  93.   // 7. Install applications: two dense CBR streams  node 0 -> node 1 and node 2 -> node 1
  94.   ApplicationContainer apps;
  95.   OnOffHelper onOffHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address ("10.0.0.2"), 9));
  96.   onOffHelper.SetAttribute ("DataRate", StringValue ("10Mbps"));
  97.   onOffHelper.SetAttribute ("PacketSize", UintegerValue (200));
  98.   onOffHelper.SetAttribute ("OnTime",  RandomVariableValue (ConstantVariable (1)));
  99.   onOffHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
  100.   for (size_t i = 0; i < 3; i += 2)
  101.     {
  102.       apps.Add (onOffHelper.Install (nodes.Get (i)));
  103.     }
  104.  
  105.   // 8. Install FlowMonitor on all nodes
  106.   FlowMonitorHelper flowmon;
  107.   Ptr<FlowMonitor> monitor = flowmon.InstallAll();
  108.  
  109.   // 9. Run simulation for 10 seconds
  110.   ConfigStore config;
  111.   config.ConfigureDefaults ();
  112.   config.ConfigureAttributes ();
  113.  
  114.   Simulator::Stop (Seconds (10));
  115.   Simulator::Run ();
  116.  
  117.   // 10. Print per flow statistics
  118.   monitor->CheckForLostPackets ();
  119.   Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
  120.   std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
  121.   for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
  122.     {
  123.       Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
  124.       std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";
  125.       std::cout << "  Tx Bytes:   " << i->second.txBytes << "\n";
  126.       std::cout << "  Rx Bytes:   " << i->second.rxBytes << "\n";
  127.       std::cout << "  Throughput: " << i->second.rxBytes * 8.0 / 10.0 / 1024 / 1024  << " Mbps\n";
  128.     }
  129.  
  130.   // 11. Cleanup
  131.   Simulator::Destroy ();
  132. }
  133.  
  134. int main (int argc, char **argv)
  135. {
  136.   Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("config.txt"));
  137.   Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save"));
  138.  
  139.  
  140.   std::cout << "Hidden station experiment with RTS/CTS disabled:\n" << std::flush;
  141.   experiment (false);
  142.   std::cout << "------------------------------------------------\n";
  143.   std::cout << "Hidden station experiment with RTS/CTS enabled:\n";
  144.   experiment (true);
  145.  
  146.   return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement