1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/multi-rate-second.cc Thu Aug 13 09:06:17 2009 +0200
1.3 @@ -0,0 +1,256 @@
1.4 +/*
1.5 + * Instructions:
1.6 + * ./waf --run multi-rate-second
1.7 + * gnuplot multi-rate-second.plt
1.8 + *
1.9 + * Output: multi-rate-second.eps
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 +#include <fstream>
1.22 +#include <vector>
1.23 +#include <string>
1.24 +
1.25 +NS_LOG_COMPONENT_DEFINE ("Main");
1.26 +
1.27 +using namespace ns3;
1.28 +
1.29 +class Experiment
1.30 +{
1.31 +public:
1.32 + Experiment ();
1.33 + Experiment (std::string name);
1.34 + Gnuplot2dDataset Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
1.35 + const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
1.36 +private:
1.37 + void ReceivePacket (Ptr<Socket> socket);
1.38 + void SetPosition (Ptr<Node> node, Vector position);
1.39 + Vector GetPosition (Ptr<Node> node);
1.40 + Ptr<Socket> SetupPacketReceive (Ptr<Node> node);
1.41 + void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
1.42 + uint32_t pktCount, Time pktInterval , Ptr<Node> node);
1.43 +
1.44 + uint32_t m_pktsTotal;
1.45 + Gnuplot2dDataset m_output;
1.46 + bool advanceStep;
1.47 +};
1.48 +
1.49 +Experiment::Experiment ()
1.50 +{
1.51 + advanceStep= true;
1.52 +}
1.53 +
1.54 +Experiment::Experiment (std::string name)
1.55 + : m_output (name)
1.56 +{
1.57 + m_output.SetStyle (Gnuplot2dDataset::LINES);
1.58 +}
1.59 +
1.60 +void
1.61 +Experiment::SetPosition (Ptr<Node> node, Vector position)
1.62 +{
1.63 + Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
1.64 + mobility->SetPosition (position);
1.65 +}
1.66 +
1.67 +Vector
1.68 +Experiment::GetPosition (Ptr<Node> node)
1.69 +{
1.70 + Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
1.71 + return mobility->GetPosition ();
1.72 +}
1.73 +
1.74 +void
1.75 +Experiment::ReceivePacket (Ptr<Socket> socket)
1.76 +{
1.77 + Ptr<Packet> packet;
1.78 + while (packet = socket->Recv ())
1.79 + {
1.80 + m_pktsTotal ++;
1.81 + }
1.82 +}
1.83 +
1.84 +Ptr<Socket>
1.85 +Experiment::SetupPacketReceive (Ptr<Node> node)
1.86 +{
1.87 + TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
1.88 + Ptr<Socket> sink = Socket::CreateSocket (node, tid);
1.89 + InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
1.90 + sink->Bind (local);
1.91 + sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
1.92 + return sink;
1.93 +}
1.94 +
1.95 +void
1.96 +Experiment::GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
1.97 + uint32_t pktCount, Time pktInterval, Ptr<Node> node )
1.98 +{
1.99 + Vector pos = GetPosition(node);
1.100 +
1.101 + ///to offset the start time
1.102 + double offSetTime = 100;
1.103 +
1.104 + if (pktCount > 0)
1.105 + {
1.106 + ///To simulate nodes moving in and out of transmission constantly
1.107 + if(pos.x <= 305 && advanceStep)
1.108 + {
1.109 + ///keep moving away
1.110 + pos.x += .1;
1.111 + SetPosition(node, pos);
1.112 + }
1.113 + else
1.114 + {
1.115 + if(pos.x < 150)
1.116 + {
1.117 + advanceStep=true;
1.118 + }
1.119 + else
1.120 + {
1.121 + advanceStep = false;
1.122 + }
1.123 +
1.124 + ///moving back in
1.125 + pos.x -= .1;
1.126 + SetPosition(node, pos);
1.127 + }
1.128 + socket->Send (Create<Packet> (pktSize));
1.129 + Simulator::Schedule (pktInterval, &Experiment::GenerateTraffic, this,
1.130 + socket, pktSize,pktCount-1, pktInterval, node);
1.131 + }
1.132 + else
1.133 + {
1.134 + m_output.Add((Simulator::Now()).GetSeconds() - offSetTime , m_pktsTotal);
1.135 + }
1.136 +}
1.137 +
1.138 +Gnuplot2dDataset
1.139 +Experiment::Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
1.140 + const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
1.141 +{
1.142 + m_pktsTotal = 0;
1.143 +
1.144 + NodeContainer c;
1.145 + c.Create (2);
1.146 +
1.147 + YansWifiPhyHelper phy = wifiPhy;
1.148 + phy.SetChannel (wifiChannel.Create ());
1.149 +
1.150 + NqosWifiMacHelper mac = wifiMac;
1.151 + NetDeviceContainer devices = wifi.Install (phy, mac, c);
1.152 +
1.153 + MobilityHelper mobility;
1.154 + Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
1.155 + positionAlloc->Add (Vector (0.0, 0.0, 0.0));
1.156 + positionAlloc->Add (Vector (5.0, 0.0, 0.0));
1.157 + mobility.SetPositionAllocator (positionAlloc);
1.158 + mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
1.159 + mobility.Install (c);
1.160 +
1.161 + InternetStackHelper internet;
1.162 + internet.Install (c);
1.163 +
1.164 + Ipv4AddressHelper ipv4;
1.165 + NS_LOG_INFO ("Assign IP Addresses.");
1.166 + ipv4.SetBase ("10.1.1.0", "255.255.255.0");
1.167 + Ipv4InterfaceContainer wifiNodesInterface = ipv4.Assign (devices);
1.168 +
1.169 +
1.170 + Ptr<Socket> recvSink = SetupPacketReceive (c.Get (0));
1.171 +
1.172 + TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
1.173 + Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
1.174 + InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
1.175 + source->Connect (remote);
1.176 + uint32_t packetSize = 1014;
1.177 + uint32_t maxPacketCount = 1000;
1.178 + Time interPacketInterval = Seconds (.1);
1.179 +
1.180 + Ptr<Node> n1 = c.Get(0);
1.181 + Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
1.182 +
1.183 +
1.184 +
1.185 + for (int i= 1; i <= 100; i++)
1.186 + {
1.187 +
1.188 + Simulator::Schedule (Seconds (i), &Experiment::GenerateTraffic,
1.189 + this, source, packetSize, maxPacketCount,interPacketInterval, c.Get(1));
1.190 +
1.191 + if( i % 5 == 0 )
1.192 + {
1.193 + ///bring a network interface down
1.194 + Simulator::Schedule (Seconds (i+.5), &Ipv4::SetDown, ipv41, 1);
1.195 + i++;
1.196 + Simulator::Schedule (Seconds (i), &Experiment::GenerateTraffic,
1.197 + this, source, packetSize, maxPacketCount,interPacketInterval, c.Get(1));
1.198 +
1.199 + ///bring a network interface up
1.200 + Simulator::Schedule (Seconds (i+.2), &Ipv4::SetUp, ipv41, 1);
1.201 + i++;
1.202 + Simulator::Schedule (Seconds (i), &Experiment::GenerateTraffic,
1.203 + this, source, packetSize, maxPacketCount,interPacketInterval, c.Get(1));
1.204 + }
1.205 + }
1.206 +
1.207 + Simulator::Run ();
1.208 + Simulator::Destroy ();
1.209 +
1.210 + return m_output;
1.211 +}
1.212 +
1.213 +int main (int argc, char *argv[])
1.214 +{
1.215 + std::vector <std::string> ratesControl;
1.216 + ratesControl.push_back ("Ideal");
1.217 + ratesControl.push_back ("Minstrel");
1.218 +
1.219 + std::vector <std::string> wifiManager;
1.220 + wifiManager.push_back("ns3::IdealWifiManager");
1.221 + wifiManager.push_back("ns3::MinstrelWifiManager");
1.222 +
1.223 + // disable fragmentation
1.224 + Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
1.225 + Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
1.226 +
1.227 + CommandLine cmd;
1.228 + cmd.Parse (argc, argv);
1.229 +
1.230 + Gnuplot gnuplot = Gnuplot ("multi-rate-second.eps");
1.231 +
1.232 + for (uint32_t i = 0; i < ratesControl.size(); i++)
1.233 + {
1.234 + Gnuplot2dDataset dataset (ratesControl[i]);
1.235 + dataset.SetStyle (Gnuplot2dDataset::LINES);
1.236 + Experiment experiment;
1.237 +
1.238 +
1.239 + WifiHelper wifi = WifiHelper::Default ();
1.240 + NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
1.241 + YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
1.242 + YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
1.243 +
1.244 + wifiMac.SetType ("ns3::AdhocWifiMac");
1.245 +
1.246 + NS_LOG_DEBUG (ratesControl[i]);
1.247 +
1.248 + experiment = Experiment (ratesControl[i]);
1.249 + dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
1.250 + gnuplot.AddDataset (dataset);
1.251 +
1.252 + }
1.253 + gnuplot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
1.254 + gnuplot.SetLegend ("Time (Seconds)", "Number of packets received");
1.255 + gnuplot.SetExtra ("set xrange [0:100]");
1.256 + gnuplot.GenerateOutput (std::cout);
1.257 +
1.258 + return 0;
1.259 +}