#!/bin/bash
#

total_count=-1

if [ "x$1" != "x" ]; then
    total_count=$1
fi

start_time=$(date +%s)
total_time=0
last_time=$start_time

seq=0
count=0
report=0
mps=0

fqdn=`uname -n`
hostname=${fqdn%%.*}
ip=`ifconfig | grep "inet addr" | head -1`
ip=${ip%%  Bcast*}
ip=${ip#*:}

port=17190

while [ $total_count -lt 0 -o $count -lt $total_count ]
do 
    (( ++seq ))
    
    if [ $seq -eq 65534 ]; then
        seq=1;
    fi
    
    (( ++count ))
    (( result = count % 100 ? 0 : 1 ))
    
    if [ $result -eq 1 ]; then

        now=$(date +%s)

        (( total_time += now - last_time ))

        last_time=$now

        (( mps = count / total_time ))
        
        echo "Total message:      $count"
        echo "Total time:         $total_time"
        echo "Message per second: $mps"
        echo " "
    fi
    

    rcf=$(./ras \
	    --grq \
	    --ras-address $ip:$port \
	    --seq $seq \
	    --terminal \
	    --retry-count 2 \
	    --retry-delay 5 \
            --multicast)

    rc=$?
        
    if [ $rc -ne 0 ]; then
        exit $rc
    fi
done
