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

How to Implement data centric protocol in ns3

To implement a data-centric protocol in ns3, we have to create a new custom module, for handling the specific functionalities of the data-centric protocol. Here we have given the steps for the implementation process of data-centric protocol. Based on the data content given it will assign the data to specific nodes and routes packets using ns3.

Step-by-Step Guide to Implementing a Data-Centric Protocol in ns-3

  1. Set Up Your Environment

Ensure that ns-3 is installed on the system.

  1. Create a New ns-3 Module

Create a new module for the data-centric protocol in the src directory of your ns-3 installation. This involves creating the necessary directory structure and files.

cd ns3.xx

cd src

mkdir -p data-centric-protocol/model

mkdir -p data-centric-protocol/helper

3. Create the Data-Centric Protocol Header File

Create the data-centric protocol header file data-centric-protocol.h in the model directory.

#ifndef DATA_CENTRIC_PROTOCOL_H

#define DATA_CENTRIC_PROTOCOL_H

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

#include “ns3/ipv4-address.h”

#include “ns3/timer.h”

#include “ns3/mobility-model.h”

#include “ns3/node.h”

#include “ns3/net-device.h”

#include “ns3/ipv4.h”

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

#include “ns3/packet.h”

#include <map>

namespace ns3 {

class DataCentricProtocol : public Ipv4RoutingProtocol

{

public:

static TypeId GetTypeId (void);

DataCentricProtocol ();

virtual ~DataCentricProtocol ();

// Inherited from Ipv4RoutingProtocol

virtual Ptr<Ipv4Route> RouteOutput (Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);

virtual bool RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb);

virtual void NotifyInterfaceUp (uint32_t interface);

virtual void NotifyInterfaceDown (uint32_t interface);

virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address);

virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address);

virtual void SetIpv4 (Ptr<Ipv4> ipv4);

virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const;

void AssignData (Ipv4Address address, std::string data);

private:

Ptr<Ipv4> m_ipv4;

std::map<Ipv4Address, Ipv4RoutingTableEntry> m_routingTable;

std::map<Ipv4Address, std::string> m_dataMap;

Ptr<NetDevice> GetNetDevice (uint32_t interface);

Ptr<MobilityModel> GetMobilityModel (Ptr<NetDevice> netDevice);

Ipv4Address GetNextHop (const std::string &data);

};

} // namespace ns3

#endif /* DATA_CENTRIC_PROTOCOL_H */

4. Create the Data-Centric Protocol Source File

Create the data-centric protocol source file data-centric-protocol.cc in the model directory.

#include “data-centric-protocol.h”

#include “ns3/log.h”

#include “ns3/ipv4-route.h”

#include “ns3/simulator.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“DataCentricProtocol”);

 

NS_OBJECT_ENSURE_REGISTERED (DataCentricProtocol);

TypeId

DataCentricProtocol::GetTypeId (void)

{

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

.SetParent<Ipv4RoutingProtocol> ()

.SetGroupName(“Internet”)

.AddConstructor<DataCentricProtocol> ();

return tid;

}

DataCentricProtocol::DataCentricProtocol ()

{

}

DataCentricProtocol::~DataCentricProtocol ()

{

}

void

DataCentricProtocol::SetIpv4 (Ptr<Ipv4> ipv4)

{

m_ipv4 = ipv4;

}

Ptr<NetDevice>

DataCentricProtocol::GetNetDevice (uint32_t interface)

{

return m_ipv4->GetNetDevice (interface);

}

Ptr<MobilityModel>

DataCentricProtocol::GetMobilityModel (Ptr<NetDevice> netDevice)

{

return netDevice->GetNode ()->GetObject<MobilityModel> ();

}

Ipv4Address

DataCentricProtocol::GetNextHop (const std::string &data)

{

for (const auto &entry : m_dataMap)

{

if (entry.second == data)

{

return entry.first;

}

}

return Ipv4Address::GetBroadcast ();

}

Ptr<Ipv4Route>

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

{

Ipv4Address dest = header.GetDestination ();

// Assuming data is encoded in the packet

std::string data = “example”; // Extract data from the packet as per your application

Ipv4Address nextHop = GetNextHop (data);

if (nextHop == Ipv4Address::GetBroadcast ())

{

sockerr = Socket::ERROR_NOROUTETOHOST;

return nullptr;

}

Ptr<Ipv4Route> route = Create<Ipv4Route> ();

route->SetDestination (dest);

route->SetGateway (nextHop);

route->SetOutputDevice (m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (nextHop)));

return route;

}

bool

DataCentricProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb)

{

Ipv4Address dest = header.GetDestination ();

// Assuming data is encoded in the packet

std::string data = “example”; // Extract data from the packet as per your application

Ipv4Address nextHop = GetNextHop (data);

if (nextHop == Ipv4Address::GetBroadcast ())

{

ecb (p, header, Socket::ERROR_NOROUTETOHOST);

return false;

}

Ptr<Ipv4Route> route = Create<Ipv4Route> ();

route->SetDestination (dest);

route->SetGateway (nextHop);

route->SetOutputDevice (m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (nextHop)));

ucb (route, p, header);

return true;

}

void

DataCentricProtocol::NotifyInterfaceUp (uint32_t interface)

{

}

void

DataCentricProtocol::NotifyInterfaceDown (uint32_t interface)

{

}

 

void

DataCentricProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

}

void

DataCentricProtocol::NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address)

{

}

void

DataCentricProtocol::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const

{

*stream->GetStream () << “Data-Centric Routing Table” << std::endl;

for (auto const &entry : m_routingTable)

{

*stream->GetStream () << entry.first << ” -> ” << entry.second.GetGateway () << ” via ” << entry.second.GetInterface () << std::endl;

}

}

void

DataCentricProtocol::AssignData (Ipv4Address address, std::string data)

{

m_dataMap[address] = data;

NS_LOG_INFO (“Assigned data ” << data << ” to address ” << address);

}

} // namespace ns3

5. Define Data-Centric Protocol Helper

Create the data-centric protocol helper header file data-centric-protocol-helper.h in the helper directory.

#ifndef DATA_CENTRIC_PROTOCOL_HELPER_H

#define DATA_CENTRIC_PROTOCOL_HELPER_H

#include “ns3/node-container.h”

#include “ns3/data-centric-protocol.h”

namespace ns3 {

class DataCentricProtocolHelper

{

public:

DataCentricProtocolHelper ();

virtual ~DataCentricProtocolHelper ();

void Install (NodeContainer nodes);

void AssignData (NodeContainer nodes, std::string data);

private:

Ptr<DataCentricProtocol> m_protocol;

};

 

} // namespace ns3

#endif /* DATA_CENTRIC_PROTOCOL_HELPER_H */

Create the data-centric protocol helper source file data-centric-protocol-helper.cc in the helper directory.

#include “data-centric-protocol-helper.h”

#include “ns3/node.h”

#include “ns3/log.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“DataCentricProtocolHelper”);

DataCentricProtocolHelper::DataCentricProtocolHelper ()

{

m_protocol = CreateObject<DataCentricProtocol> ();

}

DataCentricProtocolHelper::~DataCentricProtocolHelper ()

{

}

void

DataCentricProtocolHelper::Install (NodeContainer nodes)

{

for (NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i)

{

Ptr<Node> node = *i;

node->AggregateObject (m_protocol);

}

}

void

DataCentricProtocolHelper::AssignData (NodeContainer nodes, std::string data)

{

for (NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i)

{

Ptr<Node> node = *i;

m_protocol->AssignData (node->GetObject<Ipv4>()->GetAddress(1,0).GetLocal(), data);

}

}

} // namespace ns3

6. Update CMakeLists.txt

Add the new data-centric protocol module to the ns3 build system. Edit src/CMakeLists.txt and add the following line:

add_subdirectory (data-centric-protocol)

Create src/data-centric-protocol/CMakeLists.txt with the following content:

ns3_add_library (data-centric-protocol

model/data-centric-protocol.cc

helper/data-centric-protocol-helper.cc

)

 

target_link_libraries (data-centric-protocol)

7. Set Up the Network Topology

Create a simulation script in the scratch directory to use the data-centric protocol.

#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/data-centric-protocol-helper.h”

using namespace ns3;

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

{

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (4);

// Set up WiFi

WifiHelper wifi;

wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

wifiPhy.SetChannel (wifiChannel.Create ());

WifiMacHelper wifiMac;

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

NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

// Set up mobility model

MobilityHelper mobility;

mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

“MinX”, DoubleValue (0.0),

“MinY”, DoubleValue (0.0),

“DeltaX”, DoubleValue (5.0),

“DeltaY”, DoubleValue (10.0),

“GridWidth”, UintegerValue (2),

“LayoutType”, StringValue (“RowFirst”));

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

mobility.Install (nodes);

// Install the internet stack on nodes

InternetStackHelper stack;

stack.Install (nodes);

// Install data-centric protocol

DataCentricProtocolHelper dataProtocol;

dataProtocol.Install (nodes);

dataProtocol.AssignData(nodes, “example-data”);

 

// Assign IP addresses to the devices

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign (devices);

// Set up applications (e.g., a UDP echo server and client)

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (3));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (3), 9);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

// Enable tracing

AsciiTraceHelper ascii;

wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“data-centric-simulation.tr”));

wifiPhy.EnablePcapAll (“data-centric-simulation”);

// Run the simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

8. Build and Run the Simulation

After writing the script, we need to build and run it.

./waf build

./waf –run scratch/data-centric-simulation

9. Analyze the Results

After running the simulation, we can analyze the results using the generated trace files (data-centric-simulation.tr and data-centric-simulation-0-0.pcap).

Complete Example Script for Data-Centric Protocol

Here’s a complete example script for reference:

#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/data-centric-protocol-helper.h”

using namespace ns3;

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

{

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (4);

// Set up WiFi

WifiHelper wifi;

wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

wifiPhy.SetChannel (wifiChannel.Create ());

WifiMacHelper wifiMac;

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

NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

// Set up mobility model

MobilityHelper mobility;

mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

“MinX”, DoubleValue (0.0),

“MinY”, DoubleValue (0.0),

“DeltaX”, DoubleValue (5.0),

“DeltaY”, DoubleValue (10.0),

“GridWidth”, UintegerValue (2),

“LayoutType”, StringValue (“RowFirst”));

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

mobility.Install (nodes);

 

// Install the internet stack on nodes

InternetStackHelper stack;

stack.Install (nodes);

// Install data-centric protocol

DataCentricProtocolHelper dataProtocol;

dataProtocol.Install (nodes);

dataProtocol.AssignData(nodes, “example-data”);

// Assign IP addresses to the devices

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign (devices);

// Set up applications (e.g., a UDP echo server and client)

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (3));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (3), 9);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

// Enable tracing

AsciiTraceHelper ascii;

wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“data-centric-simulation.tr”));

wifiPhy.EnablePcapAll (“data-centric-simulation”);

// Run the simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

The implementation process of data-centric protocol is clearly explained by creating a basic data-centric protocol in that a new module is created to handle the functions such as assigning a particular data to the nodes and routes packets on simulation in ns3 environment.

Specific nodes and routes packets using ns3 simulation are handled by our experts, for help in your work feel free to contact us.