#!/bin/csh -f 

set ns_dir = "../../"
set res_dir = "./"
set util_dir = "../utils/"
# used for computing throughput in Mbps
# assumes packet size of 1000 bytes and simulation time of 10 sec
set c1   = 1250
set c    = 12500
set maxFlows = 32


# simulation duration is assumed 10 sec, packet size 1000 bytes, and link capacity 10 MBps

foreach exp (oneudp alludp)

    if ( ${exp} == "oneudp" ) then
      @ n = $maxFlows - 1
      echo "Experiment: 1 UDP and $n TCPs (Figure 3.b)"
    else
      echo "Experiment: $maxFlows UDPs (Figure 3.a)"
    endif

    foreach type (fifo red fred fredl drr csfq)

    if (-e t) then
      rm t
    endif
    if (-e ${res_dir}SharedLink-${exp}-${type}.dat) then
      rm ${res_dir}SharedLink-${exp}-${type}.dat 
    endif

    echo "  Scheduling discipline: $type"

    # buffer size = 64000 bytes, threshold size = 16000 bytes
    ${ns_dir}ns SharedLink.tcl -exp ${exp} -type ${type} -n ${maxFlows} -buf 64000 -thresh 16000

    foreach nFlow (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) 


      @ n = `awk '{print $1, $2, $3, $4, $8 "a"}' out.tr | grep " 0 1 " | grep "-" | grep " "${nFlow}"a" | wc -l`

      @ idx = $nFlow + 1
      echo $idx $n >> t
    
    end

    # compute throughput in MBps assumming the packet length 1000 bytes and sim. time = 10s
    ${util_dir}op_col t ${res_dir}SharedLink-${exp}-${type}.dat 2 2 div $c1

  end
end


echo "Experiment: 1 TCP and N UDPs (Figure 4)"

foreach type (fifo red fred fredl drr csfq)

  if (-e t) then
    rm t
  endif
  if (-e ${res_dir}SharedLink-onetcp-${type}.dat) then
    rm ${res_dir}SharedLink-onetcp-${type}.dat 
  endif

  echo "  Scheduling discipline: $type"

  foreach numFlows (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32) 

    echo "    N = $numFlows"

    # buffer size = 64 packets, threshold size = 16 packets
    ${ns_dir}ns SharedLink.tcl -exp onetcp -type ${type} -n $numFlows -buf 64000 -thresh 16000
    @ n = `awk '{print $1, $2, $3, $4, $8 "a"}' out.tr | grep " 0 1 " | grep "-" | grep " 0a" | wc -l`
    @ n = $n * $numFlows 

    echo $numFlows $n >> t
    
  end

  # compute throughput in MBps assumming the packet length 1000 bytes and sim. time = 10s
  ${util_dir}op_col t ${res_dir}SharedLink-onetcp-${type}.dat 2 2 div $c

end

rm out.tr
rm t

# use gnuplot to draw figures
echo "making SharedLink-alludp.ps (Figure 3.(a))"
gnuplot SharedLink-alludp.g
echo "making SharedLink-oneudp.ps (Figure 3.(b))"
gnuplot SharedLink-oneudp.g
echo "making SharedLink-onetcp.ps (Figure 4)"
${util_dir}max_2ndcol SharedLink-onetcp-fred.dat SharedLink-onetcp-fredl.dat SharedLink-onetcp-fred1.dat
gnuplot SharedLink-onetcp.g

echo "DONE"




