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

# Basename for the generated files
set filenameBase "S01E02"

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

# Create simulator
set ns [new Simulator]

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

$ns color 0 Blue
$ns color 1 Red
$ns color 2 Yellow
$ns color 3 Green
$ns color 4 Purple
$ns color 5 Pink

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

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

# Nam tracefile
set nf [open $filenameBase.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]
set n7 [$ns node]

# Creating links
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n0 $n2 10Mb 10ms DropTail
$ns duplex-link $n0 $n4 10Mb 10ms DropTail
$ns duplex-link $n0 $n6 10Mb 10ms DropTail
$ns duplex-link $n3 $n1 10Mb 10ms DropTail
$ns duplex-link $n5 $n1 10Mb 10ms DropTail
$ns duplex-link $n7 $n1 10Mb 10ms DropTail
#$ns duplex-link $n0 $n1 10Mb 10ms RED
#$ns duplex-link $n0 $n1 10Mb 10ms SFQ

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

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

#Give node position (for NAM)
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n0 $n1 queuePos 0.5


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

# Number of FTP connections
set numFTP 3
# Setting connections
# Connection 1
set ftpConnections(0,origin) $n2
set ftpConnections(0,destination) $n3
set ftpConnections(0,start) 0.1
set ftpConnections(0,stop) 19.1
# Connection 2
set ftpConnections(1,origin) $n4
set ftpConnections(1,destination) $n5
set ftpConnections(1,start) 0.4
set ftpConnections(1,stop) 19.5
# Connection 3
set ftpConnections(2,origin) $n6
set ftpConnections(2,destination) $n7
set ftpConnections(2,start) 0.7
set ftpConnections(2,stop) 19.7

######################################
# FTP CONNECTION GENERATE            #
######################################

# Setting winfiles for the FTP connections
for {set i 0} {$i < $numFTP} {incr i} {
	set wf($i) [open $filenameBase.FTP.$i.wf w]
}

# Generating FTP Connections
for {set i 0} {$i < $numFTP} {incr i} {
	# Starting TCP Agent
	set tcp($i) [new Agent/TCP]
	#set tcp($i) [new Agent/TCP/Newreno]
	#set tcp($i) [new Agent/TCP/Vegas]
	$ns attach-agent $ftpConnections($i,origin) $tcp($i)

	# Starting Sink agent
	set sink($i) [new Agent/TCPSink]
	$ns attach-agent $ftpConnections($i,destination) $sink($i)

	# Connecting TCP agent to Sink agent
	$ns connect $tcp($i) $sink($i)

	# Setting connection parameters
	$tcp($i) set fid_ $i
	$tcp($i) set packetSize_ 552
	$tcp($i) set window_ 60

	# Starting FTP connection
	set ftp($i) [new Application/FTP]
	$ftp($i) attach-agent $tcp($i)

	# Widow plotting
	$ns at 0.1 "plotWindow $tcp($i) $wf($i)"

	# Timing
	$ns at $ftpConnections($i,start) "$ftp($i) start"
	$ns at $ftpConnections($i,stop) "$ftp($i) stop"
}

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

# End of simulation
$ns at 20.0 "finish"

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

# Procedure for plotting window size
proc plotWindow {tcpSource file} {
	global ns
	set time 0.1
	set now [$ns now]
	set cwnd [$tcpSource set cwnd_]
	puts $file "$now $cwnd"
	$ns at [expr $now+$time] "plotWindow $tcpSource $file"
}

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

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

# Running the network simulator
$ns run

