PEGASIS (Power-Efficient GAthering in Sensor Information System) is a chain-based protocol for Wireless Sensor Networks (WSNs). Here nodes communicate with their nearest neighbours and takes its turn to be the leader for transmitting data to the base station. To implement PEGASIS in ns3, we have to incorporate several steps. Because, ns3 does not natively support PEGASIS behavior. Hence, we need to create a custom application for simulating the PEGASIS protocol behavior.
Steps for implementing PEGASIS in ns3
- Set up your environment
Make sure that ns3 is installed in the computer. If not, install it from the official ns3 website.
- Create a new ns3 script
On your ns3 installation, create a new simulation script in the scratch directory. For Example, you can create a file named pegasis-simulation.cc.
cd ns-3.xx
cd scratch
touch pegasis-simulation.cc
- Include the necessary header files
In the pegasis-simulation.cc file, create the necessary header files for your simulation.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
#include “ns3/csma-module.h”
#include “ns3/wifi-module.h”
#include “ns3/internet-apps-module.h”
using namespace ns3;
- Define PEGASIS protocol logic
To implement PEGASIS protocol logic, you need to create a custom application or a helper class. Here is a basic example of structuring your PEGASIS implementation. This is a very high-level and simplified implementation to demonstrate the process.
Create a new file pegasis-protocol.h to define the PEGASIS protocol class.
#ifndef PEGASIS_PROTOCOL_H
#define PEGASIS_PROTOCOL_H
#include “ns3/application.h”
#include “ns3/socket.h”
#include “ns3/address.h”
#include “ns3/node-container.h”
namespace ns3 {
class PegasisProtocol : public Application
{
public:
static TypeId GetTypeId (void);
PegasisProtocol ();
virtual ~PegasisProtocol ();
void Setup (Ptr<Socket> socket, Address address, NodeContainer nodes);
protected:
virtual void StartApplication (void);
virtual void StopApplication (void);
private:
void ScheduleTransmit (Time dt);
void SendPacket (void);
Ptr<Socket> m_socket;
Address m_peer;
NodeContainer m_nodes;
EventId m_sendEvent;
bool m_running;
uint32_t m_packetSize;
uint32_t m_nPackets;
uint32_t m_packetsSent;
};
} // namespace ns3
#endif /* PEGASIS_PROTOCOL_H */
Create a new file pegasis-protocol.cc to implement the PEGASIS protocol class.
#include “pegasis-protocol.h”
#include “ns3/log.h”
#include “ns3/simulator.h”
#include “ns3/packet.h”
#include “ns3/address.h”
namespace ns3 {
NS_LOG_COMPONENT_DEFINE (“PegasisProtocol”);
NS_OBJECT_ENSURE_REGISTERED (PegasisProtocol);
TypeId
PegasisProtocol::GetTypeId (void)
{
static TypeId tid = TypeId (“ns3::PegasisProtocol”)
.SetParent<Application> ()
.SetGroupName (“Tutorial”)
.AddConstructor<PegasisProtocol> ();
return tid;
}
PegasisProtocol::PegasisProtocol ()
: m_socket (0),
m_peer (),
m_sendEvent (),
m_running (false),
m_packetSize (1024),
m_nPackets (1),
m_packetsSent (0)
{
}
PegasisProtocol::~PegasisProtocol ()
{
m_socket = 0;
}
void
PegasisProtocol::Setup (Ptr<Socket> socket, Address address, NodeContainer nodes)
{
m_socket = socket;
m_peer = address;
m_nodes = nodes;
}
void
PegasisProtocol::StartApplication (void)
{
m_running = true;
m_packetsSent = 0;
m_socket->Connect (m_peer);
SendPacket ();
}
void
PegasisProtocol::StopApplication (void)
{
m_running = false;
if (m_sendEvent.IsRunning ())
{
Simulator::Cancel (m_sendEvent);
}
if (m_socket)
{
m_socket->Close ();
}
}
void
PegasisProtocol::SendPacket (void)
{
Ptr<Packet> packet = Create<Packet> (m_packetSize);
m_socket->Send (packet);
if (++m_packetsSent < m_nPackets)
{
ScheduleTransmit (Seconds (1.0));
}
}
void
PegasisProtocol::ScheduleTransmit (Time dt)
{
if (m_running)
{
m_sendEvent = Simulator::Schedule (dt, &PegasisProtocol::SendPacket, this);
}
}
} // namespace ns3
- Set the network topology
Modify the pegasis-simulation.cc directory, to use the PEGASIS protocol in your scratch directory.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
#include “ns3/csma-module.h”
#include “ns3/wifi-module.h”
#include “ns3/internet-apps-module.h”
#include “pegasis-protocol.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“PegasisSimulation”);
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (10);
// Set up mobility model
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (5.0),
“DeltaY”, DoubleValue (5.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
// Set up WiFi
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiMacHelper wifiMac;
wifiMac.SetType (“ns3::AdhocWifiMac”);
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up PEGASIS protocol
Ptr<Socket> recvSink = Socket::CreateSocket (nodes.Get (0), TypeId::LookupByName (“ns3::UdpSocketFactory”));
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
recvSink->Bind (local);
Ptr<Socket> source = Socket::CreateSocket (nodes.Get (9), TypeId::LookupByName (“ns3::UdpSocketFactory”));
InetSocketAddress remote = InetSocketAddress (interfaces.GetAddress (0), 80);
source->Connect (remote);
Ptr<PegasisProtocol> app = CreateObject<PegasisProtocol> ();
app->Setup (source, remote, nodes);
nodes.Get (9)->AddApplication (app);
app->SetStartTime (Seconds (1.0));
app->SetStopTime (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
- Build and run the simulation
Build and run your script after writing.
./waf build
./waf –run scratch/pegasis-simulation
- Analyze the results
You can analyze the results based on your needs after running the simulation. Also, you can add logging or tracing to observe the behavior of the PEGASIS protocol.
Overall, we had implemented PEGASIS (Power-Efficient GAthering in Sensor Information System) by creating a custom application to simulate the PEGASIS protocol. Also, we provide more detailed implementation help for all areas of PEGASIS (Power-Efficient GAthering in Sensor Information System).