3 * ./waf --run multi-rate-second
4 * gnuplot multi-rate-second.plt
6 * Output: multi-rate-second.eps
10 #include "ns3/core-module.h"
11 #include "ns3/common-module.h"
12 #include "ns3/node-module.h"
13 #include "ns3/helper-module.h"
14 #include "ns3/mobility-module.h"
15 #include "ns3/contrib-module.h"
22 NS_LOG_COMPONENT_DEFINE ("Main");
30 Experiment (std::string name);
31 Gnuplot2dDataset Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
32 const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
34 void ReceivePacket (Ptr<Socket> socket);
35 void SetPosition (Ptr<Node> node, Vector position);
36 Vector GetPosition (Ptr<Node> node);
37 Ptr<Socket> SetupPacketReceive (Ptr<Node> node);
38 void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
39 uint32_t pktCount, Time pktInterval , Ptr<Node> node);
42 Gnuplot2dDataset m_output;
46 Experiment::Experiment ()
51 Experiment::Experiment (std::string name)
54 m_output.SetStyle (Gnuplot2dDataset::LINES);
58 Experiment::SetPosition (Ptr<Node> node, Vector position)
60 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
61 mobility->SetPosition (position);
65 Experiment::GetPosition (Ptr<Node> node)
67 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
68 return mobility->GetPosition ();
72 Experiment::ReceivePacket (Ptr<Socket> socket)
75 while (packet = socket->Recv ())
82 Experiment::SetupPacketReceive (Ptr<Node> node)
84 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
85 Ptr<Socket> sink = Socket::CreateSocket (node, tid);
86 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
88 sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
93 Experiment::GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
94 uint32_t pktCount, Time pktInterval, Ptr<Node> node )
96 Vector pos = GetPosition(node);
98 ///to offset the start time
99 double offSetTime = 100;
103 ///To simulate nodes moving in and out of transmission constantly
104 if(pos.x <= 305 && advanceStep)
108 SetPosition(node, pos);
123 SetPosition(node, pos);
125 socket->Send (Create<Packet> (pktSize));
126 Simulator::Schedule (pktInterval, &Experiment::GenerateTraffic, this,
127 socket, pktSize,pktCount-1, pktInterval, node);
131 m_output.Add((Simulator::Now()).GetSeconds() - offSetTime , m_pktsTotal);
136 Experiment::Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
137 const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
144 YansWifiPhyHelper phy = wifiPhy;
145 phy.SetChannel (wifiChannel.Create ());
147 NqosWifiMacHelper mac = wifiMac;
148 NetDeviceContainer devices = wifi.Install (phy, mac, c);
150 MobilityHelper mobility;
151 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
152 positionAlloc->Add (Vector (0.0, 0.0, 0.0));
153 positionAlloc->Add (Vector (5.0, 0.0, 0.0));
154 mobility.SetPositionAllocator (positionAlloc);
155 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
156 mobility.Install (c);
158 InternetStackHelper internet;
159 internet.Install (c);
161 Ipv4AddressHelper ipv4;
162 NS_LOG_INFO ("Assign IP Addresses.");
163 ipv4.SetBase ("10.1.1.0", "255.255.255.0");
164 Ipv4InterfaceContainer wifiNodesInterface = ipv4.Assign (devices);
167 Ptr<Socket> recvSink = SetupPacketReceive (c.Get (0));
169 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
170 Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
171 InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
172 source->Connect (remote);
173 uint32_t packetSize = 1014;
174 uint32_t maxPacketCount = 1000;
175 Time interPacketInterval = Seconds (.1);
177 Ptr<Node> n1 = c.Get(0);
178 Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
182 for (int i= 1; i <= 100; i++)
185 Simulator::Schedule (Seconds (i), &Experiment::GenerateTraffic,
186 this, source, packetSize, maxPacketCount,interPacketInterval, c.Get(1));
190 ///bring a network interface down
191 Simulator::Schedule (Seconds (i+.5), &Ipv4::SetDown, ipv41, 1);
193 Simulator::Schedule (Seconds (i), &Experiment::GenerateTraffic,
194 this, source, packetSize, maxPacketCount,interPacketInterval, c.Get(1));
196 ///bring a network interface up
197 Simulator::Schedule (Seconds (i+.2), &Ipv4::SetUp, ipv41, 1);
199 Simulator::Schedule (Seconds (i), &Experiment::GenerateTraffic,
200 this, source, packetSize, maxPacketCount,interPacketInterval, c.Get(1));
205 Simulator::Destroy ();
210 int main (int argc, char *argv[])
212 std::vector <std::string> ratesControl;
213 ratesControl.push_back ("Ideal");
214 ratesControl.push_back ("Minstrel");
216 std::vector <std::string> wifiManager;
217 wifiManager.push_back("ns3::IdealWifiManager");
218 wifiManager.push_back("ns3::MinstrelWifiManager");
220 // disable fragmentation
221 Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
222 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
225 cmd.Parse (argc, argv);
227 Gnuplot gnuplot = Gnuplot ("multi-rate-second.eps");
229 for (uint32_t i = 0; i < ratesControl.size(); i++)
231 Gnuplot2dDataset dataset (ratesControl[i]);
232 dataset.SetStyle (Gnuplot2dDataset::LINES);
233 Experiment experiment;
236 WifiHelper wifi = WifiHelper::Default ();
237 NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
238 YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
239 YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
241 wifiMac.SetType ("ns3::AdhocWifiMac");
243 NS_LOG_DEBUG (ratesControl[i]);
245 experiment = Experiment (ratesControl[i]);
246 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
247 gnuplot.AddDataset (dataset);
250 gnuplot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
251 gnuplot.SetLegend ("Time (Seconds)", "Number of packets received");
252 gnuplot.SetExtra ("set xrange [0:100]");
253 gnuplot.GenerateOutput (std::cout);