

check_osplhome()
{
   echo "=== OSPL_HOME is "${OSPL_HOME}
   echo "=== OSPL_URI is "${OSPL_URI}
}

startOSPL()
{
   if [ "$EXRUNTYPE" = "shm" ]
   then
      echo "Starting OSPL"
      $VG_OSPL_START ospl start
      $VG_START_SLEEP
   fi
}

stopOSPL()
{
   if [ "$EXRUNTYPE" = "shm" ]
   then
      echo "Stopping OSPL"
      $VG_OSPL_STOP ospl stop
      $VG_STOP_SLEEP
   fi
}

get_libjsig()
{
   if [ -z "$JAVA_HOME" ]; then
      echo "WARNING: JAVA_HOME not defined. Cannot find libjsig.so and set LD_PRELOAD"
      echo "         This could prevent cleanup of Java application at exit"
   else
      OSPL_JSIG=`find $JAVA_HOME -follow -name libjsig.so | grep -v server/libjsig.so | grep -v client/libjsig.so`

      if [ -z "$OSPL_JSIG" ]; then
         echo "WARNING: Unable to find libjsig.so in JAVA_HOME: $JAVA_HOME"
         echo "         This could prevent cleanup of Java application at exit"
      fi
   fi
}

waitForProcessTermination()
{
   PROCESS=$1
   TIMEOUT=$2

   # This will wait for all processes of the given name to
   # have terminated (so, it's not limited to one process)
   # or when the timout has been reached

   # Start with expecting the process to be available
   AVAILABLE="process available"

   PSARGS=""
   # for testing on WRLinux 7
   #ps -h 2>&1 |  grep 'BusyBox v1.22.1 (2016-02-19 09:26:32 CET) multi-call binary.' > /dev/null
   #if [ $? != 0 ]
   #then
   #   # for testing on linux on mpc8313e
   #   ps -h 2>&1 |  grep 'BusyBox v1.14.1 (2015-09-23 16:49:28 CEST) multi-call binary'  > /dev/null
   #   if [ $? != 0 ]
   #   then
   #       # for testing on linux on armv7a_vfp_neon
   #       ps -h 2>&1 |  grep 'BusyBox v1.22.1 (2015-10-16 20:13:35 BST) multi-call binary.'  > /dev/null
   #       if [ $? != 0 ]
   #       then
   #          PSARGS="-o args"
   #       fi
   #   fi
   #fi

   # Do as long as the process is available or until timeout
   while [ ! "x$AVAILABLE" = "x" -a ! "$TIMEOUT" = "0" ]
   do
      TIMEOUT=`expr $TIMEOUT - 1`
      AVAILABLE=`ps $PSARGS | grep $PROCESS | grep -v grep`

      # Also sleep 1 when process was terminated to allow
      # for any piping to finish
      sleep 1
   done

   if [ ! "x$AVAILABLE" = "x" ] ; then
         echo "WARNING: Process $PROCESS not terminated within timeout!"
         kill -9 $1 > /dev/null
   fi
}

runZero()
{
   NAME=$1
   SUB_PARAMS=$2
   PUB_PARAMS=$3

   echo "=== Launching $NAME "

   ./subscriber > subResult.txt $SUB_PARAMS 2>&1 &
   cmd_pid=$!

   sleep 7

   ./publisher > pubResult.txt $PUB_PARAMS 2>&1
   PUB_RESULT=$?

   waitForProcessTermination $cmd_pid 10

   sleep 5
}

runZeroThroughput()
{
   echo "=== Launching Throughput "

   ./publisher 1 0 1 15 > pubResult.txt 2>&1 &
   cmd_pid=$!

   ./subscriber > subResult.txt 10 2>&1
   SUB_RESULT=$?

   waitForProcessTermination $cmd_pid 30

   sleep 5
}

runZeroStreamsThroughput()
{
   echo "=== Launching Throughput "

   ./publisher 1 10 0 15 > pubResult.txt 2>&1 &

   ./subscriber > subResult.txt 10 10 2>&1 &
   cmd_pid=$!
   SUB_RESULT=$?

   (sleep 40 ; echo "timeout: kill sub"; kill -9 $cmd_pid) &
   killerPid=$!

   (kill -0 $killerPid 2>/dev/null && kill $killerPid) || true

   sleep 5
}

runZeroThroughputJava()
{
   get_libjsig

   echo "=== Launching Throughput "

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -classpath $OSPL_HOME/jar/dcpssaj.jar:classes:$SPLICE_EXTRA_CP publisher 1 0 1 15 > pubResult.txt 2>&1 &
   cmd_pid=$!

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -classpath $OSPL_HOME/jar/dcpssaj.jar:classes:$SPLICE_EXTRA_CP subscriber > subResult.txt 10 2>&1
   SUB_RESULT=$?

   wait $cmd_pid &> /dev/null
   PUB_RESULT=$?

   sleep 5
}

runZeroThroughputJava5()
{
   get_libjsig

   echo "=== Launching Throughput "

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -jar pub/java5_Throughput_pub.jar 1 0 1 15 > pubResult.txt 2>&1 &
   cmd_pid=$!

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -jar sub/java5_Throughput_sub.jar > subResult.txt 10 2>&1
   SUB_RESULT=$?

   wait $cmd_pid &> /dev/null
   PUB_RESULT=$?

   sleep 5
}

runZeroRoundTrip()
{
   echo "=== Launching RoundTrip "

   ./pong > pongResult.txt 2>&1 &
   cmd_pid=$!

   sleep 12

   ./ping > pingResult.txt 100 0 10 2>&1
   PUB_RESULT=$?

   sleep 5

   ./ping quit >/dev/null
   SUB_RESULT=$?

   waitForProcessTermination $cmd_pid 30

   sleep 5
}

runZeroRoundTripJava()
{
   get_libjsig

   echo "=== Launching RoundTrip "

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -classpath $OSPL_HOME/jar/dcpssaj.jar:classes:$SPLICE_EXTRA_CP pong > pongResult.txt 2>&1 &
   cmd_pid=$!

   sleep 10

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -classpath $OSPL_HOME/jar/dcpssaj.jar:classes:$SPLICE_EXTRA_CP ping > pingResult.txt 100 0 10 2>&1
   PUB_RESULT=$?

   sleep 5

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -classpath $OSPL_HOME/jar/dcpssaj.jar:classes:$SPLICE_EXTRA_CP ping quit >/dev/null
   SUB_RESULT=$?

   sleep 5
}
runZeroRoundTripJava5()
{
   get_libjsig

   echo "=== Launching RoundTrip "

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -jar pong/java5_pong.jar > pongResult.txt 2>&1 &
   cmd_pid=$!

   sleep 10

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -jar ping/java5_ping.jar > pingResult.txt 100 0 10 2>&1
   PUB_RESULT=$?

   sleep 5

   LD_PRELOAD=$OSPL_JSIG $SPLICE_JAVA -jar ping/java5_ping.jar quit >/dev/null
   SUB_RESULT=$?

   sleep 5
}


checkResultZero()
{
   SUB_RESULT=$1
   PUB_RESULT=$2
   NAME=$3

   echo "=== Checking $NAME results"

   if [ $SUB_RESULT -ne 0 ] || [ $PUB_RESULT -ne 0 ]
   then
      echo NOK
      echo "*** ERROR : example $NAME failed "
   else
      echo OK
   fi
}

builtintopicsCheckResult()
{
   echo "=== Checking BuiltInTopics Subscriber results"
   NB_HOST=`grep "Hostname for node" subResult.txt | wc -l`;
   echo NB_HOST=$NB_HOST;
   if [ $NB_HOST -eq 0 ];then
      echo NOK
      echo "*** ERROR : example BuiltInTopics failed ";
      cat subResult.txt;
   else
      echo OK
   fi
}

contentfilteredtopicCheckResult()
{
   echo "=== Checking ContentFilteredTopic Subscriber results"
   NB_GE=`grep GE subResult.txt | wc -l`; echo NB_GE=$NB_GE
   NB_MSFT=`grep MSFT subResult.txt | wc -l`; echo NB_MSFT=$NB_MSFT
   if [ $NB_GE -gt 1 ] && [ $NB_MSFT -eq 0 ] ;then
      echo OK
   else
      echo NOK
      echo "*** ERROR : example ContentFilteredTopic failed ";
      cat subResult.txt;
   fi
}

durabilityCheckResult()
{
   echo "=== Checking Durability Subscriber results Scenario 3.1"
   grep "^[0-9]" subResult_3_1.txt > tail_subResult.txt

   diff -w tail_subResult.txt ../../expected_result > subdiff.txt
   if [ -s subdiff.txt ] || [ ! -r subResult_3_1.txt ];then
      echo NOK
      echo "*** ERROR : example Durability failed ";
      cat subdiff.txt;
   else
      echo OK
   fi

   echo "=== Checking Durability second Subscriber results Scenario 3.2"
   # Checking only result of second subscriber
   grep "^[0-9]" subResult_3_2_2.txt > tail_subResult.txt

   diff -w tail_subResult.txt ../../expected_result > subdiff.txt
   if [ -s subdiff.txt ] || [ ! -r subResult_3_2_2.txt ];then
      echo NOK
      echo "*** ERROR : example Durability failed ";
      cat subdiff.txt;
   else
      echo OK
   fi

   echo "=== Checking Durability second Subscriber results Scenario 3.3"
   echo "    (not empty after restarting OpenSplice)"
   # Checking only result of second subscriber
   grep "^[0-9]" subResult_3_3_2.txt > tail_subResult.txt
   if [ -s tail_subResult.txt ];then
      echo OK
   else
      echo NOK
      echo "*** ERROR : example Durability failed ";
   fi
}

durabilityCheckResultISOCPP()
{
   echo "=== Checking Durability Subscriber results Scenario 3.1"
   grep "^[0-9]" subResult_3_1.txt > tail_subResult.txt

   diff -w tail_subResult.txt ../expected_result > subdiff.txt
   if [ -s subdiff.txt ] || [ ! -r subResult_3_1.txt ];then
      echo NOK
      echo "*** ERROR : example Durability failed ";
      cat subdiff.txt;
   else
      echo OK
   fi

   echo "=== Checking Durability second Subscriber results Scenario 3.2"
   # Checking only result of second subscriber
   grep "^[0-9]" subResult_3_2_2.txt > tail_subResult.txt

   diff -w tail_subResult.txt ../expected_result > subdiff.txt
   if [ -s subdiff.txt ] || [ ! -r subResult_3_2_2.txt ];then
      echo NOK
      echo "*** ERROR : example Durability failed ";
      cat subdiff.txt;
   else
      echo OK
   fi

   echo "=== Checking Durability second Subscriber results Scenario 3.3"
   # Checking only result of second subscriber
   grep "^[0-9]" subResult_3_3_2.txt > tail_subResult.txt

   diff -w tail_subResult.txt ../expected_result > subdiff.txt
   if [ -s subdiff.txt ] || [ ! -r subResult_3_3_2.txt ];then
      echo NOK
      echo "*** ERROR : example Durability failed ";
      cat subdiff.txt;
   else
      echo OK
   fi
}

durabilitySetOSPLURI()
{
   if [ "$EXRUNTYPE" = "shm" ]
   then
      OSPL_URI=file://$OSPL_HOME/examples/dcps/Durability/ospl_shm.xml
   else
      OSPL_URI=file://$OSPL_HOME/examples/dcps/Durability/ospl_sp.xml
   fi

   export OSPL_URI
}

helloworldCheckResult()
{
   echo "=== Checking HelloWorld Subscriber results"
   head -n4 subResult.txt > result.txt
   diff -w result.txt ../../expected_results/subResult.txt > subdiff.txt

   if [ -s subdiff.txt ] || [ ! -r subResult.txt ];then
      echo NOK
      echo "*** ERROR : example HelloWorld failed ";
      cat subdiff.txt;
   else
      echo OK
   fi
}

lifecycleCheckResult()
{
   #step 1
   echo "=== Checking Lifecycle Subscriber results"
   step_1_1=`grep "sample_state:NOT_READ_SAMPLE_STATE-view_state:NEW_VIEW_STATE-instance_state:ALIVE_INSTANCE_STATE" subResult_1.txt |wc -l`;
   step_1_2=`grep "sample_state:READ_SAMPLE_STATE-view_state:NOT_NEW_VIEW_STATE-instance_state:ALIVE_INSTANCE_STATE" subResult_1.txt |wc -l`;
   step_1_3=`grep "sample_state:READ_SAMPLE_STATE-view_state:NOT_NEW_VIEW_STATE-instance_state:NOT_ALIVE_DISPOSED_INSTANCE_STATE" subResult_1.txt |wc -l`;
   echo step_1_1=$step_1_1;
   echo step_1_2=$step_1_2;
   echo step_1_3=$step_1_3;
   #step 2
   step_2_1=`grep "sample_state:NOT_READ_SAMPLE_STATE-view_state:NEW_VIEW_STATE-instance_state:ALIVE_INSTANCE_STATE" subResult_2.txt |wc -l`;
   step_2_2=`grep "sample_state:READ_SAMPLE_STATE-view_state:NOT_NEW_VIEW_STATE-instance_state:ALIVE_INSTANCE_STATE" subResult_2.txt |wc -l`;
   # ***** strange to get this step : after unregistering the instance, view_state becomes NEW_VIEW_STATE ********
   step_2_3=`grep "sample_state:READ_SAMPLE_STATE-view_state:NEW_VIEW_STATE-instance_state:NOT_ALIVE_NO_WRITERS_INSTANCE_STATE" subResult_2.txt |wc -l`;
#
   step_2_4=`grep "sample_state:READ_SAMPLE_STATE-view_state:NOT_NEW_VIEW_STATE-instance_state:NOT_ALIVE_NO_WRITERS_INSTANCE_STATE" subResult_2.txt |wc -l`;
   echo step_2_1=$step_2_1;
   echo step_2_2=$step_2_2;
   echo step_2_3=$step_2_3;
   echo step_2_4=$step_2_4;
   #step 3
   step_3_1=`grep "sample_state:NOT_READ_SAMPLE_STATE-view_state:NEW_VIEW_STATE-instance_state:ALIVE_INSTANCE_STATE" subResult_3.txt |wc -l`;
   step_3_2=`grep "sample_state:READ_SAMPLE_STATE-view_state:NOT_NEW_VIEW_STATE-instance_state:ALIVE_INSTANCE_STATE" subResult_3.txt |wc -l`;
   step_3_3=`grep "sample_state:READ_SAMPLE_STATE-view_state:NOT_NEW_VIEW_STATE-instance_state:NOT_ALIVE_NO_WRITERS_INSTANCE_STATE" subResult_3.txt |wc -l`;
   echo step_3_1=$step_3_1;
   echo step_3_2=$step_3_2;
   echo step_3_3=$step_3_3;

   if [ $step_1_1 -eq 0 ] || [ $step_1_2 -eq 0 ] || [ $step_1_3 -eq 0 ] ;then
      echo NOK
      echo "*** ERROR : example Lifecycle failed (step 1)";
      cat subResult_1.txt;
   elif  [ $step_2_1 -eq 0 ] || [ $step_2_2 -eq 0 ] || [ $step_2_4 -eq 0 ];then
      echo NOK
      echo "*** ERROR : example Lifecycle failed (step 2)";
      cat subResult_2.txt;
   elif  [ $step_3_1 -eq 0 ] || [ $step_3_2 -eq 0 ] || [ $step_3_3 -eq 0 ] ;then
      echo NOK
      echo "*** ERROR : example Lifecycle failed (step 3)";
      cat subResult_3.txt;
   else
      echo OK
   fi
}

listenerCheckResult()
{
   echo "=== Checking Listener Subscriber results"
   n1=`grep "message received" subResult.txt  |wc -l`;
   n2=`grep "userID" subResult.txt  |wc -l`;
   n3=`grep "Message :" subResult.txt  |wc -l`;
   echo === n1=$n1 n2=$n2 n3=$n3
   if [ $n1 -eq 0 ] || [ $n2 -eq 0 ] || [ $n3 -eq 0 ] || [ ! -r subResult.txt ];then
      echo NOK
      echo "*** ERROR : example Listener failed ";
      cat subdiff.txt;
   else
      echo OK
   fi
}

ownershipCheckResult()
{
   echo "=== Checking Ownership Subscriber results"
   NB_PUB1=`grep pub1 subResult.txt | wc -l`; echo NB_PUB1=$NB_PUB1
   NB_PUB2=`grep pub2 subResult.txt | wc -l`; echo NB_PUB2=$NB_PUB2
   if [ $NB_PUB1 -eq 0 ] || [ $NB_PUB2 -eq 0 ] ;then
      echo NOK
      echo "*** ERROR : example Ownership failed ";
      cat subResult.txt;
   else
      echo OK
   fi
}

queryconditionCheckResult()
{
   echo "=== Checking QueryCondition Subscriber results"
   NB_GE=`grep GE: subResult.txt | wc -l`; echo NB_GE=$NB_GE
   NB_MSFT=`grep MSFT subResult.txt | wc -l`; echo NB_MSFT=$NB_MSFT
   if [ $NB_GE -eq 0 ] && [ $NB_MSFT -gt 2 ] ;then
      echo OK
   else
      echo NOK
      echo "*** ERROR : example QueryCondition failed ";
      cat subResult.txt;
   fi
}

waitsetCheckResult()
{
   echo "=== Checking WaitSet Subscriber results"
   JOINED=`grep "a MsgWriter joined" subResult.txt | wc -l`;
   FIRST_HELLO=`grep 'Message : "First Hello"' subResult.txt | wc -l`;
   HELLO_AGAIN=`grep 'Message : "Hello again"' subResult.txt | wc -l`;
   LEFT=`grep "a MsgWriter lost its liveliness" subResult.txt | wc -l`;
   ESCAPE=`grep "escape condition triggered" subResult.txt | wc -l`;
   echo JOINED=$JOINED;
   echo FIRST_HELLO=$FIRST_HELLO;
   echo "HELLO_AGAIN(with QueryCondition)"=$HELLO_AGAIN;
   echo LEFT=$LEFT;
   echo ESCAPE=$ESCAPE;
   if [ $FIRST_HELLO -eq 0 ] || [ $HELLO_AGAIN -eq 0 ] || [ $JOINED -eq 0 ] || [ $LEFT -eq 0 ]  || [ $ESCAPE -eq 0 ];then
      echo NOK
      echo "*** ERROR : example WaitSet failed ";
      cat subResult.txt;
   else
      echo OK
   fi
}

runPingPong()
{
   BLOKSIZE=100
   BLOKCOUNT=100

   $VG_PONG ./pong PongRead PongWrite &
   cmd_pid=$!

   sleep 4

   $VG_PING_M ./ping $BLOKCOUNT $BLOKSIZE m PongRead PongWrite

   sleep 2

   $VG_PING_Q ./ping $BLOKCOUNT $BLOKSIZE q PongRead PongWrite

   sleep 2

   $VG_PING_S ./ping $BLOKCOUNT $BLOKSIZE s PongRead PongWrite

   sleep 2

   $VG_PING_F ./ping $BLOKCOUNT $BLOKSIZE f PongRead PongWrite

   sleep 2

   $VG_PING_B ./ping $BLOKCOUNT $BLOKSIZE b PongRead PongWrite

   sleep 2

   $VG_PING_T ./ping 1  10 t PongRead PongWrite

   sleep 4

   waitForProcessTermination $cmd_pid 30
}

runTutorial()
{
   echo start MessageBoard
   $VG_MESSAGEBOARD ./MessageBoard&
   mboard_pid=$!
   sleep 7

   echo start UserLoad
   $VG_USERLOAD ./UserLoad&
   uload_pid=$!

   sleep 2

   echo start Chatter
   $VG_CHATTER ./Chatter

   sleep 4

   echo start Chatter with terminate message
   $VG_CHATTER_T ./Chatter -1

   sleep 4

   waitForProcessTermination $mboard_pid 30
   waitForProcessTermination $uload_pid 30
}
