Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to generate xgraph for Packet Delivery Ratio (PDR) for wired network in NS2?

Description

Wired Network with communication model is established using TCL script. Execution of tcl script generates a trace file with all simulation events recorded in it. Packet Delivery Ratio (PDR) is calculated using awk script which processes the trace file and produces the result in a file with .xg extension. Xgraph for Packet Delivery Ratio is plotted using the results (.xg file) obtained from the execution of awk script.

Sample Code

# Filename: test14.tcl

#——-Event scheduler object creation——–#
set ns [new Simulator]

#———-creating trace objects—————-#

set nt [open test14.tr w]
$ns trace-all $nt
#———-creating nam objects—————-#

set nf [open test14.nam w]
$ns namtrace-all $nf
#———-Setting color ID—————-#
$ns color 1 darkmagenta
$ns color 2 yellow
$ns color 3 blue
$ns color 4 green
$ns color 5 black

#———- Creating Network—————-#

set totalNodes 3

for {set i 0} {$i < $totalNodes} {incr i} {
set node_($i) [$ns node]
}

set server 0
set router 1
set client 2

#———- Creating Duplex Link—————-#
$ns duplex-link $node_($server) $node_($router) 2Mb 50ms DropTail
$ns duplex-link $node_($router) $node_($client) 2Mb 50ms DropTail

$ns duplex-link-op $node_($server) $node_($router) orient right
$ns duplex-link-op $node_($router) $node_($client) orient right

#————Labelling—————-#

$ns at 0.0 "$node_($server) label Server"
$ns at 0.0 "$node_($router) label Router"
$ns at 0.0 "$node_($client) label Client"

$ns at 0.0 "$node_($server) color blue"
$ns at 0.0 "$node_($client) color blue"

$node_($server) shape hexagon
$node_($client) shape hexagon
#————Data Transfer between Nodes—————-#

# Defining a transport agent for sending
set tcp [new Agent/TCP]

# Attaching transport agent to sender node
$ns attach-agent $node_($server) $tcp

# Defining a transport agent for receiving
set sink [new Agent/TCPSink]

# Attaching transport agent to receiver node
$ns attach-agent $node_($client) $sink

#Connecting sending and receiving transport agents
$ns connect $tcp $sink

#Defining Application instance
set ftp [new Application/FTP]

# Attaching transport agent to application agent
$ftp attach-agent $tcp

# Setting flow color
$tcp set fid_ 4

# data packet generation starting time
$ns at 1.0 "$ftp start"

# data packet generation ending time
$ns at 6.0 "$ftp stop"

#———finish procedure——–#

proc finish {} {
global ns nf nt
$ns flush-trace
close $nf
close $nt
puts "running nam…"
exec nam test14.nam &
exec awk -f Packet_Delivery_Ratio.awk test14.tr >pdr.xg &
exec xgraph -P -bg white -t PacketDeliveryRatio-x time(s) -y PDR(%)pdr.xg &

exit 0
}

#Calling finish procedure
$ns at 10.0 "finish"
$ns run

############################################
#filename: Packet_Delivery_Ratio.awk

#——— Formula ————:

packet_delivery_ratio = received_packets/generated_packets*100;

#——— AWK script Format——–#

The script has the following format:
BEGIN {}
{
content
}
END {}
Begin part comprises of initialization of variable.
Commands in the content part scan every row of trace file only once.
Ex:
if (pattern) {
action;
}

If the pattern is matched with the line in the trace in the trace file specified action will be performed.

In the END part final calculation is performed on the data obtained from the content part.
#——— Steps ————:

1. set the pattern and action for generated_packets
2. set the pattern and action for received_packets
3. Find the ratio between both
4. Print the result in .xg file
#——— Execution ——–#

ns test14.tcl

Screenshots

Packet Delivery Ratio
generate xgraph for Packet Delivery Ratio (PDR) for wired network in NS2