# Create the simulator instance 
set ns [new Simulator] 
$ns color 1 Blue 
$ns color 2 Red 
#Open the NAN trace file 
set nf [open out.nam w] 
$ns namtrace-all $nf 

#Open the Trace file 

set tf [open out.tr w] 
set windowVsTime2 [open WindowVsTimeNReno w]
 $ns trace-all $tf 
#Define a ‘finish’ procedure 
proc finish {} { 
	global ns nf tf 
	$ns flush-trace 
	close $nf 
	close $tf 
	exec nam out.nam & 
	exit 0 
}
#Create four nodes 
set n0 [$ns node] 
set n1 [$ns node] 
set n2 [$ns node] 
set n3 [$ns node] 

$ns at 0.1 "$n1 label \"CBR\""
$ns at 1.0 "$n0 label \"FTP\"" 

#Create links between the nodes 

$ns duplex-link $n0 $n2 2Mb lOms DropTail 
$ns duplex-link $n1 $n2 2Mb lOms DropTail 
$ns simplex-link $n2 $n3 0.07Mb 2Oms DropTail 
$ns simplex-link $n3 $n2 0.07Mb 2Oms DropTail 
#Set Queue Size of link (n2-n3) to 10 
$ns queue-limit $n2 $n3 10 
#Monitor the queue for link (n2-n3). (for NAN)
$ns simplex-link-op $n2 $n3 queuePos 0.5 
#Set error model on link n3 to n2. 
set loss_module [new ErrorModel] 
$loss_module set rate_ 0.2 
$loss_module ranvar [new RandomVariable/Uniform] 
$loss_module drop-target [new Agent/Null] 
$ns lossmodel $loss_module $n2 $n3 

#Setup a TCP connection 
set tcp [new Agent/TCP/Newreno] 
$ns attach-agent $n0 $tcp 
set sink [new Agent/TCPSink/DelAck] 
$ns attach-agent $n3 $sink 
$ns connect $tcp $sink 
$tcp set fid_ 1 
#Setup a FTP over TCP connection 
set ftp [new Application/FTP] 
$ftp attach-agent $tcp 
$ftp set type_ FTP 

#Setup a UDP connection 
set udp [new Agent/UDP] 
$ns attach-agent $n1 $udp 
set null [new Agent/Null] 
$ns attach-agent $n3 $null 
$ns connect $udp $null 
$udp set fid_ 2
 
#Setup a CBR over UDP connection 
set cbr [new Application/Traffic/CBR] 
$cbr attach-agent $udp 
$cbr set type_ CBR 
$cbr set packetSize_ 1000 
$cbr set rate_ 0.01Mb 
$cbr set random_ false 

#Schedule events for the CBR and FTP agents 
$ns at 0.1 "$cbr start" 
$ns at 1.0 "$ftp start" 
$ns at 550.0 "$ftp stop" 
$ns at 600.5 "$cbr stop" 
# Printing the window size 

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

$ns at 1.1 "plotWindow $tcp $windowVsTime2" 
# sample the bottleneck queue every 0.1 sec. store the trace in qrn.out

 set qmon [$ns monitor-queue $n2 $n3 [open qm.out w] 0.1]; 
[$ns link $n2 $n3] queue-sample-timeout; # [$ns link $n2 $n3] start-tracing 

#Detach tcp and sink agents (not really necessary) 
$ns at 600.24 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink" 
$ns at 600.25 "finish" 
$ns run 
