updated stdout + now uses asynchronous AND synchronous 'bind-input' and 'bind-output'

This commit is contained in:
xdrm-brackets 2018-02-01 00:31:21 +01:00
parent 73e388aff8
commit 201dc16ac9
2 changed files with 74 additions and 42 deletions

79
master
View File

@ -46,12 +46,12 @@ test -z "$LOCAL_IP" && echo "error: cannot get local IP address." && exit 1;
# (1) On-Exit routine # # (1) On-Exit routine #
on_exit(){ on_exit(){
echo "killing 'bind-output' sub processes"; echo "- killing 'bind-output' sub processes";
for logger_name in "${!OUTPUT_PID[@]}"; do for logger_name in "${!OUTPUT_PID[@]}"; do
# kill each logger OUTPUT # kill each logger OUTPUT
echo " > killing output@$logger_name"; echo " - killing output@$logger_name";
kill -INT ${OUTPUT_PID[$logger_name]} 2>/dev/null; kill -INT ${OUTPUT_PID[$logger_name]} 2>/dev/null;
done; done;
@ -59,15 +59,15 @@ on_exit(){
for logger_name in "${!LOGGER_PID[@]}"; do for logger_name in "${!LOGGER_PID[@]}"; do
# kill each logger OUTPUT # kill each logger OUTPUT
echo " > killing logger@$logger_name"; echo " - killing logger@$logger_name";
kill -INT ${LOGGER_PID[$logger_name]} 2>/dev/null; kill -INT ${LOGGER_PID[$logger_name]} 2>/dev/null;
done; done;
echo "killing 'master' output socket"; echo "- killing 'master' output socket";
kill -INT $MASTER_PID 2>/dev/null; kill -INT $MASTER_PID 2>/dev/null;
echo "killing 'master' loop"; echo "- killing 'master' loop";
# kill -INT $LOOP_PID 2>/dev/null; # kill -INT $LOOP_PID 2>/dev/null;
} }
@ -81,10 +81,11 @@ trap "on_exit;" HUP INT KILL TERM;
# (3) Connect to pool # (3) Connect to pool
#--------------------------------------------------------# #--------------------------------------------------------#
# (1) Bind-output # # (1) Bind-output #
echo "(.) connect to $POOL_HOST:$POOL_PORT"; echo "+ connect to $POOL_HOST:$POOL_PORT";
( $ROOT/bind-output master $POOL_HOST $POOL_PORT )& MASTER_PID="`$ROOT/bind-output master $POOL_HOST $POOL_PORT`";
MASTER_PID=$?;
echo " + flush connection before using it";
# $ROOT/write master "";
# (4) Launch master manager # (4) Launch master manager
#--------------------------------------------------------# #--------------------------------------------------------#
@ -92,6 +93,8 @@ MASTER_PID=$?;
PORT="$MIN_PORT"; PORT="$MIN_PORT";
for logger_name in "${LOGGERS[@]}"; do for logger_name in "${LOGGERS[@]}"; do
echo " + logger '$logger_name'";
# Find an available port # Find an available port
while true; do while true; do
@ -103,31 +106,57 @@ for logger_name in "${LOGGERS[@]}"; do
done; done;
# send logger port request # listen for pool response (port number)
echo " (.) Sending logger@$logger_name request"; 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"; $ROOT/write master "$POOL_KEY:$logger_name:$LOCAL_IP:$PORT";
# check pool response echo " + waiting for response";
RESP="`nc -w 2 -lp $PORT`"; 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 # empty response -> ignore
test -z "$RESP" && continue; test -z "$RESP" && echo " - no answer > aborting" && continue;
# not a number -> ignore # 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 # Create output bound
echo " (.) Connecting '$logger_name' to $POOL_HOST:$RESP"; echo " + connecting to $POOL_HOST:$RESP";
( $ROOT/bind-output master_$logger_name $POOL_HOST $RESP )& OUTPUT_PID[$logger_name]="`$ROOT/bind-output master_$logger_name $POOL_HOST $RESP`";
OUTPUT_PID[$logger_name]=$!;
echo " (.) launching logger@$logger_name"; echo " + launching logger";
# launch logger bound to output # launch logger bound to output
( $LOGGER_DIR/$logger_name | cat > /tmp/outbuf_master_$logger_name )& ( $LOGGER_DIR/$logger_name | cat > /tmp/outbuf_master_$logger_name )&
LOGGER_PID[$logger_name]=$!; LOGGER_PID[$logger_name]=$!;
echo " + background proccess"
# increment port for next logger # increment port for next logger
PORT="`expr $PORT + 1`"; PORT="`expr $PORT + 1`";
@ -136,16 +165,12 @@ done;
# When all loggers are bound, destroy pool connection # 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 for logger_name in "${!LOGGER_PID[@]}"; do
echo "waiting for $logger_name";
wait ${LOGGER_PID[$logger_name]}; wait ${LOGGER_PID[$logger_name]};
echo "done with $logger_name";
done; done;
# on_exit;

35
pool
View File

@ -26,19 +26,19 @@ LOOP_PID="";
on_exit(){ on_exit(){
echo "killing 'pool' loop"; echo "- killing 'pool' loop";
kill -INT $LOOP_PID 2>/dev/null; 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 for logger_name in "${!INPUT_PID[@]}"; do
# kill each logger input # 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; test ! -z "${INPUT_PID[$logger_name]}" && kill -INT ${INPUT_PID[$logger_name]} 2>/dev/null;
done; done;
echo "killing 'pool' input listener ($POOL_PID)"; echo "- killing 'pool' input listener ($POOL_PID)";
kill -INT $POOL_PID 2>/dev/null; kill -INT $POOL_PID 2>/dev/null;
exit 1; exit 1;
@ -51,9 +51,8 @@ on_exit(){
# (2) Launch pool listener # (2) Launch pool listener
#--------------------------------------------------------# #--------------------------------------------------------#
# (1) Bind-input # # (1) Bind-input #
echo "(.) listen $POOL_PORT"; echo "+ listen $POOL_PORT";
( $ROOT/bind-input pool $POOL_PORT )& POOL_PID="`$ROOT/bind-input pool $POOL_PORT`";
POOL_PID=$!;
@ -72,7 +71,9 @@ PORT="$MIN_PORT";
# Do nothing if empty msg or invalid format # Do nothing if empty msg or invalid format
test -z "$MSG" && sleep .5 && continue; 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 # Extract ID - PORT
KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`"; KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`";
@ -81,10 +82,10 @@ PORT="$MIN_PORT";
LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`"; LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`";
# if ID does not match # 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 # 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 # Find an available port
PORT="`expr $PORT + 1`"; PORT="`expr $PORT + 1`";
@ -103,17 +104,23 @@ PORT="$MIN_PORT";
done; done;
# if already an input for this logger -> kill it # 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 # Bind-input for the logger at the free port found
( $ROOT/bind-input $LOGGER_NAME $PORT )&
# Store bind-input PID # 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 # 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;"; bash -c "exec 4<>/dev/tcp/$LOGGER_HOST/$LOGGER_PORT; echo -n \"$PORT\" >&4;";
echo " + background process";
done; done;
)& )&