#!/bin/bash
#
# Scott Burleigh
# February 9, 2013
#
# Tests multiple CGR payload classes.  Four nodes are in a diamond network
# topology 1->(2&3)->4.  There is continuous connectivity between 1 and 2,
# and between 1 and 3, with no connectivity between 2 and 3.  Initially,
# a low-rate contact between 2 and 4 is scheduled for 10 seconds after
# the start of the test, lasting 15 seconds.  Two bundles are sent from
# 1 to 4: a large bundle that cannot be fully transmitted during the
# scheduled contact, followed by a small bundle that can.  The large
# bundle is abandoned: from this contact plan, we can't anticipate being
# able to transmit it before it expires.  The small bundle is forwarded
# to node 2 where it is queued for transmission to node 4.  At the end of
# the 15-second contact, the small bundle has been received.
#
# Now we add two more scheduled contacts, each of 15 seconds: another
# low-rate contact between 2 and 4 plus a high-rate contact (which starts
# 10 seconds later) between 3 and 4.  Again we send a large bundle and a
# small bundle.  But now the large bundle is forwarded to node 3 and
# queued for transmission to 4, because the route through 3 -- though not
# optimal on delivery time -- has sufficient capacity to convey the
# large bundle.  At the end of all contacts, both bundles have been
# received.

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

# Start nodes.
cd 1.ipn.ltp
./ionstart >& node1.stdout &
cd ../2.ipn.ltp
./ionstart >& node2.stdout &
cd ../3.ipn.ltp
./ionstart >& node3.stdout &
cd ../4.ipn.ltp
./ionstart >& node4.stdout &
sleep 10

echo "Inserting first set of contacts...."
cd ../1.ipn.ltp
ionadmin ../global1.ionrc
cd ../2.ipn.ltp
ionadmin ../global1.ionrc
cd ../3.ipn.ltp
ionadmin ../global1.ionrc
cd ../4.ipn.ltp
ionadmin ../global1.ionrc

sleep 2
cd ../1.ipn.ltp
echo "Sending 100000-byte bundle from ipn:1.2 to ipn:4.1...."
bpsendfile ipn:1.1 ipn:4.1 bigFile
echo "Sending 100-byte bundle from ipn:1.2 to ipn:4.1...."
bpsendfile ipn:1.1 ipn:4.1 smallFile
echo "Waiting for contacts to lapse...."
sleep 30
bpstats

echo "Inserting second set of contacts...."
cd ../1.ipn.ltp
ionadmin ../global2.ionrc
cd ../2.ipn.ltp
ionadmin ../global2.ionrc
cd ../3.ipn.ltp
ionadmin ../global2.ionrc
cd ../4.ipn.ltp
ionadmin ../global2.ionrc

sleep 2
cd ../1.ipn.ltp
echo "Sending second 100000-byte bundle from ipn:1.2 to ipn:4.1...."
bpsendfile ipn:1.1 ipn:4.1 bigFile
echo "Sending second 100-byte bundle from ipn:1.2 to ipn:4.1...."
bpsendfile ipn:1.1 ipn:4.1 smallFile
echo "Waiting for contacts to lapse...."
sleep 30
bpstats

# Verify that bundles were received as expected.
RETVAL=0

# Verify at node 2

cd ../2.ipn.ltp
COUNT=`grep rcv ion.log | egrep ".1. 2 200" | wc -l`
if [ $COUNT -lt 1 ]
then
	echo "Error: small bundles not routed through node 2."
	RETVAL=1
fi

# Verify at node 3

cd ../3.ipn.ltp
COUNT=`grep rcv ion.log | egrep ".1. 1 100000" | wc -l`
if [ $COUNT -lt 1 ]
then
	echo "Error: large bundle not routed through node 3."
	RETVAL=1
fi

# Verify at node 4

cd ../4.ipn.ltp
COUNT=`grep rcv ion.log | egrep ".1. 3 100200" | wc -l`
if [ $COUNT -lt 1 ]
then
	echo "Error: bundles not received at node 4."
	RETVAL=1
fi

# Shut down ION processes.
sleep 1
echo "Stopping ION..."
cd ../1.ipn.ltp
./ionstop &
cd ../2.ipn.ltp
./ionstop &
cd ../3.ipn.ltp
./ionstop &
cd ../4.ipn.ltp
./ionstop &

# Give all nodes time to shut down, then clean up.
sleep 5
killm
echo "Payload classes test completed."
exit $RETVAL
