How to Implement Application Layer in NS2

 

To implement the Application layer in ns2, this layer is the seventh layer of the OSI model which is accountable for end-user interaction as well as offering different services includes file transmission, network monitoring and real-time communication application. NS2 has numerous in-built application protocols like FTP, CBR (Constant Bit Rate), Telnet and HTTP however you can also generate custom application-layer protocols by expanding the available frameworks or by creating new ones in TCL or C++. The given demonstration will guide you through the implementation layer 7 in ns2:

Steps to Implement the Application Layer in NS2

  1. Using Built-in Application Layer Protocols

To replicate various kinds of network application, ns2 offers in-built application layer protocols like FTP, Telnet, and CBR. These protocols can be included to transport layer agents like TCP or UDP.

Below is an example of how to use some of the existing application-layer protocols in a simulation.

Example TCL Script for Built-in Application Layer

# Create a new simulator instance

set ns [new Simulator]

# Open trace and nam files

set tracefile [open “application_layer_trace.tr” w]

$ns trace-all $tracefile

set namfile [open “application_layer.nam” w]

$ns namtrace-all-wireless $namfile

# Define network topology

set n0 [$ns node]

set n1 [$ns node]

# Create a TCP connection between n0 and n1

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 an FTP application over TCP

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

# Create a UDP connection between n0 and n1

set udp0 [new Agent/UDP]

set null0 [new Agent/Null]

$ns attach-agent $n0 $udp0

$ns attach-agent $n1 $null0

$ns connect $udp0 $null0

# Create a CBR application over UDP

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 512

$cbr0 set rate_ 1Mb

$cbr0 attach-agent $udp0

# Schedule the applications to start and stop

$ns at 1.0 “$ftp0 start”

$ns at 1.5 “$cbr0 start”

$ns at 4.0 “$ftp0 stop”

$ns at 4.5 “$cbr0 stop”

# Define finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam application_layer.nam &

exit 0

}

$ns at 5.0 “finish”

# Run the simulation

$ns run

Explanation of the Script:

  1. FTP Application:
    • The FTP application delivers data over a TCP connection. The application is added to the tcp0 agent, which is linked to a TCP sink (sink0).
  2. CBR Application:
    • The CBR (Constant Bit Rate) application creates traffic over a UDP connection. The application is added to the udp0 agent, and the traffic is received by a Null agent.
  3. Traffic Scheduling:
    • The FTP and CBR applications are planned to start and end at particular times using start and stop commands.
  1. Creating Custom Application-Layer Protocols in NS2

You can set up your own custom application-layer protocols if you need to simulate certain application activities not covered by the built-in protocols.

Option 1: Custom Application Using TCL

You can recreate a simple custom application layer in TCL by writing an approach that manages the logic for data transmission, possibly communicating with transport-layer agents like TCP or UDP.

Example of a custom application in TCL:

# Define a custom application procedure to simulate data transfer

proc custom_application { src dst size } {

global ns

# Create a TCP agent on the source node

set tcp [new Agent/TCP]

set sink [new Agent/TCPSink]

$ns attach-agent $src $tcp

$ns attach-agent $dst $sink

$ns connect $tcp $sink

# Generate data manually based on the application logic

set data [string repeat “x” $size]

puts “Sending data: $data”

# Schedule the data transmission (simulating the application-layer functionality)

$ns at 1.0 “$tcp send $data”

}

# Create the network topology

set ns [new Simulator]

set n0 [$ns node]

set n1 [$ns node]

# Schedule the custom application to send data

$ns at 1.0 “custom_application $n0 $n1 1024”

# Finish procedure

proc finish {} {

global ns

$ns flush-trace

exit 0

}

$ns at 5.0 “finish”

# Run the simulation

$ns run

In this example:

  • A custom application is stated using a TCL procedure called custom_application.
  • It simulates an application that delivers 1024 bytes of data from one node to another through a TCP connection.
  • The application logic is determined in the procedure, and it interacts with the transport layer by generating TCP agents.

Option 2: Custom Application Using C++ (Advanced)

For more advanced environment, you can develop a custom application by designing a new application class using C++ which involves extending the application class of NS2 and attaching the logic for managing data, sessions and interactions with the transport layer.

Steps for Implementing a Custom Application in C++:

  1. Create a New Application Class: You will need to state your new application class in C++ and execute techniques for sending and receiving data. NS2 offers a base class called Application that you can extend to set up your custom application.

Example C++ skeleton for a custom application:

#include “app.h”

#include “agent.h”

class CustomApp : public Application {

public:

CustomApp();

void send_data();  // Custom method to handle data sending

void recv_data(Packet* pkt);  // Custom method to handle receiving data

};

CustomApp::CustomApp() : Application() {}

void CustomApp::send_data() {

// Custom logic to send data

Packet* pkt = allocpkt();

send(pkt);

}

void CustomApp::recv_data(Packet* pkt) {

// Handle received packet

printf(“Received a packet\n”);

Packet::free(pkt);

}

static class CustomAppClass : public TclClass {

public:

CustomAppClass() : TclClass(“Application/CustomApp”) {}

TclObject* create(int, const char* const*) {

return (new CustomApp());

}

} class_customapp;

  1. Modify NS2’s Makefile: After developing your custom application class, you need to include it to NS2’s Makefile so that it gets compiled with NS2.

Add the following to Makefile:

customapp.o: customapp.cc

$(CC) $(CFLAGS) -c customapp.cc

  1. Register Your Application in TCL: To use your new application in TCL scripts, you need to stored it in the ns-default.tcl file or straight in the script:

Application/CustomApp set packetSize_ 512

  1. Recompile NS2: After adding your custom application class, recompile NS2:

cd ns-allinone-2.35/ns-2.35/

./configure

make clean

make

  1. Use Your Custom Application in TCL: You can build an instance of it in a TCL script, after the compilation is done:

set custom_app [new Application/CustomApp]

  1. Analyzing Application Layer Behavior

Use the trace files (application_layer_trace.tr) and NAM visualizations (application_layer.nam) to evaluate the activities of the application layer. The trace file will contain details about when data is sent, how many packets are transmitted, and how the transport layer communicates with the application layer.

  1. Advanced Features for Custom Application-Layer Protocols:

Here are some latest features you might consider when building your custom application-layer protocol:

  • Session Management: Execute logic for handling application sessions (that is starting and terminating sessions).
  • Traffic Patterns: Simulate realistic environment like bursty traffic, Poisson distribution and so on by tailoring traffic generation.
  • Error Handling: Manage packet loss, timeouts, and retransmissions at the application layer for situations requiring higher consistency than the transport layer offers.
  • Interaction with Presentation Layer: Replicate interaction with the presentation layer for data encoding, encryption, or compression.

Throughput this procedure, you can get to know more about how the OSI model’s Layer 7 will be executed and functions including some examples. We offered the entire instructions of the implementation of the Application Layer in the NS2 simulation tool. You can customize the simulation as per your requirements. Contact ns2project.com for best implementation guidance.