#!/bin/bash
#
# Scott Burleigh
# April 21, 2010
# documentation boilerplate
CONFIGFILES=" \
./amroc.ltprc \
./amroc.bprc \
./global.ionrc \
./amroc.ionrc \
./amroc.ionsecrc \
./amroc.ipnrc
"
echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Verify that non-zero loopback one-way light time is supported."
echo
echo "CONFIG: A contact graph with 100-second loopback one-way light time:"
echo
for N in $CONFIGFILES
do
	echo "$N:"
	cat $N
	echo "# EOF"
	echo
done
echo "OUTPUT: Searching for bundle reforwarding messages in ion.log to ensure 
	that non-zero loopback one-way light time is correctly handled."
echo
echo "########################################"

echo "Cleaning up old ION..."
./cleanup
sleep 1

echo "Starting ION..."
export ION_NODE_LIST_DIR=$PWD

# Start node 9, with 20-second loopback range.
./ionstart

# Send a loopback bundle with lifetime = 300.
echo "Sending 10-byte bundle with 5-minute lifetime from ipn:9.1 to ipn:9.2."
bptrace ipn:9.1 ipn:9.2 ipn:9.0 300 0.1 "Hello Bob"
sleep 1
echo "Watch characters should be 'abcdefg'."
sleep 1
# Send a non-loopback bundle with lifetime = 300.
echo "Sending 3-byte bundle with 5-minute lifetime from ipn:9.1 to ipn:5.2."
bptrace ipn:9.1 ipn:5.2 ipn:9.0 300 0.1 "Hi"
sleep 1
echo "Watch characters should be 'abcdefg'."
sleep 1
echo "Both should be forwarded to node 5: the loopback owlt is so long that CGR decides it will be faster to transmit to node 5 and then back again."
sleep 1

# Wait for all sessions to terminate.
echo "Both bundles should be reforwarded on block transmission failure due to excessive ack timeouts (watch character '#'), because even the loopback bundle was sent to nonexistent node 5 due to the length of the loopback RTT."
echo "Waiting for contact to terminate..."
sleep 70
echo "Contact has terminated.  Verifying results..."

# Verify bundle was forwarded.
RETVAL=0

echo "Checking ion.log for 'src' message '(+) 2 13'..."
COUNT=`grep src ion.log | grep "(+) 2 13" | wc -l`
if [ $COUNT -gt 0 ]
then
	echo "OK: All bundles sourced."
else
	echo "ERROR: Bundles not sourced."
	RETVAL=1
fi

echo "Checking ion.log for 'fwd' message '(+) 4 26'..."
COUNT=`grep fwd ion.log | grep "(+) 4 26" | wc -l`
if [ $COUNT -gt 0 ]
then
	echo "OK: All bundles forwarded."
else
	echo "ERROR: Bundles not forwarded."
	RETVAL=1
fi

echo "Checking ion.log for 'rfw' message '(+) 2 13'..."
COUNT=`grep rfw ion.log | grep "(+) 2 13" | wc -l`
if [ $COUNT -gt 0 ]
then
	echo "OK: Both bundles reforwarded."
else
	echo "ERROR: Bundles not reforwarded."
	RETVAL=1
fi

# Shut down ION processes.
echo "Stopping ION..."
./ionstop
echo "Non-zero loopback range test completed."
exit $RETVAL
