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

olsr protocol source code

set opt(chan)           Channel/WirelessChannel  ;# channel type
set opt(prop)           Propagation/TwoRayGround   ;# radio-propagation model
set opt(prop)           Propagation/Shadowing   ;# radio-propagation model
set opt(netif)          Phy/WirelessPhy          ;# network interface type
set opt(mac)            Mac/802_11               ;# MAC type
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)         50                       ;# max packet in ifq
set opt(nn)             11                       ;# number of mobilenodes
set opt(adhocRouting)   OLSR                 ;# routing protocol

set opt(cp)             ""                       ;# connection pattern file
set opt(sc)             ""                       ;# node movement file.

set opt(x)              1000                     ;# x coordinate of topology
set opt(y)              1000                     ;# y coordinate of topology
set opt(seed) X
set opt(stop)           50                       ;# time to stop simulation

set opt(cbr-start)      5.0
set opt(cbr-stop)       45.0
set opt(pa-start)       7.0
set opt(pa-stop)        37.0
set opt(pa1-start)      9.0
set opt(pa1-stop)       39.0
# ============================================================================

#
# check for random seed
#
if {$opt(seed) > 0} {
    puts "Seeding Random number generator with $opt(seed)\n"
    ns-random $opt(seed)
}

#Ganho das antenas
Antenna/OmniAntenna set Gt_ 18.0
Antenna/OmniAntenna set Gr_ 18.0

Phy/WirelessPhy set bandwidth_ 11Mb

# frequencia (2.4 GHz 802.11b) {Alcance = 276 metros}
Phy/WirelessPhy set freq_ 2.4e+9

Mac/802_11 set dataRate_ 11Mb
Mac/802_11 set basicRate_ 2Mb

Propagation/Shadowing set pathlossExp_ 2.7       ;#expoente de perdas
Propagation/Shadowing set std_db_ 4.0           ;#desvio padrao (dB)
#Propagation/TwoRayGround set L_ 1.0

#
# create simulator instance
#
set ns_ [new Simulator]

#
# control OLSR behaviour from this script -
# commented lines are not needed because
# those are default values
#
Agent/OLSR set use_mac_              true
Agent/OLSR set debug_                true
Agent/OLSR set willingness           3
Agent/OLSR set hello_ival_           2
Agent/OLSR set tc_ival_              5
Agent/OLSR set mpr_algorithm_        1
Agent/OLSR set routing_algorithm_    1
Agent/OLSR set link_quality_         1
Agent/OLSR set fish_eye_             false
Agent/OLSR set link_delay_           false
Agent/OLSR set tc_redundancy_        1
Agent/OLSR set c_alpha_              0.6

#
# open traces
#
$ns_ use-newtrace
set tracefd  [open wtrace.tr w]
set namtrace [open simulation.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)

#
# create topography object
#
set topo [new Topography]

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

#
# create God
#
create-god $opt(nn)

#
# configure mobile nodes
#
$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 OFF \
                 -agentTrace ON \
                 -routerTrace ON \
                 -macTrace OFF

for {set i 1} {$i < $opt(nn)} {incr i} {
    set node_($i) [$ns_ node]
}

#
# positions

$node_(1) set X_ 160.0  #CAPACIT
$node_(1) set Y_ 485.0
$node_(1) set Z_ 15.0

$node_(2) set X_ 305.0  #DI
$node_(2) set Y_ 277.0
$node_(2) set Z_ 15.0

$node_(3) set X_ 340.0   #SECOM
$node_(3) set Y_ 226.0
$node_(3) set Z_ 15.0

$node_(4) set X_ 270.0  #Grad Basico
$node_(4) set Y_ 32.0
$node_(4) set Z_ 15.0

$node_(5) set X_ 476.0  #Reitoria
$node_(5) set Y_ 200.0
$node_(5) set Z_ 15.0

$node_(6) set X_ 628.0  #Incubadora
$node_(6) set Y_ 320.0
$node_(6) set Z_ 15.0

$node_(7) set X_ 570.0  #Musica
$node_(7) set Y_ 440.0
$node_(7) set Z_ 15.0

$node_(8) set X_ 780.0  #LABS
$node_(8) set Y_ 480.0
$node_(8) set Z_ 15.0

$node_(9) set X_ 918.0  #CT
$node_(9) set Y_ 597.0
$node_(9) set Z_ 15.0

$node_(10) set X_ 968.0  #Grad Profissional
$node_(10) set Y_ 550.0
$node_(10) set Z_ 15.0

# cores
$ns_ color 1 red
$ns_ color 2 blue
$ns_ color 3 yellow

# setup UDP connection
# CAPACIT -> GRAD PROFISSIONAL
set udp [new Agent/UDP]
$udp set class_ 1
set null [new Agent/Null]
$ns_ attach-agent $node_(1) $udp
$ns_ attach-agent $node_(10) $null
$ns_ connect $udp $null
$udp set fid_ 1

set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 40     # RTP + UDP + Payload
$cbr set rate_ 8Kb
$cbr attach-agent $udp
$ns_ at 5.0 "$cbr start"
$ns_ at 45.0  "$cbr stop"

#GRAD PROFISSIONAL -> CAPACIT
set udp1 [new Agent/UDP]
$udp1 set class_ 2
set null1 [new Agent/Null]
$ns_ attach-agent $node_(10) $udp1
$ns_ attach-agent $node_(1) $null1
$ns_ connect $udp1 $null1
$udp1 set fid_ 2

set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 40     # RTP + UDP + Payload
$cbr1 set rate_ 8Kb
$cbr1 attach-agent $udp1
$ns_ at 5.0 "$cbr1 start"
$ns_ at 45.0  "$cbr1 stop"

#REITORIA -> CAPACIT
set udp2 [new Agent/UDP]
$udp2 set class_ 3
set null2 [new Agent/Null]
$ns_ attach-agent $node_(5) $udp2
$ns_ attach-agent $node_(1) $null2
$ns_ connect $udp2 $null2
$udp2 set fid_ 3

set cbr2 [new Application/Traffic/CBR]
$cbr2 set packetSize_ 40     # RTP + UDP + Payload
$cbr2 set rate_ 8Kb
$cbr2 attach-agent $udp2
$ns_ at 7.0 "$cbr2 start"
$ns_ at 45.0  "$cbr2 stop"

#CAPACIT -> REITORIA
set udp3 [new Agent/UDP]
$udp3 set class_ 4
set null3 [new Agent/Null]
$ns_ attach-agent $node_(1) $udp3
$ns_ attach-agent $node_(5) $null3
$ns_ connect $udp3 $null3
$udp3 set fid_ 4

set cbr3 [new Application/Traffic/CBR]
$cbr3 set packetSize_ 40     # RTP + UDP + Payload
$cbr3 set rate_ 8Kb
$cbr3 attach-agent $udp3
$ns_ at 7.0 "$cbr3 start"
$ns_ at 45.0  "$cbr3 stop"

#REITORIA -> CT
set udp4 [new Agent/UDP]
$udp4 set class_ 5
set null4 [new Agent/Null]
$ns_ attach-agent $node_(5) $udp4
$ns_ attach-agent $node_(9) $null4
$ns_ connect $udp4 $null4
$udp4 set fid_ 5

set cbr4 [new Application/Traffic/CBR]
$cbr4 set packetSize_ 40     # RTP + UDP + Payload
$cbr4 set rate_ 8Kb
$cbr4 attach-agent $udp4
$ns_ at 9.0 "$cbr4 start"
$ns_ at 45.0  "$cbr4 stop"

#CT -> REITORIA
set udp5 [new Agent/UDP]
$udp5 set class_ 6
set null5 [new Agent/Null]
$ns_ attach-agent $node_(9) $udp5
$ns_ attach-agent $node_(5) $null5
$ns_ connect $udp5 $null5
$udp5 set fid_ 6

set cbr5 [new Application/Traffic/CBR]
$cbr5 set packetSize_ 40     # RTP + UDP + Payload
$cbr5 set rate_ 8Kb
$cbr5 attach-agent $udp5
$ns_ at 9.0 "$cbr5 start"
$ns_ at 45.0  "$cbr5 stop"

#DI -> CT
set udp6 [new Agent/UDP]
$udp6 set class_ 7
set null6 [new Agent/Null]
$ns_ attach-agent $node_(2) $udp6
$ns_ attach-agent $node_(9) $null6
$ns_ connect $udp6 $null6
$udp6 set fid_ 7

set cbr6 [new Application/Traffic/CBR]
$cbr6 set packetSize_ 40     # RTP + UDP + Payload
$cbr6 set rate_ 8Kb
$cbr6 attach-agent $udp6
$ns_ at 11.0 "$cbr6 start"
$ns_ at 45.0  "$cbr6 stop"

#CT -> DI
set udp7 [new Agent/UDP]
$udp7 set class_ 8
set null7 [new Agent/Null]
$ns_ attach-agent $node_(9) $udp7
$ns_ attach-agent $node_(2) $null7
$ns_ connect $udp7 $null7
$udp7 set fid_ 8

set cbr7 [new Application/Traffic/CBR]
$cbr7 set packetSize_ 40     # RTP + UDP + Payload
$cbr7 set rate_ 8Kb
$cbr7 attach-agent $udp7
$ns_ at 11.0 "$cbr7 start"
$ns_ at 45.0  "$cbr7 stop"

#SECOM -> LABS
set udp8 [new Agent/UDP]
$udp8 set class_ 9
set null8 [new Agent/Null]
$ns_ attach-agent $node_(3) $udp8
$ns_ attach-agent $node_(8) $null8
$ns_ connect $udp8 $null8
$udp8 set fid_ 9

set cbr8 [new Application/Traffic/CBR]
$cbr8 set packetSize_ 40     # RTP + UDP + Payload
$cbr8 set rate_ 8Kb
$cbr8 attach-agent $udp8
$ns_ at 13.0 "$cbr8 start"
$ns_ at 45.0  "$cbr8 stop"

#LABS -> SECOM
set udp9 [new Agent/UDP]
$udp9 set class_ 10
set null9 [new Agent/Null]
$ns_ attach-agent $node_(8) $udp9
$ns_ attach-agent $node_(3) $null9
$ns_ connect $udp9 $null9
$udp9 set fid_ 10

set cbr9 [new Application/Traffic/CBR]
$cbr9 set packetSize_ 40     # RTP + UDP + Payload
$cbr9 set rate_ 8Kb
$cbr9 attach-agent $udp9
$ns_ at 13.0 "$cbr9 start"
$ns_ at 45.0  "$cbr9 stop"

#DI -> SECOM
set udp10 [new Agent/UDP]
$udp10 set class_ 11
set null10 [new Agent/Null]
$ns_ attach-agent $node_(2) $udp10
$ns_ attach-agent $node_(3) $null10
$ns_ connect $udp10 $null10
$udp10 set fid_ 11

set cbr10 [new Application/Traffic/CBR]
$cbr10 set packetSize_ 40     # RTP + UDP + Payload
$cbr10 set rate_ 8Kb
$cbr10 attach-agent $udp10
$ns_ at 15.0 "$cbr10 start"
$ns_ at 45.0  "$cbr10 stop"

#SECOM -> DI
set udp11 [new Agent/UDP]
$udp11 set class_ 12
set null11 [new Agent/Null]
$ns_ attach-agent $node_(3) $udp11
$ns_ attach-agent $node_(2) $null11
$ns_ connect $udp11 $null11
$udp11 set fid_ 12

set cbr11 [new Application/Traffic/CBR]
$cbr11 set packetSize_ 40     # RTP + UDP + Payload
$cbr11 set rate_ 8Kb
$cbr11 attach-agent $udp11
$ns_ at 15.0 "$cbr11 start"
$ns_ at 45.0  "$cbr11 stop"

#
# configurando trafego de background - pareto
#
# DI -> LABS
set tcp [new Agent/TCP]
$tcp set class_ 13
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(2) $tcp
$ns_ attach-agent $node_(8) $sink
$ns_ connect $tcp $sink
$tcp set fid_ 13

set p [new Application/Traffic/Pareto]
$p set packetSize_ 210
$p set burst_time_ 500ms
$p set idle_time_ 500ms
$p set rate_ 200k
$p set shape_ 1.5
$p attach-agent $tcp
$ns_ at 6.0 "$p start"
$ns_ at 35.0  "$p stop"

# GRAD BASICO -> CT
set tcp1 [new Agent/TCP]
$tcp1 set class_ 14
set sink1 [new Agent/TCPSink]
$ns_ attach-agent $node_(4) $tcp1
$ns_ attach-agent $node_(9) $sink1
$ns_ connect $tcp1 $sink1
$tcp1 set fid_ 14

set p1 [new Application/Traffic/Pareto]
$p1 set packetSize_ 210
$p1 set burst_time_ 500ms
$p1 set idle_time_ 500ms
$p1 set rate_ 200k
$p1 set shape_ 1.5
$p1 attach-agent $tcp1
$ns_ at 8.0 "$p1 start"
$ns_ at 35.0  "$p1 stop"

#SECOM -> GRAD PROFISSIONAL
set tcp2 [new Agent/TCP]
$tcp2 set class_ 15
set sink2 [new Agent/TCPSink]
$ns_ attach-agent $node_(3) $tcp2
$ns_ attach-agent $node_(10) $sink2
$ns_ connect $tcp2 $sink2
$tcp2 set fid_ 15

set p2 [new Application/Traffic/Pareto]
$p2 set packetSize_ 210
$p2 set burst_time_ 500ms
$p2 set idle_time_ 500ms
$p2 set rate_ 200k
$p2 set shape_ 1.5
$p2 attach-agent $tcp2
$ns_ at 10.0 "$p2 start"
$ns_ at 35.0  "$p2 stop"


## Label the Special Node in NAM
$ns_ at 0.0 "$node_(1) label CAPACIT"
$ns_ at 0.0 "$node_(2) label Dep_Informatica"
$ns_ at 0.0 "$node_(3) label SECOM"
$ns_ at 0.0 "$node_(4) label Grad_Basico"
$ns_ at 0.0 "$node_(5) label Reitoria"
$ns_ at 0.0 "$node_(6) label Incubadora"
$ns_ at 0.0 "$node_(7) label Musica"
$ns_ at 0.0 "$node_(8) label Laboratorios"
$ns_ at 0.0 "$node_(9) label Centro_Tec"
$ns_ at 0.0 "$node_(10) label Grad_Profissional"

#
# print (in the trace file) routing table and other
# internal data structures on a per-node basis
#
#$ns_ at 5.0 "[$node_(1) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(2) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(3) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(4) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(5) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(6) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(7) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(8) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(9) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(10) agent 255] print_rtable"
#$ns_ at 5.0 "[$node_(1) agent 255] print_linkset"
#$ns_ at 5.0 "[$node_(1) agent 255] print_nbset"
#$ns_ at 5.0 "[$node_(1) agent 255] print_nb2hopset"
#$ns_ at 5.0 "[$node_(1) agent 255] print_mprset"
#$ns_ at 5.0 "[$node_(1) agent 255] print_mprselset"
#$ns_ at 5.0 "[$node_(1) agent 255] print_topologyset"

#
# source connection-pattern and node-movement scripts
#
if { $opt(cp) == "" } {
    puts "*** NOTE: no connection pattern specified."
    set opt(cp) "none"
} else {
    puts "Loading connection pattern..."
    source $opt(cp)
}
if { $opt(sc) == "" } {
    puts "*** NOTE: no scenario file specified."
    set opt(sc) "none"
} else {
    puts "Loading scenario file..."
    source $opt(sc)
    puts "Load complete..."
}

#
# define initial node position in nam
#
for {set i 1} {$i < $opt(nn)} {incr i} {
    $ns_ initial_node_pos $node_($i) 20
}

#
# tell all nodes when the simulation ends
#
for {set i 1} {$i < $opt(nn) } {incr i} {
    $ns_ at $opt(stop).0 "$node_($i) reset";
}

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

proc stop {} {
    global ns_ tracefd namtrace
    $ns_ flush-trace
    close $tracefd
    close $namtrace
}

#
# begin simulation
#
puts "Starting Simulation..."

$ns_ run

sleep mode leach 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 65.5 "$n12 setdest 845.222 319.713 15.0"
$ns at 70.5 "$n3 setdest 355.222 519.713 15.0"
$ns at 67.5 "$n4 setdest 720.195 368.71 15.0"
$ns at 65.5 "$n2 setdest 846.279 231.381 15.0"
$ns at 67.5 "$n13 setdest 356.954 425.859 15.0"
$ns at 67.5 "$n11 setdest 459.874 558.361 15.0"
$ns at 106.5 "$n9 setdest 154.962 76.1411 15.0"
$ns at 106.5 "$n5 setdest 113.938 245.951 15.0"
$ns at 110.5 "$n8 setdest 218.432 229.934 15.0"
$ns at 110.5 "$n7 setdest 58.4876 147.719 15.0"
$ns at 115.5 "$n14 setdest 741.222 219.713 15.0"
$ns at 115.5 "$n0 setdest 546.27 490.731 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 $n2 $tcp
$ns attach-agent $n3 $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 68.2 "$ftp start"
$ns  at 78.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 $n4 $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 78.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 $n4 $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 81.0 "$ftp2 stop"

set tcp3 [new Agent/TCP]
$tcp3 set class_ 2
set sink3 [new Agent/TCPSink]
$ns  attach-agent $n12 $tcp3
$ns attach-agent $n11 $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 70.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 $n11 $tcp4
$ns attach-agent $n13 $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 88.0 "$ftp4 stop"

set tcp5 [new Agent/TCP]
$tcp5 set class_ 2
set sink5 [new Agent/TCPSink]
$ns  attach-agent $n12 $tcp5
$ns attach-agent $n13 $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 80.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 $n2 $tcp6
$ns attach-agent $n12 $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 80.2 "$ftp6 start"
$ns  at 90.0 "$ftp6 stop"

set tcp7 [new Agent/TCP]
$tcp7 set class_ 2
set sink7 [new Agent/TCPSink]
$ns  attach-agent $n4 $tcp7
$ns attach-agent $n12 $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 82.2 "$ftp7 start"
$ns  at 95.0 "$ftp7 stop"

set tcp8 [new Agent/TCP]
$tcp8 set class_ 2
set sink8 [new Agent/TCPSink]
$ns  attach-agent $n2 $tcp8
$ns attach-agent $n4 $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 80.2 "$ftp8 start"
$ns  at 96.0 "$ftp8 stop"

set tcp9 [new Agent/TCP]
$tcp9 set class_ 2
set sink9 [new Agent/TCPSink]
$ns  attach-agent $n11 $tcp9
$ns attach-agent $n3 $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 86.2 "$ftp9 start"
$ns  at 100.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 $n13 $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 90.2 "$ftp10 start"
$ns  at 101.0 "$ftp10 stop"

set tcp11 [new Agent/TCP]
$tcp11 set class_ 2
set sink11 [new Agent/TCPSink]
$ns  attach-agent $n13 $tcp11
$ns attach-agent $n3 $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 83.2 "$ftp11 start"
$ns  at 97.0 "$ftp11 stop"

set tcp12 [new Agent/TCP]
$tcp12 set class_ 2
set sink12 [new Agent/TCPSink]
$ns  attach-agent $n15 $tcp12
$ns attach-agent $n6 $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 63.2 "$ftp12 start"
$ns  at 137.0 "$ftp12 stop"

set tcp13 [new Agent/TCP]
$tcp13 set class_ 2
set sink13 [new Agent/TCPSink]
$ns  attach-agent $n15 $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 63.2 "$ftp13 start"
$ns  at 137.0 "$ftp13 stop"

set tcp14 [new Agent/TCP]
$tcp14 set class_ 2
set sink14 [new Agent/TCPSink]
$ns  attach-agent $n15 $tcp14
$ns attach-agent $n1 $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 63.2 "$ftp14 start"
$ns  at 137.0 "$ftp14 stop"

set tcp15 [new Agent/TCP]
$tcp15 set class_ 2
set sink15 [new Agent/TCPSink]
$ns  attach-agent $n14 $tcp15
$ns attach-agent $n0 $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 115.2 "$ftp15 start"
$ns  at 127.0 "$ftp15 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