mirror of
https://gitlab.com/manzerbredes/paper-lowrate-iot.git
synced 2025-06-05 22:27:39 +00:00
Update code
This commit is contained in:
parent
c40644ec64
commit
23e8f7f688
2 changed files with 74 additions and 30 deletions
Binary file not shown.
|
@ -20,15 +20,18 @@
|
||||||
#include "ns3/constant-position-mobility-model.h"
|
#include "ns3/constant-position-mobility-model.h"
|
||||||
#include "ns3/energy-module.h"
|
#include "ns3/energy-module.h"
|
||||||
#include "ns3/wifi-radio-energy-model-helper.h"
|
#include "ns3/wifi-radio-energy-model-helper.h"
|
||||||
|
#include "ns3/point-to-point-helper.h"
|
||||||
|
|
||||||
// C++ library
|
// C++ library
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <utility> // To use std::pair
|
#include <utility> // To use std::pair
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
using namespace ns3;
|
using namespace ns3;
|
||||||
NS_LOG_COMPONENT_DEFINE ("wifi-tcp");
|
NS_LOG_COMPONENT_DEFINE ("wifi-tcp");
|
||||||
|
|
||||||
|
Ipv4Address cloudIP;
|
||||||
|
int cloudPort=50;
|
||||||
|
|
||||||
|
|
||||||
// ---------- Code ----------
|
// ---------- Code ----------
|
||||||
|
@ -37,22 +40,37 @@ typedef std::pair<NodeContainer,NodeContainer> CellNodes;
|
||||||
typedef std::pair<NetDeviceContainer,NetDeviceContainer> CellNetDevices;
|
typedef std::pair<NetDeviceContainer,NetDeviceContainer> CellNetDevices;
|
||||||
typedef std::pair<CellNodes,CellNetDevices> Cell;
|
typedef std::pair<CellNodes,CellNetDevices> Cell;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void CloudSwitchRx(Ptr< const Packet > packet, const Address &address){
|
||||||
|
NS_LOG_UNCOND(std::setw(7)<<Simulator::Now ().GetSeconds ()<< " Cloud switch receive a packet!");
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TotalEnergy (std::string context,double oldValue, double newValue)
|
||||||
|
{
|
||||||
|
NS_LOG_UNCOND ("Energy Value of node " << context << " at time " <<Simulator::Now ().GetSeconds ()
|
||||||
|
<< "\t"<< newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a sensors cell
|
* Create a sensors cell base on
|
||||||
|
* nbSensors Number of temperature sensors in the cell
|
||||||
|
* ap the Access Point (usually linked to the cloud)
|
||||||
*/
|
*/
|
||||||
Cell createCell(uint32_t nbSensors){
|
Cell createCell(uint32_t nbSensors, Ptr<ns3::Node> ap){
|
||||||
// Create sensors
|
// Create sensors
|
||||||
NodeContainer sensors;
|
NodeContainer sensors;
|
||||||
sensors.Create(nbSensors);
|
sensors.Create(nbSensors);
|
||||||
NodeContainer ap;
|
|
||||||
ap.Create(1);
|
|
||||||
|
|
||||||
// Place nodes somehow, this is required by every wireless simulation
|
// Place nodes somehow, this is required by every wireless simulation
|
||||||
for (uint8_t i = 0; i < nbSensors; ++i)
|
for (uint8_t i = 0; i < nbSensors; ++i)
|
||||||
{
|
{
|
||||||
sensors.Get (i)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
|
sensors.Get (i)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
|
||||||
}
|
}
|
||||||
ap.Get (0)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
|
ap->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
|
||||||
|
|
||||||
// To apply XXWifiPhy and WifiMac on sensors
|
// To apply XXWifiPhy and WifiMac on sensors
|
||||||
WifiHelper wifiHelper;
|
WifiHelper wifiHelper;
|
||||||
|
@ -92,6 +110,7 @@ Cell createCell(uint32_t nbSensors){
|
||||||
return(std::make_pair(std::make_pair(ap,sensors),std::make_pair(apNetDevice,sensorsNetDevices)));
|
return(std::make_pair(std::make_pair(ap,sensors),std::make_pair(apNetDevice,sensorsNetDevices)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Install network stack and applications
|
* Install network stack and applications
|
||||||
*/
|
*/
|
||||||
|
@ -103,16 +122,15 @@ void applyScenarios(Cell cell,int sensorsPktSize, int sensorsSendInterval){
|
||||||
|
|
||||||
// 6. Install TCP/IP stack & assign IP addresses
|
// 6. Install TCP/IP stack & assign IP addresses
|
||||||
InternetStackHelper internet;
|
InternetStackHelper internet;
|
||||||
internet.Install (ap);
|
// internet.Install (ap);
|
||||||
internet.Install (sensors);
|
internet.Install (sensors);
|
||||||
Ipv4AddressHelper ipv4;
|
Ipv4AddressHelper ipv4;
|
||||||
ipv4.SetBase ("10.0.0.0", "255.0.0.0");
|
ipv4.SetBase ("10.0.0.0", "255.255.255.0");
|
||||||
Ipv4InterfaceContainer apInt,sensorsInt;
|
Ipv4InterfaceContainer apInt,sensorsInt;
|
||||||
apInt=ipv4.Assign(apNetDev);
|
apInt=ipv4.Assign(apNetDev);
|
||||||
sensorsInt=ipv4.Assign(sensorsNetDev);
|
sensorsInt=ipv4.Assign(sensorsNetDev);
|
||||||
|
|
||||||
uint16_t echoPort = 9;
|
UdpEchoClientHelper echoClientHelper (InetSocketAddress (cloudIP, cloudPort));
|
||||||
UdpEchoClientHelper echoClientHelper (InetSocketAddress (apInt.GetAddress (0), echoPort));
|
|
||||||
// echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (10));
|
// echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (10));
|
||||||
echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (sensorsSendInterval)));
|
echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (sensorsSendInterval)));
|
||||||
echoClientHelper.SetAttribute ("PacketSize", UintegerValue (sensorsPktSize));
|
echoClientHelper.SetAttribute ("PacketSize", UintegerValue (sensorsPktSize));
|
||||||
|
@ -121,17 +139,43 @@ void applyScenarios(Cell cell,int sensorsPktSize, int sensorsSendInterval){
|
||||||
// again using different start times to workaround Bug 388 and Bug 912
|
// again using different start times to workaround Bug 388 and Bug 912
|
||||||
echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (1))); // Start at 1 (WIFI seems to not work when t<1)
|
echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (1))); // Start at 1 (WIFI seems to not work when t<1)
|
||||||
echoClientHelper.Install (sensors);
|
echoClientHelper.Install (sensors);
|
||||||
|
|
||||||
PacketSinkHelper apSink("ns3::UdpSocketFactory",InetSocketAddress (Ipv4Address::GetAny (), echoPort));
|
|
||||||
ApplicationContainer sinkApp=apSink.Install(ap);
|
|
||||||
sinkApp.Start (Seconds (0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
Ptr<ns3::Node> buildEdgeAndCloud(int nbOp){
|
||||||
TotalEnergy (std::string context,double oldValue, double newValue)
|
|
||||||
{
|
NodeContainer OpNodes;
|
||||||
NS_LOG_UNCOND ("Energy Value of node " << context << " at time " <<Simulator::Now ().GetSeconds ()
|
OpNodes.Create(nbOp+1);
|
||||||
<< "\t"<< newValue);
|
|
||||||
|
InternetStackHelper stack;
|
||||||
|
stack.Install(OpNodes);
|
||||||
|
|
||||||
|
for(int i=0;i<nbOp;i++){ // Not nbOp-1 (We add a AP)
|
||||||
|
NodeContainer curNodes(OpNodes.Get(i),OpNodes.Get(i+1));
|
||||||
|
|
||||||
|
PointToPointHelper pointToPoint;
|
||||||
|
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
|
||||||
|
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
|
||||||
|
|
||||||
|
NetDeviceContainer p2pDevices;
|
||||||
|
p2pDevices = pointToPoint.Install (curNodes);
|
||||||
|
|
||||||
|
Ipv4AddressHelper address;
|
||||||
|
address.SetBase (("10.1."+std::to_string(i)+".0").c_str(), "255.255.255.0");
|
||||||
|
Ipv4InterfaceContainer p2pInterfaces;
|
||||||
|
p2pInterfaces = address.Assign (p2pDevices);
|
||||||
|
|
||||||
|
// NS_LOG_UNCOND(i);
|
||||||
|
if(i==nbOp-1){ // If we are on the last Op
|
||||||
|
cloudIP=p2pInterfaces.GetAddress (1);
|
||||||
|
|
||||||
|
PacketSinkHelper apSink("ns3::UdpSocketFactory",InetSocketAddress (Ipv4Address::GetAny (), cloudPort));
|
||||||
|
ApplicationContainer sinkApp=apSink.Install(curNodes.Get(1));
|
||||||
|
sinkApp.Get(0)->TraceConnectWithoutContext("Rx",MakeCallback(&CloudSwitchRx));
|
||||||
|
sinkApp.Start (Seconds (0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return(OpNodes.Get(0));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -169,9 +213,6 @@ void setupEnergy(Cell cell){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Ptr<BasicEnergySource> basicSourcePtr0 = DynamicCast<BasicEnergySource> (wifiEdgeNodesSources.Get (0));
|
// Ptr<BasicEnergySource> basicSourcePtr0 = DynamicCast<BasicEnergySource> (wifiEdgeNodesSources.Get (0));
|
||||||
|
|
||||||
// //basicSourcePtr0->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
|
// //basicSourcePtr0->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
|
||||||
|
@ -189,24 +230,27 @@ void setupEnergy(Cell cell){
|
||||||
|
|
||||||
int main(int argc, char* argv[]){
|
int main(int argc, char* argv[]){
|
||||||
|
|
||||||
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
|
//LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
|
||||||
LogComponentEnable("PacketSink", LOG_LEVEL_INFO);
|
// LogComponentEnable("PacketSink", LOG_LEVEL_INFO);
|
||||||
|
|
||||||
uint32_t sensorsFrequency=1;
|
uint32_t sensorsFrequency=1;
|
||||||
uint32_t sensorsPktSize=150;
|
uint32_t sensorsPktSize=150;
|
||||||
uint32_t sensorsNumber=2;
|
uint32_t sensorsNumber=2;
|
||||||
|
uint32_t nbHop=5;
|
||||||
|
|
||||||
CommandLine cmd;
|
CommandLine cmd;
|
||||||
cmd.AddValue ("sensorsSendInterval", "Number of temperature measurement per second (default 1)", sensorsFrequency);
|
cmd.AddValue ("sensorsSendInterval", "Number of temperature measurement per second", sensorsFrequency);
|
||||||
cmd.AddValue ("sensorsPktSize", "Sensor measurements packet size (default 64 bytes)", sensorsPktSize);
|
cmd.AddValue ("sensorsPktSize", "Sensor measurements packet size (bytes)", sensorsPktSize);
|
||||||
cmd.AddValue ("sensorsNumber", "Number of sensors (default 10)", sensorsNumber);
|
cmd.AddValue ("sensorsNumber", "Number of sensors", sensorsNumber);
|
||||||
|
cmd.AddValue ("nbHop", "Number of hop between AP and Cloud sensors", sensorsNumber);
|
||||||
cmd.Parse (argc, argv);
|
cmd.Parse (argc, argv);
|
||||||
|
|
||||||
|
|
||||||
Cell c=createCell(sensorsNumber);
|
Cell c=createCell(sensorsNumber,buildEdgeAndCloud(nbHop));
|
||||||
|
|
||||||
applyScenarios(c,sensorsPktSize,sensorsFrequency);
|
applyScenarios(c,sensorsPktSize,sensorsFrequency);
|
||||||
setupEnergy(c);
|
// setupEnergy(c);
|
||||||
|
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
|
||||||
// Run simulators
|
// Run simulators
|
||||||
Simulator::Stop (Seconds (20));
|
Simulator::Stop (Seconds (20));
|
||||||
Simulator::Run ();
|
Simulator::Run ();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue