#!/bin/bash
#

#set -x 

# usage: rrq [count]
#   
#

#trap 'false' SIGINT

# usage: do_sleep seconds
#
do_sleep()
{
    local time=$1
    if [ x$time = x ]; then
        echo "Error: do_sleep seconds"
        return
    fi
    
    while [ $time -gt 0 ]; do
        sleep 1
        echo -n "."
        (( --time ))
    done
}

# Parse command line
#
total_count=1000

if [ "x$1" != "x" -a $total_count -gt 0 ]; then
    total_count=$1
fi

report_freq=50


# Time keeper vars
#
start_time=$(date +%s)
total_time=0
last_time=$start_time

# Counters
#
total_msg=0
seq=0
count=0
grq_count=0
rrq_count=0
mps=0

# Addresses
#
id=10
fake_d=0
fake_c=0
fake_b=0
addr=17190

per_msg_delay=0

# Endpoint id arry
#
declare endpoints

# Do the GRQ/RRQ for each.
#
while [ $count -lt $total_count ]
do 
    # GRQ
    #
    #############################################################
    
    (( ++seq )); if [ $seq -eq 65534 ]; then seq=1; fi
    (( ++grq_count ))

    gcf=$(./ras \
            --address $addr \
	    --grq \
	    --ras-address $id.$fake_b.$fake_c.$fake_d:1719 \
	    --seq $seq \
	    --gateway \
	    --retry-count 2 \
	    --retry-delay 5 \
            --multicast)
    rc=$?
        
    if [ $rc -eq 13 ]; then
        echo "Error: Discovery timed out"

    elif [ $rc -ne 0 ]; then
        echo "Error: Discovery failed: $rc"
        exit $rc
    fi

    gk_ras_addr=$(echo "$gcf" | grep "RAS Address" | tail -1)
    gk_ras_addr=${gk_ras_addr#*: *}

    gk_id=$(echo "$gcf" | grep "Gatekeeper Id" | tail -1)
    gk_id=${gk_id#*: *}

    echo "Discovered gatekeeper: $gk_id at $gk_ras_addr"

    # RRQ
    #
    #############################################################

    (( ++seq )); if [ $seq -eq 65534 ]; then seq=1; fi
    (( ++rrq_count ))
    
    rcf=$(./ras \
           $gk_ras_addr \
            --rrq \
            --address $addr \
            --sequence-number $seq \
            --discovery-complete \
	    --ras-address $id.$fake_b.$fake_c.$fake_d:1719 \
            --call-sig-address $id.$fake_b.$fake_c.$fake_d:1720 \
            --gateway \
            --gk-id $gk_id \
            --retry-count 2 \
            --retry-delay 3)
    rc=$?

    if [ $rc -eq 13 ]; then
        echo "Error: Registration timed out"

    elif [ $rc -ne 0 ]; then
        echo "Error: Registration failed: $rc"
        exit $rc
    fi

    endpoint_id=$(echo "$rcf" | grep "Endpoint Id" | tail -1)
    endpoint_id=${endpoint_id#*: *}
    endpoints[count]=$endpoint_id
            
    echo "Registered endpoint: ${endpoints[$count]},  $id.$fake_b.$fake_c.$fake_d"

    
    # Update counters
    #
    #############################################################

    (( ++count ))
    (( fake_d = count % 256 ))
    (( fake_c = count / 256 ))
    (( fake_b = fake_c / 256 ))
    (( fake_c = fake_c % 256 ))
    (( fake_b = fake_b % 256 ))
    (( ++addr ))


    # Report
    #
    #############################################################

    (( result = count % $report_freq ? 0 : 1 ))
    if [ $result -eq 1 ]; then

        now=$(date +%s)
        (( total_time += now - last_time ))
        last_time=$now
        (( mps = (grq_count + rrq_count) / total_time ))
        
        echo "Count:              $count"
        echo "GRQ messages:       $grq_count"
        echo "RRQ messages:       $rrq_count"
        echo "Total time:         $total_time"
        echo "Message per second: $mps"
        echo " "
    fi

    usleep $per_msg_delay

done

now=$(date +%s)
(( total_time += now - last_time ))
(( total_time = ( total_time == 0 ) ? 1 : total_time ))
last_time=$now
(( total_msg = grq_count + rrq_count ))
(( mps = total_msg / total_time ))

echo "Total messages:     $total_msg"
echo "GRQ messages:       $grq_count"
echo "RRQ messages:       $rrq_count"
echo "Total time:         $total_time"
echo "Message per second: $mps"
echo " "


(( ttl = total_time + 15 ))
echo "Total time for $total_count registrations: $total_time"
echo "Proposed ttl: $ttl"


count=0
rrq_count=0
fake_d=0
fake_c=0
fake_b=0
addr=17190

# Do RRQ with time to live for each.
#
while [ $count -lt $total_count ]
do 
    # RRQ w/ttl
    #
    #############################################################

    (( ++seq )); if [ $seq -eq 65534 ]; then seq=1; fi
    (( ++rrq_count ))
    
    endpoint_id=${endpoints[$count]}
    rcf=$(./ras \
           $gk_ras_addr \
            --rrq \
            --address $addr \
            --sequence-number $seq \
            --endpoint-id ${endpoint_id} \
            --discovery-complete \
            --time-to-live $ttl \
	    --ras-address $id.$fake_b.$fake_c.$fake_d:1719 \
            --call-sig-address $id.$fake_b.$fake_c.$fake_d:1720 \
            --gateway \
            --gk-id $gk_id \
            --retry-count 2 \
            --retry-delay 3)
    rc=$?

    if [ $rc -eq 13 ]; then
        echo "Error: TTL Registration timed out"

    elif [ $rc -ne 0 ]; then
        echo "Error: TTL Registration failed: $rc"
        exit $rc
    fi

    echo "TTL Registration for endpoint: ${endpoint_id},  $id.$fake_b.$fake_c.$fake_d"

    
    # Update counters
    #
    #############################################################

    (( ++count ))
    (( fake_d = count % 256 ))
    (( fake_c = count / 256 ))
    (( fake_b = fake_c / 256 ))
    (( fake_c = fake_c % 256 ))
    (( fake_b = fake_b % 256 ))
    (( ++addr ))


    # Report
    #
    #############################################################

    (( result = count % $report_freq ? 0 : 1 ))
    if [ $result -eq 1 ]; then

        now=$(date +%s)
        (( total_time += now - last_time ))
        last_time=$now
        (( total_msg = grq_count + rrq_count ))
        (( mps = total_msg / total_time ))
        
        echo "Count:              $count"
        echo "Total messages:     $total_msg"
        echo "TTL RRQ messages:   $rrq_count"
        echo "Total time:         $total_time"
        echo "Message per second: $mps"
        echo " "
    fi

    usleep $per_msg_delay

done


now=$(date +%s)
(( total_time += now - last_time ))
(( total_time = ( total_time == 0 ) ? 1 : total_time ))
last_time=$now
(( total_msg += rrq_count ))
(( mps = total_msg / total_time ))

echo "Total messages:     $total_msg"
echo "TTL RRQ messages:   $rrq_count"
echo "Total time:         $total_time"
echo "Message per second: $mps"
echo " "



keepalive_count=0
total_keepalive_count=10
rrq_count=0

while [ $keepalive_count -lt $total_keepalive_count ]
do

    count=0
    fake_d=0
    fake_c=0
    fake_b=0
    addr=17190

    # Do KARRQ for each.
    #
    while [ $count -lt $total_count ]
    do 
        # KARRQ 
        #
        #############################################################

        (( ++seq )); if [ $seq -eq 65534 ]; then seq=1; fi
        (( ++rrq_count ))

	endpoint_id=${endpoints[$count]}

        rcf=$(./ras \
               $gk_ras_addr \
                --rrq \
                --address $addr \
                --sequence-number $seq \
                --endpoint-id ${endpoint_id} \
                --discovery-complete \
                --time-to-live $ttl \
	        --ras-address $id.$fake_b.$fake_c.$fake_d:1719 \
                --call-sig-address $id.$fake_b.$fake_c.$fake_d:1720 \
                --gateway \
                --keepalive \
                --retry-count 2 \
                --retry-delay 3)
        rc=$?

        if [ $rc -eq 13 ]; then
            echo "Error: KA Registration timed out"

        elif [ $rc -ne 0 ]; then
            echo "Error: KA Registration failed: $rc"
            exit $rc
        fi

        echo "KA Registration for endpoint: ${endpoint_id},  $id.$fake_b.$fake_c.$fake_d"


        # Update counters
        #
        #############################################################

        (( ++count ))
        (( fake_d = count % 256 ))
        (( fake_c = count / 256 ))
        (( fake_b = fake_c / 256 ))
        (( fake_c = fake_c % 256 ))
        (( fake_b = fake_b % 256 ))
        (( ++addr ))


        # Report
        #
        #############################################################

        (( result = count % $report_freq ? 0 : 1 ))
        if [ $result -eq 1 ]; then

            now=$(date +%s)
            (( total_time += now - last_time ))
            last_time=$now
            (( total_msg = grq_count + rrq_count ))
            (( mps = total_msg / total_time ))

            echo "Count:              $count"
            echo "Total messages:     $total_msg"
            echo "KARRQ messages:     $rrq_count"
            echo "Total time:         $total_time"
            echo "Message per second: $mps"
            echo " "
        fi

        usleep $per_msg_delay

    done

    now=$(date +%s)
    (( total_time += now - last_time ))
    (( total_time = ( total_time == 0 ) ? 1 : total_time ))
    last_time=$now
    (( total_msg = grq_count + rrq_count ))
    (( mps = total_msg / total_time ))

    (( ++keepalive_count ))

    echo "Total messages:     $total_msg"
    echo "Keep Alive round:   $keepalive_count"
    echo "KARRQ messages:     $rrq_count"
    echo "Total time:         $total_time"
    echo "Message per second: $mps"
    echo " "

done

now=$(date +%s)
(( total_time += now - last_time ))
(( total_time = ( total_time == 0 ) ? 1 : total_time ))
last_time=$now
(( total_msg = rrq_count ))
(( mps = total_msg / total_time ))

echo "Total messages:     $total_msg"
echo "KA RRQ messages:    $rrq_count"
echo "Total time:         $total_time"
echo "Message per second: $mps"
echo " "

count=0
urq_count=0
fake_d=0
fake_c=0
fake_b=0
addr=17190

while [ $count -lt $total_count ]
do 

    # URQ
    #
    #############################################################

    (( ++seq )); if [ $seq -eq 65534 ]; then seq=1; fi
    (( ++urq_count ))

    endpoint_id=${endpoints[$count]}

    ucf=$(./ras  \
            $gk_ras_addr \
            --address $addr \
	    --urq \
	    --seq $seq \
	    --ras-address $id.$fake_b.$fake_c.$fake_d:1719 \
	    --call-sig-address $id.$fake_b.$fake_c.$fake_d:1719 \
            --endpoint-id ${endpoint_id} \
	    --retry-count 1 \
	    --retry-delay 3 )
    rc=$?
        
    if [ $rc -eq 13 ]; then
        echo "Error: Unregistration timed out"

    elif [ $rc -ne 0 ]; then
        echo "Error: Unregistration failed: $rc"
        exit $rc
    fi

    echo "Unregistered endpoint: ${endpoint_id}"


    # Update counters
    #
    #############################################################

    (( ++count ))
    (( fake_d = count % 256 ))
    (( fake_c = count / 256 ))
    (( fake_b = fake_c / 256 ))
    (( fake_c = fake_c % 256 ))
    (( fake_b = fake_b % 256 ))
    (( ++addr ))


    # Report
    #
    #############################################################

    (( result = count % $report_freq ? 0 : 1 ))
    if [ $result -eq 1 ]; then

        now=$(date +%s)
        (( total_time += now - last_time ))
        last_time=$now
        (( total_msg = grq_count + rrq_count + urq_count ))
        (( mps = total_msg / total_time ))
        
        echo "Total message:      $total_msg"
        echo "URQ messages:       $urq_count"
        echo "Total time:         $total_time"
        echo "Message per second: $mps"
        echo " "
    fi
    
    usleep $per_msg_delay

done

now=$(date +%s)
(( total_time += now - last_time ))
(( total_time = ( total_time == 0 ) ? 1 : total_time ))
last_time=$now
(( total_msg += urq_count ))
(( mps = total_msg / total_time ))

echo "Total messages:     $total_msg"
echo "URQ messages:       $urq_count"
echo "Total time:         $total_time"
echo "Message per second: $mps"
echo " "

