How to Develop Spin Protocol Projects Using NS3

To develop the SPIN (Sensor Protocols for Information via Negotiation) Protocol for used NS-3. We contain the simulation outcomes for observe the following procedures:

Steps to Develop Spin Protocol Projects Using NS3

  1. Overview of SPIN Protocol

SPIN (Sensor Protocols for Information via Negotiation) is data-centric routing protocol used in Wireless Sensor Networks (WSNs). It decreases the redundant data transmission and secures the energy through using metadata negotiation before communicating data.

Key Features of SPIN:

  • Negotiation-based data transmission (Metadata prevents the unnecessary data transmission).
  • Adaptive: Nodes choose whether to communicate terms on their energy.
  • Three Types of Messages:
    • ADV (Advertisement): Advertises the accessible data.
    • REQ (Request): Demand with detailed data.
    • DATA: Forward the actual data.
  1. Setting up NS-3 for SPIN Implementation

Enable which NS-3 is installed on your system.

We start with install and download the tool NS-3

sudo apt update

sudo apt install git g++ python3 python3-pip cmake ninja-build

git clone https://gitlab.com/nsnam/ns-3-dev.git

cd ns-3-dev

./ns3 configure –enable-examples –enable-tests

./ns3 build

  1. Implementing SPIN Protocol in NS-3

Step 1: We describe the SPIN Packet Format (spin-header.h)

#ifndef SPIN_HEADER_H

#define SPIN_HEADER_H

#include “ns3/header.h”

namespace ns3 {

class SpinHeader : public Header {

public:

SpinHeader();

static TypeId GetTypeId();

virtual void Serialize(Buffer::Iterator start) const;

virtual uint32_t Deserialize(Buffer::Iterator start);

virtual uint32_t GetSerializedSize() const;

virtual void Print(std::ostream &os) const;

private:

uint8_t m_messageType; // 0 = ADV, 1 = REQ, 2 = DATA

uint16_t m_dataId;

};

} // namespace ns3

#endif

Step 2: Execute a SPIN Protocol Logic (spin-routing.cc)

#include “ns3/spin-header.h”

#include “ns3/spin-routing.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE(“SpinRouting”);

TypeId SpinRouting::GetTypeId() {

static TypeId tid = TypeId(“ns3::SpinRouting”)

.SetParent<Ipv4RoutingProtocol>()

.SetGroupName(“Internet”)

.AddConstructor<SpinRouting>();

return tid;

}

void SpinRouting::DoInitialize() {

NS_LOG_INFO(“Initializing SPIN Routing Protocol”);

Ipv4RoutingProtocol::DoInitialize();

}

// Function to send an ADV message

void SpinRouting::SendAdvertisement(Ptr<Packet> packet, Ipv4Address src, Ipv4Address dst) {

SpinHeader header;

header.SetMessageType(0); // ADV message

packet->AddHeader(header);

NS_LOG_INFO(“Sending SPIN ADV: ” << src << ” to ” << dst);

m_ipv4->Send(packet, src, dst, Ipv4Header::PROTO_SPIN);

}

// Function to send a REQ message

void SpinRouting::SendRequest(Ptr<Packet> packet, Ipv4Address src, Ipv4Address dst) {

SpinHeader header;

header.SetMessageType(1); // REQ message

packet->AddHeader(header);

NS_LOG_INFO(“Sending SPIN REQ: ” << src << ” to ” << dst);

m_ipv4->Send(packet, src, dst, Ipv4Header::PROTO_SPIN);

}

// Function to send a DATA message

void SpinRouting::SendData(Ptr<Packet> packet, Ipv4Address src, Ipv4Address dst) {

SpinHeader header;

header.SetMessageType(2); // DATA message

packet->AddHeader(header);

NS_LOG_INFO(“Sending SPIN DATA: ” << src << ” to ” << dst);

m_ipv4->Send(packet, src, dst, Ipv4Header::PROTO_SPIN);

}

} // namespace ns3

Step 3: Record the SPIN Protocol (spin-routing-helper.cc)

#include “ns3/spin-routing-helper.h”

#include “ns3/spin-routing.h”

namespace ns3 {

SpinRoutingHelper::SpinRoutingHelper() {}

Ptr<Ipv4RoutingProtocol> SpinRoutingHelper::Create() const {

return CreateObject<SpinRouting>();

}

} // namespace ns3

  1. Simulating SPIN in NS-3

Build a replication Script in (spin-test.cc)

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/mobility-module.h”

#include “ns3/wifi-module.h”

#include “ns3/spin-routing-helper.h”

using namespace ns3;

int main(int argc, char *argv[]) {

CommandLine cmd;

cmd.Parse(argc, argv);

NodeContainer sensorNodes;

sensorNodes.Create(5);

// Set mobility model

MobilityHelper mobility;

mobility.SetMobilityModel(“ns3::RandomWalk2dMobilityModel”);

mobility.Install(sensorNodes);

// Set WiFi communication

WifiHelper wifi;

wifi.SetStandard(WIFI_STANDARD_80211b);

WifiMacHelper mac;

YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

phy.SetChannel(channel.Create());

mac.SetType(“ns3::AdhocWifiMac”);

NetDeviceContainer devices;

devices = wifi.Install(phy, mac, sensorNodes);

InternetStackHelper internet;

SpinRoutingHelper spinRouting;

internet.SetRoutingHelper(spinRouting);

internet.Install(sensorNodes);

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

address.Assign(devices);

Simulator::Stop(Seconds(10));

Simulator::Run();

Simulator::Destroy();

return 0;

}

  1. Running the SPIN Simulation

Step 1: we create the simulation for NS-3 tool

./ns3 build

Step 2: Implement the replication process for this methods

./ns3 run “scratch/spin-test”

  1. Performance Analysis

Enable Logging

Changes the script for allow the recording:

LogComponentEnable(“SpinRouting”, LOG_LEVEL_INFO);

We gather the spin protocol for measure the Performance of parameter Metrics

Packet Delivery Ratio (PDR)

Ptr<PacketSink> sink = DynamicCast<PacketSink>(apps.Get(1));

double pdr = sink->GetTotalRx() / totalSentPackets;

Energy Consumption

double energyConsumed = initialEnergy – currentEnergy;

NS_LOG_INFO(“Total Energy Consumed: ” << energyConsumed << ” J”);

  1. Extending the Project
  • It associated the SPIN by Flooding and Gossiping protocols.
  • Validate for SPIN in dynamic networks (high mobility scenarios).
  • Improve the SPIN for large-scale WSNs.

The given above are brief approach to simulate the sensor protocol for information via negotiation using the tool of ns3 and also we deliver the sample snippets to execute the simulation in the network environment. Additional specific details regarding the SPIN will be provided in the upcoming manual.