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

How to Implement Bundling protocols in ns3

To implement bundling protocols in ns3, we need to create a custom application or leveraging existing libraries for DTN, such as the DTN2 reference implementation or ION-DTN. In ns3 bundling protocol doest not support directly, so here we can use Delay-Tolerant Networking (DTN) for simulation in which the bundle of messages are transmitted over a network that may experience long delays or intermittent connectivity.

Here we have provided the steps to create a basic simulation because bundling protocol does not have direct support in ns3 so by combining multiple messages into a single bundle before sending them that mimics the behavior of a bundling protocol.

Step-by-step guide to implement Bundling Protocol in ns3

Step 1: Set Up the Simulation Environment

Make sure ns3 is installed on the system.

Step 2: Create the Network Topology

Create a basic network topology using ns3 with multiple nodes.

Step 3: Define the Bundling Application

Create a custom application that mimics the bundling protocol.

Step 4: Write the Script

An example given on how to create and configure a network topology in ns3 with a basic bundling protocol:

  1. Create a New File:
    • Save the following script as bundling-protocol.cc in the scratch directory of your ns3 installation.

#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/wifi-module.h”

#include “ns3/mobility-module.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“BundlingProtocolExample”);

class BundlingApplication : public Application

{

public:

BundlingApplication ();

virtual ~BundlingApplication ();

void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, Time interPacketInterval);

protected:

virtual void StartApplication (void);

virtual void StopApplication (void);

private:

void SendPacket ();

void ScheduleTx ();

void ReceivePacket (Ptr<Socket> socket);

Ptr<Socket> m_socket;

Address m_peer;

uint32_t m_packetSize;

uint32_t m_nPackets;

Time m_interval;

uint32_t m_sent;

EventId m_sendEvent;

std::vector<Ptr<Packet>> m_bundle;

};

BundlingApplication::BundlingApplication ()

: m_socket (0),

m_packetSize (0),

m_nPackets (0),

m_sent (0)

{

}

BundlingApplication::~BundlingApplication ()

{

m_socket = 0;

}

void

BundlingApplication::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, Time interPacketInterval)

{

m_socket = socket;

m_peer = address;

m_packetSize = packetSize;

m_nPackets = nPackets;

m_interval = interPacketInterval;

}

void

BundlingApplication::StartApplication (void)

{

m_socket->Bind ();

m_socket->Connect (m_peer);

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

SendPacket ();

}

void

BundlingApplication::StopApplication (void)

{

if (m_socket)

{

m_socket->Close ();

}

Simulator::Cancel (m_sendEvent);

}

 

void

BundlingApplication::SendPacket ()

{

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

m_bundle.push_back (packet);

if (m_bundle.size () >= 5 || m_sent == m_nPackets) // Bundle packets and send when we have 5 packets or all packets are sent

{

Ptr<Packet> bundle = Create<Packet> ();

for (auto &p : m_bundle)

{

bundle->AddAtEnd (p);

}

m_socket->Send (bundle);

NS_LOG_UNCOND (“Sent bundle of ” << m_bundle.size () << ” packets”);

m_bundle.clear ();

}

if (++m_sent < m_nPackets)

{

ScheduleTx ();

}

}

 

void

BundlingApplication::ScheduleTx ()

{

m_sendEvent = Simulator::Schedule (m_interval, &BundlingApplication::SendPacket, this);

}

void

BundlingApplication::ReceivePacket (Ptr<Socket> socket)

{

Ptr<Packet> packet = socket->Recv ();

NS_LOG_UNCOND (“Received bundle of size ” << packet->GetSize ());

}

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);

uint32_t packetSize = 1024;

uint32_t nPackets = 20;

Time interPacketInterval = Seconds (1.0);

Ptr<Socket> recvSink = Socket::CreateSocket (nodes.Get (1),  UdpSocketFactory::GetTypeId());

InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 9);

recvSink->Bind (local);

recvSink->SetRecvCallback (MakeCallback (&BundlingApplication::ReceivePacket, DynamicCast<BundlingApplication> (nodes.Get (1)->GetApplication (0))));

Ptr<Socket> source = Socket::CreateSocket (nodes.Get (0), UdpSocketFactory::GetTypeId ());

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

app->Setup (source, InetSocketAddress (interfaces.GetAddress (1), 9), packetSize, nPackets, interPacketInterval);

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

app->SetStartTime (Seconds (2.0));

app->SetStopTime (Seconds (10.0));

Simulator::Run ();

Simulator::Destroy ();

 

return 0;

}

Explanation:

  1. Create Nodes and Network:
    • Create two nodes and connect them using a point-to-point link.
    • Install the Internet stack on the nodes.
    • Assign IP addresses to the devices.
  2. Define Bundling Application:
    • Create a BundlingApplication class that bundles packets before sending.
    • The application sends a bundle of packets when either 5 packets are accumulated or all packets are sent.
    • The SendPacket function creates packets and adds them to the bundle. When the bundle is ready, it is sent.
    • The ReceivePacket function handles incoming bundles and logs their size.
  3. Install and Configure the Application:
    • Create sockets for sending and receiving.
    • Install the BundlingApplication on the source node.
    • Configure the application with the necessary parameters, such as packet size, number of packets, and inter-packet interval.
  4. Run the Simulation:
    • Schedule the start and stop times for the application.
    • Run the simulation and log the results.

Step 4: Compile and Run the Script

  1. Save the script as bundling-protocol.cc in the scratch directory in ns3 installation.
  2. Compile the script using the following commands:

./waf configure

./waf build

./waf –run bundling-protocol

Step 5: Analyze the Results

We can analyze the results by looking at the logs to verify the behavior of the bundling protocol after the simulation process.

From the given above steps and examples we can completely understand the implementation process which involves creating nodes and networks, defining bundling applications and installing and configuring them

We can help you with the implementation of Bundling protocols in ns3 for your projects. Just share your details with us and we’ll provide you with the best project ideas shortly. Visit ns3simulation.com for more information.