#!/bin/bash
#
# Scott Burleigh
# adapted from a test written by Sotirios-Angelos Lenas
# August 23, 2012
#
# documentation boilerplate
CONFIGFILES=" \
./1.ipn.bss/amroc.bprc \
./1.ipn.bss/amroc.owltsim \
./1.ipn.bss/amroc.ionconfig \
./1.ipn.bss/amroc.ionrc \
./1.ipn.bss/amroc.ionsecrc \
./1.ipn.bss/amroc.ipnrc \
./2.ipn.bss/amroc.bprc \
./2.ipn.bss/amroc.ionconfig \
./2.ipn.bss/amroc.ionrc \
./2.ipn.bss/amroc.ionsecrc \
./2.ipn.bss/amroc.ipnrc
"

echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Testing the functionality of issue 311: Bundle Streaming Service.
Tests BSS forwarding mechanism and BSS API:

Bundle Streaming Service, is an architecture specifically developed to 
support real-time streaming of multimedia content in DTN environments.
BSS architecture comprises two parts: (a) the BSS forwarding mechanism
which is responsible for forwarding BSS traffic in a specialized manner
and (b) the BSS API which provides developer-friendly control over 
the stream reception procedures by implementing functions that are able 
to create BSS customized databases, control BSS receiving operations and 
perform several stream parsing tasks.

	a.  Creation of a customized BSS database, to store the received 
	    frames, by using BSS API functions.
	    
	b.  Under normal operation (real-time mode), BSS forwards data 
	    through an unreliable duct. In case of frame losses, BSS 
	    reforwards the frames for which it didn't receive a custody 
	    acceptance signal through a reliable duct.

	c.  Proper operation of stream parsing functions.

This test simulates the streaming of a HD video by sending a stream of
1000 bundles from node 1 to node 2. The communication channel between
node 1 and node 2 presents packet losses (on the order of 10%); the
playback mode of the BSS forwarding mechanism is activated in order for
these losses to be recovered. At the receiver's end, the frames that
arrive in-order are counted while the out-of-order frames are stored in
the BSS database.  Every 5 seconds the receiver checks to see if all
1000 frames have been received into the database in correct transmission
order; when this has been accomplished, the total numbers of frames
received in real time and in replay are printed and the test terminates."

echo
echo "CONFIG: 2 node custom BSS:"
echo
for N in $CONFIGFILES
do
	echo "$N:"
	cat $N
	echo "# EOF"
	echo
done
echo "OUTPUT: Terminal messages will relay results."
echo
echo "########################################"

./cleanup
sleep 1
echo "Starting ION..."
export ION_NODE_LIST_DIR=$PWD
rm -f ./ion_nodes
RETVAL=0 
# Start nodes.
cd 1.ipn.bss
./ionstart >& node1.stdout
cd ../2.ipn.bss
./ionstart >& node2.stdout

# Start BSS receiver on node 2.
cd ../2.ipn.bss
echo
echo "Starting bsscounter application..."
bsscounter 1000 bssDB ../ ipn:2.71 > bsscounter.output &
sleep 2

cd ../1.ipn.bss
echo
echo "Sending a stream of 1000 bundles from node 1 to node 2..."
bssdriver ipn:1.2 ipn:2.71 1000

# Allow time for custodial retransmission to be completed.
sleep 5

echo ""
cd ../2.ipn.bss
COUNT=`grep succeeded bsscounter.output | wc -l`
if [ $COUNT -eq 1 ]
then
	echo "BSS test succeeded."
else
	echo "BSS test failed."
	RETVAL=1
fi

# Shut down ION processes.
echo
echo "Stopping ION..."
cd ../1.ipn.bss
./ionstop &
cd ../2.ipn.bss
./ionstop &

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