# Create a simulator object
set ns [new Simulator]
# Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
# Open the trace file
set tr [open out.tr w]
$ns trace-all $tr
# Define a finish procedure
proc finish {} {
global nf ns tr
$ns flush-trace
close $tr
exec nam out.nam &
exit 0
}
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create duplex links between nodes
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n3 10Mb 10ms DropTail
$ns duplex-link $n2 $n1 10Mb 10ms DropTail
# Set link orientation
$ns duplex-link-op $n0 $n1 orient right-down
$ns duplex-link-op $n1 $n3 orient right
$ns duplex-link-op $n2 $n1 orient right-up
# Create TCP agent and attach it to node n0
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
# Create FTP application and attach it to the TCP agent
set ftp [new Application/FTP]
$ftp attach-agent $tcp
# Create a TCP sink agent and attach it to node n3
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
# Create UDP agent and attach it to node n2
set udp [new Agent/UDP]
$ns attach-agent $n2 $udp
# Create CBR application and attach it to the UDP agent
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
# Create a Null agent and attach it to node n3
set null [new Agent/Null]
$ns attach-agent $n3 $null
# Connect TCP and UDP agents with their respective receiving agents
$ns connect $tcp $sink
$ns connect $udp $null
# Simulate link failure and recovery
$ns rtmodel-at 1.0 down $n1 $n3
$ns rtmodel-at 2.0 up $n1 $n3
# Set Distance Vector routing protocol
$ns rtproto DV
# Schedule events
$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"
$ns at 5.0 "finish"
# Run the simulation
$ns run