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

How to Implement Epidemic protocol in ns3

To implement the Epidemic Routing Protocol in ns3, we have to follow some steps for the simulation process. Epidemic Routing protocol is mainly used in Delay Tolerant Networks (DTNs) in which that nodes carry and forward messages, simultaneously deliver the messages to their destination.

The below given steps will guide to implement the Epidemic Routing Protocol in ns3:

Step-by-Step Guide to Implement Epidemic Protocol in ns-3

Step 1: Set Up ns-3 Environment

Confirm that ns3 is installed and set up correctly on the system. And then Navigate to the src directory where we want to add the protocol.

Step 2: Create a New Protocol Class

Create a new directory for the protocol (e.g., src/epidemic-routing-protocol/), and add two files: epidemic-routing-protocol.h and epidemic-routing-protocol.cc.

epidemic-routing-protocol.h

This header file defines the structure and procedures of the protocol class.

#ifndef EPIDEMIC_ROUTING_PROTOCOL_H

#define EPIDEMIC_ROUTING_PROTOCOL_H

#include “ns3/ipv4-routing-protocol.h”

#include “ns3/ipv4.h”

#include “ns3/node.h”

#include “ns3/ipv4-route.h”

#include “ns3/net-device.h”

#include “ns3/timer.h”

#include “ns3/random-variable-stream.h”

namespace ns3 {

class EpidemicRoutingProtocol : public Ipv4RoutingProtocol {

public:

static TypeId GetTypeId (void);

EpidemicRoutingProtocol ();

virtual ~EpidemicRoutingProtocol ();

// Required by Ipv4RoutingProtocol

Ptr<Ipv4Route> RouteOutput (Ptr<Packet> packet, const Ipv4Header &header,

Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);

bool RouteInput (Ptr<const Packet> packet, const Ipv4Header &header,

Ptr<const NetDevice> idev, UnicastForwardCallback ucb,

MulticastForwardCallback mcb, LocalDeliverCallback lcb,

ErrorCallback ecb);

void SetIpv4 (Ptr<Ipv4> ipv4);

void DoDispose ();

private:

void Start ();

void Stop ();

void SendPacket (Ptr<Packet> packet, Ipv4Address destination);

void ReceivePacket (Ptr<Socket> socket);

void ExchangeSummaryVectors (Ptr<Socket> socket);

void SchedulePacketExchange (void);

 

Ptr<Ipv4> m_ipv4;

Ptr<Socket> m_socket;

std::map<Ipv4Address, std::vector<Ptr<Packet>>> m_packetBuffer;

Timer m_exchangeTimer;

Ptr<UniformRandomVariable> m_random;

};

} // namespace ns3

#endif // EPIDEMIC_ROUTING_PROTOCOL_H

epidemic-routing-protocol.cc

This implementation file defines the behaviour of the protocol class.

#include “epidemic-routing-protocol.h”

#include “ns3/log.h”

#include “ns3/ipv4-routing-table-entry.h”

#include “ns3/ipv4-route.h”

#include “ns3/simulator.h”

#include “ns3/inet-socket-address.h”

#include “ns3/udp-socket-factory.h”

#include “ns3/udp-header.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“EpidemicRoutingProtocol”);

NS_OBJECT_ENSURE_REGISTERED (EpidemicRoutingProtocol);

 

TypeId

EpidemicRoutingProtocol::GetTypeId (void)

{

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

.SetParent<Ipv4RoutingProtocol> ()

.SetGroupName(“Internet”)

.AddConstructor<EpidemicRoutingProtocol> ();

return tid;

}

EpidemicRoutingProtocol::EpidemicRoutingProtocol ()

: m_ipv4 (0),

m_socket (0)

{

NS_LOG_FUNCTION (this);

m_random = CreateObject<UniformRandomVariable> ();

}

EpidemicRoutingProtocol::~EpidemicRoutingProtocol ()

{

NS_LOG_FUNCTION (this);

}

void

EpidemicRoutingProtocol::DoDispose ()

{

NS_LOG_FUNCTION (this);

m_ipv4 = 0;

Ipv4RoutingProtocol::DoDispose ();

}

void

EpidemicRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)

{

NS_LOG_FUNCTION (this << ipv4);

m_ipv4 = ipv4;

}

void

EpidemicRoutingProtocol::Start ()

{

NS_LOG_FUNCTION (this);

m_socket = Socket::CreateSocket (GetObject<Node> (), UdpSocketFactory::GetTypeId ());

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

m_socket->SetRecvCallback (MakeCallback (&EpidemicRoutingProtocol::ReceivePacket, this));

m_exchangeTimer.SetFunction(&EpidemicRoutingProtocol::ExchangeSummaryVectors, this);

SchedulePacketExchange ();

}

 

void

EpidemicRoutingProtocol::Stop ()

{

NS_LOG_FUNCTION (this);

if (m_socket)

{

m_socket->Close ();

}

}

void

EpidemicRoutingProtocol::SendPacket (Ptr<Packet> packet, Ipv4Address destination)

{

NS_LOG_FUNCTION (this << packet << destination);

m_packetBuffer[destination].push_back (packet);

}

void

EpidemicRoutingProtocol::ReceivePacket (Ptr<Socket> socket)

{

NS_LOG_FUNCTION (this << socket);

Ptr<Packet> packet;

Address from;

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

{

if (packet->GetSize () == 0)

{

break;

}

Ipv4Address source = InetSocketAddress::ConvertFrom (from).GetIpv4 ();

// Handle packet reception logic here

}

}

void

EpidemicRoutingProtocol::ExchangeSummaryVectors (Ptr<Socket> socket)

{

NS_LOG_FUNCTION (this << socket);

// Implement the logic for exchanging summary vectors with neighbors

}

void

EpidemicRoutingProtocol::SchedulePacketExchange (void)

{

NS_LOG_FUNCTION (this);

m_exchangeTimer.Schedule (Seconds (m_random->GetValue (1.0, 2.0)));

}

Ptr<Ipv4Route>

EpidemicRoutingProtocol::RouteOutput (Ptr<Packet> packet, const Ipv4Header &header,    Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)

{

NS_LOG_FUNCTION (this << packet << header << oif);

// Implement the logic for routing packet to the correct output interface

return nullptr;

}

bool

EpidemicRoutingProtocol::RouteInput (Ptr<const Packet> packet, const Ipv4Header &header,

Ptr<const NetDevice> idev, UnicastForwardCallback ucb,

MulticastForwardCallback mcb, LocalDeliverCallback lcb,

ErrorCallback ecb)

{

NS_LOG_FUNCTION (this << packet << header << idev);

// Implement the logic for handling incoming packets

return false;

}

} // namespace ns3

Step 3: Integrate with ns-3

Modify the wscript file in the src directory to include the new protocol. By adding the following lines we can ensure that new files are compiled:

wscript

def build(bld):

bld.ns3_add_library(‘epidemic-routing-protocol’,

[‘epidemic-routing-protocol.cc’])

Step 4: Testing and Validation

Create a simulation script in the scratch directory to test the new protocol:

scratch/test-epidemic-routing-protocol.cc

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/epidemic-routing-protocol.h”

using namespace ns3;

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

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (2);

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

 

NetDeviceContainer devices;

devices = pointToPoint.Install (nodes);

InternetStackHelper stack;

stack.Install (nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign (devices);

Ptr<EpidemicRoutingProtocol>epidemicRouting = CreateObject<EpidemicRoutingProtocol> ();

nodes.Get (0)->AggregateObject (epidemicRouting);

Simulator::Stop (Seconds (10.0));

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 5: Build and Run

  1. We have to build the modified ns3:

./waf configure

./waf build

Run the test script:

./waf –run scratch/test-epidemic-routing-protocol

Finally, by following the several steps and scripts given above we had learnt implement the Epidemic Routing protocol in ns3tool, which is mainly used in Delay Tolerant Networks (DTNs).

Epidemic Routing protocol by using Delay Tolerant Networks (DTNs) implementation and simulation are well described and handled by our experts, for more support contact ns3simulation.com