#!/bin/bash
# check that iperf -s (is running)


function mode_description {
	mode=$1
	desc=$2
	case $mode in
	"1")
		desc=" tcp stream without forced interruption"
		;;
	"2")
		desc=" tcp stream with short ping flood interruption"
		;;
	"2b")
		desc=" tcp stream with multiple short ping flood interruption"
		;;
	"3")
		desc=" two competing tcp streams started at different times"
		;;
	*)
		desc=" invalid mode=$mode";
		;;
	esac
}

function measure_a_run {
	proto=$1
	loss=$2
	delay=$3
	mode=$4

	tracename=proto-$proto.mode-$mode
	echo "" > myTcpProbeData
	cat /proc/net/tcpprobe > myTcpProbeData &
	cat_pid=$!

	tcpdump -w myTcpDumpData -i eth0 &

	case $mode in
	"1")
	# mode=1 ein tcp stream ohne unterbrechung
	iperf -t 140 -c 10.0.2.1 &
	pid1=$!
	wait $pid1
	;;

	"2")
	# mode=2 ein tcp stream mit kurzer ping flood unterbrechung
	iperf -t 140 -c 10.0.2.1 &
	pid1=$!

	sleep 15
	ping -f 10.0.2.1 &
	sleep 5
	killall ping
	wait $pid1
	;;

	"2b")
	# mode=2 ein tcp stream mit kurzer ping flood unterbrechung
	iperf -t 140 -c 10.0.2.1 &
	pid1=$!

	sleep 25
	ping -f 10.0.2.1 &
	sleep 5
	killall ping

	sleep 25
	ping -f 10.0.2.1 &
	sleep 10
	killall ping

	sleep 25
	ping -f 10.0.2.1 &
	sleep 3
	killall ping

	wait $pid1
	;;

	"3")
	# mode=3 zwei tcp streams, der zweite geht erst nach 15 sek an
	iperf -t 140 -c 10.0.2.1 &
	pid1=$!
	sleep 25
	iperf -t 80 -c 10.0.2.1 &
	pid2=$!

	wait $pid1
	wait $pid2
	# filter tcpprobe log by pid
	;;

	*)
	echo "error no case matched"
	;;
	esac

	kill $cat_pid
	killall tcpdump
	mkdir -p traces/$tracename

	desc=""
	mode_description $mode ${desc}
	./z myTcpProbeData ${proto}'; '${desc}
	mv *.pdf traces/$tracename
	mv myTcpDumpData traces/$tracename/myTcpDumpData.txt
	mv myTcpProbeData traces/$tracename/myTcpProbeData.pcap
}

# previous session kill
killall cat
rm -Rf traces

# alle modes mit:
#  - cubic reno bic westwood highspeed hybla htcp vegas veno scalable lp yeah illinois

# optional (not implemented yet)
# alle versuche mit paket loss und ohne
# alle versuche mit unterschiedlicher RTT
for i in cubic reno bic westwood highspeed hybla htcp vegas veno scalable lp yeah illinois; do
	sysctl -w net.ipv4.tcp_congestion_control=$i
	for m in "1" "2" "2b" "3"; do
		measure_a_run $i undefined undefined $m
	done
done

#tools used:
# - iperf
# - gnuplot mit z script
# - wireshark (stevens trace)
# - alle traces aufheben und archivieren
# - versuchsaufbau dokumentieren
# - pdf
