From 86d6f8d0a3ebeee950a535e65719fb1c6348cf98 Mon Sep 17 00:00:00 2001 From: ehouse Date: Thu, 5 Nov 2009 04:31:23 +0000 Subject: [PATCH] script and helper for testing proposed store-and-forward relay feature. --- xwords4/linux/scripts/discon_ok.sh | 113 +++++++++++++++++++++++++ xwords4/linux/scripts/script_common.sh | 61 +++++++++++++ 2 files changed, 174 insertions(+) create mode 100755 xwords4/linux/scripts/discon_ok.sh create mode 100755 xwords4/linux/scripts/script_common.sh diff --git a/xwords4/linux/scripts/discon_ok.sh b/xwords4/linux/scripts/discon_ok.sh new file mode 100755 index 000000000..e6fbacff9 --- /dev/null +++ b/xwords4/linux/scripts/discon_ok.sh @@ -0,0 +1,113 @@ +#!/bin/bash + +HOST_COUNTER=0 +NGAMES=1 # games, not hosts +SAME_ROOM="" # unset means use different +DICT=./dict.xwd +HOST=localhost +PORT=10999 +XWORDS=./obj_linux_memdbg/xwords +WAIT=10 + +RUN_NAME=$(basename $0)/_$$ + +. ./scripts/script_common.sh + +exec_cmd() { + CMD="$1" + LOG="$2" + [ -z "$LOG" ] && LOG=/dev/null + echo "launching: $CMD" + exec $CMD /dev/null 2>>$LOG + echo "got here???" +} + +# Given game strings, build commands for them, run them all for a +# while, and then start running them one at a time so they can +# progress IFF the relay allows disconnected communication +run_game_set() { + GAME_STR=$1 + INDX=$2 + + COUNTER=0 + + declare -a CMDS + declare -a PIDS + declare -a LOGS + + for JJ in $(seq ${#GAME_STR}); do + GAME=$(game_name $((COUNTER+INDX))) + CMD=$(cmd_for $GAME_STR $(($JJ-1)) "room" $GAME) + CMDS[$COUNTER]="$CMD" + + LOG=$(log_name $((COUNTER+INDX))) + LOGS[$COUNTER]="$LOG" + + exec_cmd "$CMD" $LOG & + PIDS[$COUNTER]=$! + + COUNTER=$((COUNTER+1)) + done + + + # Loop until all games in set have connected, then kill them + while :; do + sleep 2 + FOUND=0 + for LOG in ${LOGS[*]}; do + if [ -f $LOG ]; then + if grep -q "relayPreProcess: connName" $LOG; then + FOUND=$((FOUND+1)) + fi + fi + done + [ $FOUND -lt ${#LOGS[*]} ] && continue + + echo "all games started!" + kill ${PIDS[*]} + break + done + + # Now launch them again + + for JJ in $(seq 0 $((${#GAME_STR}-1))); do + exec_cmd "${CMDS[$JJ]}" "${LOGS[$JJ]}" & + PIDS[$JJ]=$! + done + +} + +echo "mkdir -p $(dirname $(log_name))" +mkdir -p $(dirname $(log_name)) + +INDX=0 +for II in $(seq $NGAMES); do + N_THIS_GAME=$(( 2 + $(($RANDOM % 3 )))) + GAME_STR=$(make_game_str $N_THIS_GAME) + echo $GAME_STR + + run_game_set $GAME_STR $INDX & + INDX=$((INDX+4)) + +# for JJ in $(seq ${#GAME_STR}); do +# CMDS[$HOST_COUNTER]=$(cmd_for $GAME_STR $(($JJ-1)) $ROOM $LOG) +# echo "${CMDS[$HOST_COUNTER]}" +# HOST_COUNTER=$((HOST_COUNTER+1)) +# done +done + +# for II in $(seq 0 $((HOST_COUNTER-1))); do +# LOG=$(log_name $HOST_COUNTER) +# exec_cmd "${CMDS[$II]}" $LOG & +# PIDS[$II]=$! +# sleep 2 +# done + +# sleep 10 + +# for II in $(seq 0 $((HOST_COUNTER-1))); do +# echo "kill ${PIDS[$II]}" +# kill ${PIDS[$II]} +# done + +wait diff --git a/xwords4/linux/scripts/script_common.sh b/xwords4/linux/scripts/script_common.sh new file mode 100755 index 000000000..0831d40c4 --- /dev/null +++ b/xwords4/linux/scripts/script_common.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +r_safe_mod() { + NUM=$1 + [ $NUM -eq 0 ] || NUM=$(( RANDOM % $NUM )) + echo $NUM +} + +make_game_str() { + NTHISGAME=$1 + + NPLAYERS_HOST=$(( 1+ $(r_safe_mod $((NTHISGAME - 1))) )) + GAME_STR="$NPLAYERS_HOST" + NPLAYERS_GUESTS=$(( NTHISGAME - NPLAYERS_HOST )) + + while [ $NPLAYERS_GUESTS -gt 0 ]; do + NTHISGUEST=$(( 1 + $( r_safe_mod $((NPLAYERS_GUESTS - 1))) )) + GAME_STR="${GAME_STR}$NTHISGUEST" + NPLAYERS_GUESTS=$((NPLAYERS_GUESTS - NTHISGUEST)) + done + + echo "$GAME_STR" +} + +cmd_for() { + GAME_STR="$1" + INDX="$2" + ROOM="$3" + GAME="$4" + + LOCALS="" + HOSTSTR="" + + LOCAL_COUNT=${GAME_STR:$INDX:1} + for II in $(seq 0 $(($LOCAL_COUNT-1))); do LOCALS="$LOCALS -r Eric"; done + + if [ 0 -eq $INDX ]; then # host? + HOSTSTR="-s" + for II in $(seq 1 $((${#GAME_STR}-1))); do + REMOTE_COUNT=${GAME_STR:$II:1} + for JJ in $(seq $REMOTE_COUNT); do HOSTSTR="$HOSTSTR -N"; done + done + fi + + RESULT="$XWORDS -d $DICT -a $HOST -p $PORT -u -0 -C $ROOM -q 2 -z 0:$WAIT" + RESULT="${RESULT} $LOCALS $HOSTSTR" + [ -n "$GAME" ] && RESULT="$RESULT -f $GAME" + echo "$RESULT" +} + +log_name() { + RUN_NAME=${RUN_NAME:-$(basename $0)/_$$} + INDX=${1:-0} + echo "/tmp/$RUN_NAME/log_${INDX}.txt" +} + +game_name() { + RUN_NAME=${RUN_NAME:-$(basename $0)/_$$} + INDX=${1:-0} + echo "/tmp/$RUN_NAME/game_${INDX}.xwg" +}