1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/multi-rate-first.cc Thu Aug 13 09:06:17 2009 +0200
1.3 @@ -0,0 +1,317 @@
1.4 +/**
1.5 + *
1.6 + * Instructions:
1.7 + * ./waf --run multi-rate-first > m.data
1.8 + * gnuplot m.data
1.9 + * eog *.png
1.10 + *
1.11 + */
1.12 +
1.13 +#include "ns3/core-module.h"
1.14 +#include "ns3/common-module.h"
1.15 +#include "ns3/node-module.h"
1.16 +#include "ns3/helper-module.h"
1.17 +#include "ns3/mobility-module.h"
1.18 +#include "ns3/contrib-module.h"
1.19 +
1.20 +#include <iostream>
1.21 +
1.22 +NS_LOG_COMPONENT_DEFINE ("Main");
1.23 +
1.24 +using namespace ns3;
1.25 +
1.26 +class Experiment
1.27 +{
1.28 +public:
1.29 + Experiment ();
1.30 + Experiment (std::string name);
1.31 + Gnuplot2dDataset Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
1.32 + const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel, const MobilityHelper &mobility, int positionStep);
1.33 +private:
1.34 + void ReceivePacket (Ptr<Socket> socket);
1.35 + void SetPosition (Ptr<Node> node, Vector position);
1.36 + Vector GetPosition (Ptr<Node> node);
1.37 + void AdvancePosition (Ptr<Node> node);
1.38 + void BackTrackPosition (Ptr<Node> node);
1.39 + void StationaryPosition (Ptr<Node> node);
1.40 + Ptr<Socket> SetupPacketReceive (Ptr<Node> node);
1.41 +
1.42 + uint32_t m_bytesTotal;
1.43 + Gnuplot2dDataset m_output;
1.44 +};
1.45 +
1.46 +Experiment::Experiment ()
1.47 +{}
1.48 +
1.49 +Experiment::Experiment (std::string name)
1.50 + : m_output (name)
1.51 +{
1.52 + m_output.SetStyle (Gnuplot2dDataset::LINES);
1.53 +}
1.54 +
1.55 +void
1.56 +Experiment::SetPosition (Ptr<Node> node, Vector position)
1.57 +{
1.58 + Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
1.59 + mobility->SetPosition (position);
1.60 +}
1.61 +
1.62 +Vector
1.63 +Experiment::GetPosition (Ptr<Node> node)
1.64 +{
1.65 + Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
1.66 + return mobility->GetPosition ();
1.67 +}
1.68 +
1.69 +void
1.70 +Experiment::AdvancePosition (Ptr<Node> node)
1.71 +{
1.72 + Vector pos = GetPosition (node);
1.73 + double mbs = ((m_bytesTotal * 8.0) / 1000000);
1.74 + m_bytesTotal = 0;
1.75 + m_output.Add ((Simulator::Now()).GetSeconds(), mbs);
1.76 + pos.x += 1.0;
1.77 +
1.78 + if (pos.x >= 210.0)
1.79 + {
1.80 + return;
1.81 + }
1.82 + SetPosition (node, pos);
1.83 +
1.84 + //std::cout << "x="<<pos.x << std::endl;
1.85 + Simulator::Schedule (Seconds (1.0), &Experiment::AdvancePosition, this, node);
1.86 +}
1.87 +void
1.88 +Experiment::BackTrackPosition (Ptr<Node> node)
1.89 +{
1.90 + Vector pos = GetPosition (node);
1.91 + double mbs = ((m_bytesTotal * 8.0) / 1000000);
1.92 + m_bytesTotal = 0;
1.93 + m_output.Add ((Simulator::Now()).GetSeconds(), mbs);
1.94 + pos.x -= 1.0;
1.95 +
1.96 + if (pos.x < 0)
1.97 + {
1.98 + return;
1.99 + }
1.100 + SetPosition (node, pos);
1.101 +
1.102 + //std::cout << "x="<<pos.x << std::endl;
1.103 + Simulator::Schedule (Seconds (1.0), &Experiment::BackTrackPosition, this, node);
1.104 +}
1.105 +
1.106 +void
1.107 +Experiment::StationaryPosition (Ptr<Node> node)
1.108 +{
1.109 + double mbs = ((m_bytesTotal * 8.0) / 1000000);
1.110 + m_bytesTotal = 0;
1.111 + m_output.Add ((Simulator::Now()).GetSeconds(), mbs);
1.112 +
1.113 +}
1.114 +
1.115 +void
1.116 +Experiment::ReceivePacket (Ptr<Socket> socket)
1.117 +{
1.118 + Ptr<Packet> packet;
1.119 + while (packet = socket->Recv ())
1.120 + {
1.121 + m_bytesTotal += packet->GetSize ();
1.122 + }
1.123 +}
1.124 +
1.125 +Ptr<Socket>
1.126 +Experiment::SetupPacketReceive (Ptr<Node> node)
1.127 +{
1.128 + TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
1.129 + Ptr<Socket> sink = Socket::CreateSocket (node, tid);
1.130 + sink->Bind ();
1.131 + sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
1.132 +
1.133 + return sink;
1.134 +
1.135 +}
1.136 +
1.137 +Gnuplot2dDataset
1.138 +Experiment::Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
1.139 + const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel, const MobilityHelper &mobility, int positionStep)
1.140 +{
1.141 + m_bytesTotal = 0;
1.142 +
1.143 + NodeContainer c;
1.144 + c.Create (2);
1.145 +
1.146 + PacketSocketHelper packetSocket;
1.147 + packetSocket.Install (c);
1.148 +
1.149 + YansWifiPhyHelper phy = wifiPhy;
1.150 + phy.SetChannel (wifiChannel.Create ());
1.151 +
1.152 + NqosWifiMacHelper mac = wifiMac;
1.153 + NetDeviceContainer devices = wifi.Install (phy, mac, c);
1.154 +
1.155 + mobility.Install (c);
1.156 +
1.157 + PacketSocketAddress socket;
1.158 + socket.SetSingleDevice(devices.Get (0)->GetIfIndex ());
1.159 + socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
1.160 + socket.SetProtocol (1);
1.161 +
1.162 + OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
1.163 + onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (250)));
1.164 + onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
1.165 + onoff.SetAttribute ("DataRate", DataRateValue (DataRate (60000000)));
1.166 + onoff.SetAttribute ("PacketSize", UintegerValue (2000));
1.167 +
1.168 + ApplicationContainer apps = onoff.Install (c.Get (0));
1.169 + apps.Start (Seconds (0.5));
1.170 + apps.Stop (Seconds (250.0));
1.171 +
1.172 +
1.173 + Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
1.174 +
1.175 + if(positionStep == 1)
1.176 + {
1.177 + Simulator::Schedule (Seconds (1.5), &Experiment::AdvancePosition, this, c.Get (1));
1.178 + }
1.179 + else if(positionStep == -1)
1.180 + {
1.181 + Simulator::Schedule (Seconds (1.5), &Experiment::BackTrackPosition, this, c.Get (1));
1.182 + }
1.183 + else if(positionStep == 0)
1.184 + {
1.185 + for(int i = 1; i <= 210; i++)
1.186 + {
1.187 + Simulator::Schedule (Seconds (i), &Experiment::StationaryPosition, this, c.Get (1));
1.188 + }
1.189 + }
1.190 + Simulator::Run ();
1.191 + Simulator::Destroy ();
1.192 +
1.193 + return m_output;
1.194 +}
1.195 +
1.196 +int main (int argc, char *argv[])
1.197 +{
1.198 + // disable fragmentation
1.199 + Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
1.200 + Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
1.201 +
1.202 + CommandLine cmd;
1.203 + cmd.Parse (argc, argv);
1.204 +
1.205 + Gnuplot gnuplot = Gnuplot ("multi-rate-first.png");
1.206 + Experiment experiment;
1.207 + WifiHelper wifi = WifiHelper::Default ();
1.208 + NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
1.209 + YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
1.210 + YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
1.211 + Gnuplot2dDataset dataset;
1.212 + int myPositionStep = 0;
1.213 +
1.214 +/*
1.215 +
1.216 + // Scenario 1: moving away from one another
1.217 + // Initially set them 5 meters apart
1.218 + // Set positionStep parameter of Experiment::Run to 1
1.219 + // Set RateErrorModel of Experiment::Run to 0
1.220 + myPositionStep = 1;
1.221 +
1.222 + MobilityHelper mobility;
1.223 + Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
1.224 + positionAlloc->Add (Vector (0.0, 0.0, 0.0));
1.225 + positionAlloc->Add (Vector (5.0, 0.0, 0.0));
1.226 + mobility.SetPositionAllocator (positionAlloc);
1.227 + mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
1.228 +
1.229 + wifiMac.SetType ("ns3::AdhocWifiMac");
1.230 +
1.231 + gnuplot = Gnuplot ("multi-rate-first.png");
1.232 + Config::SetDefault ("ns3::YansWifiPhy::Standard", StringValue ("holland"));
1.233 +
1.234 + NS_LOG_DEBUG ("minstrel");
1.235 + experiment = Experiment ("minstrel");
1.236 + wifi.SetRemoteStationManager ("ns3::MinstrelWifiManager");
1.237 + dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
1.238 + gnuplot.AddDataset (dataset);
1.239 +
1.240 + NS_LOG_DEBUG ("ideal");
1.241 + experiment = Experiment ("ideal");
1.242 + wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
1.243 + dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
1.244 + gnuplot.AddDataset (dataset);
1.245 +
1.246 + gnuplot.GenerateOutput (std::cout);
1.247 + */
1.248 +
1.249 +
1.250 + // Scenario 2: two nodes out of range, moving into transmission range range
1.251 + // Initially set them 230 meters apart
1.252 + // Set positionStep parameter of Experiment::Rung to -1
1.253 + // set RateErrorModel of Experiment::Run to 0
1.254 +
1.255 + myPositionStep = -1;
1.256 +
1.257 + MobilityHelper mobility;
1.258 + Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
1.259 + positionAlloc->Add (Vector (0.0, 0.0, 0.0));
1.260 + positionAlloc->Add (Vector (230.0, 0.0, 0.0));
1.261 + mobility.SetPositionAllocator (positionAlloc);
1.262 + mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
1.263 +
1.264 + wifiMac.SetType ("ns3::AdhocWifiMac");
1.265 +
1.266 + gnuplot = Gnuplot ("multi-rate-first.png");
1.267 + Config::SetDefault ("ns3::YansWifiPhy::Standard", StringValue ("holland"));
1.268 +
1.269 + NS_LOG_DEBUG ("minstrel");
1.270 + experiment = Experiment ("minstrel");
1.271 + wifi.SetRemoteStationManager ("ns3::MinstrelWifiManager");
1.272 + dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
1.273 + gnuplot.AddDataset (dataset);
1.274 +
1.275 + NS_LOG_DEBUG ("ideal");
1.276 + experiment = Experiment ("ideal");
1.277 + wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
1.278 + dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
1.279 + gnuplot.AddDataset (dataset);
1.280 +
1.281 + gnuplot.GenerateOutput (std::cout);
1.282 +
1.283 +
1.284 +
1.285 +/*
1.286 + // Scenario 3:
1.287 + // Initially set them 25 meters apart, stationary
1.288 + // Set positionStep parameter of Experiment::Rung to 0
1.289 + // This is a sanity check
1.290 +
1.291 + myPositionStep = 0;
1.292 + MobilityHelper mobility;
1.293 + Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
1.294 + positionAlloc->Add (Vector (0.0, 0.0, 0.0));
1.295 + positionAlloc->Add (Vector (25.0, 0.0, 0.0));
1.296 + mobility.SetPositionAllocator (positionAlloc);
1.297 + mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
1.298 +
1.299 + wifiMac.SetType ("ns3::AdhocWifiMac");
1.300 +
1.301 + gnuplot = Gnuplot ("multi-rate-first.png");
1.302 + Config::SetDefault ("ns3::YansWifiPhy::Standard", StringValue ("holland"));
1.303 +
1.304 + NS_LOG_DEBUG ("minstrel");
1.305 + experiment = Experiment ("minstrel");
1.306 + wifi.SetRemoteStationManager ("ns3::MinstrelWifiManager");
1.307 + dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
1.308 + gnuplot.AddDataset (dataset);
1.309 +
1.310 + NS_LOG_DEBUG ("ideal");
1.311 + experiment = Experiment ("ideal");
1.312 + wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
1.313 + dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
1.314 + gnuplot.AddDataset (dataset);
1.315 +
1.316 + gnuplot.GenerateOutput (std::cout);
1.317 + */
1.318 +
1.319 + return 0;
1.320 +}