Sunday, 15 May 2016

clustering node ns2

#===================================
#     Simulation parameters setup
#===================================
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)     16                         ;# number of mobilenodes
set val(rp)     DSDV                       ;# routing protocol
set val(x)      881                      ;# X dimension of topography
set val(y)      652                      ;# Y dimension of topography
set val(stop)   250.0                        ;# time of simulation end
 
#===================================
#        Initialization       
#===================================
#Create a ns simulator
set ns [new Simulator]
 
#Setup topography object
set topo       [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)
 
#Open the NS trace file
set tracefile [open out1.tr w]
$ns trace-all $tracefile
 
#Open the NAM trace file
set namfile [open out1.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel
 
#===================================
#     Mobile node parameter setup
#===================================
$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) \
                -channel       $chan \
                -topoInstance  $topo \
                -agentTrace    OFF \
                -routerTrace   ON \
                -macTrace      ON \
                -movementTrace ON
 
#===================================
#        Nodes Definition       
#===================================
#Create 16 nodes
set n0 [$ns node]
$n0 set X_ 437
$n0 set Y_ 301
$n0 set Z_ 0.0
$n0 color orange
$ns at 0.0 "$n0 color orange"
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 324
$n1 set Y_ 337
$n1 set Z_ 0.0
$n1 color orange
$ns at 0.0 "$n1 color orange"
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 274
$n2 set Y_ 257
$n2 set Z_ 0.0
$n2 color orange
$ns at 0.0 "$n2 color orange"
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 291
$n3 set Y_ 456
$n3 set Z_ 0.0
$n3 color orange
$ns at 0.0 "$n3 color orange"
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 478
$n4 set Y_ 390
$n4 set Z_ 0.0
$n4 color orange
$ns at 0.0 "$n4 color orange"
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 150
$n5 set Y_ 357
$n5 set Z_ 0.0
$n5 color orange
$ns at 0.0 "$n5 color orange"
$ns initial_node_pos $n5 20
set n6 [$ns node]
$n6 set X_ 436
$n6 set Y_ 153
$n6 set Z_ 0.0
$n6 color orange
$ns at 0.0 "$n6 color orange"
$ns initial_node_pos $n6 20
set n7 [$ns node]
$n7 set X_ 451
$n7 set Y_ 509
$n7 set Z_ 0.0
$n7 color orange
$ns at 0.0 "$n7 color orange"
$ns initial_node_pos $n7 20
set n8 [$ns node]
$n8 set X_ 150
$n8 set Y_ 166
$n8 set Z_ 0.0
$n8 color orange
$ns at 0.0 "$n8 color orange"
$ns initial_node_pos $n8 20
set n9 [$ns node]
$n9 set X_ 566
$n9 set Y_ 305
$n9 set Z_ 0.0
$n9 color orange
$ns at 0.0 "$n9 color orange"
$ns initial_node_pos $n9 20
set n10 [$ns node]
$n10 set X_ 611
$n10 set Y_ 462
$n10 set Z_ 0.0
$n10 color orange
$ns at 0.0 "$n10 color orange"
$ns initial_node_pos $n10 20
set n11 [$ns node]
$n11 set X_ 244
$n11 set Y_ 552
$n11 set Z_ 0.0
$n11 color orange
$ns at 0.0 "$n11 color orange"
$ns initial_node_pos $n11 20
set n12 [$ns node]
$n12 set X_ 298
$n12 set Y_ 76
$n12 set Z_ 0.0
$n12 color orange
$ns at 0.0 "$n12 color orange"
$ns initial_node_pos $n12 20
set n13 [$ns node]
$n13 set X_ 544
$n13 set Y_ 213
$n13 set Z_ 0.0
$n13 color orange
$ns at 0.0 "$n13 color orange"
$ns initial_node_pos $n13 20
set n14 [$ns node]
$n14 set X_ 663
$n14 set Y_ 355
$n14 set Z_ 0.0
$n14 color orange
$ns at 0.0 "$n14 color orange"
$ns initial_node_pos $n14 20
set n15 [$ns node]
$n15 set X_ 503
$n15 set Y_ 119
$n15 set Z_ 0.0
$n15 color red
$ns at 0.0 "$n15 color red"
$ns initial_node_pos $n15 20
 
$ns at 0.5 "$n0 setdest 741.222 219.713 15.0"
$ns at 0.5 "$n1 setdest 661.911 293.578 15.0"
$ns at 0.5 "$n2 setdest 720.195 368.71 15.0"
$ns at 0.5 "$n3 setdest 841.832 311.792 15.0"
$ns at 0.5 "$n4 setdest 846.279 231.381 15.0"
$ns at 0.5 "$n5 setdest 154.962 76.1411 15.0"
$ns at 0.5 "$n6 setdest 277.55 146.965 15.0"
$ns at 0.5 "$n7 setdest 218.432 229.934 15.0"
$ns at 0.5 "$n8 setdest 58.4876 147.719 15.0"
$ns at 0.5 "$n9 setdest 113.938 245.951 15.0"
$ns at 0.5 "$n10 setdest 497.792 404.879 15.0"
$ns at 0.5 "$n11 setdest 356.954 425.859 15.0"
$ns at 0.5 "$n12 setdest 355.962 535.156 15.0"
$ns at 0.5 "$n13 setdest 459.874 558.361 15.0"
$ns at 0.5 "$n14 setdest 546.27 490.731 15.0"
$ns at 70.0 "$n0 setdest 546.22 499.713 15.0"
$ns at 75.0 "$n7 setdest 741.222 219.713 15.0"
$ns at 65.5 "$n12 setdest 845.222 319.713 15.0"
$ns at 80.5 "$n3 setdest 355.222 519.713 15.0"
$ns at 75.0 "$n14 setdest 218.422 229.713 15.0"
$ns at 75.5 "$n4 setdest 720.195 368.71 15.0"
$ns at 75.5 "$n2 setdest 846.279 231.381 15.0"
$ns at 80.5 "$n9 setdest 58.4876 147.719 15.0"
$ns at 80.5 "$n8 setdest 113.938 245.951 15.0"
$ns at 77.5 "$n13 setdest 356.954 425.859 15.0"
$ns at 77.5 "$n11 setdest 459.874 558.361 15.0"


#$ns at 138.0 "$n10 setdest 656.26 181.573 15.0"
#$ns at 162.0 "$n10 add-mark N2 white circle"
#$n10 color darkgreen
#$ns at 162.0 "$n10 color darkgreen"
 
 
$ns at 0.0 "$n15 label Host"
 
#$ns at 39.0 "$n1 label High-Power-Cluster"
 
#$ns at 39.0 "$n6 label High-Power-Cluster"
 
#$ns at 108.0 "$n11 label High-Power-Cluster"
 
#$ns at 39.0 "$n10 label High-Power-Cluster"
 
#$ns at 108.0 "$n10 label  ordinary node"
 
#$ns at 99.1 "$n10 color  orange"
#$ns at 99.1 "$n14 color  darkgreen"
 
#$ns at 118.0 "$n1 color  orange"
#$ns at 118.0 "$n0 color  darkgreen"
 
 
 
$n1 color darkgreen
$ns at 64.1 "$n1 color darkgreen"
 
$n6 color darkgreen
$ns at 47.1 "$n6 color darkgreen"
 
$n10 color darkgreen
$ns at 56.1 "$n10 color darkgreen"

$n1 color darkgreen
$ns at 134.1 "$n1 color darkgreen"
 
$n6 color darkgreen
$ns at 140.1 "$n6 color darkgreen"
 
$n10 color darkgreen
$ns at 146.1 "$n10 color darkgreen"
 
$ns at 64.1 "$n1 add-mark N2 white circle"
 
$ns at 47.1 "$n6 add-mark N2 white circle"
 
$ns at 56.1 "$n10 add-mark N2 white circle"

$ns at 130.1 "$n1 add-mark N2 white circle"
 
$ns at 137.1 "$n6 add-mark N2 white circle"
 
$ns at 142.1 "$n10 add-mark N2 white circle"
 
$ns at 120.1 "$n10 color darkgreen"
 
$ns at 115.1 "$n1 add-mark N2 white circle"
 
$ns at 109.1 "$n6 add-mark N2 white circle"
 
$ns at 190.1 "$n10 add-mark N2 white circle"

$ns at 199.1 "$n1 add-mark N2 white circle"
 
$ns at 210.1 "$n6 add-mark N2 white circle"
 
$ns at 220.1 "$n10 add-mark N2 white circle"


#$ns at 99.1 "$n10 delete-mark N2"
 
#$ns at 99.1 "$n14 add-mark N2 white circle"
 
 
#$ns at 118.0 "$n1 delete-mark N2"
 
#$ns at 118.0 "$n0 add-mark N2 white circle"
 
#$ns at 157.0 "$n0 delete-mark N2"
 
#$ns at 157.0 "$n0 add-mark N2 pink circle"

#$ns at 185.1 "$n10 delete-mark N2"
 
#$ns at 185.1 "$n14 add-mark N2 white circle"
 
 
#$ns at 168.0 "$n1 delete-mark N2"
 
#$ns at 168.0 "$n0 add-mark N2 white circle"
 
#$ns at 177.0 "$n0 delete-mark N2"
 
#$ns at 177.0 "$n0 add-mark N2 pink circle"


 
proc create_cluster_head_node {
 
} {
 
 
global val ns_ node_ topo contador_nodos rng
 
Phy/WirelessPhy set Pt_ $val(pt_cluster_head)
$ns_ node-config -sensorNode ON \
-adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-energyModel $val(en) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
 -agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-rxPower 0.3 \
-txPower 0.6 \
-initialEnergy 100.0 \
-movementTrace OFF
set node_($contador_nodos) [$ns_ node]
$node_($contador_nodos) random-motion 0
set x [$rng uniform 0.0 $val(x)]
set y [$rng uniform 0.0 $val(y)]
$node_($contador_nodos) set X_ $x
$node_($contador_nodos) set Y_ $y
$node_($contador_nodos) set Z_ 0.0
set interval [$rng uniform 0.0 1.0]
Node/MobileNode/SensorNode set processingPower 0.36
Node/MobileNode/SensorNode set instructionsPerSecond_ 150000000
Phy/WirelessPhy set  bandwidth_ 1000000.0
 
set udp_($contador_nodos) [new Agent/UDP]
 
set app_($contador_nodos) [create_cluster_head_app [$node_(1) node-addr] $val(disseminating_type) $val(cluster_head_disseminating_interval)]
$node_($contador_nodos) attach $udp_($contador_nodos) $val(port)
$node_($contador_nodos) add-app $app_($contador_nodos)
set processing_($contador_nodos) [new Processing/AggregateProcessing]
 
$app_($contador_nodos) node $node_($contador_nodos)
$app_($contador_nodos) attach-agent $udp_($contador_nodos)
 
$app_($contador_nodos) attach-processing $processing_($contador_nodos)
$processing_($contador_nodos) node $node_($contador_nodos)
 
$ns_ at [expr $val(start) + 1 + $interval] "$app_($contador_nodos) start"
$ns_ at $val(stop) "$app_($contador_nodos) stop"
 
incr contador_nodos
 
 
}
 
 
 
set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns  attach-agent $n0 $tcp
$ns attach-agent $n1 $sink
$ns  connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
#$ftp set rate_ 1.0Mb
#$ftp set random_ null
#$ftp  set interval_ 0.4
$ns  at 64.2 "$ftp start"
$ns  at 68.0 "$ftp stop"
 
set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
set sink1 [new Agent/TCPSink]
$ns  attach-agent $n2 $tcp1
$ns attach-agent $n1 $sink1
$ns  connect $tcp1 $sink1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
#$ftp1 set rate_ 1.0Mb
#$ftp1 set random_ null
#$ftp1  set interval_ 0.4
$ns  at 68.2 "$ftp1 start"
$ns  at 72.0 "$ftp1 stop"
 
set tcp2 [new Agent/TCP]
$tcp2 set class_ 2
set sink2 [new Agent/TCPSink]
$ns  attach-agent $n3 $tcp2
$ns attach-agent $n1 $sink2
$ns  connect $tcp2 $sink2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
#$ftp2 set rate_ 1.0Mb
#$ftp2 set random_ null
#$ftp2  set interval_ 0.4
$ns  at 72.2 "$ftp2 start"
$ns  at 76.0 "$ftp2 stop"
 
set tcp3 [new Agent/TCP]
$tcp3 set class_ 2
set sink3 [new Agent/TCPSink]
$ns  attach-agent $n4 $tcp3
$ns attach-agent $n1 $sink3
$ns  connect $tcp3 $sink3
set ftp3 [new Application/FTP]
$ftp3 attach-agent $tcp3
#$ftp3 set rate_ 1.0Mb
#$ftp3 set random_ null
#$ftp3  set interval_ 0.4
$ns  at 76.2 "$ftp3 start"
$ns  at 80.0 "$ftp3 stop"
 
set tcp4 [new Agent/TCP]
$tcp4 set class_ 2
set sink4 [new Agent/TCPSink]
$ns  attach-agent $n1 $tcp4
$ns attach-agent $n15 $sink4
$ns  connect $tcp4 $sink4
set ftp4 [new Application/FTP]
$ftp4 attach-agent $tcp4
#$ftp4 set rate_ 1.0Mb
#$ftp4 set random_ null
#$ftp4  set interval_ 0.4
$ns  at 80.2 "$ftp4 start"
$ns  at 84.0 "$ftp4 stop"
 
set tcp5 [new Agent/TCP]
$tcp5 set class_ 2
set sink5 [new Agent/TCPSink]
$ns  attach-agent $n5 $tcp5
$ns attach-agent $n6 $sink5
$ns  connect $tcp5 $sink5
set ftp5 [new Application/FTP]
$ftp5 attach-agent $tcp5
#$ftp5 set rate_ 1.0Mb
#$ftp5 set random_ null
#$ftp5  set interval_ 0.4
$ns  at 84.2 "$ftp5 start"
$ns  at 88.0 "$ftp5 stop"
 
set tcp6 [new Agent/TCP]
$tcp6 set class_ 2
set sink6 [new Agent/TCPSink]
$ns  attach-agent $n7 $tcp6
$ns attach-agent $n6 $sink6
$ns  connect $tcp6 $sink6
set ftp6 [new Application/FTP]
$ftp6 attach-agent $tcp6
#$ftp6 set rate_ 1.0Mb
#$ftp6 set random_ null
#$ftp6  set interval_ 0.4
$ns  at 88.2 "$ftp6 start"
$ns  at 92.0 "$ftp6 stop"
 
set tcp7 [new Agent/TCP]
$tcp7 set class_ 2
set sink7 [new Agent/TCPSink]
$ns  attach-agent $n8 $tcp7
$ns attach-agent $n6 $sink7
$ns  connect $tcp7 $sink7
set ftp7 [new Application/FTP]
$ftp7 attach-agent $tcp7
#$ftp7 set rate_ 1.0Mb
#$ftp7 set random_ null
#$ftp7  set interval_ 0.4
$ns  at 92.2 "$ftp7 start"
$ns  at 96.0 "$ftp7 stop"
 
set tcp8 [new Agent/TCP]
$tcp8 set class_ 2
set sink8 [new Agent/TCPSink]
$ns  attach-agent $n9 $tcp8
$ns attach-agent $n6 $sink8
$ns  connect $tcp8 $sink8
set ftp8 [new Application/FTP]
$ftp8 attach-agent $tcp8
#$ftp8 set rate_ 1.0Mb
#$ftp8 set random_ null
#$ftp8  set interval_ 0.4
$ns  at 96.2 "$ftp8 start"
$ns  at 100.0 "$ftp8 stop"
 
set tcp9 [new Agent/TCP]
$tcp9 set class_ 2
set sink9 [new Agent/TCPSink]
$ns  attach-agent $n6 $tcp9
$ns attach-agent $n15 $sink9
$ns  connect $tcp9 $sink9
set ftp9 [new Application/FTP]
$ftp9 attach-agent $tcp9
#$ftp9 set rate_ 1.0Mb
#$ftp9 set random_ null
#$ftp9  set interval_ 0.4
$ns  at 100.2 "$ftp9 start"
$ns  at 104.0 "$ftp9 stop"
 
set tcp10 [new Agent/TCP]
$tcp10 set class_ 2
set sink10 [new Agent/TCPSink]
$ns  attach-agent $n11 $tcp10
$ns attach-agent $n10 $sink10
$ns  connect $tcp10 $sink10
set ftp10 [new Application/FTP]
$ftp10 attach-agent $tcp10
#$ftp10 set rate_ 1.0Mb
#$ftp10 set random_ null
#$ftp10  set interval_ 0.4
$ns  at 104.2 "$ftp10 start"
$ns  at 108.0 "$ftp10 stop"
 
set tcp11 [new Agent/TCP]
$tcp11 set class_ 2
set sink11 [new Agent/TCPSink]
$ns  attach-agent $n12 $tcp11
$ns attach-agent $n10 $sink11
$ns  connect $tcp11 $sink11
set ftp11 [new Application/FTP]
$ftp11 attach-agent $tcp11
#$ftp11 set rate_ 1.0Mb
#$ftp11 set random_ null
#$ftp11  set interval_ 0.4
$ns  at 108.2 "$ftp11 start"
$ns  at 112.0 "$ftp11 stop"
 
set tcp12 [new Agent/TCP]
$tcp12 set class_ 2
set sink12 [new Agent/TCPSink]
$ns  attach-agent $n13 $tcp12
$ns attach-agent $n10 $sink12
$ns  connect $tcp12 $sink12
set ftp12 [new Application/FTP]
$ftp12 attach-agent $tcp12
#$ftp12 set rate_ 1.0Mb
#$ftp12 set random_ null
#$ftp12  set interval_ 0.4
$ns  at 112.2 "$ftp12 start"
$ns  at 116.0 "$ftp12 stop"
 
set tcp13 [new Agent/TCP]
$tcp13 set class_ 2
set sink13 [new Agent/TCPSink]
$ns  attach-agent $n14 $tcp13
$ns attach-agent $n10 $sink13
$ns  connect $tcp13 $sink13
set ftp13 [new Application/FTP]
$ftp13 attach-agent $tcp13
#$ftp13 set rate_ 1.0Mb
#$ftp13 set random_ null
#$ftp13  set interval_ 0.4
$ns  at 116.2 "$ftp13 start"
$ns  at 120.0 "$ftp13 stop"
 
 
set tcp14 [new Agent/TCP]
$tcp14 set class_ 2
set sink14 [new Agent/TCPSink]
$ns  attach-agent $n10 $tcp14
$ns attach-agent $n15 $sink14
$ns  connect $tcp14 $sink14
set ftp14 [new Application/FTP]
$ftp14 attach-agent $tcp14
#$ftp14 set rate_ 1.0Mb
#$ftp14 set random_ null
#$ftp14  set interval_ 0.4
$ns  at 120.2 "$ftp14 start"
$ns  at 124.0 "$ftp14 stop"
 
set tcp15 [new Agent/TCP]
$tcp15 set class_ 2
set sink15 [new Agent/TCPSink]
$ns  attach-agent $n7 $tcp15
$ns attach-agent $n6 $sink15
$ns  connect $tcp15 $sink15
set ftp15 [new Application/FTP]
$ftp15 attach-agent $tcp15
#$ftp15 set rate_ 1.0Mb
#$ftp15 set random_ null
#$ftp15  set interval_ 0.4
$ns  at 39.2 "$ftp15 start"
$ns  at 41.0 "$ftp15 stop"
 
 
 
set tcp16 [new Agent/TCP]
$tcp16 set class_ 2
set sink16 [new Agent/TCPSink]
$ns  attach-agent $n8 $tcp16
$ns attach-agent $n6 $sink16
$ns  connect $tcp16 $sink16
set ftp16 [new Application/FTP]
$ftp16 attach-agent $tcp16
#$ftp16 set rate_ 1.0Mb
#$ftp16 set random_ null
#$ftp16  set interval_ 0.4
$ns  at 41.2 "$ftp16 start"
$ns  at 43.0 "$ftp16 stop"
 
set tcp17 [new Agent/TCP]
$tcp17 set class_ 2
set sink17 [new Agent/TCPSink]
$ns  attach-agent $n9 $tcp17
$ns attach-agent $n6 $sink17
$ns  connect $tcp17 $sink17
set ftp17 [new Application/FTP]
$ftp17 attach-agent $tcp17
#$ftp17 set rate_ 1.0Mb
#$ftp17 set random_ null
#$ftp17  set interval_ 0.4
$ns  at 43.2 "$ftp17 start"
$ns  at 45.0 "$ftp17 stop"
 
set tcp18 [new Agent/TCP]
$tcp18 set class_ 2
set sink18 [new Agent/TCPSink]
$ns  attach-agent $n5 $tcp18
$ns attach-agent $n8 $sink18
$ns  connect $tcp18 $sink18
set ftp18 [new Application/FTP]
$ftp18 attach-agent $tcp18
#$ftp18 set rate_ 1.0Mb
#$ftp18 set random_ null
#$ftp18  set interval_ 0.4
$ns  at 45.2 "$ftp18 start"
$ns  at 47.0 "$ftp18 stop"
 
 
set tcp19 [new Agent/TCP]
$tcp19 set class_ 2
set sink19 [new Agent/TCPSink]
$ns  attach-agent $n11 $tcp19
$ns attach-agent $n10 $sink19
$ns  connect $tcp19 $sink19
set ftp19 [new Application/FTP]
$ftp19 attach-agent $tcp19
#$ftp19 set rate_ 1.0Mb
#$ftp19 set random_ null
#$ftp19  set interval_ 0.4
$ns  at 47.2 "$ftp19 start"
$ns  at 49.0 "$ftp19 stop"
 
set tcp20 [new Agent/TCP]
$tcp20 set class_ 2
set sink20 [new Agent/TCPSink]
$ns  attach-agent $n12 $tcp20
$ns attach-agent $n10 $sink20
$ns  connect $tcp20 $sink20
set ftp20 [new Application/FTP]
$ftp20 attach-agent $tcp20
#$ftp20 set rate_ 1.0Mb
#$ftp20 set random_ null
#$ftp20  set interval_ 0.4
$ns  at 50.2 "$ftp20 start"
$ns  at 52.0 "$ftp20 stop"
 
set tcp21 [new Agent/TCP]
$tcp21 set class_ 2
set sink21 [new Agent/TCPSink]
$ns  attach-agent $n13 $tcp21
$ns attach-agent $n10 $sink21
$ns  connect $tcp21 $sink21
set ftp21 [new Application/FTP]
$ftp21 attach-agent $tcp21
#$ftp21 set rate_ 1.0Mb
#$ftp21 set random_ null
#$ftp21  set interval_ 0.4
$ns  at 52.2 "$ftp21 start"
$ns  at 54.0 "$ftp21 stop"
 
set tcp22 [new Agent/TCP]
$tcp22 set class_ 2
set sink22 [new Agent/TCPSink]
$ns  attach-agent $n14 $tcp22
$ns attach-agent $n11 $sink22
$ns  connect $tcp22 $sink22
set ftp22 [new Application/FTP]
$ftp22 attach-agent $tcp22
#$ftp22 set rate_ 1.0Mb
#$ftp22 set random_ null
#$ftp22  set interval_ 0.4
$ns  at 54.2 "$ftp22 start"
$ns  at 56.0 "$ftp22 stop"
 
set tcp23 [new Agent/TCP]
$tcp23 set class_ 2
set sink23 [new Agent/TCPSink]
$ns  attach-agent $n4 $tcp23
$ns attach-agent $n1 $sink23
$ns  connect $tcp23 $sink23
set ftp23 [new Application/FTP]
$ftp23 attach-agent $tcp23
#$ftp23 set rate_ 1.0Mb
#$ftp23 set random_ null
#$ftp23  set interval_ 0.4
$ns  at 56.2 "$ftp23 start"
$ns  at 58.0 "$ftp23 stop"
 
set tcp24 [new Agent/TCP]
$tcp24 set class_ 2
set sink24 [new Agent/TCPSink]
$ns  attach-agent $n0 $tcp24
$ns attach-agent $n1 $sink24
$ns  connect $tcp24 $sink24
set ftp24 [new Application/FTP]
$ftp24 attach-agent $tcp24
#$ftp24 set rate_ 1.0Mb
#$ftp24 set random_ null
#$ftp24  set interval_ 0.4
$ns  at 59.2 "$ftp24 start"
$ns  at 60.0 "$ftp24 stop"
 
set tcp25 [new Agent/TCP]
$tcp25 set class_ 2
set sink25 [new Agent/TCPSink]
$ns  attach-agent $n2 $tcp25
$ns attach-agent $n1 $sink25
$ns  connect $tcp25 $sink25
set ftp25 [new Application/FTP]
$ftp25 attach-agent $tcp25
#$ftp24 set rate_ 1.0Mb
#$ftp24 set random_ null
#$ftp24  set interval_ 0.4
$ns  at 60.2 "$ftp25 start"
$ns  at 62.0 "$ftp25 stop"
 
set tcp26 [new Agent/TCP]
$tcp26 set class_ 2
set sink26 [new Agent/TCPSink]
$ns  attach-agent $n1 $tcp26
$ns attach-agent $n0 $sink26
$ns  connect $tcp26 $sink26
set ftp26 [new Application/FTP]
$ftp26 attach-agent $tcp26
#$ftp24 set rate_ 1.0Mb
#$ftp24 set random_ null
#$ftp24  set interval_ 0.4
$ns  at 62.2 "$ftp26 start"
$ns  at 64.0 "$ftp26 stop"
 

 
#Define a 'finish' procedure
proc finish {
 
} {
 
    global ns tracefile namfile
    $ns flush-trace
    close $tracefile
    close $namfile
    exec nam out1.nam &
    exit 0
 
}
for {
set i 0
} {
$i < $val(nn)
} {
 incr i
} {
 
    $ns at $val(stop) "\$n$i reset"
 
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

Friday, 13 May 2016

Leach algorithm

The algorithm for the Low Energy Adaptive Clustering Hierarchy (LEACH) implemented is:

Setup phase :

1. CN=> r
2. If r > T(n) then, CH = CN else, goto step1
3. CH => G : id(CH) , join adv
4. A(i) -> CH(j) : id(A(i)) , id(CH(j)) , join req
5. CH(j)-> A(i) : id(CH(j)) , < t(i) , id(A(i)) >

Steady phase :

1. A(i) -> CH(j) : id(A(i)) , id(CH(j)) , info
2. CH -> BS : id(CH) , id(BS) , aggr info

The various symbols used here are :
CN : candidate node to become the cluster head.
r : randomvariable(0 > r > 1)
T(n) : threshold value
CH : cluster head
G : all nodes in the network
id : identi_cation number
join adv : advertisement to join the cluster
A : normal node
Join adv : request to join the cluster
t : time-slot to send the sensed data
=> : broadcast
->: unicast


Leach protocol assumption

(1) It assumes that nodes always have data to send & the nodes including CH are started with the
same initial energy.

(2) No. of CHs are predefined i.e. 5% or 10% of total nodes. It might not be sufficient to cover
entire area when sensor nodes are not uniformly distributed.

(3) The CHs are randomly selected rotationally and Residual Energy of the node is not considered
for cluster formation.

(4) CHs in the network are not uniformly distributed, so sometimes elected CHs will be
concentrated in one part of n/w, hence some nodes in the n/w will not have any CH in their
vicinity, so it not provides proper location of CH.

(5) CHs send aggregated data to BS in single hop manner so LEACH is not applicable to networks
deployed in large regions.

(6) It consist of rounds while in each round, all sensor nodes take part in reconstructing new
clusters and this action consumes a lot of energy.

Leach protocol Description

W.Heinzelman, introduced a hierarchical clustering algorithm for sensor networks, called Low Energy Adaptive Clustering Hierarchy (LEACH). LEACH arranges the nodes in the network into small clusters and chooses one of them as the cluster-head. Node first senses its target and then sends the relevant information to its cluster-head. Then the cluster head aggregates and compresses the information received from all the nodes and sends it to the base station. The nodes chosen as the cluster head drain out more energy as compared to the other nodes as it is required to send data to the base station which may be far located. Hence LEACH uses random rotation of the nodes required to be the cluster-heads to evenly distribute energy consumption in the network. After a number of simulations by the author, it was found that only 5 percent of the total number of nodes needs to act as the cluster-heads. TDMA/CDMA MAC is used to reduce inter-cluster and intra-cluster collisions. This protocol is used were a constant monitoring by the sensor nodes are required as data collection is centralized (at the base station) and is performed periodically.
                                      

 OPERATION

LEACH operations can be divided into two phases:-
1. Setup phase
2. Steady phase
In the setup phase, the clusters are formed and a cluster-head is chosen for each cluster. While in the steady phase, data is sensed and sent to the central base station. The steady phase is longer than the setup phase. This is done in order to minimize the overhead cost.

2.2.1. Setup phase :-

 During the setup phase, a predetermined fraction of nodes, p, choose themselves as cluster-heads. This is done according to a threshold value, T(n). The threshold value depends upon the desired percentage to become a cluster-head- p, the current round r, and the set of nodes that have not become the cluster-head in the last 1/p rounds, which is denoted by G. The formulae is as follows :
T(n) = p/1-p[r mod(1/p)] if n E G
T(n) = 0 otherwise
Every node wanting to be the cluster-head chooses a value, between 0 and 1. If this random number is less than the threshold value, T(n), then the node becomes the cluster-head for the current round. Then each elected CH broadcasts an advertisement message to the rest of the nodes in the network to invite them to join their clusters. Based upon the strength of the advertisement signal, the non-cluster head nodes decide to join the clusters. The non-cluster head nodes then informs their respective cluster heads that they will be under their cluster by sending an acknowledgement message. After receiving the acknowledgement message, depending upon the number of nodes under their cluster and the type of information required by the system (in which the WSN is setup), the cluster heads creates a TDMA schedule and assigns each node a time slot in which it can transmit the sensed data. The TDMA schedule is broadcasted  to all the cluster-members. If the size of any cluster becomes too large, the cluster head may choose another cluster- head for its cluster. The cluster-head chosen for the current round cannot again become the cluster-head until all the other nodes in the network haven't become the cluster-head.

Steady phase :-

During the steady phase, the sensor nodes i.e. the non-cluster head nodes starts sensing data and sends it to their cluster-head according to the TDMA schedule. The cluster-head node, after receiving data from all the member nodes, aggregates it and then sends it to the base-station.
After a certain time, which is determined a priori, the network again goes back into the setup phase and new cluster-heads are chosen. Each cluster communicates using different CDMA codes in order to reduce interference from nodes belonging to other clusters.



Thursday, 12 May 2016

wireless communication using NS2

   
# 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)         8 ;# number of mobilenodes
    set val(rp)        AODV ;# routing protocol
    set val(x)        500 ;# X dimension of topography
    set val(y)        400 ;# Y dimension of topography
    set val(stop)      100 ;# time of simulation end


set ns              [new Simulator]
#creating trace file and nam file
set tracefd       [open wireless1.tr w]
set windowVsTime2 [open win.tr w]
set namtrace      [open wirelessf.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 node_($i) [$ns node]    
      
}

# Provide initial location of mobilenodes
$node_(0) set X_ 5.0
$node_(0) set Y_ 5.0
$node_(0) set Z_ 0.0

$node_(1) set X_ 290.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

$node_(3) set X_ 150.0
$node_(3) set Y_ 350.0
$node_(3) set Z_ 0.0

$node_(4) set X_ 10.0
$node_(4) set Y_ 140.0
$node_(4) set Z_ 0.0

$node_(5) set X_ 250.0
$node_(5) set Y_ 140.0
$node_(5) set Z_ 0.0

$node_(6) set X_ 100.0
$node_(6) set Y_ 100.0
$node_(6) set Z_ 0.0

$node_(7) set X_ 70.0
$node_(7) set Y_ 180.0
$node_(7) set Z_ 0.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"


# Set a TCP connection between node_(1) and node_(4)
set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $node_(1) $tcp
$ns attach-agent $node_(4) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"

#defining heads
$ns at 0.0 "$node_(1) label Sender"
$ns at 0.0 "$node_(4) label Receiver"

# Generation of movements
$ns at 20.0 "$node_(2) setdest 400.0 20.0 5.0"


#set tcp [new Agent/TCP/Newreno]
#$tcp set class_ 2
#set sink [new Agent/TCPSink]
#$ns attach-agent $node_(1) $tcp
#$ns attach-agent $node_(2) $sink
#$ns connect $tcp $sink
#set ftp [new Application/FTP]
#$ftp attach-agent $tcp
#$ns at 10.0 "$ftp start"

# Printing the window size
proc plotWindow {
tcpSource file
} {

global ns
set time 0.01
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file"
}
$ns at 10.0 "plotWindow $tcp $windowVsTime2"

# Define node initial position in nam
for {
set i 0
} {
$i < $val(nn)
} {
 incr i
} {

# 30 defines the node size for nam
$ns initial_node_pos $node_($i) 30

}

# Telling nodes when the simulation ends
for {
set i 0
} {
$i < $val(nn)
} {
 incr i
} {

    $ns at $val(stop) "$node_($i) reset";

}

# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 100.01 "puts \"end simulation\" ; $ns halt"
proc stop {

} {

    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
exec nam wirelessf.nam &

#exec xgraph wireless1.tr &
exit 0

}

$ns run

simulating broadcasting in NS2.


  # Create scheduler
  #Create an event scheduler wit multicast turned o
  set ns [new Simulator -multicast on]
  #$ns multicast
  #Turn on Tracing
  set tf [open output.tr w]
  $ns trace-all $tf
# Turn on nam Tracing
set fd [open mcast.nam w]
$ns namtrace-all $fd
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

# Create links
$ns duplex-link $n0  $n2 1.5Mb 10ms DropTail
$ns duplex-link $n0  $n1 1.5Mb 10ms DropTail
$ns duplex-link $n0  $n3 1.5Mb 10ms DropTail
$ns duplex-link $n0  $n4 1.5Mb 10ms DropTail

# Routing protocol: say distance vector
#Protocols: CtrMcast, DM, ST, BST
set mproto DM
set mrthandle [$ns mrtproto $mproto {

}]
# Allocate group addresses
set group1 [Node allocaddr]

# UDP Transport agent for the traffic source
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
$udp0 set dst_addr_ $group1
$udp0 set dst_port_ 0
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp0


# Create receiver
set rcvr1 [new Agent/Null]
$ns attach-agent $n1 $rcvr1
$ns at 0.1 "$n1 join-group $rcvr1 $group1"
set rcvr2 [new Agent/Null]
$ns attach-agent $n2 $rcvr2
$ns at 0.1 "$n2 join-group $rcvr2 $group1"
set rcvr3 [new Agent/Null]
$ns attach-agent $n3 $rcvr3
$ns at 0.1 "$n3 join-group $rcvr3 $group1"

set rcvr4 [new Agent/Null]
$ns attach-agent $n4 $rcvr4
$ns at 0.1 "$n4 join-group $rcvr4 $group1"


#$ns at 4.0 "$n1 leave-group $rcvr1 $group1"
#$ns at 4.5 "$n2 leave-group $rcvr2 $group1"
#$ns at 5.0 "$n3 leave-group $rcvr3 $group1"

# Schedule events
$ns at 0.5 "$cbr1 start"
$ns at 9.5 "$cbr1 stop"
#post-processing
$ns at 10.0 "finish"
proc finish {

} {

   global ns tf
   $ns flush-trace
   close $tf
   exec nam mcast.nam &
   exit 0

}
# For nam
#Colors for packets from two mcast groups
$ns color 10 red
$ns color 11 green
$ns color 30 purple
$ns color 31 green
# Manual layout: order of the link is significant
#$ns duplex-link-op $n0 $n1 orient right
#$ns duplex-link-op $n0 $n2 orient right-up
#$ns duplex-link-op $n0 $n3 orient right-down
# Show queue on simplex link n0->n1
#$ns duplex-link-op $n2 $n3 queuePos 0.5
# Group 0 source
$udp0 set fid_ 10
$n0 color red
$n0 label "Source 1"
# Group 1 source

$n1 label "Receiver 1"
$n1 color blue
$n2 label "Receiver 2"
 $n2 color blue

 $n3 label "Receiver 3"
 $n3 color blue
 $n4 label "Receiver 4"
 $n4 color blue
 #$n2 add-mark m0 red
 #$n2 delete-mark m0"
 # Animation rate
 $ns set-animation-rate 3.0ms
 $ns run

simulate mobile nodes that move between different base stations.


# Define options
# ======================================================================

set opt(chan)       Channel/WirelessChannel         ;# channel type
set opt(prop)       Propagation/TwoRayGround        ;# radio-propagation
set opt(netif)      Phy/WirelessPhy                 ;# network interface
set opt(mac)        Mac/802_11                      ;# MAC typ
set opt(ifq)        Queue/DropTail/PriQueue         ;# interface queue type
set opt(ll)         LL                              ;# link layer type
set opt(ant)        Antenna/OmniAntenna             ;# antenna model
set opt(ifqlen)         1000                          ;# max packet in ifq
set opt(nn)             4                              ;# number of mobilenodes
set opt(adhocRouting)   DSDV                           ;# routing protocol
set opt(threshold)     1.41828e-9            ;# the distance of coverage 75m
set opt(x)          100                              ;# x coordinate of topology
set opt(y)          100                              ;# y coordinate of topology
set opt(stop)       10                                ;# time to stop simulation
set num_wired_nodes     4
set num_bs_nodes        2                  ;# this is not really used here.
set size        500
# ======================================================================

set basic         1.0e6
set data        2.0e6


# create simulator instance

set ns_   [new Simulator]

# set up for hierarchical routing
$ns_ node-config -addressType hierarchical

AddrParams set domain_num_ 3           ;# number of domains
lappend cluster_num 1 1 1             ;# number of clusters in each domain
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 7 3 3        ;# number of nodes in each cluster
AddrParams set nodes_num_ $eilastlevel ;# of each domain

set tracefd  [open out.tr w]
set namtrace [open out.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)

set f1 [open s1 a]
set f2 [open s2 a]
set f3 [open s3 a]
set f4 [open s4 a]


# Create topography object
set topo   [new Topography]

# define topology
$topo load_flatgrid $opt(x) $opt(y)

# create God
create-god $opt(nn)

#create wired nodes

set W1 [$ns_ node 0.0.0]
set W2 [$ns_ node 0.0.1]
set W3 [$ns_ node 0.0.2]
set W4 [$ns_ node 0.0.3]
set W5 [$ns_ node 0.0.4]
#set W6 [$ns_ node 0.0.5]
#set W7 [$ns_ node 0.0.6]
set W8 [$ns_ node 0.0.5]
set W9 [$ns_ node 0.0.6]

# Configure for Basestation Node
$ns_ node-config -adhocRouting $opt(adhocRouting) \
                 -llType $opt(ll) \
                 -macType $opt(mac) \
                 -ifqType $opt(ifq) \
                 -ifqLen $opt(ifqlen) \
                 -antType $opt(ant) \
                 -propType $opt(prop) \
                 -phyType $opt(netif) \
                 -channelType $opt(chan) \
         -topoInstance $topo \
                 -wiredRouting ON \
         -agentTrace ON \
                 -routerTrace ON \
                 -macTrace OFF

Phy/WirelessPhy set RXThresh_ $opt(threshold)

# Position (fixed) for base-station nodes (HA & FA).

set BS1 [$ns_ node 1.0.0]
set BS2 [$ns_ node 2.0.0]



# create a mobilenode that would be moving between HA and FA.
# note address of MH indicates its in the same domain as HA.
$ns_ node-config -wiredRouting OFF

set R1 [$ns_ node 1.0.2]
set R2 [$ns_ node 1.0.3]
set R3 [$ns_ node 2.0.2]
set R4 [$ns_ node 2.0.3]


$R1 base-station [AddrParams addr2id [$BS1 node-addr]]
$R2 base-station [AddrParams addr2id [$BS1 node-addr]]
$R3 base-station [AddrParams addr2id [$BS2 node-addr]]
$R4 base-station [AddrParams addr2id [$BS2 node-addr]]

# position of the nodes
$R1 set X_ 120.000000000000
$R1 set Y_ 80.000000000000
$R1 set Z_ 0.000000000000

$R2 set X_ 160.000000000000
$R2 set Y_ 40.000000000000
$R2 set Z_ 0.000000000000

$R3 set X_ 160.000000000000
$R3 set Y_ 0.000000000000
$R3 set Z_ 0.000000000000

$R4 set X_ 160.000000000000
$R4 set Y_ -40.000000000000
$R4 set Z_ 0.000000000000

$ns_ at 2.0 "$R1 setdest 90.0 20.0 20.0"
$ns_ at 2.0 "$R3 setdest 90.0 82.0 20.0"

# create links between wired and BaseStation nodes
$ns_ duplex-link $W1 $W3 2Mb 20ms DropTail
$ns_ duplex-link $W2 $W4 2Mb 20ms DropTail
$ns_ duplex-link $W8 $W3 2Mb 20ms DropTail
$ns_ duplex-link $W9 $W4 2Mb 20ms DropTail

$ns_ duplex-link $W3 $W5 5Mb 20ms DropTail
$ns_ duplex-link $W4 $W5 5Mb 20ms DropTail
#$ns_ duplex-link $W5 $W6 10Mb 20ms DropTail
#$ns_ duplex-link $W5 $W7 10Mb 20ms DropTail
$ns_ duplex-link $W5 $BS1 5Mb 20ms DropTail
$ns_ duplex-link $W5 $BS2 5Mb 20ms DropTail

# set the layout of links in NAM
$ns_ duplex-link-op $W1 $W3 orient right
$ns_ duplex-link-op $W8 $W3 orient right-down

$ns_ duplex-link-op $W2 $W4 orient right-up
$ns_ duplex-link-op $W9 $W4 orient right

$ns_ duplex-link-op $W3 $W5 orient right-down
$ns_ duplex-link-op $W4 $W5 orient right-up

#$ns_ duplex-link-op $W5 $W6 orient right-up
#$ns_ duplex-link-op $W5 $W7 orient right-down
$ns_ duplex-link-op $W5 $BS1 orient right-up
$ns_ duplex-link-op $W5 $BS2 orient right-down



$ns_ at 0.0 "$W1 label W1"
$ns_ at 0.0 "$W2 label W2"
$ns_ at 0.0 "$W8 label W3"
$ns_ at 0.0 "$W9 label W4"
$ns_ at 0.0 "$W3 label R1"
$ns_ at 0.0 "$W4 label R2"
$ns_ at 0.0 "$W5 label R3"
#$ns_ at 0.0 "$W6 label R4"
#$ns_ at 0.0 "$W7 label R5"
$ns_ at 0.0 "$BS1 label BS1"
$ns_ at 0.0 "$BS2 label BS2"
$ns_ at 0.0 "$R1 label R1"
$ns_ at 0.0 "$R2 label R2"
$ns_ at 0.0 "$R3 label R3"
$ns_ at 0.0 "$R4 label R4"
$ns_ at 0.0 "$R1 add-mark m1 green circle"
$ns_ at 0.0 "$R2 add-mark m1 red circle"
$ns_ at 0.0 "$R3 add-mark m1 blue circle"
$ns_ at 0.0 "$R4 add-mark m1 purple circle"
# setup TCP connections

  set tcp1 [new Agent/TCP/Newreno]
  $tcp1 set packetSize_  $size
  $ns_ attach-agent $W1  $tcp1
  set sink1 [new Agent/TCPSink]
  $ns_ attach-agent $R1  $sink1
  $ns_ connect $tcp1 $sink1
  set ftp1 [new Application/FTP]
  $ftp1 attach-agent $tcp1
  $ns_ at 1.0 "$ftp1 start"
  $ns_ at 1.0 "$ns_ trace-annotate \"W1 Sends packets to R1 via Home
Agent(BS1). \""

  set tcp2 [new Agent/TCP/Newreno]
  $tcp2 set packetSize_  $size
  $ns_ attach-agent $W8  $tcp2
  set sink2 [new Agent/TCPSink]
  $ns_ attach-agent $R2  $sink2
  $ns_ connect $tcp2 $sink2
  set ftp2 [new Application/FTP]
  $ftp2 attach-agent $tcp2
  $ns_ at 2.0 "$ftp2 start"
  $ns_ at 2.0 "$ns_ trace-annotate \"W3 Sends packets to R2 via Home
Agent(BS1). \""

  set tcp3 [new Agent/TCP/Newreno]
  $tcp3 set packetSize_  $size
  $ns_ attach-agent $W2  $tcp3
  set sink3 [new Agent/TCPSink]
  $ns_ attach-agent $R3  $sink3
  $ns_ connect $tcp3 $sink3
  set ftp3 [new Application/FTP]
  $ftp3 attach-agent $tcp3
  $ns_ at 3.0 "$ftp3 start"
  $ns_ at 3.0 "$ns_ trace-annotate \"W2 Sends packets to R3 via Home
Agent(BS2). \""

  set tcp4 [new Agent/TCP/Newreno]
  $tcp4 set packetSize_  $size
  $ns_ attach-agent $W9  $tcp4
  set sink4 [new Agent/TCPSink]
  $ns_ attach-agent $R4  $sink4
  $ns_ connect $tcp4 $sink4
  set ftp4 [new Application/FTP]
  $ftp4 attach-agent $tcp4
  $ns_ at 4.0 "$ftp4 start"
  $ns_ at 4.0 "$ns_ trace-annotate \"W4 Sends packets to R4 via Home
Agent(BS2). \""


# Define initial node position in nam

$ns_ initial_node_pos $R1 10
$ns_ initial_node_pos $R2 10
$ns_ initial_node_pos $R3 10
$ns_ initial_node_pos $R4 10


# Tell all nodes when the siulation ends
$ns_ at $opt(stop).0 "$R1 reset";
$ns_ at $opt(stop).0 "$R2 reset";
$ns_ at $opt(stop).0 "$R3 reset";
$ns_ at $opt(stop).0 "$R4 reset";

$ns_ at $opt(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"
$ns_ at $opt(stop).0001 "stop"

proc stop {

} {

    global ns_ tracefd namtrace opt

global ns_  f1 f2 f3 f4 tcp1 tcp2 tcp3 tcp4 mbw1 mbw2 mbw3 mbw4 rate size
global sink
    #received bytes
        set bw1  [$tcp1 set ndatabytes_]
    set bw2  [$tcp2 set ndatabytes_]
    set bw3  [$tcp3 set ndatabytes_]
    set bw4  [$tcp4 set ndatabytes_]

    #set current time
    set now [$ns_ now]
    set time $now

    # convert bytes into Mb/s
    set mbw1 [expr $bw1/$time*8/1000000]
    set mbw2 [expr $bw2/$time*8/1000000]
    set mbw3 [expr $bw3/$time*8/1000000]
    set mbw4 [expr $bw4/$time*8/1000000]

    set tot [expr $mbw1 + $mbw2 + $mbw3 + $mbw4 ]
        puts $tot
    puts "NS Exiting ..."
    puts $f1 "$mbw1"
    puts $f2 "$mbw2"
    puts $f3 "$mbw3"
    puts $f4 "$mbw4"


    close $tracefd
    close $namtrace
    #exec  nam out.nam &
    exit 0

}

puts "Starting Simulation..."
$ns_ run