#######################################
# EXERCISE SETUP                      #
#######################################

# Basename for the generated files
set filenameBase "S03E01"
#set ftpConnection "1"
#set ftpConnection "2"
set ftpConnection "both"
#set ftpCapped "0"
set ftpCapped "1"

#######################################
# SIMULATOR STARTUP                   #
#######################################

# Create simulator
set ns [new Simulator]

#######################################
# COLOR SETUP                         #
#######################################

$ns color 0 Blue
$ns color 1 Red

#######################################
# FILES                               #
#######################################

# ftpInfo for in filename
set ftpInfo FTP.$ftpConnection.Capped.$ftpCapped

# Trace file
set tf [open $filenameBase.$ftpInfo.out.tr w]
$ns trace-all $tf

# Nam tracefile
set nf [open $filenameBase.$ftpInfo.out.nam w]
$ns namtrace-all $nf

#######################################
# NODE SETUP                          #
#######################################

# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]

# Creating links
$ns duplex-link $n0 $n2 10Mb 10ms DropTail
$ns duplex-link $n1 $n2 10Mb 10ms DropTail
$ns simplex-link $n2 $n3 100Kb 10ms DropTail
$ns simplex-link $n3 $n2 512Kb 10ms DropTail
$ns duplex-link $n3 $n4 10Mb 10ms DropTail
$ns duplex-link $n4 $n5 100Mb 1ms DropTail
$ns duplex-link $n4 $n6 100Mb 1ms DropTail

#Queue limiting
#$ns queue-limit $n0 $n1 20

#######################################
# NAM SETUP                           #
#######################################

# Node labels
$n0 label "Normal"
$n1 label "Uploader"
$n2 label "CATV Modem"
$n3 label "CATV Modem"
$n4 label "CATV Head-end"
$n5 label "Server 1"
$n6 label "Server 2"

#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns simplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right
$ns duplex-link-op $n4 $n5 orient right-up
$ns duplex-link-op $n4 $n6 orient right-down

#$ns duplex-link-op $n0 $n1 queuePos 0.5


#######################################
# FTP CONNECTION SETUP		      #
#######################################

if { $ftpConnection == "1" || $ftpConnection == "both" } {
	# Starting TCP Agent
	set tcp1 [new Agent/TCP]
	#set tcp($i) [new Agent/TCP/Newreno]
	#set tcp($i) [new Agent/TCP/Vegas]
	$ns attach-agent $n5 $tcp1

	# Starting Sink agent
	set sink1 [new Agent/TCPSink]
	$ns attach-agent $n0 $sink1

	# Connecting TCP agent to Sink agent
	$ns connect $tcp1 $sink1

	# Setting connection parameters
	$tcp1 set fid_ 0
	$tcp1 set window_ 200

	# Starting FTP connection
	set ftp1 [new Application/FTP]
	$ftp1 attach-agent $tcp1

	# Timing
	$ns at 0.1 "$ftp1 start"
	$ns at 9.9 "$ftp1 stop"
}

if { $ftpConnection == "2" || $ftpConnection == "both" } {
	# Starting TCP Agent
	set tcp2 [new Agent/TCP]
	#set tcp($i) [new Agent/TCP/Newreno]
	#set tcp($i) [new Agent/TCP/Vegas]
	$ns attach-agent $n1 $tcp2

	# Starting Sink agent
	set sink2 [new Agent/TCPSink]
	$ns attach-agent $n6 $sink2

	# Connecting TCP agent to Sink agent
	$ns connect $tcp2 $sink2

	# Setting connection parameters
	$tcp2 set fid_ 1
	if { $ftpCapped == "1" } { 
		$tcp2 set rate_ 10Kb
	} else {
		$tcp2 set window_ 200
	}

	# Starting FTP connection
	set ftp2 [new Application/FTP]
	$ftp2 attach-agent $tcp2

	# Timing
	$ns at 3.0 "$ftp2 start"
	$ns at 6.0 "$ftp2 stop"
}

#######################################
# TIMING                              #
#######################################

# End of simulation
$ns at 10.0 "finish"

#######################################
# FUNCTIONS                           #
#######################################

# Finish function
proc finish {} {
	#finalize trace files
	global ns nf tf filenameBase ftpInfo
	$ns flush-trace
	close $tf
	close $nf
	
	exec nam $filenameBase.$ftpInfo.out.nam &
	exit 0
}

#######################################
# RUNNING SIMULATOR                   #
#######################################

# Running the network simulator
$ns run

