set ns [new Simulator]

$ns color 0 brown
$ns color 1 red
$ns color 2 blue
$ns color 3 yellow

$ns rtproto DV

set nf [open dv.nam w]
$ns trace-all $nf

set f [open dv.tr w]
$ns trace-all $f

proc finish {} {
        global ns nf
        $ns flush-trace
	#Close the trace file
        close $nf
	#Execute nam on the trace file
        exec nam dv.nam &
        exit 0
}

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n1 5Mb 10ms DropTail
$ns duplex-link $n1 $n2 5Mb 10ms DropTail
$ns duplex-link $n2 $n3 5Mb 10ms DropTail
$ns duplex-link $n3 $n0 2.5Mb 20ms DropTail

############################################
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 3Mb
$cbr0 attach-agent $udp0

set null0 [new Agent/Null]
$ns attach-agent $n3 $null0

$ns connect $udp0 $null0

###########################################

set udp1 [new Agent/UDP]
$ns attach-agent $n0 $udp1

set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 1Mb
$cbr1 attach-agent $udp1

set null1 [new Agent/Null]
$ns attach-agent $n3 $null1

$ns connect $udp1 $null1
############################################

set udp2 [new Agent/UDP]
$ns attach-agent $n0 $udp2

set cbr2 [new Application/Traffic/CBR]
$cbr2 set packetSize_ 500
$cbr2 set interval_ 2Mb
$cbr2 attach-agent $udp2

set null2 [new Agent/Null]
$ns attach-agent $n3 $null2

$ns connect $udp2 $null2
############################################
$udp0 set fid_ 1
$udp1 set fid_ 2
$udp2 set fid_ 3

$ns at 0.5 "$cbr0 start"
$ns at 0.7 "$cbr1 start"
$ns at 0.9 "$cbr2 start"

$ns at 5.0 "finish"

#Run the simulation
$ns run

