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

How to Implement Presentation Layer in ns3

To implement the Presentation Layer in ns3, a module need to be created for handling the tasks for application before data is sent over the network because in ns3 it mainly consider the lower layers like network, transport etc., In OSI model presentation layer is responsible for the translation, encryption, and compression of data.

Here we have provided the instruction to implement the presentation layer in ns3.

Steps to Implement a Custom Presentation Layer in ns3:

  1. Set Up Your ns-3 Workspace: Make sure ns3 is installed on the system.
    • Navigate to the src directory of ns3 and create a new directory named custom-presentation-layer.
    • Inside the custom-presentation-layer directory, create subdirectories: model, helper, and examples.
  2. Define the Presentation Layer Classes:
    • In the model directory, create .cc and .h files for the presentation layer. Define the classes that simulate the behavior of the presentation layer, such as CustomPresentationProtocol, CustomEncoder, and CustomDecoder.
  3. Implement the Presentation Layer Classes:
    • Implement the key components of the presentation layer: data translation, encryption, and compression.
    • Create classes for encoding and decoding data, handling encryption and decryption, and managing compression and decompression.
  4. Integrate the Presentation Layer with ns3’s Application Layer:
    • Use existing ns3 classes for underlying transport layer communication.
    • Register the custom presentation layer as a protocol in ns3.
  5. Create a Simulation Script:
    • Set up a network topology.
    • Install the Internet stack.
    • Use the custom presentation layer helper to enable the presentation layer.
    • Set up applications and run the simulation.

Example Code Structure

Here’s an example code structure for implementing a custom Presentation Layer in ns3.

Define Custom Presentation Layer Classes

// src/custom-presentation-layer/model/custom-presentation-protocol.h

#ifndef CUSTOM_PRESENTATION_PROTOCOL_H

#define CUSTOM_PRESENTATION_PROTOCOL_H

#include “ns3/application.h”

#include “ns3/address.h”

#include “ns3/ptr.h”

#include “ns3/socket.h”

#include “ns3/packet.h”

#include “ns3/traced-callback.h”

namespace ns3 {

class CustomPresentationProtocol : public Application

{

public:

static TypeId GetTypeId (void);

CustomPresentationProtocol ();

virtual ~CustomPresentationProtocol ();

void Setup (Ptr<Socket> socket, Address address);

protected:

virtual void StartApplication (void);

virtual void StopApplication (void);

private:

void HandleRead (Ptr<Socket> socket);

void HandleSend (Ptr<Socket> socket, uint32_t available);

Ptr<Socket> m_socket;

Address m_peerAddress;

Ptr<Packet> m_packet;

};

} // namespace ns3

#endif // CUSTOM_PRESENTATION_PROTOCOL_H

// src/custom-presentation-layer/model/custom-presentation-protocol.cc

#include “custom-presentation-protocol.h”

#include “ns3/log.h”

#include “ns3/ipv4-address.h”

#include “ns3/socket-factory.h”

#include “ns3/packet.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“CustomPresentationProtocol”);

NS_OBJECT_ENSURE_REGISTERED (CustomPresentationProtocol);

TypeId

CustomPresentationProtocol::GetTypeId (void)

{

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

.SetParent<Application> ()

.SetGroupName (“Applications”)

.AddConstructor<CustomPresentationProtocol> ();

return tid;

}

CustomPresentationProtocol::CustomPresentationProtocol ()

{

NS_LOG_FUNCTION (this);

}

CustomPresentationProtocol::~CustomPresentationProtocol ()

{

NS_LOG_FUNCTION (this);

}

void

CustomPresentationProtocol::Setup (Ptr<Socket> socket, Address address)

{

NS_LOG_FUNCTION (this << socket << address);

m_socket = socket;

m_peerAddress = address;

}

void

CustomPresentationProtocol::StartApplication (void)

{

NS_LOG_FUNCTION (this);

m_socket->SetRecvCallback (MakeCallback (&CustomPresentationProtocol::HandleRead, this));

m_socket->SetSendCallback (MakeCallback (&CustomPresentationProtocol::HandleSend, this));

}

void

CustomPresentationProtocol::StopApplication (void)

{

NS_LOG_FUNCTION (this);

if (m_socket)

{

m_socket->Close ();

}

}

 

void

CustomPresentationProtocol::HandleRead (Ptr<Socket> socket)

{

NS_LOG_FUNCTION (this << socket);

Ptr<Packet> packet;

Address from;

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

{

// Decrypt and decompress the received packet

// …

NS_LOG_INFO (“Received ” << packet->GetSize () << ” bytes from ” << InetSocketAddress::ConvertFrom (from).GetIpv4 ());

}

}

void

CustomPresentationProtocol::HandleSend (Ptr<Socket> socket, uint32_t available)

{

NS_LOG_FUNCTION (this << socket << available);

// Implement the logic to handle packet sending

}

} // namespace ns3

Define Custom Encoder and Decoder

// src/custom-presentation-layer/model/custom-encoder.h

#ifndef CUSTOM_ENCODER_H

#define CUSTOM_ENCODER_H

#include “ns3/object.h”

#include “ns3/packet.h”

 

namespace ns3 {

class CustomEncoder : public Object

{

public:

static TypeId GetTypeId (void);

CustomEncoder ();

virtual ~CustomEncoder ();

Ptr<Packet> Encode (Ptr<Packet> packet);

Ptr<Packet> Decode (Ptr<Packet> packet);

};

} // namespace ns3

#endif // CUSTOM_ENCODER_H

// src/custom-presentation-layer/model/custom-encoder.cc

#include “custom-encoder.h”

#include “ns3/log.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“CustomEncoder”);

NS_OBJECT_ENSURE_REGISTERED (CustomEncoder);

TypeId

CustomEncoder::GetTypeId (void)

{

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

.SetParent<Object> ()

.SetGroupName (“Applications”)

.AddConstructor<CustomEncoder> ();

return tid;

}

 

CustomEncoder::CustomEncoder ()

{

NS_LOG_FUNCTION (this);

}

CustomEncoder::~CustomEncoder ()

{

NS_LOG_FUNCTION (this);

}

Ptr<Packet>

CustomEncoder::Encode (Ptr<Packet> packet)

{

NS_LOG_FUNCTION (this << packet);

// Implement encoding (e.g., encryption, compression)

return packet;

}

Ptr<Packet>

CustomEncoder::Decode (Ptr<Packet> packet)

{

NS_LOG_FUNCTION (this << packet);

// Implement decoding (e.g., decryption, decompression)

return packet;

}

} // namespace ns3

Define Custom Helper

// src/custom-presentation-layer/helper/custom-presentation-helper.h

 

#ifndef CUSTOM_PRESENTATION_HELPER_H

#define CUSTOM_PRESENTATION_HELPER_H

#include “ns3/application-container.h”

#include “ns3/application-helper.h”

#include “ns3/custom-presentation-protocol.h”

namespace ns3 {

class CustomPresentationHelper : public ApplicationHelper

{

public:

CustomPresentationHelper (Address address);

ApplicationContainer Install (NodeContainer c) const;

ApplicationContainer Install (Ptr<Node> node) const;

private:

ObjectFactory m_factory;

Address m_address;

};

} // namespace ns3

#endif // CUSTOM_PRESENTATION_HELPER_H

// src/custom-presentation-layer/helper/custom-presentation-helper.cc

#include “custom-presentation-helper.h”

#include “ns3/custom-presentation-protocol.h”

#include “ns3/node.h”

#include “ns3/names.h”

namespace ns3 {

CustomPresentationHelper::CustomPresentationHelper (Address address)

{

m_factory.SetTypeId (CustomPresentationProtocol::GetTypeId ());

m_address = address;

}

ApplicationContainer

CustomPresentationHelper::Install (NodeContainer c) const

{

ApplicationContainer apps;

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

{

apps.Add (Install (*i));

}

return apps;

}

ApplicationContainer

CustomPresentationHelper::Install (Ptr<Node> node) const

{

Ptr<CustomPresentationProtocol> app = m_factory.Create<CustomPresentationProtocol> ();

app->Setup (CreateObject<Socket> (), m_address);

node->AddApplication (app);

return ApplicationContainer (app);

}

} // namespace ns3

Example Simulation Script

// examples/custom-presentation-layer-simulation.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/applications-module.h”

#include “ns3/custom-presentation-helper.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“CustomPresentationLayerSimulation”);

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

{

CommandLine cmd;

cmd.Parse (argc, argv);

// Create nodes

NodeContainer nodes;

nodes.Create (4);

// Create point-to-point links

PointToPointHelper p2p;

p2p.SetDeviceAttribute (“DataRate”, StringValue (“1Gbps”));

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

NetDeviceContainer devices01 = p2p.Install (nodes.Get (0), nodes.Get (1));

NetDeviceContainer devices12 = p2p.Install (nodes.Get (1), nodes.Get (2));

NetDeviceContainer devices23 = p2p.Install (nodes.Get (2), nodes.Get (3));

NetDeviceContainer devices30 = p2p.Install (nodes.Get (3), nodes.Get (0));

// Install Internet stack

InternetStackHelper internet;

internet.Install (nodes);

// Assign IP addresses

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces01 = address.Assign (devices01);

 

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

Ipv4InterfaceContainer interfaces12 = address.Assign (devices12);

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

Ipv4InterfaceContainer interfaces23 = address.Assign (devices23);

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

Ipv4InterfaceContainer interfaces30 = address.Assign (devices30);

// Create a custom presentation protocol application to receive packets

Address sinkAddress (InetSocketAddress (Ipv4Address::GetAny (), 8080));

CustomPresentationHelper customPresentationHelper (sinkAddress);

ApplicationContainer sinkApps = customPresentationHelper.Install (nodes.Get (0));

sinkApps.Start (Seconds (1.0));

sinkApps.Stop (Seconds (10.0));

// Create a custom presentation protocol application to send packets

Address clientAddress (InetSocketAddress (Ipv4Address (“10.1.1.1”), 8080));

OnOffHelper clientHelper (“ns3::TcpSocketFactory”, clientAddress);

clientHelper.SetAttribute(“OnTime”,StringValue (“ns3::ConstantRandomVariable[Constant=1]”));

clientHelper.SetAttribute(“OffTime”,StringValue (“ns3::ConstantRandomVariable[Constant=0]”));

clientHelper.SetAttribute (“DataRate”, DataRateValue (DataRate (“1Mbps”)));

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

ApplicationContainer clientApps = clientHelper.Install (nodes.Get (1));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

// Enable tracing

AsciiTraceHelper ascii;

p2p.EnableAsciiAll (ascii.CreateFileStream (“custom-presentation-layer.tr”));

p2p.EnablePcapAll (“custom-presentation-layer”);

// Run the simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Running the Simulation

To run the simulation, compile the script and execute it:

./waf configure –enable-examples

./waf build

./waf –run custom-presentation-layer-simulation

The presentation layer is successfully implemented in the ns3 by creating the module to handle the tasks such as encoding, decoding data and managing compression decompression. For simulating the application, we need to use presentation layer helper so contact us for best simulation support.