#!/usr/bin/env bash
trap 'killm f' EXIT
#
# 132-udplso-tx-rate-limit.sh
# Greg Menke
# July 6, 2010
# copied from 1000.loopback test

# script used in "make test" to ensure ion is functioning properly.
# script is expected to be run by automake, meaning:
# 1: Environment variable "srcdir" exists and points to the root directory of
# the ion source code.
# 2: The current working directory contains the built programs of ion.
CONFIGFILES=" \
./config/ionconfig.nodeconf \
./config/132-udplso-tx-rate-limit.bprc \
./config/132-udplso-tx-rate-limit.ipnrc \
./config/132-udplso-tx-rate-limit.ionsecrc \
./config/132-udplso-tx-rate-limit.ionrc \
./config/132-udplso-tx-rate-limit.ltprc \
"

echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Testing the functionality of issue 132: a transmission rate
    limit on udp link service adapter to ltp.  Beyond the configuration
    options, this is a simple loopback test."
echo
echo "CONFIG: Custom loopback: "
echo
for N in $CONFIGFILES
do
    echo "$N:"
    cat $N
    echo "# EOF"
    echo
done
echo "OUTPUT: Terminal messages will relay results."
echo
echo "########################################"

# file sent over ion
IONSENDFILE=./ionsendfile.dat
IONRECEIVEFILE=./testfile1


echo "Starting ION..."
CONFIGDIR="./config"
ionstart                           \
    -i ${CONFIGDIR}/132-udplso-tx-rate-limit.ionrc \
    -l ${CONFIGDIR}/132-udplso-tx-rate-limit.ltprc \
    -b ${CONFIGDIR}/132-udplso-tx-rate-limit.bprc  \
    -s ${CONFIGDIR}/132-udplso-tx-rate-limit.ionsecrc \
    -p ${CONFIGDIR}/132-udplso-tx-rate-limit.ipnrc


# receive the message and store it in a file via test bundle sink
echo "Starting File receiver..."
bprecvfile ipn:1.2 &
BPRECVFILEPID=$!

# give app some time to start up
sleep 5
#
# create a 1 megabyte file of random data
#
echo "Creating 1 megabyte test file..."
dd if=/dev/urandom of=$IONSENDFILE bs=1024 count=1024
# send the file to the receiver
echo "Sending file (includes 45 sec pause for the limited tx)..."
bpsendfile ipn:1.1 ipn:1.2 $IONSENDFILE &
BPSENDFILEPID=$!

# wait long enough for file to transfer at 80000 bytes/sec (~13s for 1MB)
sleep 45

# kill everything in case it didn't finish properly
echo "Stopping bpsendfile & bprecvfile..."
kill -2 $BPRECVFILEPID $BPSENDFILEPID >/dev/null 2>&1
sleep 1
kill -15 $BPRECVFILEPID $BPSENDFILEPID >/dev/null 2>&1
sleep 1
kill -9 $BPRECVFILEPID $BPSENDFILEPID >/dev/null 2>&1

# shut down ion processes
echo "Stopping ion..."
ionstop

echo "Checking files..."

# compare the sent message to the received one

if ! `cmp -s $IONSENDFILE $IONRECEIVEFILE`; then

    echo "Oh noes, data corruption!"
    echo
    echo "Send file $IONSENDFILE differs from $IONRECEIVEFILE or $IONRECEIVEFILE was not created"
    echo
    RETVAL=1

else
    #sendfilets=`stat --format='%Z' $IONSENDFILE`
    #recvfilets=`stat --format='%Z' $IONRECEIVEFILE`

    sendfilets=`perl -le 'print scalar  ((lstat shift)[9])' $IONSENDFILE`
    recvfilets=`perl -le 'print scalar  ((lstat shift)[9])' $IONRECEIVEFILE`

    tsdiff=$((recvfilets - sendfilets))
    echo "Send file timestamp: $sendfilets  Recv file timestamp: $recvfilets  Difference: ${tsdiff}s"

    if [ $tsdiff -ge 10 ]; then
        echo "bundle transfer successful!"
        RETVAL=0
    else
        echo "Oh noes, data OK but recv file timestamp not 10secs older than send file!"
        RETVAL=1
    fi

fi

killm
exit $RETVAL
