#!/bin/bash
#
# Scott Burleigh
# April 21, 2010
# documentation boilerplate
CONFIGFILES=" \
./amroc.ltprc \
./amroc.bprc \
./amroc.ionconfig \
./global.ionrc \
./amroc.ionrc \
./amroc.ionsecrc \
./amroc.ipnrc \
"
echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Test out behavior using asymmetrically configured ranges in the
	contact graph.  Bundle forwarding, reforwarding, and expiration are
	expected."
echo
echo "CONFIG: A contact graph with asymmetric delay between nodes.
	Only one node is actually configured and run in this network:"
echo
for N in $CONFIGFILES
do
	echo "$N:"
	cat $N
	echo "# EOF"
	echo
done
echo "OUTPUT: Searching for bundle forwarding messages in ion.log to ensure 
	that the CGR engine can make correct decisions with asymmetric ranges.
	The terminal should also contain a variety of 'watch' characters for
	further debugging."
echo
echo "########################################"

echo "Cleaning up old ION..."
rm -f ion.log
killm
sleep 1

echo "Starting ION..."
export ION_NODE_LIST_DIR=$PWD
rm -f ./ion_nodes

# Start node 9, with asymmetric range to node 5.
./ionstart

echo "Sending bundle with 10-second lifetime from ipn:9.1 to ipn:6.1."
# Send a bundle destined for (nonexistent) node 6 with lifetime = 10.
echo "Should be abandoned as unroutable prior to expiration."
bptrace ipn:9.1 ipn:6.1 ipn:9.0 10 0.1 "Hi"
sleep 1
echo "Watch characters should be 'a~'."

echo "Sending bundle with 25-second lifetime from ipn:9.1 to ipn:6.1."
# Send a bundle destined for (nonexistent) node 6 with lifetime = 25.
echo "Should be forwarded and transmitted but expire prior to end of session."
bptrace ipn:9.1 ipn:6.1 ipn:9.0 25 0.1 "Hi"
sleep 1
echo "Watch characters should be 'abwcdefg'."

echo "Sending bundle with 120-second lifetime from ipn:9.1 to ipn:6.1."
# Send a bundle destined for (nonexistent) node 6 with lifetime = 120.
echo "Should be forwarded and transmitted, but session will end before ack."
echo "Should be retransmitted and later reforwarded."
bptrace ipn:9.1 ipn:6.1 ipn:9.0 120 0.1 "Hi"

# Wait for all sessions to terminate.
echo "Waiting for contact to terminate..."
sleep 60
echo "Contact has terminated.  Verifying results..."

# Verify bundle was forwarded.
RETVAL=0

echo "Checking ion.log for 'src' message '(+) 3 9'..."
COUNT=`grep src ion.log | grep "(+) 3 9" | 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 '(+) 2 6'..."
COUNT=`grep fwd ion.log | grep "(+) 2 6" | wc -l`
if [ $COUNT -gt 0 ]
then
	echo "OK: Two bundles forwarded."
else
	echo "ERROR: Bundles not forwarded."
	RETVAL=1
fi

echo "Checking ion.log for 'exp' message '(+) 1 3'..."
COUNT=`grep exp ion.log | grep "(+) 1 3" | wc -l`
if [ $COUNT -gt 0 ]
then
	echo "OK: One bundle expired."
else
	echo "ERROR: Bundle not expired."
	RETVAL=1
fi

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

# Shut down ION processes.
echo "Stopping ION..."
ionstop
killm
echo "Asymmetric range test completed."
exit $RETVAL
