58 #include "ns3/gnuplot.h" 59 #include "ns3/command-line.h" 60 #include "ns3/config.h" 61 #include "ns3/uinteger.h" 62 #include "ns3/double.h" 64 #include "ns3/yans-wifi-helper.h" 66 #include "ns3/mobility-helper.h" 67 #include "ns3/internet-stack-helper.h" 68 #include "ns3/ipv4-address-helper.h" 69 #include "ns3/packet-sink-helper.h" 70 #include "ns3/on-off-helper.h" 71 #include "ns3/yans-wifi-channel.h" 72 #include "ns3/wifi-net-device.h" 73 #include "ns3/wifi-mac.h" 74 #include "ns3/wifi-mac-header.h" 75 #include "ns3/flow-monitor-helper.h" 76 #include "ns3/ipv4-flow-classifier.h" 91 void CheckStatistics (
double time);
106 double GetBusyTime ();
109 typedef std::vector<std::pair<Time, DataRate> >
TxTime;
113 std::map<Mac48Address, double> currentPower;
114 std::map<Mac48Address, DataRate> currentRate;
115 uint32_t m_bytesTotal;
144 double power =
phy->GetTxPowerEnd ();
145 for (uint32_t j = 0; j < stas.
GetN (); j++)
150 currentPower[addr] = power;
151 currentRate[addr] = dataRate;
153 currentRate[
Mac48Address (
"ff:ff:ff:ff:ff:ff")] = dataRate;
165 m_output.SetTitle (
"Throughput Mbits/s");
166 m_output_idle.SetTitle (
"Idle Time");
167 m_output_busy.SetTitle (
"Busy Time");
168 m_output_rx.SetTitle (
"RX Time");
169 m_output_tx.SetTitle (
"TX Time");
175 for (
const auto & mode :
phy->GetModeList ())
184 timeTable.push_back (std::make_pair (time, dataRate));
191 for (TxTime::const_iterator i = timeTable.begin (); i != timeTable.end (); i++)
193 if (rate == i->second)
211 totalEnergy += pow (10.0, currentPower[dest] / 10.0) * GetCalcTxTime (currentRate[dest]).GetSeconds ();
212 totalTime += GetCalcTxTime (currentRate[dest]).GetSeconds ();
219 currentPower[dest] = newPower;
225 currentRate[dest] = newRate;
256 m_bytesTotal += packet->
GetSize ();
262 double mbs = ((m_bytesTotal * 8.0) / (1000000 * time));
264 double atp = totalEnergy / time;
270 m_output_idle.Add ((
Simulator::Now ()).GetSeconds (), idleTime * 100);
271 m_output_busy.Add ((
Simulator::Now ()).GetSeconds (), busyTime * 100);
272 m_output_tx.Add ((
Simulator::Now ()).GetSeconds (), txTime * 100);
273 m_output_rx.Add ((
Simulator::Now ()).GetSeconds (), rxTime * 100);
291 return m_output_power;
297 return m_output_idle;
303 return m_output_busy;
321 return totalBusyTime + totalRxTime;
326 NS_LOG_INFO ((
Simulator::Now ()).GetSeconds () <<
" " << dest <<
" Old power=" << oldPower <<
" New power=" << newPower);
334 int main (
int argc,
char *argv[])
338 double maxPower = 17;
340 uint32_t powerLevels = 18;
342 uint32_t rtsThreshold = 2346;
343 std::string manager =
"ns3::ParfWifiManager";
344 std::string outputFileName =
"parf";
353 uint32_t simuTime = 100;
356 cmd.AddValue (
"manager",
"PRC Manager", manager);
357 cmd.AddValue (
"rtsThreshold",
"RTS threshold", rtsThreshold);
358 cmd.AddValue (
"outputFileName",
"Output filename", outputFileName);
359 cmd.AddValue (
"simuTime",
"Total simulation time (sec)", simuTime);
360 cmd.AddValue (
"maxPower",
"Maximum available transmission level (dbm).", maxPower);
361 cmd.AddValue (
"minPower",
"Minimum available transmission level (dbm).", minPower);
362 cmd.AddValue (
"powerLevels",
"Number of transmission power levels available between " 363 "TxPowerStart and TxPowerEnd included.", powerLevels);
364 cmd.AddValue (
"AP1_x",
"Position of AP1 in x coordinate", ap1_x);
365 cmd.AddValue (
"AP1_y",
"Position of AP1 in y coordinate", ap1_y);
366 cmd.AddValue (
"STA1_x",
"Position of STA1 in x coordinate", sta1_x);
367 cmd.AddValue (
"STA1_y",
"Position of STA1 in y coordinate", sta1_y);
368 cmd.AddValue (
"AP2_x",
"Position of AP2 in x coordinate", ap2_x);
369 cmd.AddValue (
"AP2_y",
"Position of AP2 in y coordinate", ap2_y);
370 cmd.AddValue (
"STA2_x",
"Position of STA2 in x coordinate", sta2_x);
371 cmd.AddValue (
"STA2_y",
"Position of STA2 in y coordinate", sta2_y);
372 cmd.Parse (argc, argv);
395 wifi.SetRemoteStationManager (
"ns3::AarfWifiManager",
"RtsCtsThreshold",
UintegerValue (rtsThreshold));
400 wifiMac.
SetType (
"ns3::StaWifiMac",
406 wifiMac.
SetType (
"ns3::StaWifiMac",
417 wifiMac.
SetType (
"ns3::ApWifiMac",
419 wifiApDevices.
Add (
wifi.Install (wifiPhy, wifiMac, wifiApNodes.
Get (0)));
422 wifiMac.
SetType (
"ns3::ApWifiMac",
425 wifiApDevices.
Add (
wifi.Install (wifiPhy, wifiMac, wifiApNodes.
Get (1)));
427 wifiDevices.
Add (wifiStaDevices);
428 wifiDevices.
Add (wifiApDevices);
433 positionAlloc->
Add (Vector (ap1_x, ap1_y, 0.0));
434 positionAlloc->
Add (Vector (sta1_x, sta1_y, 0.0));
435 positionAlloc->
Add (Vector (ap2_x, ap2_y, 0.0));
436 positionAlloc->
Add (Vector (sta2_x, sta2_y, 0.0));
437 mobility.SetPositionAllocator (positionAlloc);
438 mobility.SetMobilityModel (
"ns3::ConstantPositionMobilityModel");
447 stack.Install (wifiApNodes);
450 address.SetBase (
"10.1.1.0",
"255.255.255.0");
473 apps_source.
Add (onoff1.Install (wifiApNodes.
Get (1)));
493 Config::Connect (
"/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + manager +
"/PowerChange",
495 Config::Connect (
"/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + manager +
"/RateChange",
497 Config::Connect (
"/NodeList/1/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + manager +
"/PowerChange",
499 Config::Connect (
"/NodeList/1/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + manager +
"/RateChange",
502 Config::Connect (
"/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxBegin",
504 Config::Connect (
"/NodeList/1/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxBegin",
508 Config::Connect (
"/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/Phy/$ns3::YansWifiPhy/State/State",
510 Config::Connect (
"/NodeList/1/DeviceList/*/$ns3::WifiNetDevice/Phy/$ns3::YansWifiPhy/State/State",
517 Config::Connect (
"/NodeList/[0-1]/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + manager +
"/PowerChange",
519 Config::Connect (
"/NodeList/[0-1]/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + manager +
"/RateChange",
532 std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->
GetFlowStats ();
533 for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
539 NS_LOG_INFO (
" Tx Bytes: " << i->second.txBytes <<
"\n");
540 NS_LOG_INFO (
" Rx Bytes: " << i->second.rxBytes <<
"\n");
541 NS_LOG_UNCOND (
" Throughput to 10.1.1.1: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds () - i->second.timeFirstTxPacket.GetSeconds ()) / 1024 / 1024 <<
" Mbps\n");
542 NS_LOG_INFO (
" Mean delay: " << i->second.delaySum.GetSeconds () / i->second.rxPackets <<
"\n");
543 NS_LOG_INFO (
" Mean jitter: " << i->second.jitterSum.GetSeconds () / (i->second.rxPackets - 1) <<
"\n");
549 NS_LOG_INFO (
" Tx Bytes: " << i->second.txBytes <<
"\n");
550 NS_LOG_INFO (
" Rx Bytes: " << i->second.rxBytes <<
"\n");
551 NS_LOG_UNCOND (
" Throughput to 10.1.1.2: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds () - i->second.timeFirstTxPacket.GetSeconds ()) / 1024 / 1024 <<
" Mbps\n");
552 NS_LOG_INFO (
" Mean delay: " << i->second.delaySum.GetSeconds () / i->second.rxPackets <<
"\n");
553 NS_LOG_INFO (
" Mean jitter: " << i->second.jitterSum.GetSeconds () / (i->second.rxPackets - 1) <<
"\n");
559 std::ofstream outfileTh0 ((
"throughput-" + outputFileName +
"-0.plt").c_str ());
560 Gnuplot gnuplot =
Gnuplot ((
"throughput-" + outputFileName +
"-0.eps").c_str (),
"Throughput");
562 gnuplot.
SetLegend (
"Time (seconds)",
"Throughput (Mb/s)");
563 gnuplot.
SetTitle (
"Throughput (AP0 to STA) vs time");
567 if (manager.compare (
"ns3::ParfWifiManager") == 0
568 || manager.compare (
"ns3::AparfWifiManager") == 0
569 || manager.compare (
"ns3::RrpaaWifiManager") == 0)
571 std::ofstream outfilePower0 ((
"power-" + outputFileName +
"-0.plt").c_str ());
572 gnuplot =
Gnuplot ((
"power-" + outputFileName +
"-0.eps").c_str (),
"Average Transmit Power");
574 gnuplot.
SetLegend (
"Time (seconds)",
"Power (mW)");
575 gnuplot.
SetTitle (
"Average transmit power (AP0 to STA) vs time");
580 std::ofstream outfileTx0 ((
"tx-" + outputFileName +
"-0.plt").c_str ());
581 gnuplot =
Gnuplot ((
"tx-" + outputFileName +
"-0.eps").c_str (),
"Time in TX State");
583 gnuplot.
SetLegend (
"Time (seconds)",
"Percent");
584 gnuplot.
SetTitle (
"Percentage time AP0 in TX state vs time");
588 std::ofstream outfileRx0 ((
"rx-" + outputFileName +
"-0.plt").c_str ());
589 gnuplot =
Gnuplot ((
"rx-" + outputFileName +
"-0.eps").c_str (),
"Time in RX State");
591 gnuplot.
SetLegend (
"Time (seconds)",
"Percent");
592 gnuplot.
SetTitle (
"Percentage time AP0 in RX state vs time");
596 std::ofstream outfileBusy0 ((
"busy-" + outputFileName +
"-0.plt").c_str ());
597 gnuplot =
Gnuplot ((
"busy-" + outputFileName +
"-0.eps").c_str (),
"Time in Busy State");
599 gnuplot.
SetLegend (
"Time (seconds)",
"Percent");
600 gnuplot.
SetTitle (
"Percentage time AP0 in Busy state vs time");
604 std::ofstream outfileIdle0 ((
"idle-" + outputFileName +
"-0.plt").c_str ());
605 gnuplot =
Gnuplot ((
"idle-" + outputFileName +
"-0.eps").c_str (),
"Time in Idle State");
607 gnuplot.
SetLegend (
"Time (seconds)",
"Percent");
608 gnuplot.
SetTitle (
"Percentage time AP0 in Idle state vs time");
613 std::ofstream outfileTh1 ((
"throughput-" + outputFileName +
"-1.plt").c_str ());
614 gnuplot =
Gnuplot ((
"throughput-" + outputFileName +
"-1.eps").c_str (),
"Throughput");
616 gnuplot.
SetLegend (
"Time (seconds)",
"Throughput (Mb/s)");
617 gnuplot.
SetTitle (
"Throughput (AP1 to STA) vs time");
621 if (manager.compare (
"ns3::ParfWifiManager") == 0
622 || manager.compare (
"ns3::AparfWifiManager") == 0
623 || manager.compare (
"ns3::RrpaaWifiManager") == 0)
625 std::ofstream outfilePower1 ((
"power-" + outputFileName +
"-1.plt").c_str ());
626 gnuplot =
Gnuplot ((
"power-" + outputFileName +
"-1.eps").c_str (),
"Average Transmit Power");
628 gnuplot.
SetLegend (
"Time (seconds)",
"Power (mW)");
629 gnuplot.
SetTitle (
"Average transmit power (AP1 to STA) vs time");
634 std::ofstream outfileTx1 ((
"tx-" + outputFileName +
"-1.plt").c_str ());
635 gnuplot =
Gnuplot ((
"tx-" + outputFileName +
"-1.eps").c_str (),
"Time in TX State");
637 gnuplot.
SetLegend (
"Time (seconds)",
"Percent");
638 gnuplot.
SetTitle (
"Percentage time AP1 in TX state vs time");
642 std::ofstream outfileRx1 ((
"rx-" + outputFileName +
"-1.plt").c_str ());
643 gnuplot =
Gnuplot ((
"rx-" + outputFileName +
"-1.eps").c_str (),
"Time in RX State");
645 gnuplot.
SetLegend (
"Time (seconds)",
"Percent");
646 gnuplot.
SetTitle (
"Percentage time AP1 in RX state vs time");
650 std::ofstream outfileBusy1 ((
"busy-" + outputFileName +
"-1.plt").c_str ());
651 gnuplot =
Gnuplot ((
"busy-" + outputFileName +
"-1.eps").c_str (),
"Time in Busy State");
653 gnuplot.
SetLegend (
"Time (seconds)",
"Percent");
654 gnuplot.
SetTitle (
"Percentage time AP1 in Busy state vs time");
658 std::ofstream outfileIdle1 ((
"idle-" + outputFileName +
"-1.plt").c_str ());
659 gnuplot =
Gnuplot ((
"idle-" + outputFileName +
"-1.eps").c_str (),
"Time in Idle State");
661 gnuplot.
SetLegend (
"Time (seconds)",
"Percent");
662 gnuplot.
SetTitle (
"Percentage time AP1 in Idle state vs time");
Gnuplot2dDataset m_output_idle
void Set(std::string name, const AttributeValue &v)
holds a vector of ns3::Application pointers.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
void SetupPhy(Ptr< WifiPhy > phy)
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Simulation virtual time values and global simulation resolution.
void SetType(std::string type, Args &&... args)
Gnuplot2dDataset m_output_busy
Gnuplot2dDataset m_output_tx
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
Class to represent a 2D points plot.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void SetChannelWidth(uint16_t channelWidth)
Sets the selected channelWidth (in MHz)
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Make it easy to create and manage PHY objects for the YANS model.
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
Ipv4Address destinationAddress
Destination address.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
static void Run(void)
Run the simulation.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
const FlowStatsContainer & GetFlowStats() const
Retrieve all collected the flow statistics.
aggregate IP/TCP/UDP functionality to existing Nodes.
void RateCallback(std::string path, DataRate oldRate, DataRate newRate, Mac48Address dest)
void AddDataset(const GnuplotDataset &dataset)
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
helps to create WifiNetDevice objects
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Gnuplot2dDataset GetPowerDatafile()
a polymophic address class
Ptr< YansWifiChannel > Create(void) const
Gnuplot2dDataset GetDatafile()
void RxCallback(std::string path, Ptr< const Packet > packet, const Address &from)
void StateCallback(std::string path, Time init, Time duration, WifiPhyState state)
Class for representing data rates.
void SetChannel(Ptr< YansWifiChannel > channel)
Time GetCalcTxTime(DataRate rate)
void PhyCallback(std::string path, Ptr< const Packet > packet, double powerW)
void PowerCallback(std::string path, double oldPower, double newPower, Mac48Address dest)
Gnuplot2dDataset GetRxDatafile()
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
AttributeValue implementation for Time.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
void SetTitle(const std::string &title)
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Hold an unsigned integer type.
holds a vector of ns3::NetDevice pointers
The PHY layer has sense the medium busy through the CCA mechanism.
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Ptr< FlowMonitor > InstallAll()
Enable flow monitoring on all nodes.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
void Connect(std::string path, const CallbackBase &cb)
void SetLegend(const std::string &xLegend, const std::string &yLegend)
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Ptr< WifiPhy > GetPhy(void) const
Ptr< FlowClassifier > GetClassifier()
Retrieve the FlowClassifier object for IPv4 created by the Install* methods.
void RateCallback(std::string path, DataRate oldRate, DataRate newRate, Mac48Address dest)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void SetPreambleType(WifiPreamble preamble)
Sets the preamble type.
void PowerCallback(std::string path, double oldPower, double newPower, Mac48Address dest)
WifiPhyState
The state of the PHY layer.
std::vector< std::pair< Time, DataRate > > TxTime
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
Helper to enable IP flow monitoring on a set of Nodes.
manage and create wifi channel objects for the YANS model.
create MAC layers for a ns3::WifiNetDevice.
Structure to classify a packet.
static Time Now(void)
Return the current simulation virtual time.
Gnuplot2dDataset GetIdleDatafile()
The IEEE 802.11 SSID Information Element.
void SetMode(WifiMode mode)
Sets the selected payload transmission mode.
static const uint32_t packetSize
Gnuplot2dDataset m_output_rx
Helper class used to assign positions and mobility models to nodes.
FiveTuple FindFlow(FlowId flowId) const
Searches for the FiveTuple corresponding to the given flowId.
Ipv4 addresses are stored in host order in this class.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ptr< WifiMac > GetMac(void) const
The PHY layer is sending a packet.
Gnuplot2dDataset GetBusyDatafile()
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Time Seconds(double value)
Construct a Time in the indicated unit.
uint32_t GetN(void) const
Get the number of Ptr<NetDevice> stored in this container.
AttributeValue implementation for Ssid.
The PHY layer is receiving a packet.
void Add(Vector v)
Add a position to the list of positions.
NodeStatistics(NetDeviceContainer aps, NetDeviceContainer stas)
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void CheckStatistics(double time)
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetTerminal(const std::string &terminal)
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Gnuplot2dDataset GetTxDatafile()
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Ipv4Address sourceAddress
Source address.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...