How To Implement New Protocol in Ns2
How To Implement New Protocol in Ns2
35
This is an Implementation tutorial of new manet(Mobile Ad-hoc NETworks) unicast
protocol name protoname.
The whole credit goes to Francisco J. Ros and Pedro M. Ruiz for their beautiful work
and excellent explanation they provide. The link to their work is below. It contains a PDF
file also which explains you in detail and I recommend you to go through it once.
List of files to be modified
~/ns-allinone-2.35/ns-2.35/common/packet.h
~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.h
~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.cc
~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-packet.tcl
~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-default.tcl
~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-lib.tcl
~/ns-allinone-2.35/ns-2.35/queue/priqueue.cc
** Note: Text in red is the part that is to be added or modified in given file and location
and purple colour denotes the terminal commands.
** Note: mod stands for modification.
** Note: Line number is approx and is provided for your convenience. Please check the
methods below and above before inserting.
Step 1:
download protoname.rar
http://www.cs.nccu.edu.tw/~g10031/protoname.rar
Extract the files and place them in ~/ns-allinone-2.35/ns-2.35/protoname
directory(create the directory if not present)
Step 2:
gedit ~/ns-allinone-2.35/ns-2.35/common/packet.h (mods at 3 places)
a.
c.
name_[PT_DCCP_DATAACK]="DCCP_DataAck";
name_[PT_DCCP_CLOSE]="DCCP_Close";
name_[PT_DCCP_CLOSEREQ]="DCCP_CloseReq";
name_[PT_DCCP_RESET]="DCCP_Reset";
name_[PT_PROTONAME]="protoname";
name_[PT_NTYPE]= "undefined";
}
Step 4:
gedit ~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.cc (mods at 3 places)
case PT_PING:
break;
/ / -------------------------------------------case PT_PROTONAME:
format_protoname(p, offset);
break;
/ / -------------------------------------------default:
...
}
save and close.
Step 5:
gedit ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-packet.tcl (mod at 1 place)
# diffusion/diffusion.cc
...
ManualRtg {
set ragent [$ self create-manual-rtg-agent $
}
# / ------------------------------------------------ Protoname
{
set ragent [$self create-protoname-agent $node]
}
# / ------------------------------------------------ default {
...
}
Step 8:
gedit ~/ns-allinone-2.35/ns-2.35/queue/priqueue.cc (mod at 1 place)
if (Prefer_Routing_Protocols) {
switch (ch-> ptype ()) {
...
case PT_MDART:
/ / -------------------------------------------case PT_PROTONAME:
/ / -------------------------------------------recvHighPriority (p, h);
break;
default:
Queue :: recv (p, h);
}
}
else {
Queue :: recv (p, h);
}
}
save and close.
Step 9:
gedit ~/ns-allinone-2.35/ns-2.35/Makefile (mod at 1 place)
a. make clean
b. touch common/packet.cc
c. make
(if you are getting some errors check the spaces in the editing you did above)
Step 11:
gedit ~/ns-allinone-2.35/ns-2.35/test.tcl
copy and paste
set ns [new Simulator]
$ns node-config -Routing protoname
set nf [open out.nam w]
$ns namtrace-all $nf
set nd [open out.tr w]
$ns trace-all $nd
proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam out.nam &
exit 0
}
for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns connect $udp0 $null0
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2)