To implement DTN (Delay-Tolerant Networking) prototypes in ns3, we need to create a custom application. This application will simulate the store-carry-forward mechanism and potentially intermittent connectivity specially for DTN type of networks and scenarios. Here the given below steps will guide on how to implement a DTN prototype in ns3.
Step-by-step guide to implement DTN prototypes:
Step 1: Set Up the Simulation Environment
Make sure ns3 is installed on the system.
Step 2: Create the Network Topology
Create a network topology using ns3 with multiple nodes. For this example, we’ll simulate a simple scenario where Node A sends data to Node C via Node B.
Step 3: Define the DTN Application
Create a custom application to simulate the DTN store-carry-forward mechanism.
Step 4: Write the Script
An example given below that describes how to create and configure a DTN prototype in ns3:
- Create a New File:
- Save the following script as dtn-prototype.cc in the scratch directory of 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/mobility-module.h”
#include “ns3/log.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“DtnPrototypeExample”);
class DtnApplication : public Application
{
public:
DtnApplication ();
virtual ~DtnApplication ();
void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, Time interPacketInterval);
void AddIntermediateNode (Ptr<Node> node);
protected:
virtual void StartApplication (void);
virtual void StopApplication (void);
private:
void SendPacket ();
void ReceivePacket (Ptr<Socket> socket);
void ForwardBundles ();
Ptr<Socket> m_socket;
Address m_peer;
uint32_t m_packetSize;
uint32_t m_nPackets;
Time m_interval;
uint32_t m_sent;
std::vector<Ptr<Packet>> m_bundleBuffer;
std::vector<Ptr<Node>> m_intermediateNodes;
EventId m_forwardEvent;
};
DtnApplication::DtnApplication ()
: m_socket (0),
m_packetSize (0),
m_nPackets (0),
m_sent (0)
{
}
DtnApplication::~DtnApplication ()
{
m_socket = 0;
}
void
DtnApplication::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
DtnApplication::AddIntermediateNode (Ptr<Node> node)
{
m_intermediateNodes.push_back (node);
}
void
DtnApplication::StartApplication (void)
{
m_socket->Bind ();
m_socket->Connect (m_peer);
m_socket->SetRecvCallback (MakeCallback (&DtnApplication::ReceivePacket, this));
SendPacket ();
m_forwardEvent = Simulator::Schedule (Seconds (5.0), &DtnApplication::ForwardBundles, this);
}
void
DtnApplication::StopApplication (void)
{
if (m_socket)
{
m_socket->Close ();
}
Simulator::Cancel (m_forwardEvent);
}
void
DtnApplication::SendPacket ()
{
Ptr<Packet> packet = Create<Packet> (m_packetSize);
m_bundleBuffer.push_back (packet);
if (++m_sent < m_nPackets)
{
Simulator::Schedule (m_interval, &DtnApplication::SendPacket, this);
}
}
void
DtnApplication::ReceivePacket (Ptr<Socket> socket)
{
Ptr<Packet> packet = socket->Recv ();
NS_LOG_UNCOND (“Received packet of size ” << packet->GetSize ());
m_bundleBuffer.push_back (packet);
}
void
DtnApplication::ForwardBundles ()
{
for (auto &bundle : m_bundleBuffer)
{
for (auto &node : m_intermediateNodes)
{
Ptr<Socket> socket = Socket::CreateSocket (node, UdpSocketFactory::GetTypeId ());
socket->Bind ();
socket->Connect (InetSocketAddress (Ipv4Address::ConvertFrom (m_peer), 9));
socket->Send (bundle);
}
}
m_bundleBuffer.clear ();
m_forwardEvent = Simulator::Schedule (Seconds (5.0), &DtnApplication::ForwardBundles, this);
}
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (3); // A, B, C
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devicesAB, devicesBC;
devicesAB = pointToPoint.Install (nodes.Get (0), nodes.Get (1)); // A to B
devicesBC = pointToPoint.Install (nodes.Get (1), nodes.Get (2)); // B to C
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfacesAB = address.Assign (devicesAB);
address.SetBase (“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfacesBC = address.Assign (devicesBC);
// Set up mobility
MobilityHelper mobility;
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
nodes.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0.0, 0.0, 0.0));
nodes.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (50.0, 0.0, 0.0));
nodes.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (100.0, 0.0, 0.0));
// Set up applications
uint32_t packetSize = 1024;
uint32_t nPackets = 10;
Time interPacketInterval = Seconds (1.0);
Ptr<Socket> recvSink = Socket::CreateSocket (nodes.Get (2), UdpSocketFactory::GetTypeId ());
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 9);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&DtnApplication::ReceivePacket, DynamicCast<DtnApplication> (nodes.Get (2)->GetApplication (0))));
Ptr<Socket> source = Socket::CreateSocket (nodes.Get (0), UdpSocketFactory::GetTypeId ());
Ptr<DtnApplication> app = CreateObject<DtnApplication> ();
app->Setup (source, InetSocketAddress (interfacesBC.GetAddress (1), 9), packetSize, nPackets, interPacketInterval);
app->AddIntermediateNode (nodes.Get (1)); // B as intermediate node
nodes.Get (0)->AddApplication (app);
app->SetStartTime (Seconds (2.0));
app->SetStopTime (Seconds (20.0));
app->ForwardBundles(); // Start the forwarding process
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Explanation:
- Create Nodes and Network:
- Create three nodes (A, B, and C) and connect them using point-to-point links.
- Install the Internet stack on the nodes.
- Assign IP addresses to the devices.
- Define DTN Application:
- Implements the DTN in a Created DtnApplication class that store-carry-forward mechanism.
- The application sends packets, stores them in a buffer, and forwards them to intermediate nodes.
- The SendPacket function creates packets and adds them to the bundle buffer.
- The ReceivePacket function handles incoming packets and stores them in the buffer.
- The ForwardBundles function forwards the bundles to the next hop or the destination at scheduled intervals.
- Install and Configure the Application:
- Create sockets for sending and receiving.
- Install the DtnApplication on the source node (A).
- Configure the application with the necessary parameters, such as packet size, number of packets, and inter-packet interval.
- Add the intermediate node (B) to the application.
- Start the forwarding process.
- 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
- Save the script as dtn-prototype.cc in the scratch directory of ns3 installation.
- Compile the script using the following commands:
./waf configure
./waf build
./waf –run dtn-prototype
Step 5: Analyze the Results
We can analyze the results by looking at the logs to verify the behavior of the DTN application, after running the simulation.
From the given example and step we had learnt about the DTN (Delay Tolerant Networking) prototypes implementation in ns3 by using store-carry-forward mechanism with the intermediate node typically used for DTN scenarios.
We are here to assist you with top project suggestions and coding assistance for your DTN prototypes. Our team is equipped with the essential tools to help you succeed. Reach out to us for expert support and achieve successful implementation of DTN prototypes in ns3 .