How to Implement Session Layer in NS2

 

To implement Session Layer (Layer 5 of the OSI Model) in ns2 has numerous steps and it is liable for introducing, sustaining, and terminating interaction sessions among applications. In network simulators such as NS2, the session layer is not explicitly executed, by way of NS2 is mainly concentrate on the lower layers (transport and below). But, we can replicate some of the functions of the session layer by executing a custom logic for session management on top of the existing transport layer protocols.

The session layer deals with:

  • Session establishment: Configuring the communication session.
  • Session maintenance: Handling the sessions during data transfer like handling session timeouts and resuming sessions.
  • Session termination: Gracefully closing sessions.

Since NS2 doesn’t have a direct implementation of the session layer, we can mimic the session-layer behaviour using TCL scripting and customizing the transport layer protocols in NS2.

Steps to Implement Session Layer Behaviour in NS2:

  1. Use Transport Layer Protocols to Manage Sessions: We can use TCP or UDP to mimic the session establishment and termination. We can control when sessions (represented by connections) are initiated and terminated using TCL commands.
  2. Simulate Session Layer Functions with TCL: We can generate custom scripts that mimic the session establishment, maintenance, and termination by controlling how applications communicate with the transport layer protocols.

Example TCL Script for Simulating Session Layer

Below is an example that mimics the behaviour of session establishment, maintenance, and termination using TCP and UDP:

# Create a new simulator instance

set ns [new Simulator]

# Open trace and nam files

set tracefile [open “session_layer_trace.tr” w]

$ns trace-all $tracefile

set namfile [open “session_layer.nam” w]

$ns namtrace-all-wireless $namfile

# Define the network topology

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

# Create TCP agents

set tcp0 [new Agent/TCP]

set sink0 [new Agent/TCPSink]

$ns attach-agent $n0 $tcp0

$ns attach-agent $n1 $sink0

$ns connect $tcp0 $sink0

# Create a session application (simulated by an FTP application over TCP)

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

# Create UDP agents

set udp0 [new Agent/UDP]

set null0 [new Agent/Null]

$ns attach-agent $n1 $udp0

$ns attach-agent $n2 $null0

$ns connect $udp0 $null0

# Create a CBR application (simulates another session over UDP)

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 512

$cbr0 set rate_ 1Mb

$cbr0 attach-agent $udp0

# Define session behavior (session start, maintenance, and termination)

proc start_session {} {

global ftp0 cbr0

puts “Starting sessions…”

$ftp0 start

$cbr0 start

}

proc maintain_session {} {

puts “Maintaining sessions…”

# Simulate session maintenance (e.g., periodic checks or timeouts)

# For simplicity, we keep the session running here

}

proc end_session {} {

global ftp0 cbr0

puts “Ending sessions…”

$ftp0 stop

$cbr0 stop

}

# Schedule the start, maintenance, and end of the sessions

$ns at 1.0 “start_session”

$ns at 3.0 “maintain_session”

$ns at 4.5 “end_session”

# End the simulation after 5 seconds

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam session_layer.nam &

exit 0

}

$ns at 5.0 “finish”

# Run the simulation

$ns run

Explanation of the TCL Script:

  1. TCP and TCPSink Agents: Agent/TCP and Agent/TCPSink denotes the transport layer functionality, mimic a session among two nodes over TCP.
  2. UDP and Null Agents: Agent/UDP and Agent/Null denotes a connectionless session over UDP.
  3. Session Layer Functions: The start_session, maintain_session, and end_session procedures mimic the core functionalities of the session layer:
    • Start the session: This is where the communication session is introduced (FTP over TCP, CBR over UDP).
    • Maintain the session: In a real session layer, this would contain to keeping track of session state, managing the timeouts, etc. In this sample, it is denoted by a simple procedure call.
    • End the session: Terminates the communication by stopping the FTP and CBR applications.
  4. Session Scheduling: The script schedules the session functions (start_session, maintain_session, end_session) at certain times.
  1. Advanced Session Management

For more advanced session management, we need to mimic session timeouts, resume sessions, or handle multiple simultaneous sessions. Here are some ideas for expanding session-layer functionality:

  1. Session Timeouts:

We can mimic the session timeouts by adding a timeout condition and stopping the session if there is no activity after a specific time.

proc check_timeout {session_id} {

if {$session_active == 0} {

puts “Session $session_id timed out.”

end_session

} else {

# Continue maintaining the session

puts “Session $session_id is active.”

}

}

  1. Session Resumption:

If a session is intermittent like due to network failure, we can execute logic to resume the session by re-establishing the connection and resuming data transmission.

  1. Multiple Sessions:

If we want to mimic the multiple simultaneous sessions, we can handle a list of active sessions and manage each session independently.

set session_list {session1 session2 session3}

foreach session $session_list {

start_session $session

}

  1. Creating Custom Session Layer Protocol

If we need to generate a custom session layer protocol, we will need to describe how sessions are established, maintained, and terminated. Here’s a high-level outline of how we might execute a custom session layer protocol in NS2.

Steps to Implement a Custom Session Layer Protocol:

  1. Create Session Layer Logic in TCL:
    • Describe session states (e.g., established, maintained, and terminated).
    • Compose logic for transitioning among session states according to the events like timeouts, errors, or successful data transfer.
  2. Integrate with Transport Layer:
    • Use TCP or UDP for essential data transfer and handles session control logic at the TCL level.
    • Manage the session control messages like session initiation requests, session acknowledgments, and session termination signals.
  3. Custom Session Management C++ Code (Optional):
    • If we need to generate more sophisticated session control (beyond what TCL scripting allows), we can execute a custom session management protocol in NS2 by adjusting the C++ source code.
    • Add a new agent that manages session initiation, maintenance, and termination.

Example:

class SessionAgent : public Agent {

public:

SessionAgent();

void startSession();

void maintainSession();

void endSession();

};

  1. Recompile NS2: After implementing custom session management logic, recompile NS2 to contain the new session layer agent.

cd ns-allinone-2.35/ns-2.35/

./configure

make clean

make

  1. Analysing Session Layer Behavior

We can evaluate the behaviour of the session layer by investigative the trace file (session_layer_trace.tr) and NAM visualization (session_layer.nam). These files will support you monitor when sessions are introduced, how they are maintained, and when they are terminated.

We can generate parsing scripts to extract parameters like:

  • Session establishment time.
  • Session duration.
  • Number of sessions introduced, maintained, and terminated.

As we discussed earlier about how the Session Layer will perform in ns2 simulator and we help to deliver additional information about how the Session Layer will adapt in diverse scenarios.

We share project ideas, offering you innovative topics to explore. Get personalized support from us for implementing the Session Layer in NS2. Additionally, we can share detailed comparison analysis for your projects. Feel free to send us your project details for further assistance.