mirror of
https://gitlab.com/manzerbredes/paper-lowrate-iot.git
synced 2025-04-19 04:09:43 +00:00
335 lines
13 KiB
C++
335 lines
13 KiB
C++
![]() |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
||
|
/*
|
||
|
* This program is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License version 2 as
|
||
|
* published by the Free Software Foundation;
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; if not, write to the Free Software
|
||
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
*/
|
||
|
|
||
|
#include <fstream>
|
||
|
#include <iostream>
|
||
|
#include <string>
|
||
|
#include "ns3/core-module.h"
|
||
|
#include "ns3/point-to-point-module.h"
|
||
|
#include "ns3/network-module.h"
|
||
|
#include "ns3/config-store-module.h"
|
||
|
#include "ns3/energy-module.h"
|
||
|
#include "ns3/applications-module.h"
|
||
|
#include "ns3/wifi-module.h"
|
||
|
#include "ns3/mobility-module.h"
|
||
|
#include "ns3/internet-module.h"
|
||
|
#include "ns3/ipv4-global-routing-helper.h"
|
||
|
#include "ns3/flow-monitor-module.h"
|
||
|
|
||
|
// Default Network Topology
|
||
|
//
|
||
|
// Number of wifi Sta or Edge nodes can be increased up to 250
|
||
|
// |
|
||
|
// Rank 0 | Rank 1
|
||
|
// -------------------------|----------------------------
|
||
|
// Wifi Sta 10.1.3.0
|
||
|
// AP
|
||
|
// * * * *
|
||
|
// | | | | 10.1.1.0
|
||
|
// n3 n4 n5 n0 -------------- n1 n2
|
||
|
// point-to-point | |
|
||
|
// * *
|
||
|
// Ap
|
||
|
// WiFi Edge 10.1.2.0
|
||
|
|
||
|
using namespace ns3;
|
||
|
|
||
|
NS_LOG_COMPONENT_DEFINE ("WifiEnergyExample");
|
||
|
|
||
|
uint32_t PhyTxDropCount =0;
|
||
|
uint32_t PhyRxDropCount =0;
|
||
|
double offeredLoad = 0.0;
|
||
|
double throughput = 0.0;
|
||
|
double lastPacketTime = 0.0;
|
||
|
|
||
|
//FlowMonitorHelper flowmon;
|
||
|
|
||
|
/// Trace function for remaining energy at node.
|
||
|
void
|
||
|
RemainingEnergy (double oldValue, double remainingEnergy)
|
||
|
{
|
||
|
NS_LOG_UNCOND (Simulator::Now ().GetSeconds ()
|
||
|
<< "s Current remaining energy = " << remainingEnergy << "J");
|
||
|
}
|
||
|
|
||
|
/// Trace function for total energy consumption at node.
|
||
|
void
|
||
|
TotalEnergy (double oldValue, double newValue)
|
||
|
{
|
||
|
NS_LOG_UNCOND (Simulator::Now ().GetSeconds ()
|
||
|
<< "AIE\t"<< newValue-oldValue<<"\t"<<newValue);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
PhyTxDrop(Ptr<const Packet> p)
|
||
|
{
|
||
|
NS_LOG_UNCOND(Simulator::Now ().GetSeconds ()<<" Tx Packet Drop "<<++PhyTxDropCount);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
PhyRxDrop(Ptr<const Packet> p)
|
||
|
{
|
||
|
NS_LOG_UNCOND(Simulator::Now ().GetSeconds ()<<" Rx Packet Drop "<<++PhyRxDropCount);
|
||
|
}
|
||
|
void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon)
|
||
|
{
|
||
|
std::map<FlowId, FlowMonitor::FlowStats> flowStats = flowMon->GetFlowStats();
|
||
|
Ptr<Ipv4FlowClassifier> classing = DynamicCast<Ipv4FlowClassifier> (fmhelper->GetClassifier());
|
||
|
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator stats = flowStats.begin (); stats != flowStats.end (); ++stats)
|
||
|
{
|
||
|
Ipv4FlowClassifier::FiveTuple fiveTuple = classing->FindFlow (stats->first);
|
||
|
std::cout<<"---------------------------------------------------------------------------"<<std::endl;
|
||
|
std::cout<<"Flow ID : " << stats->first <<" ; "<< fiveTuple.sourceAddress <<" -----> "<<fiveTuple.destinationAddress<<std::endl;
|
||
|
std::cout<<"Tx Packets = " << stats->second.txPackets<<std::endl;
|
||
|
std::cout<<"Rx Packets = " << stats->second.rxPackets<<std::endl;
|
||
|
std::cout<<"Duration : "<<stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds()<<std::endl;
|
||
|
std::cout<<"Last Received Packet : "<< stats->second.timeLastRxPacket.GetSeconds()<<" Seconds"<<std::endl;
|
||
|
std::cout<<"Throughput: " << stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps"<<std::endl;
|
||
|
std::cout<<"---------------------------------------------------------------------------"<<std::endl;
|
||
|
}
|
||
|
Simulator::Schedule(Seconds(0.1),&ThroughputMonitor, fmhelper, flowMon);
|
||
|
|
||
|
}
|
||
|
|
||
|
int
|
||
|
main (int argc, char *argv[])
|
||
|
{
|
||
|
bool verbose = true;
|
||
|
uint32_t nWifiEdge = 1;
|
||
|
bool tracing = false;
|
||
|
double interval = 0.01; // ms
|
||
|
uint32_t maxPackets = 100000;
|
||
|
double simulationTime = 2.1;
|
||
|
|
||
|
CommandLine cmd;
|
||
|
cmd.AddValue ("nWifiEdge", "Number of wifi edge nodes/devices", nWifiEdge);
|
||
|
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
|
||
|
cmd.AddValue ("tracing", "Enable pcap tracing", tracing);
|
||
|
cmd.AddValue ("interval", "Interval between packets", interval);
|
||
|
cmd.AddValue ("maxPackets", "Maximum number of packets", maxPackets);
|
||
|
|
||
|
|
||
|
cmd.Parse (argc,argv);
|
||
|
|
||
|
// turn off RTS/CTS for frames below 2200 bytes
|
||
|
//Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
|
||
|
|
||
|
NodeContainer edgeApNode;
|
||
|
edgeApNode.Create (1);
|
||
|
NodeContainer wifiEdgeNodes;
|
||
|
wifiEdgeNodes.Create (nWifiEdge);
|
||
|
|
||
|
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
|
||
|
YansWifiPhyHelper phyEdge = YansWifiPhyHelper::Default ();
|
||
|
phyEdge.SetChannel (channel.Create ());
|
||
|
phyEdge.Set ("TxAntennas", UintegerValue (4));
|
||
|
phyEdge.Set ("RxAntennas", UintegerValue (4));
|
||
|
|
||
|
|
||
|
WifiHelper wifi;
|
||
|
wifi.SetStandard (WIFI_PHY_STANDARD_80211n_5GHZ);
|
||
|
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager","DataMode", StringValue("HtMcs31"),"ControlMode",StringValue("HtMcs31"));
|
||
|
|
||
|
|
||
|
|
||
|
WifiMacHelper macEdge;
|
||
|
Ssid ssid = Ssid ("ns-3-ssid-edge");
|
||
|
macEdge.SetType ("ns3::StaWifiMac",
|
||
|
"Ssid", SsidValue (ssid),
|
||
|
"ActiveProbing", BooleanValue (false));
|
||
|
|
||
|
NetDeviceContainer edgeDevices;
|
||
|
edgeDevices = wifi.Install (phyEdge, macEdge, wifiEdgeNodes);
|
||
|
|
||
|
macEdge.SetType ("ns3::ApWifiMac",
|
||
|
"Ssid", SsidValue (ssid));
|
||
|
|
||
|
NetDeviceContainer edgeApDevices;
|
||
|
edgeApDevices = wifi.Install (phyEdge, macEdge, edgeApNode);
|
||
|
|
||
|
MobilityHelper mobility;
|
||
|
|
||
|
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
|
||
|
"MinX", DoubleValue (0.0),
|
||
|
"MinY", DoubleValue (0.0),
|
||
|
"DeltaX", DoubleValue (0.5),
|
||
|
"DeltaY", DoubleValue (0.5),
|
||
|
"GridWidth", UintegerValue (3),
|
||
|
"LayoutType", StringValue ("RowFirst"));
|
||
|
|
||
|
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
|
||
|
"Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
|
||
|
|
||
|
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
|
||
|
mobility.Install (edgeApNode);
|
||
|
mobility.Install (wifiEdgeNodes);
|
||
|
|
||
|
InternetStackHelper stack;
|
||
|
stack.Install (edgeApNode);
|
||
|
stack.Install (wifiEdgeNodes);
|
||
|
|
||
|
Ipv4AddressHelper address;
|
||
|
|
||
|
address.SetBase ("10.1.2.0", "255.255.255.0");
|
||
|
Ipv4InterfaceContainer edgeInterfaces;
|
||
|
Ipv4InterfaceContainer edgeApInterfaces;
|
||
|
edgeInterfaces = address.Assign (edgeDevices);
|
||
|
edgeApInterfaces = address.Assign(edgeApDevices);
|
||
|
|
||
|
|
||
|
/** Energy Model **/
|
||
|
/***************************************************************************/
|
||
|
/* energy source */
|
||
|
BasicEnergySourceHelper edgeBasicSourceHelper;
|
||
|
// configure energy source
|
||
|
|
||
|
edgeBasicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (2.9009));
|
||
|
edgeBasicSourceHelper.Set ("BasicEnergySupplyVoltageV", DoubleValue (3.3));
|
||
|
// install source
|
||
|
EnergySourceContainer wifiEdgeNodesSources = edgeBasicSourceHelper.Install (wifiEdgeNodes);
|
||
|
EnergySourceContainer apEdgeNodesSources = edgeBasicSourceHelper.Install (edgeApNode);
|
||
|
|
||
|
/* device energy model */
|
||
|
WifiRadioEnergyModelHelper radioEnergyHelper;
|
||
|
// configure radio energy model
|
||
|
radioEnergyHelper.Set ("TxCurrentA", DoubleValue (0.38));
|
||
|
radioEnergyHelper.Set ("RxCurrentA", DoubleValue (0.313));
|
||
|
radioEnergyHelper.Set ("IdleCurrentA", DoubleValue (0.273));
|
||
|
// install device model
|
||
|
DeviceEnergyModelContainer edgeDeviceModels = radioEnergyHelper.Install (edgeDevices, wifiEdgeNodesSources);
|
||
|
DeviceEnergyModelContainer edgeApDeviceModels = radioEnergyHelper.Install (edgeApDevices, apEdgeNodesSources);
|
||
|
|
||
|
/***************************************************************************/
|
||
|
//Setting applications
|
||
|
UdpServerHelper myServer (9);
|
||
|
|
||
|
ApplicationContainer serverApps = myServer.Install (edgeApNode.Get (0));
|
||
|
serverApps.Start (Seconds (0));
|
||
|
serverApps.Stop (Seconds (simulationTime));
|
||
|
|
||
|
//UdpEchoClientHelper client (serverAddress, port);
|
||
|
UdpClientHelper myClient (edgeApInterfaces.GetAddress (0), 9);
|
||
|
myClient.SetAttribute ("MaxPackets", UintegerValue (maxPackets));
|
||
|
myClient.SetAttribute ("Interval", TimeValue (Seconds ((interval/1000.0))));
|
||
|
myClient.SetAttribute ("PacketSize", UintegerValue (1472)); // in Bytes
|
||
|
|
||
|
ApplicationContainer clientApps = myClient.Install (wifiEdgeNodes.Get (0));
|
||
|
clientApps.Start (Seconds (1.0));
|
||
|
clientApps.Stop (Seconds (simulationTime));
|
||
|
|
||
|
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
|
||
|
|
||
|
/** connect trace sources **/
|
||
|
/***************************************************************************/
|
||
|
// all sources are connected to node 1
|
||
|
// energy source
|
||
|
|
||
|
Ptr<BasicEnergySource> basicSourcePtr0 = DynamicCast<BasicEnergySource> (wifiEdgeNodesSources.Get (0));
|
||
|
|
||
|
basicSourcePtr0->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
|
||
|
//device energy model
|
||
|
|
||
|
Ptr<DeviceEnergyModel> basicRadioModelPtr0 =
|
||
|
basicSourcePtr0->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel").Get (0);
|
||
|
|
||
|
NS_ASSERT (basicRadioModelPtr0 != NULL);
|
||
|
basicRadioModelPtr0->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
|
||
|
|
||
|
|
||
|
if (tracing == true)
|
||
|
{
|
||
|
//AsciiTraceHelper ascii;
|
||
|
//pointToPoint.EnableAsciiAll(ascii.CreateFileStream ("energy-wifi.tr"));
|
||
|
//pointToPoint.EnablePcapAll ("wifiEnergy");
|
||
|
// phySta.EnablePcapAll ("wifiEnergy");
|
||
|
phyEdge.EnablePcapAll ("wifiEnergy");
|
||
|
|
||
|
}
|
||
|
|
||
|
//Set simulation time and launch simulation
|
||
|
Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxDrop", MakeCallback(&PhyRxDrop));
|
||
|
Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxDrop", MakeCallback(&PhyTxDrop));
|
||
|
|
||
|
|
||
|
// edgeDevices.Get(0)->TraceConnectWithoutContext("PhyTxDrop",MakeCallback(&PhyTxDrop));
|
||
|
|
||
|
|
||
|
///
|
||
|
// FlowMonitor
|
||
|
///
|
||
|
FlowMonitorHelper fmHelper;
|
||
|
Ptr<FlowMonitor> allMon = fmHelper.InstallAll();
|
||
|
Simulator::Schedule(Seconds(1),&ThroughputMonitor,&fmHelper, allMon);
|
||
|
|
||
|
|
||
|
FlowMonitorHelper flowmon;
|
||
|
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
|
||
|
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
|
||
|
|
||
|
Simulator::Stop (Seconds (simulationTime));
|
||
|
Simulator::Run ();
|
||
|
|
||
|
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
|
||
|
|
||
|
double totalThroughput = 0.0;
|
||
|
double totalLostPackets = 0.0;
|
||
|
for (std::map< FlowId, FlowMonitor::FlowStats>::iterator flow=stats.begin(); flow!=stats.end(); flow++)
|
||
|
{
|
||
|
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow(flow->first);
|
||
|
NS_LOG_UNCOND( "\nFlowID: " << flow->first << " ("
|
||
|
<< t.sourceAddress << " / " << t.sourcePort << " --> "
|
||
|
<< t.destinationAddress << " / " << t.destinationPort << ")" );
|
||
|
NS_LOG_UNCOND( " Tx Packets: " << flow->second.txPackets );
|
||
|
NS_LOG_UNCOND( " Tx Bytes: " << flow->second.txBytes );
|
||
|
|
||
|
offeredLoad = flow->second.txBytes * 8.0 / (flow->second.timeLastTxPacket.GetSeconds () - flow->second.timeFirstTxPacket.GetSeconds ()) / 1000000 ;
|
||
|
NS_LOG_UNCOND( " Offered Load: " << offeredLoad << " Mbps");
|
||
|
|
||
|
NS_LOG_UNCOND( " Rx Packets: " << flow->second.rxPackets );
|
||
|
NS_LOG_UNCOND( " Rx Bytes: " << flow->second.rxBytes );
|
||
|
|
||
|
throughput = flow->second.rxBytes * 8.0 / (flow->second.timeLastRxPacket.GetSeconds () - flow->second.timeFirstRxPacket.GetSeconds ()) / 1000000;
|
||
|
NS_LOG_UNCOND( " Throughput: " << throughput << " Mbps");
|
||
|
|
||
|
NS_LOG_UNCOND( " Lost Packets: " << flow->second.lostPackets );
|
||
|
NS_LOG_UNCOND( " Time last packet Transmited: " << flow->second.timeLastTxPacket.GetSeconds() );
|
||
|
NS_LOG_UNCOND( " Time last packet Received: " << flow->second.timeLastRxPacket.GetSeconds() );
|
||
|
|
||
|
if(t.destinationPort == 9){
|
||
|
|
||
|
totalThroughput = totalThroughput + throughput;
|
||
|
totalLostPackets = totalLostPackets + flow->second.lostPackets;
|
||
|
|
||
|
}
|
||
|
if(t.destinationPort == 9 && (lastPacketTime < flow->second.timeLastRxPacket.GetSeconds ())){
|
||
|
|
||
|
lastPacketTime = flow->second.timeLastRxPacket.GetSeconds ();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
double averageThroughput = totalThroughput/nWifiEdge;
|
||
|
double averageLostPackets = totalLostPackets/nWifiEdge;
|
||
|
|
||
|
NS_LOG_UNCOND( "\nLast Packet Time: " << lastPacketTime);
|
||
|
NS_LOG_UNCOND( "Average Throughput: " << averageThroughput << " Mbps");
|
||
|
NS_LOG_UNCOND( "Average Lost Packet : " << averageLostPackets);
|
||
|
|
||
|
|
||
|
Simulator::Destroy ();
|
||
|
|
||
|
return 0;
|
||
|
}
|