Wednesday, 11 May 2016

AOMDV PROTOCOL NS2


#add-packet-header IP LL Mac AODV AOMDV ATR DSDV DSR OLSR UDP TCP CBR FTP      ;# needed headers
Mac/802_11 set CWMin_                 31
Mac/802_11 set CWMax_                 1023
Mac/802_11 set SlotTime_              0.000020                     ;# 20us
Mac/802_11 set SIFS_                  0.000010                     ;# 10us
Mac/802_11 set PreambleLength_        144                         ;# 144 bit
Mac/802_11 set ShortPreambleLength_   72                           ;# 72 bit
Mac/802_11 set PreambleDataRate_      1.0e6                        ;# 1Mbps
Mac/802_11 set PLCPHeaderLength_      48                           ;# 48 bits
Mac/802_11 set PLCPDataRate_          1.0e6                        ;# 1Mbps
Mac/802_11 set ShortPLCPDataRate_     2.0e6                        ;# 2Mbps
Mac/802_11 set RTSThreshold_          3000                         ;# bytes
Mac/802_11 set ShortRetryLimit_       7                            ;# retransmissions
Mac/802_11 set LongRetryLimit_        4                            ;# retransmissions
Mac/802_11 set newchipset_            false                        ;# use new chipset, allowing a more recent  packet to be correctly received in place of the first sensed packet
Mac/802_11 set dataRate_ 11Mb                                      ;# 802.11 data transmission rate
Mac/802_11 set basicRate_ 1Mb                                      ;# 802.11 basic transmission rate
Mac/802_11 set aarf_ false                                         ;# 802.11 Auto Rate Fallback



#------------------------------------------------------------------------------
# Defining options
# ------------------------------------------------------------------------------
set val(chan) Channel/WirelessChannel                              ;# channel type
set val(ant) Antenna/OmniAntenna                                   ;# antenna type
set val(propagation) Shado ;# propagation model
set val(netif) Phy/WirelessPhy                                     ;# network interface type
set val(ll) LL                                                     ;# link layer type
set val(ifq) Queue/DropTail/PriQueue                               ;# interface queue type
set val(ifqlen) 50                                                 ;# max packet in ifq
set val(mac) Mac/802_11                                            ;# MAC type
set val(rp) AOMDV ;# routing protocol
set val(n) 16.0 ;# node number
set val(density) 4096 ;# node density [node/km^2]
set val(end) 2060.0                                                ;# simulation time [s]
set val(mobility) Static ;# mobility model
set val(minSpeed) 0.5                                              ;# movement minimum speed [m/s]
set val(maxSpeed) 1.5                                              ;# movement maximum speed [m/s]
set val(minPause) 0.0                                              ;# movement minimum pause time [s]
set val(maxPause) 0.0                                              ;# movement maximum pause time [s]
set val(movementStart) 950.0                                       ;# movement start time [s]
set val(traffic) Node-UDP ;# data pattern
set val(dataStart) 1000.0                                          ;# data start time [s]
set val(dataStop) [expr $val(end) - 60.0]                          ;# data stop time [s]
set val(seed) 1 ;# general pseudo-random sequence generator
set val(macFailed) true                                            ;# ATR protocol: ns2 MAC failed callback
set val(etxMetric) true                                            ;# ATR protocol: ETX route metric
set val(throughput) 5.4 ;# CBR rate (<= 5.4Mb/s)
set val(flow) 100                                                  ;# number of concurrent data flow for FLOW-**P data pattern
set val(nodes) $val(n)                                             ;# number of concurrent transmitting nodes for NODE-**P data pattern



# ------------------------------------------------------------------------------
# Fixing DSR bug
# ------------------------------------------------------------------------------
if {$val(rp) == "DSR"} {
    set val(ifq) CMUPriQueue
}



# ------------------------------------------------------------------------------
# Channel model
# ------------------------------------------------------------------------------
Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1                                      ;# transmitter antenna gain
Antenna/OmniAntenna set Gr_ 1                                      ;# receiver antenna gain
Phy/WirelessPhy set L_ 1.0                                         ;# system loss factor (mostly 1.0)
if {$val(propagation) == "TwoRay"} {                               ;# range tx = 250m
    set val(prop) Propagation/TwoRayGround
    set prop [new $val(prop)]
    Phy/WirelessPhy set CPThresh_ 10.0                         ;# capture threshold in Watt
    Phy/WirelessPhy set CSThresh_ 1.559e-11                    ;# Carrier Sensing threshold
    Phy/WirelessPhy set RXThresh_ 3.652e-10                    ;# receiver signal threshold
    Phy/WirelessPhy set freq_ 2.4e9                            ;# channel frequency (Hz)
    Phy/WirelessPhy set Pt_ 0.28                               ;# transmitter signal power (Watt)
}
if {$val(propagation) == "Shado"} {
    set val(prop) Propagation/Shadowing
    set prop [new $val(prop)]
    $prop set pathlossExp_ 3.8                                 ;# path loss exponent
    $prop set std_db_ 2.0                                      ;# shadowing deviation (dB)
    $prop set seed_ 1                                          ;# seed for RNG
    $prop set dist0_ 1.0                                       ;# reference distance (m)
    $prop set CPThresh_ 10.0                                   ;# capture threshold in Watt
    $prop set RXThresh_ 2.37e-13                               ;# receiver signal threshold
    $prop set CSThresh_ [expr 2.37e-13 * 0.0427]               ;# Carrier Sensing threshold
    $prop set freq_ 2.4e9                                      ;# channel frequency (Hz)
    Phy/WirelessPhy set Pt_ 0.28
}



# ------------------------------------------------------------------------------
# Topology definition
# ------------------------------------------------------------------------------
#Creazione dello scenario in funzione della densita' di nodo scelta
set val(dim) [expr $val(n) / $val(density)]
set val(x) [expr [expr sqrt($val(dim))] * 1000]
set val(y) [expr [expr sqrt($val(dim))] * 1000]



# ------------------------------------------------------------------------------
# Pseudo-random sequence generator
# ------------------------------------------------------------------------------

# General pseudo-random sequence generator
set genSeed [new RNG]
$genSeed seed $val(seed)
set randomSeed [new RandomVariable/Uniform]
$randomSeed use-rng $genSeed
$randomSeed set min_ 1.0
$randomSeed set max_ 100.0

# Mobility model: x node position [m]
set genNodeX [new RNG]
$genNodeX seed [expr [$randomSeed value]]
set randomNodeX [new RandomVariable/Uniform]
$randomNodeX use-rng $genNodeX
$randomNodeX set min_ 1.0
$randomNodeX set max_ [expr $val(x) - 1.0]

# Mobility model: y node position [m]
set posNodeY [new RNG]
$posNodeY seed [expr [$randomSeed value]]
set randomNodeY [new RandomVariable/Uniform]
$randomNodeY use-rng $posNodeY
$randomNodeY set min_ 1.0
$randomNodeY set max_ [expr $val(y) - 1.0]

# Mobility model: node speed [m/s]
set genNodeSpeed [new RNG]
$genNodeSpeed seed [expr [$randomSeed value]]
set randomNodeSpeed [new RandomVariable/Uniform]
$randomNodeSpeed use-rng $genNodeSpeed
$randomNodeSpeed set min_ $val(minSpeed)
$randomNodeSpeed set max_ $val(maxSpeed)

# Mobility model: node movement pause [s]
set genNodePause [new RNG]
$genNodePause seed [expr [$randomSeed value]]
set randomNodePause [new RandomVariable/Uniform]
$randomNodePause use-rng $genNodePause
$randomNodePause set min_ $val(minPause)
$randomNodePause set max_ $val(maxPause)

# Data pattern: node
set genNode [new RNG]
$genNode seed [expr [$randomSeed value]]
set randomNode [new RandomVariable/Uniform]
$randomNode use-rng $genNode
$randomNode set min_ 0
$randomNode set max_ [expr $val(n) - 1]

# Data pattern "Random": flow start time [s]
set genStartData [new RNG]
$genStartData seed [expr [$randomSeed value]]
set randomStartData [new RandomVariable/Uniform]
$randomStartData use-rng $genStartData
$randomStartData set min_ $val(dataStart)
$randomStartData set max_ $val(dataStop)

# Data pattern "Full: flow start time [s]
set genStartDataFull [new RNG]
$genStartDataFull seed [expr [$randomSeed value]]
set randomStartDataFull [new RandomVariable/Uniform]
$randomStartDataFull use-rng $genStartDataFull
$randomStartDataFull set min_ 0
$randomStartDataFull set max_ [expr ($val(n) * ($val(n) - 1)) - 1]

# Data pattern: flow end time [s]
set genEndData [new RNG]
$genEndData seed [expr [$randomSeed value]]
set randomEndData [new RandomVariable/Uniform]
$randomEndData use-rng $genEndData
$randomEndData set min_ 0.0
$randomEndData set max_ [expr $val(end) - $val(dataStart) - 20]



# ------------------------------------------------------------------------------
# General definition
# ------------------------------------------------------------------------------
;#Instantiate the simulator
set ns [new Simulator]
;#Define topology
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
;#Create channel
set chan [new $val(chan)]
$prop topography $topo
;#Create God
create-god $val(n)
;#Global node setting
$ns node-config -adhocRouting $val(rp) \
    -llType $val(ll) \
    -macType $val(mac) \
    -ifqType $val(ifq) \
    -ifqLen $val(ifqlen) \
    -antType $val(ant) \
    -propInstance $prop \
    -phyType $val(netif) \
    -channel $chan \
    -topoInstance $topo \
    -agentTrace ON \
    -routerTrace ON \
    -macTrace OFF \
    -movementTrace OFF



# ------------------------------------------------------------------------------
# Trace file definition
# ------------------------------------------------------------------------------

#New format for wireless traces
$ns use-newtrace

#Create trace object for ns, nam, monitor and Inspect
set nsTrc [open ns.trc w]
$ns trace-all $nsTrc
#set namTrc [open nam.trc w]
#$ns namtrace-all-wireless $namTrc $val(x) $val(y)
set scenarioTrc [open scenario.trc w]

proc fileTrace {} {
#    global ns nsTrc namTrc
    global ns nsTrc scenarioTrc
    $ns flush-trace
    close $nsTrc
    close $scenarioTrc
#    close $namTrc
#    exec nam nam.trc &
}



# ------------------------------------------------------------------------------
# Nodes definition
# ------------------------------------------------------------------------------
;#  Create the specified number of nodes [$val(n)] and "attach" them to the channel.
for {set i 0} {$i < $val(n) } {incr i} {
    set node($i) [$ns node]
    $node($i) random-motion 0        ;# disable random motion
}   



# ------------------------------------------------------------------------------
# Nodes mobility
# ------------------------------------------------------------------------------
#parameters for trace Inspect
puts $scenarioTrc "# nodes: $val(n), max time: $val(end)"
puts $scenarioTrc "# nominal range: 250"

if {$val(mobility) == "Static"} {
    for {set i 0} {$i < $val(n)} {incr i} {
        set X [expr [$randomNodeX value] ]
        $node($i) set X_ $X
        set Y [expr [$randomNodeY value] ]
        $node($i) set Y_ $Y
        $node($i) set Z_ 0.0
        $ns initial_node_pos $node($i) 20
        puts $scenarioTrc "\$node_($i) set X_ $X"
        puts $scenarioTrc "\$node_($i) set Y_ $Y"
        puts $scenarioTrc "\$node_($i) set Z_ 0.0"
    }
}



# ------------------------------------------------------------------------------
# Data load
# ------------------------------------------------------------------------------
if {$val(traffic) == "Node-UDP"} {
    for {set i 0} {$i < $val(nodes)} {incr i} {
        set udp($i) [new Agent/UDP]
        $ns attach-agent $node($i) $udp($i)
        set dest [expr round([$randomNode value])]
        while {$dest == $i} {
            set dest [expr round([$randomNode value])]
        }
        set monitor($dest) [new Agent/LossMonitor]
        $ns attach-agent $node($dest) $monitor($dest)
        $ns connect $udp($i) $monitor($dest)
        set cbr($i) [new Application/Traffic/CBR]
        $cbr($i) attach-agent $udp($i)
        $cbr($i) set packetSize_ 1000
        $cbr($i) set random_ false
        $cbr($i) set rate_ [expr $val(throughput) / [expr $val(n) * sqrt($val(n))]]Mb
#        $cbr($i) set rate_ [expr $val(throughput)]Mb
        $ns at [expr $val(dataStart) + [$randomSeed value]] "$cbr($i) start"
        $ns at $val(dataStop) "$cbr($i) stop"
#        $ns at [expr [$randomStartData value]] "$cbr($i) start"
#        set endData [expr [$randomStartData value] + [$randomEndData value]]
#        if {$endData > $val(dataStop)} {
#            set endData $val(dataStop)
#        }
#        $ns at $endData "$cbr($i) stop"
    }
}



# ------------------------------------------------------------------------------
# Tracing
# ------------------------------------------------------------------------------

# printing simulation time
proc timeTrace { tracePause} {
    global ns
       set now [$ns now]
       $ns at [expr $now + $tracePause] "timeTrace $tracePause"
       puts "$now simulation seconds"
}

$ns at 10.0 "timeTrace 10.0"

#$ns at 500 "[$node(0) agent 255] storeInfo id1 tex#t1"
#$ns at 500 "[$node(0) agent 255] storeInfo id2 text2"

#$ns at 520 "[$node(0) agent 255] requestInfo id1"

# ------------------------------------------------------------------------------
# Starting & ending
# ------------------------------------------------------------------------------
for {set i 0} {$i < $val(n) } {incr i} {
        $ns at $val(end) "$node($i) reset";
}

$ns at $val(end) "fileTrace"
$ns at $val(end) "$ns halt"

$ns run

DSR PROTOCOL NS2 SCRIPTS

# ======================================================================
# Define options
# ======================================================================
 set val(chan)         Channel/WirelessChannel  ;# channel type
 set val(prop)         Propagation/TwoRayGround ;# radio-propagation model
 set val(ant)          Antenna/OmniAntenna      ;# Antenna type
 set val(ll)           LL                       ;# Link layer type
 set val(ifq)          Queue/DropTail/PriQueue  ;# Interface queue type
 set val(ifqlen)       50                       ;# max packet in ifq
 set val(netif)        Phy/WirelessPhy          ;# network interface type
 set val(mac)          Mac/802_11               ;# MAC type
 set val(nn)           6                        ;# number of mobilenodes
 set val(rp)           DSR                     ;# routing protocol
 set val(x)            800
 set val(y)            800

set ns [new Simulator]
#ns-random 0

set f [open 1_out.tr w]
$ns trace-all $f
set namtrace [open 1_out.nam w]
set runall [open runall.bat w]
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
set f0 [open packets_received.tr w]
set f1 [open packets_lost.tr w]
set f2 [open proj_out2.tr w]
set f3 [open proj_out3.tr w]

set topo [new Topography]
$topo load_flatgrid 800 800

create-god $val(nn)

set chan_1 [new $val(chan)]
set chan_2 [new $val(chan)]
set chan_3 [new $val(chan)]
set chan_4 [new $val(chan)]
set chan_5 [new $val(chan)]
set chan_6 [new $val(chan)]

# CONFIGURE AND CREATE 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 OFF \
                 -routerTrace ON \
                 -macTrace ON \
                 -movementTrace OFF \
                 -channel $chan_1  # \
                 #-channel $chan_2   \
                 #-channel $chan_3   \
                 #-channel $chan_4   \ 
                 #-channel $chan_5   \
                 #-channel $chan_6 


proc finish {} {
    global ns f f0 f1 f2 f3 namtrace
    $ns flush-trace
        close $namtrace  
    close $f0
        close $f1
     close $f2
        close $f3
        exec xgraph packets_received.tr packets_lost.tr
        #exec nam -r 5m 1_out.nam &
             
           exit 0
}

proc record {} {
  global sink0 sink1 sink2 sink3 sink4 sink5 f0 f1 f2 f3
   #Get An Instance Of The Simulator
   set ns [Simulator instance]
  
   #Set The Time After Which The Procedure Should Be Called Again
   set time 0.05
   #How Many Bytes Have Been Received By The Traffic Sinks?
   set bw0 [$sink5 set npkts_]
   set bw1 [$sink5 set nlost_]
   #set bw2 [$sink2 set npkts_]
   #set bw3 [$sink3 set npkts_]
  
   #Get The Current Time
   set now [$ns now]
  
   #Save Data To The Files
   puts $f0 "$now [expr $bw0]"
   puts $f1 "$now [expr $bw1]"
   #puts $f2 "$now [expr $bw2]"
   #puts $f3 "$now [expr $bw3]"

   #Re-Schedule The Procedure
   $ns at [expr $now+$time] "record"
  }

# define color index
$ns color 0 blue
$ns color 1 red
$ns color 2 chocolate
$ns color 3 red
$ns color 4 brown
$ns color 5 tan
$ns color 6 gold
$ns color 7 black
                       
set n(0) [$ns node]
#$ns at 0.0 "$n(0) color red"
$n(0) color "0"
$n(0) shape "circle"
set n(1) [$ns node]
$n(1) color "blue"
$n(1) shape "circle"
set n(2) [$ns node]
$n(2) color "tan"
$n(2) shape "circle"
set n(3) [$ns node]
$n(3) color "red"
$n(3) shape "circle"
set n(4) [$ns node]
$n(4) color "tan"
$n(4) shape "circle"
set n(5) [$ns node]
$n(5) color "red"
$n(5) shape "circle"






for {set i 0} {$i < $val(nn)} {incr i} {
    $ns initial_node_pos $n($i) 30+i*100
}

$n(0) set X_ 0.0
$n(0) set Y_ 0.0
$n(0) set Z_ 0.0

$n(1) set X_ 0.0
$n(1) set Y_ 0.0
$n(1) set Z_ 0.0

$n(2) set X_ 0.0
$n(2) set Y_ 0.0
$n(2) set Z_ 0.0

$n(3) set X_ 0.0
$n(3) set Y_ 0.0
$n(3) set Z_ 0.0

$n(4) set X_ 0.0
$n(4) set Y_ 0.0
$n(4) set Z_ 0.0

$n(5) set X_ 0.0
$n(5) set Y_ 0.0
$n(5) set Z_ 0.0

$ns at 0.0 "$n(0) setdest 100.0 100.0 3000.0"
$ns at 0.0 "$n(1) setdest 200.0 200.0 3000.0"
$ns at 0.0 "$n(2) setdest 300.0 200.0 3000.0"
$ns at 0.0 "$n(3) setdest 400.0 300.0 3000.0"
$ns at 0.0 "$n(4) setdest 500.0 300.0 3000.0"
$ns at 0.0 "$n(5) setdest 600.0 400.0 3000.0"

$ns at 2.0 "$n(5) setdest 100.0 400.0 500.0"
#$ns at 1.5 "$n(3) setdest 450.0 150.0 500.0"


# CONFIGURE AND SET UP A FLOW


set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
set sink3 [new Agent/LossMonitor]
set sink4 [new Agent/LossMonitor]
set sink5 [new Agent/LossMonitor]
$ns attach-agent $n(0) $sink0
$ns attach-agent $n(1) $sink1
$ns attach-agent $n(2) $sink2
$ns attach-agent $n(3) $sink3
$ns attach-agent $n(4) $sink4
$ns attach-agent $n(5) $sink5

#$ns attach-agent $sink2 $sink3
set tcp0 [new Agent/TCP]
$ns attach-agent $n(0) $tcp0
set tcp1 [new Agent/TCP]
$ns attach-agent $n(1) $tcp1
set tcp2 [new Agent/TCP]
$ns attach-agent $n(2) $tcp2
set tcp3 [new Agent/TCP]
$ns attach-agent $n(3) $tcp3
set tcp4 [new Agent/TCP]
$ns attach-agent $n(4) $tcp4
set tcp5 [new Agent/TCP]
$ns attach-agent $n(5) $tcp5


proc attach-CBR-traffic { node sink size interval } {
   #Get an instance of the simulator
   set ns [Simulator instance]
   #Create a CBR  agent and attach it to the node
   set cbr [new Agent/CBR]
   $ns attach-agent $node $cbr
   $cbr set packetSize_ $size
   $cbr set interval_ $interval

   #Attach CBR source to sink;
   $ns connect $cbr $sink
   return $cbr
  }

set cbr0 [attach-CBR-traffic $n(0) $sink5 1000 .015]
#set cbr1 [attach-CBR-traffic $n(1) $sink2 1000 .015]
#set cbr2 [attach-CBR-traffic $n(2) $sink3 1000 .015]
#set cbr3 [attach-CBR-traffic $n(3) $sink0 1000 .015]
#set cbr4 [attach-CBR-traffic $n(4) $sink3 1000 .015]
#set cbr5 [attach-CBR-traffic $n(5) $sink0 1000 .015]



$ns at 0.0 "record"
#$ns at 0.5 "$cbr0 start"
#$ns at 0.5 "$cbr2 start"
#$ns at 2.0 "$cbr0 stop"
#$ns at 2.0 "$cbr2 stop"
$ns at 1.0 "$cbr0 start"
#$ns at 4.0 "$cbr3 stop"

$ns at 10.0 "finish"

puts "Start of simulation.."
$ns run

DSDV PROTOCOL NS2

# ======================================================================
# Define options
# ======================================================================
 set val(chan)         Channel/WirelessChannel  ;# channel type
 set val(prop)         Propagation/TwoRayGround ;# radio-propagation model
 set val(ant)          Antenna/OmniAntenna      ;# Antenna type
 set val(ll)           LL                       ;# Link layer type
 set val(ifq)          Queue/DropTail/PriQueue  ;# Interface queue type
 set val(ifqlen)       50                       ;# max packet in ifq
 set val(netif)        Phy/WirelessPhy          ;# network interface type
 set val(mac)          Mac/802_11               ;# MAC type
 set val(nn)           6                        ;# number of mobilenodes
 set val(rp)           DSDV                     ;# routing protocol
 set val(x)            800
 set val(y)            800

set ns [new Simulator]
#ns-random 0

set f [open 1_out.tr w]
$ns trace-all $f
set namtrace [open 1_out.nam w]
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
set f0 [open proj_out0.tr w]
set f1 [open proj_out1.tr w]
set f2 [open proj_out2.tr w]
set f3 [open proj_out3.tr w]

set topo [new Topography]
$topo load_flatgrid 800 800

create-god $val(nn)

set chan_1 [new $val(chan)]
set chan_2 [new $val(chan)]
set chan_3 [new $val(chan)]
set chan_4 [new $val(chan)]
set chan_5 [new $val(chan)]
set chan_6 [new $val(chan)]

# CONFIGURE AND CREATE 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 OFF \
                 -routerTrace ON \
                 -macTrace ON \
                 -movementTrace OFF \
                 -channel $chan_1  # \
                 #-channel $chan_2   \
                 #-channel $chan_3   \
                 #-channel $chan_4   \ 
                 #-channel $chan_5   \
                 #-channel $chan_6 


proc finish {} {
    global ns f f0 f1 f2 f3 namtrace
    $ns flush-trace
        close $namtrace  
    close $f0
        close $f1
     close $f2
        close $f3
       # exec xgraph proj_out0.tr proj_out1.tr
                                     # proj_out2.tr proj_out3.tr
        exec nam -r 5m 1_out.nam &
    exit 0
}

proc record {} {
  global sink0 sink1 sink2 sink3 sink4 sink5 f0 f1 f2 f3
   #Get An Instance Of The Simulator
   set ns [Simulator instance]
  
   #Set The Time After Which The Procedure Should Be Called Again
   set time 0.05
   #How Many Bytes Have Been Received By The Traffic Sinks?
   set bw0 [$sink5 set npkts_]
   set bw1 [$sink5 set nlost_]
   #set bw2 [$sink2 set npkts_]
   #set bw3 [$sink3 set npkts_]
  
   #Get The Current Time
   set now [$ns now]
  
   #Save Data To The Files
   puts $f0 "$now [expr $bw0]"
   puts $f1 "$now [expr $bw1]"
   #puts $f2 "$now [expr $bw2]"
   #puts $f3 "$now [expr $bw3]"

   #Re-Schedule The Procedure
   $ns at [expr $now+$time] "record"
  }

# define color index
$ns color 0 blue
$ns color 1 red
$ns color 2 chocolate
$ns color 3 red
$ns color 4 brown
$ns color 5 tan
$ns color 6 gold
$ns color 7 black
                       
set n(0) [$ns node]
#$ns at 0.0 "$n(0) color red"
$n(0) color "0"
$n(0) shape "circle"
set n(1) [$ns node]
$n(1) color "blue"
$n(1) shape "circle"
set n(2) [$ns node]
$n(2) color "tan"
$n(2) shape "circle"
set n(3) [$ns node]
$n(3) color "red"
$n(3) shape "circle"
set n(4) [$ns node]
$n(4) color "tan"
$n(4) shape "circle"
set n(5) [$ns node]
$n(5) color "red"
$n(5) shape "circle"






for {set i 0} {$i < $val(nn)} {incr i} {
    $ns initial_node_pos $n($i) 30+i*100
}

$n(0) set X_ 0.0
$n(0) set Y_ 0.0
$n(0) set Z_ 0.0

$n(1) set X_ 0.0
$n(1) set Y_ 0.0
$n(1) set Z_ 0.0

$n(2) set X_ 0.0
$n(2) set Y_ 0.0
$n(2) set Z_ 0.0

$n(3) set X_ 0.0
$n(3) set Y_ 0.0
$n(3) set Z_ 0.0

$n(4) set X_ 0.0
$n(4) set Y_ 0.0
$n(4) set Z_ 0.0

$n(5) set X_ 0.0
$n(5) set Y_ 0.0
$n(5) set Z_ 0.0

$ns at 0.0 "$n(0) setdest 100.0 100.0 3000.0"
$ns at 0.0 "$n(1) setdest 200.0 200.0 3000.0"
$ns at 0.0 "$n(2) setdest 300.0 200.0 3000.0"
$ns at 0.0 "$n(3) setdest 400.0 300.0 3000.0"
$ns at 0.0 "$n(4) setdest 500.0 300.0 3000.0"
$ns at 0.0 "$n(5) setdest 600.0 400.0 3000.0"

$ns at 2.0 "$n(5) setdest 100.0 400.0 500.0"
#$ns at 1.5 "$n(3) setdest 450.0 150.0 500.0"


# CONFIGURE AND SET UP A FLOW


set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
set sink3 [new Agent/LossMonitor]
set sink4 [new Agent/LossMonitor]
set sink5 [new Agent/LossMonitor]
$ns attach-agent $n(0) $sink0
$ns attach-agent $n(1) $sink1
$ns attach-agent $n(2) $sink2
$ns attach-agent $n(3) $sink3
$ns attach-agent $n(4) $sink4
$ns attach-agent $n(5) $sink5

#$ns attach-agent $sink2 $sink3
set tcp0 [new Agent/TCP]
$ns attach-agent $n(0) $tcp0
set tcp1 [new Agent/TCP]
$ns attach-agent $n(1) $tcp1
set tcp2 [new Agent/TCP]
$ns attach-agent $n(2) $tcp2
set tcp3 [new Agent/TCP]
$ns attach-agent $n(3) $tcp3
set tcp4 [new Agent/TCP]
$ns attach-agent $n(4) $tcp4
set tcp5 [new Agent/TCP]
$ns attach-agent $n(5) $tcp5


proc attach-CBR-traffic { node sink size interval } {
   #Get an instance of the simulator
   set ns [Simulator instance]
   #Create a CBR  agent and attach it to the node
   set cbr [new Agent/CBR]
   $ns attach-agent $node $cbr
   $cbr set packetSize_ $size
   $cbr set interval_ $interval

   #Attach CBR source to sink;
   $ns connect $cbr $sink
   return $cbr
  }

set cbr0 [attach-CBR-traffic $n(0) $sink5 1000 .015]
#set cbr1 [attach-CBR-traffic $n(1) $sink2 1000 .015]
#set cbr2 [attach-CBR-traffic $n(2) $sink3 1000 .015]
#set cbr3 [attach-CBR-traffic $n(3) $sink0 1000 .015]
#set cbr4 [attach-CBR-traffic $n(4) $sink3 1000 .015]
#set cbr5 [attach-CBR-traffic $n(5) $sink0 1000 .015]



$ns at 0.0 "record"
#$ns at 0.5 "$cbr0 start"
#$ns at 0.5 "$cbr2 start"
#$ns at 2.0 "$cbr0 stop"
#$ns at 2.0 "$cbr2 stop"
$ns at 1.0 "$cbr0 start"
#$ns at 4.0 "$cbr3 stop"

$ns at 10.0 "finish"

puts "Start of simulation.."
$ns run

cbr traffic

#  $ ns cbrgen.tcl -type cbr -nn 50 -seed 950000.222 -mc 10 -rate 8.0 > cbr-50-10-8  

#
# nodes: 50, max conn: 10, send rate: 0.125, seed: 950000.222
#
#
# 0 connecting to 1 at time 81.933906610093032
#
set udp_(0) [new Agent/UDP]
$ns_ attach-agent $node_(0) $udp_(0)
set null_(0) [new Agent/Null]
$ns_ attach-agent $node_(1) $null_(0)
set cbr_(0) [new Application/Traffic/CBR]
$cbr_(0) set packetSize_ 512
$cbr_(0) set interval_ 0.125
$cbr_(0) set random_ 1
$cbr_(0) set maxpkts_ 10000
$cbr_(0) attach-agent $udp_(0)
$ns_ connect $udp_(0) $null_(0)
$ns_ at 81.933906610093032 "$cbr_(0) start"
#
# 1 connecting to 2 at time 144.52721277462655
#
set udp_(1) [new Agent/UDP]
$ns_ attach-agent $node_(1) $udp_(1)
set null_(1) [new Agent/Null]
$ns_ attach-agent $node_(2) $null_(1)
set cbr_(1) [new Application/Traffic/CBR]
$cbr_(1) set packetSize_ 512
$cbr_(1) set interval_ 0.125
$cbr_(1) set random_ 1
$cbr_(1) set maxpkts_ 10000
$cbr_(1) attach-agent $udp_(1)
$ns_ connect $udp_(1) $null_(1)
$ns_ at 144.52721277462655 "$cbr_(1) start"
#
# 1 connecting to 3 at time 144.95019923194786
#
set udp_(2) [new Agent/UDP]
$ns_ attach-agent $node_(1) $udp_(2)
set null_(2) [new Agent/Null]
$ns_ attach-agent $node_(3) $null_(2)
set cbr_(2) [new Application/Traffic/CBR]
$cbr_(2) set packetSize_ 512
$cbr_(2) set interval_ 0.125
$cbr_(2) set random_ 1
$cbr_(2) set maxpkts_ 10000
$cbr_(2) attach-agent $udp_(2)
$ns_ connect $udp_(2) $null_(2)
$ns_ at 144.95019923194786 "$cbr_(2) start"
#
# 2 connecting to 3 at time 35.579250974384721
#
set udp_(3) [new Agent/UDP]
$ns_ attach-agent $node_(2) $udp_(3)
set null_(3) [new Agent/Null]
$ns_ attach-agent $node_(3) $null_(3)
set cbr_(3) [new Application/Traffic/CBR]
$cbr_(3) set packetSize_ 512
$cbr_(3) set interval_ 0.125
$cbr_(3) set random_ 1
$cbr_(3) set maxpkts_ 10000
$cbr_(3) attach-agent $udp_(3)
$ns_ connect $udp_(3) $null_(3)
$ns_ at 35.579250974384721 "$cbr_(3) start"
#
# 4 connecting to 5 at time 43.762275894993117
#
set udp_(4) [new Agent/UDP]
$ns_ attach-agent $node_(4) $udp_(4)
set null_(4) [new Agent/Null]
$ns_ attach-agent $node_(5) $null_(4)
set cbr_(4) [new Application/Traffic/CBR]
$cbr_(4) set packetSize_ 512
$cbr_(4) set interval_ 0.125
$cbr_(4) set random_ 1
$cbr_(4) set maxpkts_ 10000
$cbr_(4) attach-agent $udp_(4)
$ns_ connect $udp_(4) $null_(4)
$ns_ at 43.762275894993117 "$cbr_(4) start"
#
# 4 connecting to 6 at time 123.83755247287338
#
set udp_(5) [new Agent/UDP]
$ns_ attach-agent $node_(4) $udp_(5)
set null_(5) [new Agent/Null]
$ns_ attach-agent $node_(6) $null_(5)
set cbr_(5) [new Application/Traffic/CBR]
$cbr_(5) set packetSize_ 512
$cbr_(5) set interval_ 0.125
$cbr_(5) set random_ 1
$cbr_(5) set maxpkts_ 10000
$cbr_(5) attach-agent $udp_(5)
$ns_ connect $udp_(5) $null_(5)
$ns_ at 123.83755247287338 "$cbr_(5) start"
#
# 12 connecting to 13 at time 154.86410073696825
#
set udp_(6) [new Agent/UDP]
$ns_ attach-agent $node_(12) $udp_(6)
set null_(6) [new Agent/Null]
$ns_ attach-agent $node_(13) $null_(6)
set cbr_(6) [new Application/Traffic/CBR]
$cbr_(6) set packetSize_ 512
$cbr_(6) set interval_ 0.125
$cbr_(6) set random_ 1
$cbr_(6) set maxpkts_ 10000
$cbr_(6) attach-agent $udp_(6)
$ns_ connect $udp_(6) $null_(6)
$ns_ at 154.86410073696825 "$cbr_(6) start"
#
# 14 connecting to 15 at time 68.140699802032074
#
set udp_(7) [new Agent/UDP]
$ns_ attach-agent $node_(14) $udp_(7)
set null_(7) [new Agent/Null]
$ns_ attach-agent $node_(15) $null_(7)
set cbr_(7) [new Application/Traffic/CBR]
$cbr_(7) set packetSize_ 512
$cbr_(7) set interval_ 0.125
$cbr_(7) set random_ 1
$cbr_(7) set maxpkts_ 10000
$cbr_(7) attach-agent $udp_(7)
$ns_ connect $udp_(7) $null_(7)
$ns_ at 68.140699802032074 "$cbr_(7) start"
#
# 14 connecting to 16 at time 154.31561598289554
#
set udp_(8) [new Agent/UDP]
$ns_ attach-agent $node_(14) $udp_(8)
set null_(8) [new Agent/Null]
$ns_ attach-agent $node_(16) $null_(8)
set cbr_(8) [new Application/Traffic/CBR]
$cbr_(8) set packetSize_ 512
$cbr_(8) set interval_ 0.125
$cbr_(8) set random_ 1
$cbr_(8) set maxpkts_ 10000
$cbr_(8) attach-agent $udp_(8)
$ns_ connect $udp_(8) $null_(8)
$ns_ at 154.31561598289554 "$cbr_(8) start"
#
# 18 connecting to 19 at time 57.203785086611191
#
set udp_(9) [new Agent/UDP]
$ns_ attach-agent $node_(18) $udp_(9)
set null_(9) [new Agent/Null]
$ns_ attach-agent $node_(19) $null_(9)
set cbr_(9) [new Application/Traffic/CBR]
$cbr_(9) set packetSize_ 512
$cbr_(9) set interval_ 0.125
$cbr_(9) set random_ 1
$cbr_(9) set maxpkts_ 10000
$cbr_(9) attach-agent $udp_(9)
$ns_ connect $udp_(9) $null_(9)
$ns_ at 57.203785086611191 "$cbr_(9) start"
#
#Total sources/connections: 7/10
#

30 node AODV protocol TCL script

Phy/WirelessPhy set freq_ 2.472e9  
Phy/WirelessPhy set RXThresh_ 2.62861e-09; #100m radius
Phy/WirelessPhy set CSThresh_ [expr 0.9*[Phy/WirelessPhy set RXThresh_]]
Phy/WirelessPhy set bandwidth_ 11.0e6 
Mac/802_11 set dataRate_ 11Mb 
Mac/802_11 set basicRate_ 2Mb 

set val(chan) Channel/WirelessChannel ;
set val(prop) Propagation/TwoRayGround ;
set val(netif) Phy/WirelessPhy ;
set val(mac) Mac/802_11 ;
set val(ifq) Queue/DropTail/PriQueue ;
set val(ll) LL ;
set val(ant) Antenna/OmniAntenna ;
set val(ifqlen) 30 ;
set val(nn) 30 ;
set val(rp) AODV ;
set val(x) 300 ;
set val(y) 300 ;
set val(stop) 50 ;

set ns [new Simulator]

set tracefd [open AODV_30.tr w]

set winFile [open CwMaodv_30 w]

set namtracefd [open namwrls.nam w]

$ns trace-all $tracefd
$ns use-newtrace

$ns namtrace-all-wireless $namtracefd $val(x) $val(y)

set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

create-god $val(nn)


$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 OFF \


for {set i 0} {$i < $val(nn) } {incr i} {
  set node_($i) [$ns node]
 }

$node_(0) set X_ 95.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0

$node_(1) set X_ 60.0
$node_(1) set Y_ 50.0
$node_(1) set Z_ 0.0

$node_(2) set X_ 25.0
$node_(2) set Y_ 190.0
$node_(2) set Z_ 0.0



$node_(3) set X_ 135.0
$node_(3) set Y_ 155.0
$node_(3) set Z_ 0.0



$node_(4) set X_ 105.0
$node_(4) set Y_ 180.0
$node_(4) set Z_ 0.0


$node_(5) set X_ 110.0
$node_(5) set Y_ 200.0
$node_(5) set Z_ 0.0


$node_(6) set X_ 55.0
$node_(6) set Y_ 75.0
$node_(6) set Z_ 0.0


$node_(7) set X_ 1.0
$node_(7) set Y_ 20.0
$node_(7) set Z_ 0.0

$node_(8) set X_ 175.0
$node_(8) set Y_ 90.0
$node_(8) set Z_ 0.0

$node_(9) set X_ 115.0
$node_(9) set Y_ 115.0
$node_(9) set Z_ 0.0

$node_(10) set X_ 75.0
$node_(10) set Y_ 175.0
$node_(10) set Z_ 0.0

$node_(11) set X_ 150.0
$node_(11) set Y_ 135.0
$node_(11) set Z_ 0.0

$node_(12) set X_ 45.0
$node_(12) set Y_ 90.0
$node_(12) set Z_ 0.0

$node_(13) set X_ 10.0
$node_(13) set Y_ 45.0
$node_(13) set Z_ 0.0

$node_(14) set X_ 90.0
$node_(14) set Y_ 1.0
$node_(14) set Z_ 0.0

$node_(15) set X_ 110.0
$node_(15) set Y_ 100.0
$node_(15) set Z_ 0.0

$node_(16) set X_ 160.0
$node_(16) set Y_ 120.0
$node_(16) set Z_ 0.0

$node_(17) set X_ 180.0
$node_(17) set Y_ 20.0
$node_(17) set Z_ 0.0

$node_(18) set X_ 120.0
$node_(18) set Y_ 10.0
$node_(18) set Z_ 0.0

$node_(19) set X_ 190.0
$node_(19) set Y_ 1.0
$node_(19) set Z_ 0.0

$node_(20) set X_ 150.0
$node_(20) set Y_ 135.0
$node_(20) set Z_ 0.0

$node_(21) set X_ 45.0
$node_(21) set Y_ 190.0
$node_(21) set Z_ 0.0

$node_(22) set X_ 200.0
$node_(22) set Y_ 45.0
$node_(2) set Z_ 0.0

$node_(23) set X_ 275.0
$node_(23) set Y_ 1.0
$node_(23) set Z_ 0.0

$node_(24) set X_ 260.0
$node_(24) set Y_ 100.0
$node_(24) set Z_ 0.0

$node_(25) set X_ 245.0
$node_(25) set Y_ 120.0
$node_(25) set Z_ 0.0

$node_(26) set X_ 230.0
$node_(26) set Y_ 225.0
$node_(26) set Z_ 0.0

$node_(27) set X_ 215.0
$node_(27) set Y_ 220.0
$node_(27) set Z_ 0.0

$node_(28) set X_ 300.0
$node_(28) set Y_ 200.0
$node_(28) set Z_ 0.0


$node_(29) set X_ 290.0
$node_(29) set Y_ 210.0
$node_(29) set Z_ 0.0












$ns at 10.0 "$node_(2) setdest 235.0 210.0 8.0"

$ns at 10.0 "$node_(4) setdest 140.0 80.0 8.0"

$ns at 10.0 "$node_(8) setdest 125.0 100.0 8.0"

$ns at 10.0 "$node_(10) setdest 190.0 160.0 8.0"

$ns at 10.0 "$node_(18) setdest 70.0 120.0 8.0"

$ns at 10.0 "$node_(14) setdest 210.0 170.0 8.0"

$ns at 10.0 "$node_(22) setdest 125.0 100.0 8.0"

$ns at 10.0 "$node_(25) setdest 100.0 185.0 8.0"

$ns at 10.0 "$node_(29) setdest 10.0 120.0 8.0"






set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(2) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp


$ns at 0.1 "$ftp start"

for {set i 0} {$i < $val(nn) } {incr i} {
 $ns initial_node_pos $node_($i) 10
 }

for {set i 0} {$i < $val(nn) } {incr i} {
 $ns at $val(stop) "$node_($i) reset"
}

$ns at $val(stop) "stop"


proc plotWindow {tcpSource file} {
global ns
set time 0.1
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 0.1 "plotWindow $tcp $winFile"

proc stop {} {
global ns tracefd namtracefd
$ns flush-trace
close $tracefd
close $namtracefd
exec nam namwrls.nam &
exit 0
}


$ns run

AODV tcl script

 ======================================================================
# Define options
# ======================================================================
 set val(chan)         Channel/WirelessChannel  ;# channel type
 set val(prop)         Propagation/TwoRayGround ;# radio-propagation model
 set val(ant)          Antenna/OmniAntenna      ;# Antenna type
 set val(ll)           LL                       ;# Link layer type
 set val(ifq)          Queue/DropTail/PriQueue  ;# Interface queue type
 set val(ifqlen)       50                       ;# max packet in ifq
 set val(netif)        Phy/WirelessPhy          ;# network interface type
 set val(mac)          Mac/802_11               ;# MAC type
 set val(nn)           6                        ;# number of mobilenodes
 set val(rp)           AODV                     ;# routing protocol
 set val(x)            800
 set val(y)            800

set ns [new Simulator]
#ns-random 0

set f [open 1_out.tr w]
$ns trace-all $f
set namtrace [open 1_out.nam w]
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
set f0 [open packets_received.tr w]
set f1 [open packets_lost.tr w]
set f2 [open proj_out2.tr w]
set f3 [open proj_out3.tr w]

set topo [new Topography]
$topo load_flatgrid 800 800

create-god $val(nn)

set chan_1 [new $val(chan)]
set chan_2 [new $val(chan)]
set chan_3 [new $val(chan)]
set chan_4 [new $val(chan)]
set chan_5 [new $val(chan)]
set chan_6 [new $val(chan)]

# CONFIGURE AND CREATE 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 OFF \
                 -routerTrace ON \
                 -macTrace ON \
                 -movementTrace OFF \
                 -channel $chan_1  # \
                 #-channel $chan_2   \
                 #-channel $chan_3   \
                 #-channel $chan_4   \ 
                 #-channel $chan_5   \
                 #-channel $chan_6 


proc finish {} {
    global ns f f0 f1 f2 f3 namtrace
    $ns flush-trace
        close $namtrace  
    close $f0
        close $f1
     close $f2
        close $f3
        exec xgraph packets_received.tr packets_lost.tr
                                     # proj_out2.tr proj_out3.tr
        exec nam -r 5m 1_out.nam &
    exit 0
}

proc record {} {
  global sink0 sink1 sink2 sink3 sink4 sink5 f0 f1 f2 f3
   #Get An Instance Of The Simulator
   set ns [Simulator instance]
  
   #Set The Time After Which The Procedure Should Be Called Again
   set time 0.05
   #How Many Bytes Have Been Received By The Traffic Sinks?
   set bw0 [$sink5 set npkts_]
   set bw1 [$sink5 set nlost_]
   #set bw2 [$sink2 set npkts_]
   #set bw3 [$sink3 set npkts_]
  
   #Get The Current Time
   set now [$ns now]
  
   #Save Data To The Files
   puts $f0 "$now [expr $bw0]"
   puts $f1 "$now [expr $bw1]"
   #puts $f2 "$now [expr $bw2]"
   #puts $f3 "$now [expr $bw3]"

   #Re-Schedule The Procedure
   $ns at [expr $now+$time] "record"
  }

# define color index
$ns color 0 blue
$ns color 1 red
$ns color 2 chocolate
$ns color 3 red
$ns color 4 brown
$ns color 5 tan
$ns color 6 gold
$ns color 7 black
                       
set n(0) [$ns node]
#$ns at 0.0 "$n(0) color red"
$n(0) color "0"
$n(0) shape "circle"
set n(1) [$ns node]
$n(1) color "blue"
$n(1) shape "circle"
set n(2) [$ns node]
$n(2) color "tan"
$n(2) shape "circle"
set n(3) [$ns node]
$n(3) color "red"
$n(3) shape "circle"
set n(4) [$ns node]
$n(4) color "tan"
$n(4) shape "circle"
set n(5) [$ns node]
$n(5) color "red"
$n(5) shape "circle"






for {set i 0} {$i < $val(nn)} {incr i} {
    $ns initial_node_pos $n($i) 30+i*100
}

$n(0) set X_ 0.0
$n(0) set Y_ 0.0
$n(0) set Z_ 0.0

$n(1) set X_ 0.0
$n(1) set Y_ 0.0
$n(1) set Z_ 0.0

$n(2) set X_ 0.0
$n(2) set Y_ 0.0
$n(2) set Z_ 0.0

$n(3) set X_ 0.0
$n(3) set Y_ 0.0
$n(3) set Z_ 0.0

$n(4) set X_ 0.0
$n(4) set Y_ 0.0
$n(4) set Z_ 0.0

$n(5) set X_ 0.0
$n(5) set Y_ 0.0
$n(5) set Z_ 0.0

$ns at 0.0 "$n(0) setdest 100.0 100.0 3000.0"
$ns at 0.0 "$n(1) setdest 200.0 200.0 3000.0"
$ns at 0.0 "$n(2) setdest 300.0 200.0 3000.0"
$ns at 0.0 "$n(3) setdest 400.0 300.0 3000.0"
$ns at 0.0 "$n(4) setdest 500.0 300.0 3000.0"
$ns at 0.0 "$n(5) setdest 600.0 400.0 3000.0"

$ns at 2.0 "$n(5) setdest 100.0 400.0 500.0"
#$ns at 1.5 "$n(3) setdest 450.0 150.0 500.0"


# CONFIGURE AND SET UP A FLOW


set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
set sink3 [new Agent/LossMonitor]
set sink4 [new Agent/LossMonitor]
set sink5 [new Agent/LossMonitor]
$ns attach-agent $n(0) $sink0
$ns attach-agent $n(1) $sink1
$ns attach-agent $n(2) $sink2
$ns attach-agent $n(3) $sink3
$ns attach-agent $n(4) $sink4
$ns attach-agent $n(5) $sink5

#$ns attach-agent $sink2 $sink3
set tcp0 [new Agent/TCP]
$ns attach-agent $n(0) $tcp0
set tcp1 [new Agent/TCP]
$ns attach-agent $n(1) $tcp1
set tcp2 [new Agent/TCP]
$ns attach-agent $n(2) $tcp2
set tcp3 [new Agent/TCP]
$ns attach-agent $n(3) $tcp3
set tcp4 [new Agent/TCP]
$ns attach-agent $n(4) $tcp4
set tcp5 [new Agent/TCP]
$ns attach-agent $n(5) $tcp5


proc attach-CBR-traffic { node sink size interval } {
   #Get an instance of the simulator
   set ns [Simulator instance]
   #Create a CBR  agent and attach it to the node
   set cbr [new Agent/CBR]
   $ns attach-agent $node $cbr
   $cbr set packetSize_ $size
   $cbr set interval_ $interval

   #Attach CBR source to sink;
   $ns connect $cbr $sink
   return $cbr
  }

set cbr0 [attach-CBR-traffic $n(0) $sink5 1000 .015]
#set cbr1 [attach-CBR-traffic $n(1) $sink2 1000 .015]
#set cbr2 [attach-CBR-traffic $n(2) $sink3 1000 .015]
#set cbr3 [attach-CBR-traffic $n(3) $sink0 1000 .015]
#set cbr4 [attach-CBR-traffic $n(4) $sink3 1000 .015]
#set cbr5 [attach-CBR-traffic $n(5) $sink0 1000 .015]



$ns at 0.0 "record"
#$ns at 0.5 "$cbr0 start"
#$ns at 0.5 "$cbr2 start"
#$ns at 2.0 "$cbr0 stop"
#$ns at 2.0 "$cbr2 stop"
$ns at 1.0 "$cbr0 start"
#$ns at 4.0 "$cbr3 stop"

$ns at 10.0 "finish"

puts "Start of simulation.."
$ns run

AODV protocol ns2


if {$argc !=3} {
        puts "Usage: ns adhoc.tcl  Routing_Protocol Traffic_Pattern Scene_Pattern "
        puts "Example:ns adhoc.tcl AODV cbr-50-10-8 scene-50-0-20"
        exit
}

set par1 [lindex $argv 0]
set par2 [lindex $argv 1]
set par3 [lindex $argv 2]

set val(chan)           Channel/WirelessChannel    ;# channel type
set val(prop)           Propagation/TwoRayGround   ;# radio-propagationmodel
set val(netif)            Phy/WirelessPhy            ;# network interface type
set val(mac)            Mac/802_11                 ;# MAC type

if { $par1=="DSR"} {
  set val(ifq)           CMUPriQueue
} else {
  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(rp)             $par1                       ;# routing protocol
set val(x)                   500
set val(y)                500
set val(seed)               0.0
set val(tr)                aodv.tr
set val(nn)             50
set val(cp)                $par2
set val(sc)        $par3
set val(stop)              100.0

set ns_              [new Simulator]

set tracefd     [open $val(tr) w]
$ns_ trace-all $tracefd
$ns_ use-newtrace

set topo       [new Topography]
$topo load_flatgrid $val(x) $val(y)

set god_ [create-god $val(nn)]

set chan_1_ [new $val(chan)]

        $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_1_ \
                         -topoInstance $topo \
                         -agentTrace ON \
                         -routerTrace ON \
                         -macTrace OFF                                       

        for {set i 0} {$i < $val(nn) } {incr i} {
                set node_($i) [$ns_ node]
                $node_($i) random-motion 0            ;# disable random motion
        }

puts "Loading connection pattern..."
source $val(cp)

puts "Loading scenario file..."
source $val(sc)

for {set i 0} {$i < $val(nn) } {incr i} {
    $ns_ initial_node_pos $node_($i) 20
}

for {set i 0} {$i < $val(nn) } {incr i} {
    $ns_ at $val(stop).000000001 "$node_($i) reset";
}

$ns_ at $val(stop).000000001 "puts \"NS EXITING...\"; $ns_ halt"
puts "Start Simulation..."
$ns_ run