Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

MOBILE COMPUTING

PRACTICAL FILE

Name Rajesh kumar


Roll no. = 2020UIT3052
INDEX

1 Download and install the NS2/NS3 SIMULATOR.

2 Implement a point – to – point network consisting of


FIVE (05) nodes using the NS2/NS3 simulator with duplex links between them.

3 Implement and study the performance of GSM on NS2/NS3 using MAC layer.

4
Implement and study the performance of GPRS on
NS2/NS3 using MAC layer

5
Implement an Infrastructure-less wireless network comprising of 'N' nodes in
an area of 500m x 600m and
set multiple traffic nodes

6 Implement an Infrastructure-less wireless network comprising of 'N' MOBILE


nodes. Perform the simulations with varying speed of the nodes.

Implement an Infrastructure-less wireless network comprising of 'N' nodes in an


area of 1000 x 1000 square meters. At routing layer use AODV and DSR protocols.
PRACTICAL-1
Implement a point – to – point network consisting of FIVE
(05) nodes using the NS2/NS3 simulator with duplex links
between them. Initiate a communication between these
nodes. Set the queue size, vary the bandwidth and find the
number of packets dropped. Finally plot a graph showing the
performance of this network in terms of the number of
packets dropped with varying bandwidth.

set ns [new Simulator]

#Open Trace file and NAM file


set ntrace [open prog1.tr w]
$ns trace-all $ntrace set
namfile [open prog1.nam w]
$ns namtrace-all $namfile

#Finish Procedure proc


Finish {} { global ns
ntrace namfile

#Dump all the trace data and close the files


$ns flush-trace
close $ntrace close
$namfile

#Execute the nam animation file exec


nam prog1.nam &

#Show the number of packets dropped exec


echo "The number of packet drops is " &
exec grep -c "^d" prog1.tr &
exit 0
}

#Create 5 nodes set


n0 [$ns node] set
n1 [$ns node] set
n2 [$ns node] set
n3 [$ns node] set
n4 [$ns node]

#Label the nodes


$n0 label "TCP Source"
$n4 label "Sink"

#Set the color


$ns color 1 blue
#Create Links between nodes
#You need to modify the bandwidth to observe the variation in packet
drop
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
# 4 links between 5 nodes

#Make the Link Orientation


$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right

#Set Queue Size


#You can modify the queue length as well to observe the variation in
packet drop
$ns queue-limit $n0 $n1 6
$ns queue-limit $n1 $n2 6
$ns queue-limit $n2 $n3 6
$ns queue-limit $n3 $n4 6

#Set up a Transport layer connection


set tcp0 [new Agent/TCP] $ns
attach-agent $n0 $tcp0 set sink0
[new Agent/TCPSink]
$ns attach-agent $n4 $sink0
$ns connect $tcp0 $sink0
#Set up an Application layer Traffic set cbr0
[new Application/Traffic/CBR] $cbr0 set type_
CBR $cbr0 set packetSize_ 100
$cbr0 set rate_ 1Mb
$cbr0 set random_ false
$cbr0 attach-agent $tcp0

$tcp0 set class_ 1


#Schedule Events
$ns at 0.0 "$cbr0 start"
$ns at 10.0 "Finish"
#Run the Simulation
$ns run

For Queue size =6 , we get following no of packet drops for different


bandwidth size:
Bandwidth Packets Drop
15 mb 4
10 mb 5
5 mb 5
1 mb 9
0.5 mb 14
PRACTICAL-3
Implement GSM using NS2/NS3 Simulator

# General Parameters set


stop 100 ;# Stop time.

# Topology set type gsm


;#type of link:

# AQM parameters
set minth 30 ; set
maxth 0 ;
set adaptive 1 ;# 1 for Adaptive RED, 0 for plain RED

# Traffic generation. set flows 0 ;# number of


long-lived TCP flows set window 30 ;#
window for long-lived traffic
set web 2 ;# number of web sessions

# Plotting statics. set opt(wrap) 100 ;#


wrap plots? set opt(srcTrace) is ;# where
to plot traffic
set opt(dstTrace) bs2 ;# where to plot traffic

#default downlink bandwidth in bps set


bwDL(gsm) 9600

#default uplink bandwidth in bps set


bwUL(gsm) 9600
#default downlink propagation delay in seconds set
propDL(gsm) .500

#default uplink propagation delay in seconds set


propUL(gsm) .500

set ns [new Simulator]


set tf [open out.tr w] $ns
trace-all $tf set nodes(is)
[$ns node] set
nodes(ms) [$ns node]
set nodes(bs1) [$ns node]
set nodes(bs2) [$ns node]
set nodes(lp) [$ns node]
proc cell_topo {} {
global ns nodes
$ns duplex-link $nodes(lp) $nodes(bs1) 3Mbps 10nodes(ms) DropTail
$ns duplex-link $nodes(bs1) $nodes(ms) 1 1 RED
$ns duplex-link $nodes(ms) $nodes(bs2) 1 1 RED
$ns duplex-link $nodes(bs2) $nodes(is) 3Mbps 50nodes(ms) DropTail puts
" GSM Cell Topology"
}
proc set_link_para {t} {
global ns nodes bwUL bwDL propUL propDL buf $ns
bandwidth $nodes(bs1) $nodes(ms) $bwDL($t) duplex
$ns bandwidth $nodes(bs2) $nodes(ms) $bwDL($t) duplex
$ns delay $nodes(bs1) $nodes(ms) $propDL($t) duplex
$ns delay $nodes(bs2) $nodes(ms) $propDL($t) duplex
$ns queue-limit $nodes(bs1) $nodes(ms) 10
$ns queue-limit $nodes(bs2) $nodes(ms) 10
}

# RED and TCP parameters


Queue/RED set adaptive_ $adaptive
Queue/RED set thresh_ $minth
Queue/RED set maxthresh_ $maxth Agent/TCP
set window_ $window
source web.tcl

#Create topology
switch $type {
gsm - gprs -
umts {cell_topo}
}
set_link_para $type
$ns insert-delayer $nodes(ms) $nodes(bs1) [new Delayer]
$ns insert-delayer $nodes(ms) $nodes(bs2) [new Delayer]

# Set up forward TCP connection


if {$flows == 0} {
set tcp1 [$ns create-connection TCP/Sack1 $nodes(is) TCPSink/Sack1
$nodes(lp) 0]
set ftp1 [[set tcp1] attach-app FTP]
$ns at 0.8 "[set ftp1] start"
}
if {$flows > 0} {
set tcp1 [$ns create-connection TCP/Sack1 $nodes(is) TCPSink/Sack1
$nodes(lp) 0]
set ftp1 [[set tcp1] attach-app FTP]
$tcp1 set window_ 100
$ns at 0.0 "[set ftp1] start" $ns
at 3.5 "[set ftp1] stop"
set tcp2 [$ns create-connection TCP/Sack1 $nodes(is) TCPSink/Sack1
$nodes(lp) 0]
set ftp2 [[set tcp2] attach-app FTP]
$tcp2 set window_ 3 $ns at 1.0
"[set ftp2] start"
$ns at 8.0 "[set ftp2] stop"
} proc stop {} { global nodes opt
nf set wrap $opt(wrap) set sid
[$nodes($opt(srcTrace)) id] set
did [$nodes($opt(dstTrace)) id]
set a "out.tr" set GETRC
"../../../bin/getrc" set RAW2XG
"../../../bin/raw2xg" exec $GETRC -s $sid
-d $did -f 0 out.tr | \ $RAW2XG -s 0.01 -
m $wrap -r > plot.xgr exec $GETRC -s
$did -d $sid -f 0 out.tr | \
$RAW2XG -a -s 0.01 -m $wrap >> plot.xgr
exec xgraph -x time -y packets plot.xgr & exit
0
}
$ns at $stop "stop"
$ns run

X Graph:-
Trace File:-

PRACTICAL-4
Implement GPRS on NS2 using Mac Layer

# Define options
set opt(chan) Channel/WirelessChannel ;#Channel type set opt(prop)
Propagation/TwoRayGround ;# radio-propagation model set opt(netif)
Phy/WirelessPhy ;# network interface type set opt(mac) Mac/Gprs;#
MAC type
set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type
set opt(ll) LL ;# Link layer type set opt(rl) RLC
set opt(ant) Antenna/OmniAntenna ;# antenna model
set opt(ifqlen) 5000 ;# max paket in ifq set opt(adhoc)
NOAH ;# routing protool set opt(x) 70 ;# xoordinate
of topology set opt(y) 70 ;# yoordinate of topology
set opt(seed) 0.0 ;# seed for random num gen.
set opt(tr) "/tmp/riha/sim1.tr" set
opt(start) 0.0
set opt(stop) 80 ;# time to stop simulation set
num_bs_nodes 1
set opt(nn) 5 ;# number of mobilenodes set
opt(rate) 15k
Mac/Gprs set gprs_slots_per_frame_ 7
Mac/Gprs set slot_packet_len_ 53
Mac/Gprs set max_num_ms_ 15
Mac/Gprs set max_num_freq_ 2
Mac/Gprs set gprs_ 1
Mac/Gprs set rlc_error_ 0
Mac/Gprs set error_rate_ 1000
Mac/Gprs set verbose_ 0
LL set acked_ 0
LL set llfraged_ 1
LL set llfragsz_ 1520
LL set llverbose_ 0
RLC set acked_ 0
RLC set rlcfraged_ 1
RLC set rlcfragsz_ 50
RLC set rlcverbose_ 0
#remove unneessary paket headers, else eah pkt takes 2kb!
remove-packet-header LDP MPLS Snoop remove-
packet-header Ping TFRC TFRC_ACK remove-
packet-header Diffusion RAP IMEP remove-
packet-header AODV SR TORA IPinIP remove-
packet-header MIP HttpInval remove-packet-
header MFTP SRMEXT SRM aSRM remove-
packet-header mcastCtrl CtrMcast IVS remove-
paket-header Resv UMP Flags #Create simulator
instane set ns_ [new Simulator]
# set up for hierarchical routing
$ns_ node-config -addressType hierarchical
AddrParams set domain_num_ 1 ;# number of domains
lappend cluster_num 1 ;# number ofclusters in each domain
AddrParams set cluster_num_ $cluster_num lappend
eilastlevel 6 ;# number of nodes in eachcluster AddrParams
set nodes_num_ $eilastlevel ;# of each domain set tracefd
[open $opt(tr) w] $ns_ trace-all $tracefd # Create
topography object set topo [new Topography]
# define topology
$topo load_flatgrid $opt(x) $opt(y)
#create God create-god
$opt(nn) set chan1 [new
$opt(chan)]
#configure for base-station node
$ns_ node-config -adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-rlcType $opt(rlc) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif)\
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace OFF \
-macTrace OFF \
-movementTrace OFF \
-channel $chan1 #create
base-station node
set temp {1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 }
# hier address to be used for wireless domain set BS(0)
[$ns_ node [lindex $temp 0]]
$BS(0) random-motion 0 ;# disable random motion
#provide some co-ord (fixed) to base station node
$BS(0) set X_ 1.0 $BS(0)
set Y_ 2.0 $BS(0) set Z_
0.0
#configure for mobilenodes
$ns_ node-config -wiredRouting OFF
#create mobilenodes in the same domain as BS(0) for
{set j 0} {$j < $opt(nn)} {incr j} {
set node_($j) [ $ns_ node [lindex $temp [expr $j+1]]]
$node_($j) base-station [AddrParams addr2id [$BS(0) node-addr]]
}
for {set j 0} {$j < $opt(nn)} {incr j} { set
s($j) [new Agent/TCP]
$ns_ attach-agent $node_($j) $s($j)
$s($j) set packetSize_ 1500 set
null($j) [new Agent/TCPSink] $ns_
attach-agent $BS(0) $null($j)

$null($j) set packetSize_ 30 $ns_connect $s($j)


$null($j) set exp($j) [new
Application/Traffic/Exponential]
$exp($j) set burst_time_ 500ms
$exp($j) set idle_time_ 500ms
$exp($j) set rate_ $opt(rate)
$exp($j) attach-agent $s($j)
$ns_ at $opt(start) "$exp($j) start"
}
# Tell all nodes when the simulation ends for
{set i } {$i < $opt(nn) } {incr i} {
$ns_ at $opt(stop).0 "$node_($i) reset";
}
$ns_ at $opt(stop).0 "$BS(0) reset";
$ns_ at $opt(stop).0002 "puts \" \" ; $ns_ halt"
$ns_ at $opt(stop).0001 "stop" proc stop {} {
global ns_ tracefd $ns_ flush-trace lose
$tracefd
}
#puts "Starting Simulation..."
$ns_ run

PRACTICAL-5
Implement an Infrastructure-less wireless network comprising of 'N' nodes in an
area of 500m x 600m and set multiple traffic nodes. After simulation plot graphs
showing the performance of the network in terms of the End-to-End delay,
Throughput and packet delivery ratio for varying for Number of nodes with
different source/destination pair in NS2/NS3.

set ns [ new Simulator ]

#Open the nam trace file set


tf [ open lab3.tr w ]
$ns trace-all $tf

#Open the nam trace file set


nf [ open lab1.nam w ]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish { } { global ns nf
tf $ns flush-trace exec nam
lab1.nam &
close $tf close $nf
exec xgraph lab1.tr
exit 0
}

#Creating nodes

set n0 [$ns node] set


n1 [$ns node] set n2
[$ns node] set n3
[$ns node] set n4
[$ns node] set n5
[$ns node] set n6
[$ns node]

#Define different colors and labels for data flows


$n0 label "Source/udp0" $n1
label "Source/udp1" $n2
label "Source/udp2" $n3
label "Source/udp3"
$n4 label "Source/udp4"
$n5 label "Router"
$n6 label "Destination/Null"

#Create link between nodes


$ns duplex-link $n0 $n5 100Mb 300ms DropTail $ns
duplex-link $n1 $n5 100Mb 300ms DropTail $ns
duplex-link $n2 $n5 100Mb 300ms DropTail $ns
duplex-link $n3 $n5 100Mb 300ms DropTail
$ns duplex-link $n4 $n5 100Mb 300ms DropTail
$ns duplex-link $n5 $n6 1Mb 300ms DropTail

#Set queue size of links


$ns set queue-limit $n0 $n5 50 $ns
set queue-limit $n1 $n5 50 $ns set
queue-limit $n2 $n5 50
$ns set queue-limit $n3 $n5 50
$ns set queue-limit $n4 $n5 50
$ns set queue-limit $n5 $n6 5

#Setup UDP connection set udp0 [new


Agent/UDP] $ns attach-agent $n0
$udp0 set cbr0 [new
Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set udp1 [new Agent/UDP] $ns attach-


agent $n1 $udp1 set cbr1 [new
Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.006
$cbr1 attach-agent $udp1

set udp2 [new Agent/UDP] $ns attach-


agent $n2 $udp2 set cbr2 [new
Application/Traffic/CBR]
$cbr2 set packetSize_ 500
$cbr2 set interval_ 0.007
$cbr2 attach-agent $udp2

set udp3 [new Agent/UDP] $ns attach-


agent $n3 $udp3 set cbr3 [new
Application/Traffic/CBR]
$cbr3 set packetSize_ 500
$cbr3 set interval_ 0.008
$cbr3 attach-agent $udp3

set udp4 [new Agent/UDP]

$ns attach-agent $n4 $udp4 set cbr4


[new Application/Traffic/CBR]
$cbr4 set packetSize_ 500
$cbr4 set interval_ 0.009
$cbr4 attach-agent $udp4

#Create a Null agent (a traffic sink) and attach it to node n6 set


null6 [new Agent/Null]
$ns attach-agent $n6 $null6
#Connect the traffic sources with the traffic sink
$ns connect $udp0 $null6 $ns
connect $udp1 $null6 $ns
connect $udp2 $null6 $ns
connect $udp3 $null6
$ns connect $udp4 $null6
#Schedule events for the CBR agents
$ns at 0.2 "$cbr0 start" $ns
at 0.4 "$cbr1 start"
$ns at 0.6 "$cbr2 start" $ns
at 0.8 "$cbr3 start"
$ns at 1.0 "$cbr4 start"
$ns at 4.5 "$cbr0 stop" $ns
at 4.6 "$cbr1 stop" $ns at
4.7 "$cbr2 stop" $ns at 4.8
"$cbr3 stop" $ns at 4.9
"$cbr4 stop"

#Call the finish procedure after 5 seconds of simulation time


$ns at 10.0 "finish"

#Run the simulation


$ns run

PRACTICAL-6
Implement an Infrastructure-less wireless network comprising of 'N' MOBILE
nodes.
Perform the simulations with varying speed of the nodes.
Plot and compare the performance of different MAC layer protocols using
NS2/NS3 simulator in terms of
(1) Number of collisions,
(2) Number of Control packets

puts "Enter number of nodes" set


tnn [gets stdin]
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy set
val(mac) Mac/802_11 set val(ifq)
Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna set
val(x) 1500;#add manually set val(y)
1500;#add manually set val(ifqlen)
1000;#add manually set val(adhocRouting)
AODV;#add manually set val(nn) $tnn set
val(stop) 10.0
#add manually
Mac/802_11 set cdma_code_bw_start_ 0;# cdma code for bw request (start)
Mac/802_11 set cdma_code_bw_stop_ 63;# cdma code for bw request (stop)
Mac/802_11 set cdma_code_init_start_ 64;# cdma code for initial request (start)
Mac/802_11 set cdma_code_init_stop_ 127;# cdma code for initial request (stop)
Mac/802_11 set cdma_code_cqich_start_ 128 ;# cdma code for cqich request (start)
Mac/802_11 set cdma_code_cqich_stop_ 195;# cdma code for cqich request (stop)
Mac/802_11 set cdma_code_handover_start_ 196 ;# cdma code for handover request
(start)

Mac/802_11 set cdma_code_handover_stop_ 255;# cdma code for handover request


(stop) #end
set f0 [open out02.tr w] set f1
[open lost02.tr w] set f2 [open
delay02.tr w] set ns_ [new
Simulator] set topo [new
Topography] set tracefd [open
out.tr w] set namtrace [open
out.nam w]

$ns_ trace-all $tracefd


$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
$topo load_flatgrid $val(x) $val(y)
set god_ [create-god $val(nn)]
$ns_ color 0 red
$ns_ node-config -adhocRouting AODV \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-energyModel EnergyModel \
-initialEnergy 100 \
-rxPower 0.3 \
-txPower 0.6 \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF

#add manually
for {set i 0} {$i < $val(nn) } {incr i} { set
node_($i) [$ns_ node]
$node_($i) set X_ [expr rand() * 500] $node_($i)
set Y_ [expr rand() * 500]
$node_($i) set Z_ 0.000000000000;
}
for {set i 0} {$i < $val(nn) } {incr i} {
set xx [expr rand() * 1500] set yy
[expr rand() * 1000]
$ns_ at 0.1 "$node_($i) setdest $xx 4yy 5"
}
puts "Loading connection pattern..."
puts "Loading scenario file..." for
{set i 0} {$i < $val(nn) } {incr i} {
$ns_ initial_node_pos $node_($i) 55
}
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop).0 "$node_($i) reset";
}
puts "Enter source node" set source [gets
stdin] puts "Enter destination node" set
dest [gets stdin] set udp_(0) [new
Agent/UDP] $ns_ attach-agent
$node_($source) $udp_(0) set sink [new
Agent/LossMonitor] $ns_ attach-agent
$node_($dest) $sink set cbr1_(0) [new
Application/Traffic/CBR]
$cbr1_(0) set packetSize_ 1000
$cbr1_(0) set interval_ 0.1

$cbr1_(0) set maxpkts_ 10000


$cbr1_(0) attach-agent $udp_(0)
$ns_ connect $udp_(0) $sink
$ns_ at 1.00 "$cbr1_(0) start"
set holdtime 0 set holdseq 0
set holdrate1 0 proc record
{} {
global sink f0 f1 f2 holdtime holdseq holdrate1 set ns [Simulator instance]
set time 0.9 ;#Set Sampling Time to 0.9 Sec
set bw0 [$sink set bytes_] set bw1 [$sink
set nlost_] set bw2 [$sink set
lastPktTime_] set bw3 [$sink set npkts_]
set now [$ns now]
# Record Bit Rate in Trace Files
puts $f0 "$now [expr (($bw0+$holdrate1)*8)/(2*$time*1000000)]"
# Record Packet Loss Rate in File
puts $f1 "$now [expr $bw1/$time]" if
{ $bw3 > $holdseq } {
puts $f2 "$now [expr ($bw2 - $holdtime)/($bw3 - $holdseq)]"
} else {
puts $f2 "$now [expr ($bw3 - $holdseq)]"
}
$sink set bytes_ 0
$sink set nlost_ 0 set
holdtime $bw2 set
holdseq $bw3 set
holdrate1 $bw0
$ns at [expr $now+$time] "record" ;# Schedule Record after $time interval sec}

#end
# Start Recording at Time 0
$ns_ at 0.0 "record" source
link.tcl proc stop {} { global
ns_ tracefd f0 f1 f2
# Close Trace Files
close $f0 close $f1
close $f2 exec
nam out.nam
exec xgraph out02.tr -geometry -x TIME -y thr -t Throughput 800x400 & exec
xgraph lost02.tr -geometry -x TIME -y loss -t Packet_loss 800x400 & exec xgraph
delay02.tr -geometry -x TIME -y delay -t End-to-End-Delay 800x400 & $ns_ flush-
trace
}
$ns_ at $val(stop) "stop"
$ns_ at $val(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"
puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp "
puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)" puts
"Starting Simulation..."
$ns_ run
PRACTICAL-7
Implement an Infrastructure-less wireless network comprising of 'N' nodes in an
area of 1000 x 1000 square metres.
At routing layer use AODV and DSR protocols.
Perform the simulations with varying number of nodes.
Plot and compare the performance of AODV and DSR in terms of the End-to-End
delay, Throughput, packet delivery ratio using NS2/NS3 simulator.

puts "Enter number of nodes" set


tnn [gets stdin]
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy set
val(mac) Mac/802_11 set val(ifq)
Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna set
val(x) 1500;#add manually set val(y)
1500;#add manually set val(ifqlen)
1000;#add manually set val(adhocRouting)
AODV;#add manually set val(nn) $tnn set
val(stop) 10.0

#add manually
Mac/802_11 set cdma_code_bw_start_ 0;# cdma code for bw request (start)
Mac/802_11 set cdma_code_bw_stop_ 63;# cdma code for bw request (stop)
Mac/802_11 set cdma_code_init_start_ 64;# cdma code for initial request (start)
Mac/802_11 set cdma_code_init_stop_ 127;# cdma code for initial request (stop)
Mac/802_11 set cdma_code_cqich_start_ 128 ;# cdma code for cqich request (start)
Mac/802_11 set cdma_code_cqich_stop_ 195;# cdma code for cqich request (stop)
Mac/802_11 set cdma_code_handover_start_ 196 ;# cdma code for handover request
(start)

Mac/802_11 set cdma_code_handover_stop_ 255;# cdma code for handover request


(stop) #end
set f0 [open out02.tr w] set f1
[open lost02.tr w] set f2 [open
delay02.tr w] set ns_ [new
Simulator] set topo [new
Topography] set tracefd [open
out.tr w] set namtrace [open
out.nam w]

$ns_ trace-all $tracefd


$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
$topo load_flatgrid $val(x) $val(y) set
god_ [create-god $val(nn)]
$ns_ color 0 red
$ns_ node-config -adhocRouting AODV \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-energyModel EnergyModel \
-initialEnergy 100 \
-rxPower 0.3 \
-txPower 0.6 \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF

#add manually
for {set i 0} {$i < $val(nn) } {incr i} { set
node_($i) [$ns_ node]
$node_($i) set X_ [expr rand() * 500] $node_($i)
set Y_ [expr rand() * 500]
$node_($i) set Z_ 0.000000000000;
}
for {set i 0} {$i < $val(nn) } {incr i} {
set xx [expr rand() * 1500] set yy
[expr rand() * 1000]
$ns_ at 0.1 "$node_($i) setdest $xx 4yy 5"
}
puts "Loading connection pattern..."
puts "Loading scenario file..." for
{set i 0} {$i < $val(nn) } {incr i} {
$ns_ initial_node_pos $node_($i) 55
}
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop).0 "$node_($i) reset";
}
puts "Enter source node" set source [gets
stdin] puts "Enter destination node" set
dest [gets stdin] set udp_(0) [new
Agent/UDP] $ns_ attach-agent
$node_($source) $udp_(0) set sink [new
Agent/LossMonitor] $ns_ attach-agent
$node_($dest) $sink set cbr1_(0) [new
Application/Traffic/CBR]
$cbr1_(0) set packetSize_ 1000
$cbr1_(0) set interval_ 0.1

$cbr1_(0) set maxpkts_ 10000


$cbr1_(0) attach-agent $udp_(0)
$ns_ connect $udp_(0) $sink
$ns_ at 1.00 "$cbr1_(0) start"
set holdtime 0 set holdseq 0
set holdrate1 0 proc record
{} {
global sink f0 f1 f2 holdtime holdseq holdrate1
set ns [Simulator instance] set time 0.9 ;#Set
Sampling Time to 0.9 Sec set bw0 [$sink set
bytes_] set bw1 [$sink set nlost_] set bw2
[$sink set lastPktTime_] set bw3 [$sink set
npkts_] set now [$ns now]

# Record Bit Rate in Trace Files


puts $f0 "$now [expr (($bw0+$holdrate1)*8)/(2*$time*1000000)]"
# Record Packet Loss Rate in File
puts $f1 "$now [expr $bw1/$time]" if
{ $bw3 > $holdseq } {
puts $f2 "$now [expr ($bw2 - $holdtime)/($bw3 - $holdseq)]"
} else {
puts $f2 "$now [expr ($bw3 - $holdseq)]"
}
$sink set bytes_ 0
$sink set nlost_ 0 set
holdtime $bw2 set
holdseq $bw3 set
holdrate1 $bw0
$ns at [expr $now+$time] "record" ;# Schedule Record after $time interval sec}

#end
# Start Recording at Time 0
$ns_ at 0.0 "record" source
link.tcl proc stop {} { global
ns_ tracefd f0 f1 f2 # Close
Trace Files close $f0 close
$f1 close $f2 exec nam
out.nam
exec xgraph out02.tr -geometry -x TIME -y thr -t Throughput 800x400 & exec
xgraph lost02.tr -geometry -x TIME -y loss -t Packet_loss 800x400 & exec xgraph
delay02.tr -geometry -x TIME -y delay -t End-to-End-Delay 800x400 & $ns_ flush-
trace

}
$ns_ at $val(stop) "stop"
$ns_ at $val(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"
puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp "
puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)" puts
"Starting Simulation..."
$ns_ run

You might also like