From 201dc16ac982dcee48db70152c7504ff2f886c42 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 1 Feb 2018 00:31:21 +0100 Subject: [PATCH] updated stdout + now uses asynchronous AND synchronous 'bind-input' and 'bind-output' --- master | 81 ++++++++++++++++++++++++++++++++++++++-------------------- pool | 35 +++++++++++++++---------- 2 files changed, 74 insertions(+), 42 deletions(-) diff --git a/master b/master index 32c8824..deb2a35 100755 --- a/master +++ b/master @@ -46,12 +46,12 @@ test -z "$LOCAL_IP" && echo "error: cannot get local IP address." && exit 1; # (1) On-Exit routine # on_exit(){ - echo "killing 'bind-output' sub processes"; + echo "- killing 'bind-output' sub processes"; for logger_name in "${!OUTPUT_PID[@]}"; do # kill each logger OUTPUT - echo " > killing output@$logger_name"; + echo " - killing output@$logger_name"; kill -INT ${OUTPUT_PID[$logger_name]} 2>/dev/null; done; @@ -59,15 +59,15 @@ on_exit(){ for logger_name in "${!LOGGER_PID[@]}"; do # kill each logger OUTPUT - echo " > killing logger@$logger_name"; + echo " - killing logger@$logger_name"; kill -INT ${LOGGER_PID[$logger_name]} 2>/dev/null; done; - echo "killing 'master' output socket"; + echo "- killing 'master' output socket"; kill -INT $MASTER_PID 2>/dev/null; - echo "killing 'master' loop"; + echo "- killing 'master' loop"; # kill -INT $LOOP_PID 2>/dev/null; } @@ -81,10 +81,11 @@ trap "on_exit;" HUP INT KILL TERM; # (3) Connect to pool #--------------------------------------------------------# # (1) Bind-output # -echo "(.) connect to $POOL_HOST:$POOL_PORT"; -( $ROOT/bind-output master $POOL_HOST $POOL_PORT )& -MASTER_PID=$?; +echo "+ connect to $POOL_HOST:$POOL_PORT"; +MASTER_PID="`$ROOT/bind-output master $POOL_HOST $POOL_PORT`"; +echo " + flush connection before using it"; +# $ROOT/write master ""; # (4) Launch master manager #--------------------------------------------------------# @@ -92,6 +93,8 @@ MASTER_PID=$?; PORT="$MIN_PORT"; for logger_name in "${LOGGERS[@]}"; do + echo " + logger '$logger_name'"; + # Find an available port while true; do @@ -103,31 +106,57 @@ for logger_name in "${LOGGERS[@]}"; do done; - # send logger port request - echo " (.) Sending logger@$logger_name request"; + # listen for pool response (port number) + echo " + listen $PORT"; + FREEPORT_PID="`$ROOT/bind-input freeport $PORT`"; + + # send port request + echo " + sending port request"; $ROOT/write master "$POOL_KEY:$logger_name:$LOCAL_IP:$PORT"; - # check pool response - RESP="`nc -w 2 -lp $PORT`"; + echo " + waiting for response"; + RESP=""; + timeout=8; # 4sec = 8 x .5sec + while [ "$timeout" -gt "0" ]; do + + RESP="`$ROOT/read freeport`"; + + # if empty answer, try again in .5 sec + if [ -z "$RESP" ]; then + # echo " + empty response (remaining $timeout)"; + timeout="`expr $timeout - 1`"; + sleep .5; + continue; + + # else -> stop reading + else + # echo " + got response '$RESP'"; + break; + fi + + done; + + # closing listen socket + echo " + received '$RESP'"; + echo " + stop listening on $PORT"; + kill -INT $FREEPORT_PID 2>/dev/null; # empty response -> ignore - test -z "$RESP" && continue; + test -z "$RESP" && echo " - no answer > aborting" && continue; # not a number -> ignore - echo -n "$RESP" | grep -P '^\d+$' >/dev/null || continue; - + echo -n "$RESP" | grep -P '^\d+$' >/dev/null || ( echo " - invalid format > aborting"; exit 1 ) || continue; # Create output bound - echo " (.) Connecting '$logger_name' to $POOL_HOST:$RESP"; - ( $ROOT/bind-output master_$logger_name $POOL_HOST $RESP )& - OUTPUT_PID[$logger_name]=$!; + echo " + connecting to $POOL_HOST:$RESP"; + OUTPUT_PID[$logger_name]="`$ROOT/bind-output master_$logger_name $POOL_HOST $RESP`"; - echo " (.) launching logger@$logger_name"; + echo " + launching logger"; # launch logger bound to output ( $LOGGER_DIR/$logger_name | cat > /tmp/outbuf_master_$logger_name )& LOGGER_PID[$logger_name]=$!; - + echo " + background proccess" # increment port for next logger PORT="`expr $PORT + 1`"; @@ -136,16 +165,12 @@ done; # When all loggers are bound, destroy pool connection -# kill -INT $MASTER_PID 2>/dev/null; +echo "+ close connection to $POOL_HOST:$POOL_PORT "; +kill -INT $MASTER_PID 2>/dev/null; -echo "waiting for ${#LOGGER_PID[@]} + ${#OUTPUT_PID[@]} .."; +echo " + waiting for ${#LOGGER_PID[@]} inputs/loggers to end"; for logger_name in "${!LOGGER_PID[@]}"; do - echo "waiting for $logger_name"; wait ${LOGGER_PID[$logger_name]}; - echo "done with $logger_name"; -done; - - -# on_exit; \ No newline at end of file +done; \ No newline at end of file diff --git a/pool b/pool index e676c82..908dc07 100755 --- a/pool +++ b/pool @@ -26,19 +26,19 @@ LOOP_PID=""; on_exit(){ - echo "killing 'pool' loop"; + echo "- killing 'pool' loop"; kill -INT $LOOP_PID 2>/dev/null; - echo "killing ${#INPUT_PID[@]} background 'bind-input'"; + echo "- killing ${#INPUT_PID[@]} background listeners"; for logger_name in "${!INPUT_PID[@]}"; do # kill each logger input - echo ".killing input@$logger_name (${INPUT_PID[$logger_name]})"; + echo " - killing input@$logger_name (${INPUT_PID[$logger_name]})"; test ! -z "${INPUT_PID[$logger_name]}" && kill -INT ${INPUT_PID[$logger_name]} 2>/dev/null; done; - echo "killing 'pool' input listener ($POOL_PID)"; + echo "- killing 'pool' input listener ($POOL_PID)"; kill -INT $POOL_PID 2>/dev/null; exit 1; @@ -51,9 +51,8 @@ on_exit(){ # (2) Launch pool listener #--------------------------------------------------------# # (1) Bind-input # -echo "(.) listen $POOL_PORT"; -( $ROOT/bind-input pool $POOL_PORT )& -POOL_PID=$!; +echo "+ listen $POOL_PORT"; +POOL_PID="`$ROOT/bind-input pool $POOL_PORT`"; @@ -72,7 +71,9 @@ PORT="$MIN_PORT"; # Do nothing if empty msg or invalid format test -z "$MSG" && sleep .5 && continue; - echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' /!\ invalid format' && sleep .5 && continue; + + echo " + received '$MSG'"; + echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' + invalid format' && sleep .5 && continue; # Extract ID - PORT KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`"; @@ -81,10 +82,10 @@ PORT="$MIN_PORT"; LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`"; # if ID does not match - test "$KEY" != "$POOL_KEY" && echo ' /!\ wrong id' && sleep .5 && continue; + test "$KEY" != "$POOL_KEY" && echo ' + wrong id' && sleep .5 && continue; # create new listening socket - echo " (.) binding input for logger@$LOGGER_NAME ($LOGGER_HOST:$LOGGER_PORT)"; + echo " + received logger: $LOGGER_NAME on $LOGGER_HOST:$LOGGER_PORT"; # Find an available port PORT="`expr $PORT + 1`"; @@ -103,17 +104,23 @@ PORT="$MIN_PORT"; done; # if already an input for this logger -> kill it - test ! -z "${INPUT_PID[$LOGGER_NAME]}" && kill -INT ${INPUT_PID[$LOGGER_NAME]}; + if [ ! -z "${INPUT_PID[$LOGGER_NAME]}" ]; then + echo " + already an input -> killing it"; + kill -INT ${INPUT_PID[$LOGGER_NAME]}; + fi; # Bind-input for the logger at the free port found - ( $ROOT/bind-input $LOGGER_NAME $PORT )& - # Store bind-input PID - INPUT_PID["$LOGGER_NAME"]=$!; + echo " + listen $PORT"; + INPUT_PID["$LOGGER_NAME"]="`$ROOT/bind-input $LOGGER_NAME $PORT`"; + # Notify that input is active to MASTER + echo " + sending back to port '$PORT' $LOGGER_HOST:$LOGGER_PORT"; bash -c "exec 4<>/dev/tcp/$LOGGER_HOST/$LOGGER_PORT; echo -n \"$PORT\" >&4;"; + echo " + background process"; + done; )&