# ################################################
# Make a simulator (scheduler)
  set ns [new Simulator]

# ################################################
#Define different colors for data flows (for NAM)
  $ns color 1 Blue
  $ns color 2 Red


# -------------------------------
# Define a 'finish' procedure
# -------------------------------
  proc finish {} {
     global ns  trace_file1 trace_file2
     $ns flush-trace

     close $trace_file1
     close $trace_file2

     exit 0
  }

# ################################################
# Create this configuration:
#
#            0                 4
#    2Mb/10ms \  0.3Mb/100ms  / 0.5Mb/40ms
#              2 ----------- 3
#   2Mb/500ms /               \ 0.5Mb/30ms
#            1                 5
#
#  TCP1: (t=0)   0 -> 4
#  TCP2: (t=20)  1 -> 5
# ################################################

# ################################################
# Create the 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]

# ################################################
# Create the links:
  $ns duplex-link $n0 $n2   2Mb  10ms DropTail
  $ns duplex-link $n2 $n3 0.3Mb 100ms DropTail
  $ns duplex-link $n3 $n4 0.5Mb  40ms DropTail
  $ns duplex-link $n3 $n5 0.5Mb  30ms DropTail


# -------------------------------------------
# Extra long delay
# -------------------------------------------
  $ns duplex-link $n1 $n2 2Mb 500ms DropTail

# ################################################
#Monitor the queue for link (n0-n1). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.1


# ################################################
# 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 simplex-link-op $n3 $n2 orient left
  $ns duplex-link-op  $n3 $n4 orient right-up
  $ns duplex-link-op  $n3 $n5 orient right-down


# ########################################################
# Set Queue Size of link (n2-n3) to 10 (default is 50 ?)
  $ns queue-limit $n2 $n3 50 

# -----------------------------------------------------
# Setup First TCP connection
#
#
#            0                 4
#    2Mb/10ms \  0.3Mb/100ms  / 0.5Mb/40ms
#              2 ----------- 3
#   2Mb/500ms /               \ 0.5Mb/30ms
#            1                 5
#
#  TCP1: (t=0)   0 -> 4
# -----------------------------------------------------

  set tcp [new Agent/TCP/Reno]

  $tcp set windowOption_ 2 		;# Constant Through increase rate
  $tcp set windowConstant_ 4 		;# XXX - you can change the constant

# puts [$tcp set windowConstant_]

  $ns attach-agent $n0 $tcp

# set sink [new Agent/TCPSink/DelAck]
  set sink [new Agent/TCPSink]
  $ns attach-agent $n4 $sink

  $ns connect $tcp $sink
  $tcp set fid_ 1
  $tcp set window_ 8000
  $tcp set packetSize_ 552

# ########################################################
# Setup a FTP over TCP connection
  set ftp [new Application/FTP]
  $ftp attach-agent $tcp
  $ftp set type_ FTP


# ########################################################
# Schedule start/stop times
  $ns at 0.1 "$ftp start"


# -----------------------------------------------------
# Setup Second TCP connection
#
#
#            0                 4
#    2Mb/10ms \  0.3Mb/100ms  / 0.5Mb/40ms
#              2 ----------- 3
#   2Mb/500ms /               \ 0.5Mb/30ms
#            1                 5
#
#  TCP2: (t=20)   1 -> 5
# -----------------------------------------------------

  set tcp2 [new Agent/TCP/Reno]

  $tcp2 set windowOption_ 2 		;# Constant Through increase rate
  $tcp2 set windowConstant_ 4 		;# XXX - you can change the constant
# puts [$tcp set windowConstant_]

  $ns attach-agent $n1 $tcp2

# set sink2 [new Agent/TCPSink/DelAck]
  set sink2 [new Agent/TCPSink]
  $ns attach-agent $n5 $sink2

  $ns connect $tcp2 $sink2
  $tcp2 set window_ 8000
  $tcp2 set packetSize_ 552

# This give the packet of TCP flow 2 a different color...
  $tcp2 set fid_ 2


# ########################################################
# Setup a FTP over TCP connection
  set ftp2 [new Application/FTP]
  $ftp2 attach-agent $tcp2
  $ftp2 set type_ FTP


# ########################################################
# Schedule start/stop times
  $ns at 20.0 "$ftp2 start"





# -----------------------------------------------------------------
# plotWindow(tcpSource,file): write CWND from $tcpSource
#			      to output file $file every 0.1 sec
# -----------------------------------------------------------------
  proc plotWindow {tcpSource file} {
     global ns

     set time 0.1
     set now [$ns now]
     set cwnd [$tcpSource set cwnd_]
     set wnd [$tcpSource set window_]
     puts $file "$now $cwnd"
     $ns at [expr $now+$time] "plotWindow $tcpSource $file" 
  }

# -------------------------------
# Open the Window plot file
# -------------------------------
  set winfile [open ShortRTTWinFile w]
  set winfile2 [open LongRTTWinFile w]

# -----------------------------------------------------------
# Start plotWindow for TCP 1 and TCP 2
# -----------------------------------------------------------
  $ns at 0.1 "plotWindow $tcp $winfile"
  $ns at 0.1 "plotWindow $tcp2 $winfile2"


##################################################
## Obtain Trace date for throughput
##################################################

set trace_file1 [open  "ShortRTT.tr"  w]
$ns  trace-queue  $n3  $n4  $trace_file1
set trace_file2 [open  "LongRTT.tr"  w]
$ns  trace-queue  $n3  $n5  $trace_file2



# ####################################################################
# Set simulation end time
  $ns at 1000.0 "finish"

# ####################################################################
# Run !!!!
  $ns run
