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

How to Implement Session Layer in ns3

To implement Session Layer in ns3, we need to create a module that can handle the lifecycle of application session. Because in ns3 it mainly focus on the lower layers like network, transport etc., But in OSI model session layer is responsible for establishing, managing, and terminating sessions between applications so, it needs a new module to perform these tasks.

Here the steps given will guide on implementing the session layer in ns3:

Step-by-step guide to Implement a Custom Session Layer in ns-3

  1. Set Up Your ns-3 Workspace: Make sure ns3 is installed on the system.
  2. Create a New Module for the Session Layer:
    • Navigate to the src directory of ns3 and create a new directory named custom-session-layer.
    • Inside the custom-session-layer directory, create subdirectories: model, helper, and examples.
  3. Define the Session Layer Classes:
    • In the model directory, create .cc and .h files for the session layer. Define the classes that simulate the behavior of the session layer, such as CustomSessionManager, CustomSession, and CustomSessionApp.
  4. Implement the Session Layer Classes:
    • Implement the key components of the session layer: session establishment, session maintenance, and session termination.
    • Create classes for managing sessions and session-based applications.
  5. Integrate the Session Layer with ns-3’s Network System:
    • Use existing ns-3 classes for underlying transport layer communication.
    • Register the custom session layer as a protocol in ns-3.
  6. Create a Simulation Script:
    • Set up a network topology.
    • Install the Internet stack.
    • Use the custom session layer helper to enable the session layer.
    • Set up applications and run the simulation.

Example Code Structure

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

Define Custom Session Layer Classes

// src/custom-session-layer/model/custom-session.h

#ifndef CUSTOM_SESSION_H

#define CUSTOM_SESSION_H

#include “ns3/application.h”

#include “ns3/address.h”

#include “ns3/ptr.h”

#include “ns3/socket.h”

#include “ns3/packet.h”

#include “ns3/timer.h”

#include “ns3/traced-callback.h”

namespace ns3 {

class CustomSession : public Object

{

public:

static TypeId GetTypeId (void);

CustomSession ();

virtual ~CustomSession ();

void Setup (Address address);

void StartSession ();

void EndSession ();

protected:

void HandleRead (Ptr<Socket> socket);

void HandleAccept (Ptr<Socket> socket, const Address& from);

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

 

private:

Ptr<Socket> m_socket;

Address m_peerAddress;

Ptr<Packet> m_packet;

};

} // namespace ns3

#endif // CUSTOM_SESSION_H

// src/custom-session-layer/model/custom-session.cc

#include “custom-session.h”

#include “ns3/log.h”

#include “ns3/ipv4-address.h”

#include “ns3/socket-factory.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“CustomSession”);

NS_OBJECT_ENSURE_REGISTERED (CustomSession);

TypeId

CustomSession::GetTypeId (void)

{

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

.SetParent<Object> ()

.SetGroupName (“Applications”)

.AddConstructor<CustomSession> ();

return tid;

}

CustomSession::CustomSession ()

{

NS_LOG_FUNCTION (this);

}

CustomSession::~CustomSession ()

{

NS_LOG_FUNCTION (this);

}

void

CustomSession::Setup (Address address)

{

NS_LOG_FUNCTION (this << address);

m_peerAddress = address;

}

void

CustomSession::StartSession ()

{

NS_LOG_FUNCTION (this);

if (InetSocketAddress::IsMatchingType (m_peerAddress))

{

m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName (“ns3::TcpSocketFactory”));

m_socket->Connect (m_peerAddress);

}

else

{

NS_ASSERT (false);

}

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

m_socket->SetAcceptCallback (

MakeNullCallback<bool, Ptr<Socket>, const Address&> (),

MakeCallback (&CustomSession::HandleAccept, this));

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

}

void

CustomSession::EndSession ()

{

NS_LOG_FUNCTION (this);

if (m_socket)

{

m_socket->Close ();

}

}

void

CustomSession::HandleRead (Ptr<Socket> socket)

{

NS_LOG_FUNCTION (this << socket);

Ptr<Packet> packet;

Address from;

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

{

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

}

}

void

CustomSession::HandleAccept (Ptr<Socket> socket, const Address& from)

{

NS_LOG_FUNCTION (this << socket << from);

}

void

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

{

NS_LOG_FUNCTION (this << socket << available);

// Implement the logic to handle packet sending

}

} // namespace ns3

Define Custom Session Manager

// src/custom-session-layer/model/custom-session-manager.h

#ifndef CUSTOM_SESSION_MANAGER_H

#define CUSTOM_SESSION_MANAGER_H

#include “ns3/object.h”

#include “ns3/address.h”

#include “ns3/traced-callback.h”

#include “custom-session.h”

#include <vector>

namespace ns3 {

class CustomSessionManager : public Object

{

public:

static TypeId GetTypeId (void);

CustomSessionManager ();

virtual ~CustomSessionManager ();

 

void AddSession (Ptr<CustomSession> session);

void RemoveSession (Ptr<CustomSession> session);

void StartAllSessions ();

void StopAllSessions ();

private:

std::vector<Ptr<CustomSession>> m_sessions;

};

} // namespace ns3

#endif // CUSTOM_SESSION_MANAGER_H

// src/custom-session-layer/model/custom-session-manager.cc

#include “custom-session-manager.h”

#include “ns3/log.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“CustomSessionManager”);

NS_OBJECT_ENSURE_REGISTERED (CustomSessionManager);

TypeId

CustomSessionManager::GetTypeId (void)

{

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

.SetParent<Object> ()

.SetGroupName (“Applications”)

.AddConstructor<CustomSessionManager> ();

return tid;

}

CustomSessionManager::CustomSessionManager ()

{

NS_LOG_FUNCTION (this);

}

CustomSessionManager::~CustomSessionManager ()

{

NS_LOG_FUNCTION (this);

}

void

CustomSessionManager::AddSession (Ptr<CustomSession> session)

{

NS_LOG_FUNCTION (this << session);

m_sessions.push_back (session);

}

void

CustomSessionManager::RemoveSession (Ptr<CustomSession> session)

{

NS_LOG_FUNCTION (this << session);

m_sessions.erase (std::remove (m_sessions.begin (), m_sessions.end (), session), m_sessions.end ());

}

void

CustomSessionManager::StartAllSessions ()

{

NS_LOG_FUNCTION (this);

for (auto session : m_sessions)

{

session->StartSession ();

}

}

void

CustomSessionManager::StopAllSessions ()

{

NS_LOG_FUNCTION (this);

for (auto session : m_sessions)

{

session->EndSession ();

}

}

} // namespace ns3

Define Custom Helper

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

#ifndef CUSTOM_SESSION_HELPER_H

#define CUSTOM_SESSION_HELPER_H

#include “ns3/application-container.h”

#include “ns3/application-helper.h”

#include “ns3/custom-session-manager.h”

#include “ns3/custom-session.h”

namespace ns3 {

class CustomSessionHelper

{

public:

CustomSessionHelper (Address address);

void AddSession (Ptr<Node> node);

void InstallSessions (NodeContainer c) const;

private:

ObjectFactory m_sessionFactory;

Address m_address;

Ptr<CustomSessionManager> m_sessionManager;

};

} // namespace ns3

#endif // CUSTOM_SESSION_HELPER_H

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

#include “custom-session-helper.h”

#include “ns3/custom-session-manager.h”

#include “ns3/node.h”

#include “ns3/names.h”

namespace ns3 {

CustomSessionHelper::CustomSessionHelper (Address address)

{

m_sessionFactory.SetTypeId (CustomSession::GetTypeId ());

m_address = address;

m_sessionManager = CreateObject<CustomSessionManager> ();

}

void

CustomSessionHelper::AddSession (Ptr<Node> node)

{

Ptr<CustomSession> session = m_sessionFactory.Create<CustomSession> ();

session->Setup (m_address);

m_sessionManager->AddSession (session);

node->AggregateObject (session);

}

void

CustomSessionHelper::InstallSessions (NodeContainer c) const

{

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

{

Ptr<Node> node = *i;

Ptr<CustomSession> session = m_sessionFactory.Create<CustomSession> ();

session->Setup (m_address);

node->AggregateObject (session);

}

m_sessionManager->StartAllSessions ();

}

} // namespace ns3

Example Simulation Script

// examples/custom-session-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-session-helper.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“CustomSessionLayerSimulation”);

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 session layer application to manage sessions

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

CustomSessionHelper customSessionHelper (sessionAddress);

customSessionHelper.InstallSessions (nodes);

// Create a custom session layer 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-session-layer.tr”));

p2p.EnablePcapAll (“custom-session-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-session-layer-simulation

Finally, we had implemented the session layer in ns3 by creating classes for managing sessions and session-based applications. Also, we  create the script to run the Session Layer in ns3 simulation.