Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement Decentralized Networks in ns3

To implement the decentralized networks in ns3 consists to make a network topology where nodes interact directly with each other without depend on the central server or control node. This is usually in peer to peer (P2P) network or mesh network. Here are the complete procedures on how to set up a decentralized network in ns3 environment.

Step-by-Step Implementation

  1. Install ns-3

Ensure ns3 is installed on your system.

  1. Define the Network Topology

Define the network topology including multiple nodes that will communicate with each other in a decentralized manner.

  1. Create Network Nodes

Create network nodes using NodeContainer.

NodeContainer nodes;

nodes.Create(10); // Create 10 nodes for the decentralized network

  1. Set Up Network Devices

Install network devices on the nodes using appropriate network interfaces, such as WiFi for wireless communication.

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211n_5GHZ);

 

WifiMacHelper mac;

mac.SetType(“ns3::AdhocWifiMac”); // Use AdhocWifiMac for a decentralized network

YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

phy.SetChannel(channel.Create());

NetDeviceContainer devices = wifi.Install(phy, mac, nodes);

  1. Configure Mobility Model

Set up the mobility model for the nodes to simulate movement in the network.

MobilityHelper mobility;

mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,

                          “Speed”, StringValue(“ns3::ConstantRandomVariable[Constant=1.0]”),

                          “Pause”, StringValue(“ns3::ConstantRandomVariable[Constant=0.5]”),

                          “PositionAllocator”, StringValue(“ns3::GridPositionAllocator”));

mobility.Install(nodes);

  1. Set Up Routing Protocols

Configure routing protocols for the decentralized network. Ad hoc routing protocols like AODV, DSDV, or OLSR are suitable for decentralized networks.

AodvHelper aodv;

InternetStackHelper internet;

internet.SetRoutingHelper(aodv); // Use AODV as the routing protocol

internet.Install(nodes);

  1. Assign IP Addresses

Assign IP addresses to the network devices.

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

  1. Implement a Decentralized Application

Create an application that sends and receives data between nodes in the network. Below is a simple example of an application that sends packets between nodes.

Decentralized Application (Example)

class DecentralizedApplication : public Application {

public:

    void StartApplication() override {

        recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), 9));

        recvSocket->SetRecvCallback(MakeCallback(&DecentralizedApplication::HandleRead, this));

        sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        // Schedule the first packet send

        Simulator::Schedule(Seconds(1.0), &DecentralizedApplication::SendPacket, this);

    }

    void HandleRead(Ptr<Socket> socket) {

        Ptr<Packet> packet;

        Address from;

        while ((packet = socket->RecvFrom(from))) {

            std::cout << “Packet received at node ” << GetNode()->GetId() << ” from node ” << InetSocketAddress::ConvertFrom(from).GetIpv4() << std::endl;

        }

    }

    void SendPacket() {

        Ptr<UniformRandomVariable> rand = CreateObject<UniformRandomVariable>();

        uint32_t destNode = rand->GetInteger(0, nodes.GetN() – 1);

        while (destNode == GetNode()->GetId()) {

            destNode = rand->GetInteger(0, nodes.GetN() – 1);

        }

        Ipv4Address destAddr = nodes.Get(destNode)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal();

        Ptr<Packet> packet = Create<Packet>(100);

        sendSocket->SendTo(packet, 0, InetSocketAddress(destAddr, 9));

        // Schedule the next packet send

        Simulator::Schedule(Seconds(1.0), &DecentralizedApplication::SendPacket, this);

    }

private:

    Ptr<Socket> recvSocket;

    Ptr<Socket> sendSocket;

};

ApplicationContainer apps;

for (uint32_t i = 0; i < nodes.GetN(); ++i) {

    Ptr<DecentralizedApplication> app = CreateObject<DecentralizedApplication>();

    nodes.Get(i)->AddApplication(app);

    app->SetStartTime(Seconds(1.0));

    app->SetStopTime(Seconds(20.0));

    apps.Add(app);

}

  1. Set Up Packet Capture

Configure packet capture on the nodes to analyze the network traffic.

PcapHelper pcapHelper;

phy.EnablePcapAll(“decentralized_network”);

  1. Run the Simulation

Configure the simulation runtime and execute it.

Simulator::Stop(Seconds(20.0));

Simulator::Run();

Simulator::Destroy();

Example of a Simple Decentralized Network Script

Here is the sample to complete the script for decentralized network in ns-3 environment that are;

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/applications-module.h”

#include “ns3/aodv-module.h”

using namespace ns3;

NodeContainer nodes;

class DecentralizedApplication : public Application {

public:

    void StartApplication() override {

        recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), 9));

        recvSocket->SetRecvCallback(MakeCallback(&DecentralizedApplication::HandleRead, this));

        sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        // Schedule the first packet send

        Simulator::Schedule(Seconds(1.0), &DecentralizedApplication::SendPacket, this);

    }

    void HandleRead(Ptr<Socket> socket) {

        Ptr<Packet> packet;

        Address from;

        while ((packet = socket->RecvFrom(from))) {

            std::cout << “Packet received at node ” << GetNode()->GetId() << ” from node ” << InetSocketAddress::ConvertFrom(from).GetIpv4() << std::endl;

        }

    }

    void SendPacket() {

        Ptr<UniformRandomVariable> rand = CreateObject<UniformRandomVariable>();

        uint32_t destNode = rand->GetInteger(0, nodes.GetN() – 1);

        while (destNode == GetNode()->GetId()) {

            destNode = rand->GetInteger(0, nodes.GetN() – 1);

        }

        Ipv4Address destAddr = nodes.Get(destNode)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal();

        Ptr<Packet> packet = Create<Packet>(100);

        sendSocket->SendTo(packet, 0, InetSocketAddress(destAddr, 9));

        // Schedule the next packet send

        Simulator::Schedule(Seconds(1.0), &DecentralizedApplication::SendPacket, this);

    }

private:

    Ptr<Socket> recvSocket;

    Ptr<Socket> sendSocket;

};

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

    nodes.Create(10);

    // WiFi setup

    WifiHelper wifi;

    wifi.SetStandard(WIFI_PHY_STANDARD_80211n_5GHZ);

    WifiMacHelper mac;

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

    YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

    YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

    phy.SetChannel(channel.Create());

    NetDeviceContainer devices = wifi.Install(phy, mac, nodes);

    // Mobility setup

    MobilityHelper mobility;

    mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,

                              “Speed”, StringValue(“ns3::ConstantRandomVariable[Constant=1.0]”),

                              “Pause”, StringValue(“ns3::ConstantRandomVariable[Constant=0.5]”),

                              “PositionAllocator”, StringValue(“ns3::GridPositionAllocator”));

   mobility.Install(nodes);

    // Internet stack and routing

    AodvHelper aodv;

    InternetStackHelper internet;

    internet.SetRoutingHelper(aodv);

    internet.Install(nodes);

    Ipv4AddressHelper address;

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

    Ipv4InterfaceContainer interfaces = address.Assign(devices);

    // Install decentralized applications

    ApplicationContainer apps;

    for (uint32_t i = 0; i < nodes.GetN(); ++i) {

        Ptr<DecentralizedApplication> app = CreateObject<DecentralizedApplication>();

        nodes.Get(i)->AddApplication(app);

        app->SetStartTime(Seconds(1.0));

        app->SetStopTime(Seconds(20.0));

        apps.Add(app);

    }

    // Set up packet capture

    PcapHelper pcapHelper;

    phy.EnablePcapAll(“decentralized_network”);

    Simulator::Stop(Seconds(20.0));

    Simulator::Run();

    Simulator::Destroy();

    return 0;

}

To conclusion we have seen about the performance analysis in decentralized network in ns-3 environment and further we provide and help further information related to decentralized network that adjust in diverse environment. Performance Analysis will be done by comparing  latest papers on Decentralized Networks in ns3.