test.cc
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <fstream>
#include <iterator>
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
#include “ns3/internet-module.h”
#include “ns3/yans-wifi-helper.h”
#include “ns3/energy-module.h”
#include “ns3/wifi-radio-energy-model-helper.h”
#include “ns3/config-store-module.h”
#include “ns3/netanim-module.h”
#include “ns3/energy-source-container.h”
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE (“Test1”);
static void
GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount,Time pktInterval)
{
if(pktCount > 0)
{
socket->Send (Create<Packet> (pktSize));Simulator::Schedule(pktInterval, &GenerateTraffic, socket, pktSize,
pktCount - 1, pktInterval);
}
}
void EnergyPrint(EnergySourceContainer source, int rounds)
{
int id = 0; int k = 1;
double remain = 50;
for (EnergySourceContainer::Iterator c = source.Begin(); c!= source.End(); c++)
{
double energyRemain = (*c)->GetRemainingEnergy();if (energyRemain < remain)
{
remain = energyRemain;id = k;
}
NS_LOG_UNCOND(“Node “<<k<<“:”<<energyRemain<<“J”);
k++;
}
NS_LOG_UNCOND(“Rounds “<<rounds<<“: Node “<<id<<“ lowest energy “<<remain<<“J”);
}
void SendPacket(Ptr<Socket> source[], int Cluster_Size, double start,Ptr<DeviceEnergyModel> modelPtr)
{
double interval = 0.1;uint32_t PpacketSize = 5000;uint32_t numPackets = 1;
Time interPacketInterval = Seconds (interval);
for (int i = 0; i < Cluster_Size; i++)
{
if (i == 1)
{
modelPtr->SetAttribute(“RxCurrentA”, DoubleValue (0.45));
modelPtr->SetAttribute(“TxCurrentA”, DoubleValue (0.21));
modelPtr->SetAttribute(“IdleCurrentA”, DoubleValue (0.1));
modelPtr->SetAttribute(“SleepCurrentA”, DoubleValue (0.03));
}
Time time = Seconds(start + i*interval*1.5);
Simulator::Schedule (time, &GenerateTraffic, source[i], PpacketSize, numPackets, interPacketInterval);
if (i == 1)
{
modelPtr->SetAttribute(“RxCurrentA”, DoubleValue (0.00001));
modelPtr->SetAttribute(“TxCurrentA”, DoubleValue (0.00001));
modelPtr->SetAttribute(“IdleCurrentA”, DoubleValue (0.00001));
modelPtr->SetAttribute(“SleepCurrentA”, DoubleValue (0.00001));
}
}
}
void Remain(double old, double newv)
{
NS_LOG_UNCOND(Simulator::Now().GetSeconds()<<“s “<<newv<<“J”);
}
int main(int argc, const char** argv) {int Cluster_Size = 15;
// Create nodes
Ptr<Node> sink = CreateObject<Node>();NodeContainer sensor;sensor.Create(Cluster_Size);NodeContainer allnode;allnode.Add(sink);
allnode.Add(sensor);
// set up the phy layer (Wifi model used)YansWifiChannelHelper channel;
channel.SetPropagationDelay(“ns3::ConstantSpeedPropagationDelayModel”); //propagation delay & loss
channel.AddPropagationLoss(“ns3::RangePropagationLossModel”,“MaxRange”, DoubleValue(35.36));
YansWifiPhyHelper phy = YansWifiPhyHelper::Default();phy.SetChannel(channel.Create()); // error rate
// mac layerWifiHelper wifi;
wifi.SetStandard(WIFI_STANDARD_80211b);wifi.SetRemoteStationManager(“ns3::ConstantRateWifiManager”,
“DataMode”, StringValue (“DsssRate1Mbps”),“ControlMode”, StringValue (“DsssRate1Mbps”));
WifiMacHelper mac;mac.SetType(“ns3::AdhocWifiMac”);
NetDeviceContainer wifide;NetDeviceContainer sinkde;
wifide = wifi.Install(phy, mac, sensor);sinkde = wifi.Install(phy, mac, sink);NetDeviceContainer allde;allde.Add(sinkde);
allde.Add(wifide);
// The location of the nodesMobilityHelper model;
model.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);model.SetPositionAllocator(“ns3::RandomRectanglePositionAllocator”,
“X”,
StringValue(“ns3::UniformRandomVariable[Min=0|Max=50]”),“Y”,
StringValue(“ns3::UniformRandomVariable[Min=0|Max=50]”));model.Install(sensor);model.SetPositionAllocator(“ns3::GridPositionAllocator”,
“MinX”, DoubleValue(25),“MinY”, DoubleValue(25));
model.Install(sink);
// energy moduleBasicEnergySourceHelper energyhelper;
energyhelper.Set(“BasicEnergySourceInitialEnergyJ”, DoubleValue(5));EnergySourceContainer sources = energyhelper.Install(sensor);
WifiRadioEnergyModelHelper energymodel;energymodel.Set(“TxCurrentA”, DoubleValue(0.45));energymodel.Set(“RxCurrentA”, DoubleValue(0.21));energymodel.Set(“IdleCurrentA”, DoubleValue(0.1));energymodel.Set(“SleepCurrentA”, DoubleValue(0.03));DeviceEnergyModelContainer energydevice = energymodel.Install(wifide,
sources);
Ptr<BasicEnergySource> sourcePtr =DynamicCast<BasicEnergySource>(sources.Get(0));
sourcePtr->TraceConnectWithoutContext(“RemainingEnergy”,MakeCallback(&Remain));
Ptr<DeviceEnergyModel> modelPtr = sourcePtr-
>FindDeviceEnergyModels(“ns3::WifiRadioEnergyModel”).Get(0);
/** Internet stack **/InternetStackHelper internet;internet.Install (allnode);
Ipv4AddressHelper ipv4;
ipv4.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer il = ipv4.Assign (allde);TypeId tid = UdpSocketFactory::GetTypeId();
Ptr<Socket> recvSink = Socket::CreateSocket (sink, tid);
InetSocketAddress local = InetSocketAddress (Ipv4Address(il.GetAddress(0)), 80);
recvSink->Bind (local);
recvSink->SetAllowBroadcast(false);Ptr<Socket> source[Cluster_Size];
for(int i=0; i < Cluster_Size; i++){
InetSocketAddress remote = InetSocketAddress(Ipv4Address(il.GetAddress(i+1)), 80);
source[i] = Socket::CreateSocket(sensor.Get(i), tid);source[i]->Bind(remote);
source[i]->SetAllowBroadcast(false);source[i]->Connect(local);
}
/** simulation setup **/AnimationInterface anim (“test4.xml”);
// start trafficint rounds = 5;
// The Send Packet function: In each round, every node will send a packet to sink node.
// And it will repeat for given figure, at last, the energyprint function will list the
// remain energy of each node and tell the one with lowest energy.
// A real time trace function will tell the time energy stop tracing. for (int i = 0; i < rounds; i++)
{
SendPacket(source, Cluster_Size, double(2.2*i), modelPtr);
}
Simulator::Stop (Seconds (2.2*(rounds+1)));
Simulator::Run ();EnergyPrint(sources, rounds);
Simulator::Destroy ();return 0;
}
Output