#routeDV.tcl

set ns [new Simulator]

set nf [open routeDV.nam w]
$ns namtrace-all $nf

#Tutti i nodi utilizzeranno il DV routing
$ns rtproto DV

proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam routeDV.nam &
        exit 0
}

#Creazione della topologia
for  {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
}

for  {set i 0 } {$i < 6} {incr i} {
    $ns duplex-link $n($i) $n([expr ($i+1)%6]) 1Mb 10ms DropTail
}

$ns duplex-link $n(0) $n(4) 1Mb 10ms DropTail

#La metrica del DV implementato su NS e' basata sui costi e, 
#a parita' di costo, sul numero di hop
$ns cost $n(0) $n(1) 2
$ns cost $n(1) $n(0) 2
$ns cost $n(1) $n(2) 1
$ns cost $n(2) $n(1) 1
$ns cost $n(2) $n(3) 5
$ns cost $n(3) $n(2) 5
$ns cost $n(3) $n(4) 10
$ns cost $n(4) $n(3) 10
$ns cost $n(4) $n(5) 10
$ns cost $n(5) $n(4) 10
$ns cost $n(5) $n(0) 10
$ns cost $n(0) $n(5) 10
$ns cost $n(4) $n(0) 10
$ns cost $n(0) $n(4) 10

#Il livello di trasporto
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
$udp0 set fid_ 0
$ns color 0 darkgreen
 
set udp1 [new Agent/CBR]
$ns attach-agent $n(1) $udp1
$udp1 set fid_ 1
$ns color 1 red 

set null4 [new Agent/Null]
$ns attach-agent $n(4) $null4

$ns connect $udp0 $null4
$ns connect $udp1 $null4


#Il livello di applicazione
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0

set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1

#Commenti
puts "\n\n##### COMMENTI #####\n"
puts "Scambio iniziale di DV"
puts "0.5 : cbr0 starts"
puts "0.7 : cbr1 starts"
puts "1.1 : link 0-4 down"
puts "2   : link 0-4 up\n"

#Temporizzazione della simulazione
$ns at 0.5 "$cbr0 start"
$ns at 0.7 "$cbr1 start"
$ns rtmodel-at 1.1 down $n(0) $n(4)
$ns rtmodel-at 2 up  $n(0) $n(4)
$ns at 3 "finish"

$ns run 





