|
|
| 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License version 2 as |
| 5 |
* published by the Free Software Foundation; |
| 6 |
* |
| 7 |
* This program is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
* GNU General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with this program; if not, write to the Free Software |
| 14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 |
* |
| 16 |
* Author: MatÃas Richart <mrichart@fing.edu.uy> |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* This simulation consist of 2 nodes, one AP and one STA. |
| 21 |
* The AP generates UDP traffic with a CBR of 54 Mbps to the STA. |
| 22 |
* The AP and STA can use any rate control mechanism. |
| 23 |
* The STA can be configured to move away from (or towards to) the AP. |
| 24 |
* |
| 25 |
* The output consists of: |
| 26 |
* - A plot of average throughput vs. distance. |
| 27 |
* - If enabled, the changes of rate to standard output. |
| 28 |
* |
| 29 |
* Example usage: |
| 30 |
* ./waf --run "rate-adaptation-distance --manager=ns3::MinstrelWifiManager --outputFileName=minstrel" |
| 31 |
* |
| 32 |
* Another example (moving towards the AP): |
| 33 |
* ./waf --run "rate-adaptation-distance --manager=ns3::MinstrelWifiManager --outputFileName=minstrel --stepsSize=1 --STA1_x=-200" |
| 34 |
* |
| 35 |
* To enable the log of rate changes: |
| 36 |
* export NS_LOG=RateAdaptationDistance=level_info |
| 37 |
*/ |
| 38 |
|
| 39 |
#include <sstream> |
| 40 |
#include <fstream> |
| 41 |
#include <math.h> |
| 42 |
|
| 43 |
#include "ns3/core-module.h" |
| 44 |
#include "ns3/network-module.h" |
| 45 |
#include "ns3/internet-module.h" |
| 46 |
#include "ns3/csma-module.h" |
| 47 |
#include "ns3/mobility-module.h" |
| 48 |
#include "ns3/wifi-module.h" |
| 49 |
#include "ns3/applications-module.h" |
| 50 |
|
| 51 |
#include "ns3/stats-module.h" |
| 52 |
|
| 53 |
#include "ns3/flow-monitor-module.h" |
| 54 |
|
| 55 |
using namespace ns3; |
| 56 |
using namespace std; |
| 57 |
|
| 58 |
NS_LOG_COMPONENT_DEFINE ("RateAdaptationDistance"); |
| 59 |
|
| 60 |
class APStatics |
| 61 |
{ |
| 62 |
public: |
| 63 |
APStatics(NetDeviceContainer aps, NetDeviceContainer stas); |
| 64 |
|
| 65 |
void CheckStatics (double time); |
| 66 |
|
| 67 |
void RxCallback (std::string path, Ptr<const Packet> packet, const Address &from); |
| 68 |
void SetPosition (Ptr<Node> node, Vector position); |
| 69 |
void AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime); |
| 70 |
Vector GetPosition (Ptr<Node> node); |
| 71 |
|
| 72 |
Gnuplot2dDataset GetDatafile(); |
| 73 |
|
| 74 |
private: |
| 75 |
uint32_t m_bytesTotal; |
| 76 |
Gnuplot2dDataset m_output; |
| 77 |
}; |
| 78 |
|
| 79 |
APStatics::APStatics(NetDeviceContainer aps, NetDeviceContainer stas) |
| 80 |
{ |
| 81 |
m_bytesTotal = 0; |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
APStatics::RxCallback (std::string path, Ptr<const Packet> packet, const Address &from) |
| 86 |
{ |
| 87 |
m_bytesTotal += packet->GetSize(); |
| 88 |
} |
| 89 |
|
| 90 |
void |
| 91 |
APStatics::CheckStatics(double time) |
| 92 |
{ |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
APStatics::SetPosition (Ptr<Node> node, Vector position) |
| 98 |
{ |
| 99 |
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> (); |
| 100 |
mobility->SetPosition (position); |
| 101 |
} |
| 102 |
|
| 103 |
Vector |
| 104 |
APStatics::GetPosition (Ptr<Node> node) |
| 105 |
{ |
| 106 |
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> (); |
| 107 |
return mobility->GetPosition (); |
| 108 |
} |
| 109 |
|
| 110 |
void |
| 111 |
APStatics::AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime) |
| 112 |
{ |
| 113 |
Vector pos = GetPosition (node); |
| 114 |
double mbs = ((m_bytesTotal * 8.0) / (1000000*stepsTime)); |
| 115 |
m_bytesTotal = 0; |
| 116 |
m_output.Add (pos.x, mbs); |
| 117 |
pos.x += stepsSize; |
| 118 |
SetPosition (node, pos); |
| 119 |
//std::cout << "x="<<pos.x << std::endl; |
| 120 |
Simulator::Schedule (Seconds (stepsTime), &APStatics::AdvancePosition, this, node, stepsSize, stepsTime); |
| 121 |
} |
| 122 |
|
| 123 |
Gnuplot2dDataset |
| 124 |
APStatics::GetDatafile() |
| 125 |
{ return m_output; } |
| 126 |
|
| 127 |
|
| 128 |
void RateCallback (std::string path, uint32_t rate, Mac48Address dest) { |
| 129 |
NS_LOG_INFO ((Simulator::Now ()).GetSeconds () << " " << dest << " Rate " << rate); |
| 130 |
} |
| 131 |
|
| 132 |
int main (int argc, char *argv[]) |
| 133 |
{ |
| 134 |
uint32_t rtsThreshold=2346; |
| 135 |
std::string manager="ns3::MinstrelWifiManager"; |
| 136 |
std::string outputFileName="minstrel"; |
| 137 |
int ap1_x = 0; |
| 138 |
int ap1_y = 0; |
| 139 |
int sta1_x = 5; |
| 140 |
int sta1_y = 0; |
| 141 |
int steps = 200; |
| 142 |
int stepsSize = 1; |
| 143 |
int stepsTime = 1; |
| 144 |
|
| 145 |
CommandLine cmd; |
| 146 |
cmd.AddValue ("manager", "PRC Manager", manager); |
| 147 |
cmd.AddValue ("rtsThreshold", "RTS threshold", rtsThreshold); |
| 148 |
cmd.AddValue ("outputFileName", "Output filename", outputFileName); |
| 149 |
cmd.AddValue ("steps", "How many different distances to try", steps); |
| 150 |
cmd.AddValue ("stepsTime", "Time on each step", stepsTime); |
| 151 |
cmd.AddValue ("stepsSize", "Distance between steps", stepsSize); |
| 152 |
cmd.AddValue ("AP1_x", "Position of AP1 in x coordinate", ap1_x); |
| 153 |
cmd.AddValue ("AP1_y", "Position of AP1 in y coordinate", ap1_y); |
| 154 |
cmd.AddValue ("STA1_x", "Position of STA1 in x coordinate", sta1_x); |
| 155 |
cmd.AddValue ("STA1_y", "Position of STA1 in y coordinate", sta1_y); |
| 156 |
cmd.Parse (argc, argv); |
| 157 |
|
| 158 |
int simuTime = steps*stepsTime; |
| 159 |
|
| 160 |
// Define the APs |
| 161 |
NodeContainer wifiApNodes; |
| 162 |
wifiApNodes.Create (1); |
| 163 |
|
| 164 |
//Define the STAs |
| 165 |
NodeContainer wifiStaNodes; |
| 166 |
wifiStaNodes.Create(1); |
| 167 |
|
| 168 |
WifiHelper wifi = WifiHelper::Default (); |
| 169 |
wifi.SetStandard(WIFI_PHY_STANDARD_80211a); |
| 170 |
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
| 171 |
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
| 172 |
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); |
| 173 |
|
| 174 |
wifiPhy.SetChannel (wifiChannel.Create ()); |
| 175 |
|
| 176 |
NetDeviceContainer wifiApDevices; |
| 177 |
NetDeviceContainer wifiStaDevices; |
| 178 |
NetDeviceContainer wifiDevices; |
| 179 |
|
| 180 |
//Configure the STA node |
| 181 |
wifi.SetRemoteStationManager (manager, "RtsCtsThreshold", UintegerValue(rtsThreshold)); |
| 182 |
|
| 183 |
Ssid ssid = Ssid ("AP"); |
| 184 |
wifiMac.SetType ("ns3::StaWifiMac", |
| 185 |
"Ssid", SsidValue (ssid), |
| 186 |
"ActiveProbing", BooleanValue (false)); |
| 187 |
wifiStaDevices.Add(wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get(0))); |
| 188 |
|
| 189 |
//Configure the AP node |
| 190 |
wifi.SetRemoteStationManager (manager, "RtsCtsThreshold", UintegerValue(rtsThreshold)); |
| 191 |
|
| 192 |
ssid = Ssid ("AP"); |
| 193 |
wifiMac.SetType ("ns3::ApWifiMac", |
| 194 |
"Ssid", SsidValue (ssid)); |
| 195 |
wifiApDevices.Add(wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get(0))); |
| 196 |
|
| 197 |
wifiDevices.Add(wifiStaDevices); |
| 198 |
wifiDevices.Add(wifiApDevices); |
| 199 |
|
| 200 |
// Configure the mobility. |
| 201 |
MobilityHelper mobility; |
| 202 |
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> (); |
| 203 |
//Initial position of AP and STA |
| 204 |
positionAlloc->Add (Vector (ap1_x, ap1_y, 0.0)); |
| 205 |
positionAlloc->Add (Vector (sta1_x, sta1_y, 0.0)); |
| 206 |
mobility.SetPositionAllocator (positionAlloc); |
| 207 |
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 208 |
mobility.Install (wifiApNodes.Get(0)); |
| 209 |
mobility.Install (wifiStaNodes.Get(0)); |
| 210 |
|
| 211 |
//Statics counter |
| 212 |
APStatics atpCounter = APStatics(wifiApDevices, wifiStaDevices); |
| 213 |
|
| 214 |
//Move the STA by stepsSize meters every stepsTime seconds |
| 215 |
Simulator::Schedule (Seconds (0.5+stepsTime), &APStatics::AdvancePosition, &atpCounter, wifiStaNodes.Get (0), stepsSize, stepsTime); |
| 216 |
|
| 217 |
//Configure the IP stack |
| 218 |
InternetStackHelper stack; |
| 219 |
stack.Install (wifiApNodes); |
| 220 |
stack.Install (wifiStaNodes); |
| 221 |
Ipv4AddressHelper address; |
| 222 |
address.SetBase ("10.1.1.0", "255.255.255.0"); |
| 223 |
Ipv4InterfaceContainer i = address.Assign (wifiDevices); |
| 224 |
Ipv4Address sinkAddress = i.GetAddress(0); |
| 225 |
uint16_t port = 9; |
| 226 |
|
| 227 |
//Configure the CBR generator |
| 228 |
PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port)); |
| 229 |
ApplicationContainer apps_sink = sink.Install (wifiStaNodes.Get (0)); |
| 230 |
|
| 231 |
OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port)); |
| 232 |
onoff.SetConstantRate (DataRate ("54Mb/s"), 1420); |
| 233 |
onoff.SetAttribute ("StartTime", TimeValue (Seconds (0.5))); |
| 234 |
onoff.SetAttribute ("StopTime", TimeValue (Seconds (simuTime))); |
| 235 |
ApplicationContainer apps_source = onoff.Install (wifiApNodes.Get (0)); |
| 236 |
|
| 237 |
apps_sink.Start (Seconds (0.5)); |
| 238 |
apps_sink.Stop (Seconds (simuTime)); |
| 239 |
|
| 240 |
//------------------------------------------------------------ |
| 241 |
//-- Setup stats and data collection |
| 242 |
//-------------------------------------------- |
| 243 |
|
| 244 |
//Register packet receptions to calculate throughput |
| 245 |
Config::Connect ("/NodeList/1/ApplicationList/*/$ns3::PacketSink/Rx", |
| 246 |
MakeCallback (&APStatics::RxCallback, &atpCounter)); |
| 247 |
|
| 248 |
//Callbacks to print every change of rate |
| 249 |
Config::Connect ("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + manager + "/RateChange", |
| 250 |
MakeCallback (RateCallback)); |
| 251 |
|
| 252 |
Simulator::Stop (Seconds (simuTime)); |
| 253 |
Simulator::Run (); |
| 254 |
|
| 255 |
std::ofstream outfile (("throughput-" + outputFileName + ".plt").c_str ()); |
| 256 |
Gnuplot gnuplot = Gnuplot(("throughput-" + outputFileName + ".eps").c_str (), "Throughput"); |
| 257 |
gnuplot.SetTerminal("post eps color enhanced"); |
| 258 |
gnuplot.AddDataset (atpCounter.GetDatafile()); |
| 259 |
gnuplot.GenerateOutput (outfile); |
| 260 |
|
| 261 |
Simulator::Destroy (); |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |