Ns2 Sample Coding in Wireless
Ns2 Sample Coding in Wireless
Ns2 Sample Coding in Wireless
# Define options set val(chan) Channel/WirelessChannel ;# channel type set val(prop) Propagation/TwoRayGround ;# radio-propagation model set val(netif) Phy/WirelessPhy ;# network interface type set val(mac) Mac/802_11 ;# MAC type set val(ifq) Queue/DropTail/PriQueue ;# interface queue type set val(ll) LL ;# link layer type set val(ant) Antenna/OmniAntenna ;# antenna model set val(ifqlen) 50 ;# max packet in ifq set val(nn) 50 ;# number of mobilenodes set val(rp) DSDV ;# routing protocol set val(x) 1000 ;# X dimension of topography set val(y) 1000 ;# Y dimension of topography set val(stop) 150 ;# time of simulation end set ns [new Simulator] set tracefd [open simple.tr w] set namtrace [open simwrls.nam w] $ns trace-all $tracefd $ns namtrace-all-wireless $namtrace $val(x) $val(y) # set up topography object set topo [new Topography] $topo load_flatgrid $val(x) $val(y) create-god $val(nn)
# configure the nodes $ns node-config -adhocRouting $val(rp) \ -llType $val(ll) \ -macType $val(mac) \ -ifqType $val(ifq) \ -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \
-phyType $val(netif) \ -channelType $val(chan) \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace OFF \ -movementTrace ON for {set i 0} {$i < $val(nn) } { incr i } { set n($i) [$ns node] } # Provide initial location of mobilenodes $n(0) set X_ 347.0 $n(0) set Y_ 3.0 $n(0) set Z_ 0.0 $n(1) set X_ 345.0 $n(1) set Y_ 36.0 $n(1) set Z_ 0.0 $n(2) set X_ 330.0 $n(2) set Y_ 121.0 $n(2) set Z_ 0.0 $n(3) set X_ 316.0 $n(3) set Y_ 152.0 $n(3) set Z_ 0.0 $n(4) set X_ 246.0 $n(4) set Y_ 90.0 $n(4) set Z_ 0.0 $n(5) set X_ 379.0 $n(5) set Y_ 6.0 $n(5) set Z_ 0.0
# Set a TCP connection between n(1) and n(31) set tcp [new Agent/TCP/Newreno] $tcp set class_ 2 set sink [new Agent/TCPSink] $ns attach-agent $n(1) $tcp
$ns attach-agent $n(31) $sink $ns connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns at 10.0 "$ftp start" # Set a TCP connection between n(31) and n(43) set tcp [new Agent/TCP/Newreno] $tcp set class_ 2 set sink [new Agent/TCPSink] $ns attach-agent $n(31) $tcp $ns attach-agent $n(43) $sink $ns connect $tcp $sink #defining heads $ns at 0.0 "$n(0) label CH" $ns at 0.0 "$n(1) label Source" #$ns at 0.0 "$n(2) label N2"
$ns at 10.0 "$n(5) setdest 785.0 228.0 5.0" $ns at 13.0 "$n(26) setdest 700.0 20.0 5.0" $ns at 15.0 "$n(14) setdest 115.0 85.0 5.0" #Color change while moving from one group to another $ns at 73.0 "$n(2) delete-mark N2" $ns at 73.0 "$n(2) add-mark N2 pink circle" $ns at 124.0 "$n(11) delete-mark N11" $ns at 124.0 "$n(11) add-mark N11 purple circle" $ns at 103.0 "$n(5) delete-mark N5" $ns at 103.0 "$n(5) add-mark N5 white circle" $ns at 87.0 "$n(26) delete-mark N26" $ns at 87.0 "$n(26) add-mark N26 yellow circle" $ns at 92.0 "$n(14) delete-mark N14" $ns at 92.0 "$n(14) add-mark N14 green circle" # Define node initial position in nam for {set i 0} {$i < $val(nn)} { incr i } { # 20 defines the node size for nam $ns initial_node_pos $n($i) 20 } # Telling nodes when the simulation ends
for {set i 0} {$i < $val(nn) } { incr i } { $ns at $val(stop) "$n($i) reset"; } # ending nam and the simulation $ns at $val(stop) "$ns nam-end-wireless $val(stop)" $ns at $val(stop) "stop" $ns at 150.01 "puts \"end simulation\" ; $ns halt" proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam simwrls.nam & } $ns run
Description:
Three wireless nodes are created and they are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Location of the nodes is fixed by specifying X, Y coordinates. Z coordinate is always zero. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation.
File name: wireless1.tcl
} for {set i 0} {$i < $val(nn) } {incr i } { $node_($i) color black $ns at 0.0 "$node_($i) color black" }
#stop procedure:
proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam wireless1.nam & } $ns run
# How to run the program $ns wireless1.tcl # snapshot of the program output
Description:
Eight wireless nodes are created and they are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Location of the nodes is fixed by specifying X, Y coordinates. Z coordinate is always zero. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation. In this program nodes are given with cyan color
File name: wireless2.tcl
for {set i 0} {$i < $val(nn) } {incr i } { $node_($i) color cyan $ns at 0.0 "$node_($i) color cyan" }
$ns at $val(stop) "stop" $ns at 10.01 "puts \"end simulation\" ; $ns halt" proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam wireless2.nam & } $ns run
# How to run the program $ns wireless2.tcl # snapshot of the program output
Description:
Number of nodes is not fixed in the program. We can give the number of nodes to be created during runtime. Number of nodes should be given as command line arguments when we run the coding. Dynamically created nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Location of the nodes is fixed by specifying X, Y coordinates. Z coordinate is always zero Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation. In this program nodes are given with yellow color.
File name: wireless1.tcl
if {$argc != 1} { error "\nCommand: ns wireless1.tcl <no.of.mobile-nodes>\n\n " }
$node_(5) set Z_ 0.0 $node_(6) set X_ -128.0 $node_(6) set Y_ 260.0 $node_(6) set Z_ 0.0 $node_(7) set X_ 727.0 $node_(7) set Y_ 269.0 $node_(7) set Z_ 0.0 $node_(8) set X_ 130.0 $node_(8) set Y_ 126.0 $node_(8) set Z_ 0.0 $node_(9) set X_ 318.0 $node_(9) set Y_ 45.0 $node_(9) set Z_ 0.0 $node_(10) set X_ 505.0 $node_(10) set Y_ 446.0 $node_(10) set Z_ 0.0 $node_(11) set X_ 421.0 $node_(11) set Y_ 158.0 $node_(11) set Z_ 0.0
$ns run
# How to run the program $ns wireless1.tcl # snapshot of the program output
Tcl script to create the dynamic number of nodes and its initial location
Description:
Number of nodes is not fixed in the program. We can give the number of nodes to be created during runtime. Number of nodes should be given as command line arguments when we run the coding. Dynamically created nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is not fixed. X, Y coordinates are randomly selected and
assigned to every node. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation. In this program nodes are given with yellow color.
File name: wireless3.tcl
if {$argc != 1} { error "\nCommand: ns wireless3.tcl <no.of.mobile-nodes>\n\n " }
-ifqType $val(ifq) \ -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \ -phyType $val(netif) \ -channelType $val(chan) \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace OFF \ -movementTrace ON
#stop procedure...
proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam wireless3.nam &
} $ns run
# How to run the program $ns wireless3.tcl # snapshot of the program output
Tcl script to create the dynamic color and initial location to nodes
Description:
Number of nodes is fixed in the program. Nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is not fixed. X, Y coordinates are randomly selected and assigned to every node. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end
time of the simulation. In this program nodes are not given color. Their color will change dynamically based on time.
File name: wireless1.tcl
## Trace the nam and trace details from the main simulation..
$ns trace-all $tracefd $ns namtrace-all-wireless $namtrace $val(x) $val(y)
## Color Descriptions..
$ns color 1 dodgerblue $ns color 2 blue $ns color 3 cyan $ns color 4 green $ns color 5 yellow $ns color 6 black $ns color 7 magenta $ns color 8 gold $ns color 9 red
set colorname(2) green set colorname(3) red set colorname(4) gold set colorname(5) magenta
## stop procedure..
$ns at $val(stop) "stop" proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace puts "running nam..." exec nam wireless1.nam & exit 0 } $ns runs
Description:
Number of nodes is fixed in the program. Nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is fixed. Specific X, Y coordinates are assigned to every node. Nodes are given mobility with fixed speed and specific destination location which is generated randomly. Nodes mobility will change time to time. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation.
File name: wireless3.tcl
if {$argc != 1} { error "\nCommand: ns test2.tcl <no.of.mobile-nodes>\n\n " }
## Trace the nam and trace details from the main simulation..
$ns trace-all $tracefd $ns namtrace-all-wireless $namtrace $val(x) $val(y)
## Color Descriptions..
$ns color 1 dodgerblue $ns color 2 blue $ns color 3 cyan $ns color 4 green $ns color 5 yellow $ns color 6 black $ns color 7 magenta $ns color 8 gold $ns color 9 red
set dist(35m) 1.56962e-07 set dist(40m) 1.56962e-10 set dist(45m) 1.56962e-11 set dist(50m) 1.20174e-13 #Phy/WirelessPhy set CSThresh_ $dist(50m) #Phy/WirelessPhy set RXThresh_ $dist(50m)
$node_(1) set Z_ 0.0 $node_(2) set X_ 294.0 $node_(2) set Y_ 235.0 $node_(2) set Z_ 0.0 $node_(3) set X_ 414.0 $node_(3) set Y_ 342.0 $node_(3) set Z_ 0.0 $node_(4) set X_ 562.0 $node_(4) set Y_ 267.0 $node_(4) set Z_ 0.0 $node_(5) set X_ 279.0 $node_(5) set Y_ 447.0 $node_(5) set Z_ 0.0 $node_(6) set X_ -128.0 $node_(6) set Y_ 260.0 $node_(6) set Z_ 0.0 $node_(7) set X_ 727.0 $node_(7) set Y_ 269.0 $node_(7) set Z_ 0.0 $node_(8) set X_ 130.0 $node_(8) set Y_ 126.0 $node_(8) set Z_ 0.0 $node_(9) set X_ 318.0 $node_(9) set Y_ 45.0 $node_(9) set Z_ 0.0 $node_(10) set X_ 505.0 $node_(10) set Y_ 446.0 $node_(10) set Z_ 0.0 $node_(11) set X_ 421.0 $node_(11) set Y_ 158.0 $node_(11) set Z_ 0.0 $node_(12) set X_ 72.0 $node_(12) set Y_ 397.0 $node_(12) set Z_ 0.0 if {$val(nn) >12} { for {set i 13} {$i < $val(nn) } { incr i } { set xx [expr rand()*600] set yy [expr rand()*500] $node_($i) set X_ $xx $node_($i) set Y_ $yy $node_($i) set Z_ 0.0
} }
## Stop procedure..
$ns at 0.0 "destination" proc destination {} { global ns val node_ set time 1.0 set now [$ns now] for {set i 0} {$i<$val(nn)} {incr i} { set xx [expr rand()*600] set yy [expr rand()*500] $ns at $now "$node_($i) setdest $xx $yy 20.0" } $ns at [expr $now+$time] "destination" } $ns at $val(stop) "stop"
#stop procedure:
proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace puts "running nam..." exec nam wireless2.nam & exit 0 } $ns run
Description:
Number of nodes (3) is fixed in the program. Nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is fixed. Specific X, Y coordinates are assigned to every node. Nodes are given mobility with fixed speed and fixed destination location. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation. TCP agent is attached to node_ (0). TCPSink agent is attached to node_(1). Both the agents are connected and FTP application is attached to TCP agent. Now communication set up for node_(0) and node_(1) is established. Similarly communication between node_(1) and node_(2) is established.
File name: wireless1.tcl
# Generation of movements
$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0" $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0" $ns at 19.0 "$node_(2) setdest 480.0 300.0 5.0"
Tcl script to make TCP communication between nodes using DSR routing protocol
Description:
Number of nodes (3) is fixed in the program. Nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is fixed. Specific X, Y coordinates are assigned to every node. Nodes are given mobility with fixed speed and fixed destination location. Here we set the initial size for the every node by using initial_node_pos. DSR routing protocol is used here. $val(stop) specifies the end time of the simulation. TCP agent is attached to node_ (0). TCPSink agent is attached to node_(1). Both the agents are connected and FTP application is attached to TCP agent. Now communication set up for node_(0) and node_(1) is established
File name: Dsr.tcl
# Generation of movements
$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0" $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0" $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
$ns at $val(stop) "$ns nam-end-wireless $val(stop)" $ns at $val(stop) "stop" $ns at 150.01 "puts \"end simulation\" ; $ns halt" proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam dsr.nam & exit 0 } $ns run
Tcl script to make communication between nodes using AODV routing protocol and CBR traffic
Description:
Number of nodes (22) is fixed in the program. Nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is fixed. Specific X, Y coordinates are assigned to every node. Nodes are given mobility with fixed speed and fixed destination location. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation. UDP agent is attached to sender node. LossMonitor agent is attached to receiver node. Both the agents are connected and CBR traffic is attached to UDP agent. Now communication set up for nodes are established.
File name: Aodv.tcl
$ns_ node-config -adhocRouting $val(rp) \ -llType $val(ll) \ -macType $val(mac) \ -ifqType $val(ifq) \ -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \ -phyType $val(netif) \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace ON \ -movementTrace ON \ -channel $chan_1_
$Server1 set Y_ 517.0 $Server1 set Z_ 0.0 $Server2 set X_ 1445.0 $Server2 set Y_ 474.0 $Server2 set Z_ 0.0 $n2 set X_ 36.0 $n2 set Y_ 529.0 $n2 set Z_ 0.0 $n3 set X_ 143.0 $n3 set Y_ 666.0 $n3 set Z_ 0.0 $n4 set X_ 201.0 $n4 set Y_ 552.0 $n4 set Z_ 0.0 $n5 set X_ 147.0 $n5 set Y_ 403.0 $n5 set Z_ 0.0 $n6 set X_ 230.0 $n6 set Y_ 291.0 $n6 set Z_ 0.0 $n7 set X_ 295.0 $n7 set Y_ 419.0 $n7 set Z_ 0.0 $n8 set X_ 363.0 $n8 set Y_ 335.0 $n8 set Z_ 0.0 $n9 set X_ 334.0 $n9 set Y_ 647.0 $n9 set Z_ 0.0 $n10 set X_ 304.0 $n10 set Y_ 777.0 $n10 set Z_ 0.0 $n11 set X_ 412.0 $n11 set Y_ 194.0 $n11 set Z_ 0.0 $n12 set X_ 519.0 $n12 set Y_ 361.0 $n12 set Z_ 0.0 $n13 set X_ 569.0
$n13 set Y_ 167.0 $n13 set Z_ 0.0 $n14 set X_ 349.0 $n14 set Y_ 546.0 $n14 set Z_ 0.0 $n15 set X_ 466.0 $n15 set Y_ 668.0 $n15 set Z_ 0.0 $n16 set X_ 489.0 $n16 set Y_ 794.0 $n16 set Z_ 0.0 $n17 set X_ 606.0 $n17 set Y_ 711.0 $n17 set Z_ 0.0 $n18 set X_ 630.0 $n18 set Y_ 626.0 $n18 set Z_ 0.0 $n19 set X_ 666.0 $n19 set Y_ 347.0 $n19 set Z_ 0.0 $n20 set X_ 741.0 $n20 set Y_ 152.0 $n20 set Z_ 0.0 $n21 set X_ 882.0 $n21 set Y_ 264.0 $n21 set Z_ 0.0 $n22 set X_ 761.0 $n22 set Y_ 441.0 $n22 set Z_ 0.0
$ns_ at 0.25 "$n13 setdest 551.0 624.0 25.0" $ns_ at 1.25 "$n14 setdest 397.0 647.0 25.0" $ns_ at 1.25 "$n15 setdest 748.0 688.0 25.0" $ns_ at 1.25 "$n16 setdest 842.0 623.0 25.0" $ns_ at 1.25 "$n17 setdest 678.0 548.0 25.0" $ns_ at 0.75 "$n18 setdest 741.0 809.0 20.0" $ns_ at 0.75 "$n19 setdest 437.0 799.0 20.0" $ns_ at 0.20 "$n20 setdest 159.0 722.0 20.0" $ns_ at 1.25 "$n21 setdest 700.0 350.0 20.0" $ns_ at 0.75 "$n22 setdest 839.0 444.0 20.0"
set udp2 [$ns_ create-connection UDP $n21 LossMonitor $n20 0] $udp2 set fid_ 1 set cbr2 [$udp2 attach-app Traffic/CBR] $cbr2 set packetSize_ 1000 $cbr2 set interval_ .07 $ns_ at 2.4 "$cbr2 start" $ns_ at 4.1 "$cbr2 stop" set udp3 [$ns_ create-connection UDP $Server1 LossMonitor $n15 0] $udp3 set fid_ 1 set cbr3 [$udp3 attach-app Traffic/CBR]
$cbr3 set packetSize_ 1000 $cbr3 set interval_ 5 $ns_ at 4.0 "$cbr3 start" $ns_ at 4.1 "$cbr3 stop" set udp4 [$ns_ create-connection UDP $Server1 LossMonitor $n14 0] $udp4 set fid_ 1 set cbr4 [$udp4 attach-app Traffic/CBR] $cbr4 set packetSize_ 1000 $cbr4 set interval_ 5 $ns_ at 4.0 "$cbr4 start" $ns_ at 4.1 "$cbr4 stop" set udp5 [$ns_ create-connection UDP $n15 LossMonitor $n16 0] $udp5 set fid_ 1 set cbr5 [$udp5 attach-app Traffic/CBR] $cbr5 set packetSize_ 1000 $cbr5 set interval_ 5 $ns_ at 4.0 "$cbr5 start" $ns_ at 4.1 "$cbr5 stop" set udp6 [$ns_ create-connection UDP $n15 LossMonitor $n17 0] $udp6 set fid_ 1 set cbr6 [$udp6 attach-app Traffic/CBR] $cbr6 set packetSize_ 1000 $cbr6 set interval_ 5 $ns_ at 4.0 "$cbr6 start" $ns_ at 4.1 "$cbr6 stop" set udp7 [$ns_ create-connection UDP $n14 LossMonitor $n4 0] $udp7 set fid_ 1 set cbr7 [$udp7 attach-app Traffic/CBR] $cbr7 set packetSize_ 1000 $cbr7 set interval_ 5 $ns_ at 4.0 "$cbr7 start" $ns_ at 4.1 "$cbr7 stop" set udp8 [$ns_ create-connection UDP $n14 LossMonitor $n9 0] $udp8 set fid_ 1 set cbr8 [$udp8 attach-app Traffic/CBR] $cbr8 set packetSize_ 1000 $cbr8 set interval_ 5 $ns_ at 4.0 "$cbr8 start" $ns_ at 4.1 "$cbr8 stop" set udp9 [$ns_ create-connection UDP $n4 LossMonitor $n3 0] $udp9 set fid_ 1 set cbr9 [$udp9 attach-app Traffic/CBR] $cbr9 set packetSize_ 1000 $cbr9 set interval_ 5 $ns_ at 4.0 "$cbr9 start" $ns_ at 4.1 "$cbr9 stop"
set udp10 [$ns_ create-connection UDP $n4 LossMonitor $n2 0] $udp10 set fid_ 1 set cbr10 [$udp10 attach-app Traffic/CBR] $cbr10 set packetSize_ 1000 $cbr10 set interval_ 5 $ns_ at 4.0 "$cbr10 start" $ns_ at 4.1 "$cbr10 stop" set udp11 [$ns_ create-connection UDP $n9 LossMonitor $n16 0] $udp11 set fid_ 1 set cbr11 [$udp11 attach-app Traffic/CBR] $cbr11 set packetSize_ 1000 $cbr11 set interval_ 5 $ns_ at 4.0 "$cbr11 start" $ns_ at 4.1 "$cbr11 stop" set udp12 [$ns_ create-connection UDP $n9 LossMonitor $n10 0] $udp12 set fid_ 1 set cbr12 [$udp12 attach-app Traffic/CBR] $cbr12 set packetSize_ 1000 $cbr12 set interval_ 5 $ns_ at 4.0 "$cbr12 start" $ns_ at 4.1 "$cbr12 stop"
#ANNOTATIONS DETAILS
$ns_ at 0.0 "$ns_ trace-annotate \"MOBILE NODE MOVEMENTS\"" $ns_ at 4.1 "$ns_ trace-annotate \"NODE27 CACHE THE DATA FRO SERVER\"" #$ns_ at 4.59 "$ns_ trace-annotate \"PACKET LOSS AT NODE27\"" $ns_ at 4.71 "$ns_ trace-annotate \"NODE10 CACHE THE DATA\""
Tcl script to make TCP communication between nodes using DSDV routing protocol
Description:
Number of nodes (3) is fixed in the program. Nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is fixed. Specific X, Y coordinates are assigned to every node. Nodes are given mobility with fixed speed and fixed destination location. Here we set the initial size for the every node by using initial_node_pos. DSDV routing protocol is used here. $val(stop) specifies the end time of the simulation. TCP agent is attached to node_ (0). TCPSink agent is attached to node_(1). Both the agents are connected and FTP application is attached to TCP agent. Now communication set up for node_(0) and node_(1) is established.
File name: dsdv.tcl
#Creating trace file and nam file set tracefd [open dsdv.tr w] set windowVsTime2 [open win.tr w] set namtrace [open dsdv.nam w]
$ns trace-all $tracefd $ns namtrace-all-wireless $namtrace $val(x) $val(y)
$node_(0) set Z_ 0.0 $node_(1) set X_ 490.0 $node_(1) set Y_ 285.0 $node_(1) set Z_ 0.0 $node_(2) set X_ 150.0 $node_(2) set Y_ 240.0 $node_(2) set Z_ 0.0
# Generation of movements
$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0" $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0" $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam dsdv.nam & exit 0 } $ns run
Description:
Number of nodes (4) is fixed in the program. Nodes are configured with specific parameters of a mobile wireless node. Here we use the energy model for each node with -initialEnergy 20 \ -txPower 0.9 \-rxPower 0.8 \-idlePower 0.0 \ -sensePower 0.0175. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is fixed. Specific X, Y coordinates are assigned to every node. Nodes are given mobility with fixed speed and random destination location. Here we set the initial size for the every node by using initial_node_pos. DSR routing protocol is used here. $val(stop) specifies the end time of the simulation
File name: wireless1.tcl ## Setting The wireless Channels..
set val(chan) set val(prop) set val(netif) set val(mac) set val(ifq) set val(ll) set val(ant) set val(ifqlen) set val(nn) set val(rp) set val(x) set val(y) set val(stop) Channel/WirelessChannel ;# channel type Propagation/TwoRayGround ;# radio-propagation model Phy/WirelessPhy ;# network interface type Mac/802_11 ;# MAC type Queue/DropTail/PriQueue ;# interface queue type LL ;# link layer type Antenna/OmniAntenna ;# antenna model 5 ;# max packet in ifq 6 ;# number of mobilenodes DSR ;# routing protocol 750 ;# X dimension of topography 550 ;# Y dimension of topography 18.0 ;# time of simulation end
## Trace the nam and trace details from the main simulation..
$ns trace-all $tracefd $ns namtrace-all-wireless $namtrace $val(x) $val(y)
## Color Descriptions..
$ns color 1 dodgerblue $ns color 2 blue
$ns color 3 cyan $ns color 4 green $ns color 5 yellow $ns color 6 black $ns color 7 magenta $ns color 8 gold $ns color 9 red
$ns node-config -adhocRouting $val(rp) \ -llType $val(ll) \ -macType $val(mac) \ -ifqType $val(ifq) \ -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \ -phyType $val(netif) \ -channelType $val(chan) \ -topoInstance $topo \ -agentTrace ON \
# Energy model
$ns node-config -energyModel EnergyModel \ -initialEnergy 20 \ -txPower 0.9 \ -rxPower 0.8 \ -idlePower 0.0 \ -sensePower 0.0175
## Stop procedure..
proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam wireless1.nam & exit 0 } $ns run
Destination:
Number of nodes is not fixed in the program. We can give the number of nodes to be created during runtime. Number of nodes should be given as command line arguments when we run the coding. Dynamically created nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is not fixed. X, Y coordinates are randomly selected and assigned to every node. Nodes are given mobility with fixed speed and random destination. Destination location will be changed time to time. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation.
File name: wireless1.tcl # Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) set val(netif) set val(mac) set val(ifq) set val(ll) set val(ant) set val(ifqlen) set val(nn) set val(rp) set val(x) set val(y) set val(stop)
Propagation/TwoRayGround ;# radio-propagation model Phy/WirelessPhy ;# network interface type Mac/802_11 ;# MAC type Queue/DropTail/PriQueue ;# interface queue type LL ;# link layer type Antenna/OmniAntenna ;# antenna model 50 ;# max packet in ifq 5 ;# number of mobilenodes AODV ;# routing protocol 500 ;# X dimension of topography 400 ;# Y dimension of topography 10 ;# time of simulation end
set ns
[new Simulator]
} for {set i 0} {$i < $val(nn) } {incr i } { $node_($i) color black $ns at 0.0 "$node_($i) color black" }
#Destination procedure..
$ns at 0.0 "destination" proc destination {} { global ns val node_ set time 1.0 set now [$ns now] for {set i 0} {$i<$val(nn)} {incr i} { set xx [expr rand()*500] set yy [expr rand()*400] $ns at $now "$node_($i) setdest $xx $yy 10.0" } $ns at [expr $now+$time] "destination" } $ns at $val(stop) "stop"
#Stop procedure
proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam wireless1.nam &
} $ns run
Destination:
Number of nodes is not fixed in the program. We can give the number of nodes to be created during runtime. Number of nodes should be given as command line arguments when we run the coding. Dynamically created nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace
file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is not fixed. X, Y coordinates are randomly selected and assigned to every node. Nodes are given mobility with fixed speed and random destination. Destination location will be changed time to time. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation. Each group of nodes are given different color.
File name: wireless2.tcl # Define setting options
set val(chan) set val(prop) set val(netif) set val(mac) set val(ifq) set val(ll) set val(ant) set val(ifqlen) set val(nn) set val(rp) set val(x) set val(y) set val(stop) set ns Channel/WirelessChannel ;# channel type Propagation/TwoRayGround ;# radio-propagation model Phy/WirelessPhy ;# network interface type Mac/802_11 ;# MAC type Queue/DropTail/PriQueue ;# interface queue type LL ;# link layer type Antenna/OmniAntenna ;# antenna model 50 ;# max packet in ifq 8 ;# number of mobilenodes AODV ;# routing protocol 500 ;# X dimension of topography 400 ;# Y dimension of topography 10 ;# time of simulation end [new Simulator]
proc destination {} { global ns val node_ set time 1.0 set now [$ns now] for {set i 0} {$i<$val(nn)} {incr i} { set xx [expr rand()*500] set yy [expr rand()*400] $ns at $now "$node_($i) setdest $xx $yy 10.0" } $ns at [expr $now+$time] "destination" }
#stop procedure..
$ns at $val(stop) "stop" proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam wireless2.nam & } $ns run
Destination:
Number of nodes is fixed (8) in the program. Nodes are configured with specific parameters of a mobile wireless node. After creating the nam file and trace file, we set up topography object. set node_ ($i) [$ns node] is used to create the nodes. Initial location of the nodes is not fixed. X, Y coordinates are randomly selected and assigned to every node. Nodes are given mobility with fixed speed and random destination. Destination location will be changed time to time. Here we set the initial size for the every node by using initial_node_pos. AODV routing protocol is used here. $val(stop) specifies the end time of the simulation. Each group of nodes is given different color.
File name: wireless3.tcl # Define options
set val(chan) set val(prop) set val(netif) Channel/WirelessChannel Propagation/TwoRayGround Phy/WirelessPhy ;# channel type ;# radio-propagation model ;# network interface type
set val(mac) set val(ifq) set val(ll) set val(ant) set val(ifqlen) set val(nn) set val(rp) set val(x) set val(y) set val(stop)
;# MAC type ;# interface queue type ;# link layer type ;# antenna model ;# max packet in ifq ;# number of mobilenodes ;# routing protocol ;# X dimension of topography ;# Y dimension of topography ;# time of simulation end
#Creating simulation:
set ns [new Simulator]
for {set i 3} {$i < 6 } { incr i } { set node_($i) [$ns node] } for {set i 3} {$i < 6 } {incr i } { $node_($i) color cyan $ns at 1.0 "$node_($i) color cyan" } for {set i 6} {$i < 8 } { incr i } { set node_($i) [$ns node] } for {set i 5} {$i < 8 } {incr i } { $node_($i) color red $ns at 2.0 "$node_($i) color red" }
#stop procedure..
$ns at $val(stop) "stop" proc stop {} {
global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec nam wireless3.nam & } $ns run
Tcl script to draw the graph and set x and y coordinates randomly.
Description:
X, Y coordinates for the graph is generated randomly and put it in a trace file. The trace file is given as input file to xgraph to plot the graph.
File name: graph1.tcl
# Creating simulation
set ns [new Simulator]
-movementTrace ON
#Graph procedure..
$ns at 1.0 "Graph" set g [open graph.tr w] proc Graph {} { global ns g set time 1.0 set now [$ns now] puts $g "[expr rand()*8] [expr rand()*6]" $ns at [expr $now+$time] "Graph"
#Stop proceture
proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec xgraph -M -bb -geometry 700X800 graph.tr & exec nam Graph1.nam & exit 0 } $ns run
# How to run the program $ns Graph1.tcl # snapshot of the program output
Description:
X, Y coordinates points for the graph is generated randomly and put it in a trace file. The trace file is given as input file to xgraph to plot the graph. Here two trace files are plotted in the single graph. Point of each trace file will be differentiated in color.
File name: graph2.tcl
# Creating simulation
set ns [new Simulator]
-phyType $val(netif) \ -channelType $val(chan) \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace OFF \ -movementTrace ON
#Graph procedure..
#procedure.. $ns at 1.0 "Graph" set g [open graph.tr w] set g1 [open graph1.tr w]
proc Graph {} { global ns g g1 set time 1.0 set now [$ns now] puts $g "[expr rand()*8] [expr rand()*6]" puts $g1 "[expr rand()*8] [expr rand()*6]" $ns at [expr $now+$time] "Graph" }
#stop procedure:
proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec xgraph -P -bb -geometry 700X800 graph.tr graph1.tr & exec nam Graph2.nam & exit 0 } $ns run
# How to run the program $ns Graph2.tcl # snapshot of the program output
Tcl script to generate graph taking more than two parameter files as input
Description:
X, Y coordinates points for more than one graph is generated randomly using recursive procedure and put it in the trace files. All the trace files are given as input file to xgraph.
$node_(1) set X_ 200.0 $node_(1) set Y_ 250.0 $node_(1) set Z_ 0.0 $node_(2) set X_ 300.0 $node_(2) set Y_ 300.0 $node_(2) set Z_ 0.0
#Graph procedure..
$ns at 1.0 "Graph" set g [open graph.tr w] set g1 [open graph1.tr w] set g2 [open graph2.tr w] proc Graph {} { global ns g g1 set time 1.0 set now [$ns now] puts $g "[expr rand()*8] [expr rand()*6]" puts $g1 "[expr rand()*8] [expr rand()*6]" puts $g2 "[expr rand()*8] [expr rand()*6]" $ns at [expr $now+$time] "Graph" }
#Stop proceture
proc stop {} { global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exec xgraph -M -bb -geometry 700X800 graph.tr graph1.tr graph2.tr & exec nam Graph3.nam & exit 0 }
$ns run
# How to run the program $ns Graph3.tcl # snapshot of the program output
Description:
An OTcl script that defines two object classes, "teacher" and "student", where "Student" is the child class of "teacher", and a member function called "greet" for each class. After the class definitions, each object instance is declared, the "subject" variable of each instance is set to maths for teacher and name for student and the
"greet" member function of each object instance is called. The keyword Class is to create an object class and instproc is to define a member function to an object class. Class inheritance is specified using the keyword -superclass. In defining member functions, $self acts same as the "this" pointer in C++, and instvar checks if the following variable name is already declared in its class or in its superclass. Finally, to create an object instance, the keyword new is used.
File name: Otcl.tcl # Add a member function callgreet
Class Teacher Teacher instproc init {subject} { $self instvar subject_ set subject_ $subject } # Creating a child class of teacher and student Teacher instproc greet {} { $self instvar subject_ puts "$subject_ teacher ask which subject u studied?" } #creating a teacher and student object Class student -superclass Teacher student instproc greet {} { $self instvar subject_ puts "$subject_ say i studied maths" } # calling member function greet of each node set a [new Teacher maths] set b [new student Jey] $a greet $b greet