#route_asym.tcl

#Mettendo a 2 il costo del link 4->3 i cammini minimi tra n1 ed n4 sono 
#diversi:
# n1-n4: 1-0-4
# n4-n1: 4-3-2-1 

set ns [new Simulator]

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

#Impostazione del protocollo di routing
$ns rtproto DV
 
proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam route_asym.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

#Costi dei link
$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) 2
$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

#nodo1 :sorgente e destinatario
set udp1 [new Agent/UDP]
$ns attach-agent $n(1) $udp1
$udp1 set fid_ 1
$ns color 1 red

set null1 [new Agent/Null]
$ns attach-agent $n(1) $null1

#nodo4 :sorgente e destinatario
set udp4 [new Agent/UDP]
$ns attach-agent $n(4) $udp4
$udp4 set fid_ 4
$ns color 4 blue 

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

$ns connect $udp1 $null4
$ns connect $udp4 $null1

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

set cbr4 [new Application/Traffic/CBR]
$cbr4 attach-agent $udp4

#Commenti
puts "\n\n##### COMMENTI #####\n"
puts "Scambio iniziale di DV"
puts "0.2 :cbr1 starts"
puts "0.5 :cbr4 starts"
puts "I pacchetti tra i due nodi viaggiano\nsu percorsi\
diversi nei due sensi\n"

#Temporizzazione della simulazione
$ns at 0.2 "$cbr1 start"
$ns at 0.5 "$cbr4 start"
$ns at 2 "finish"

$ns run 


