2018-01-30 16:00:11 +00:00
#!/bin/bash
# (1) Init.
#--------------------------------------------------------#
# (1) Get current absolute dir
ROOT=$(dirname `realpath $0`);
# (2) Check argc
2018-01-30 17:50:44 +00:00
test $# -lt 2 && echo -e "error: too fewarguments\n\n\e[1mUSAGE\e[0m\n\tpool <key> <port>\n\n\e[1mARGUMENTS\e[0m\n\t<key>\tThe key of the pool (client must match it)\n\t<port>\tTo port to listen to\n" && exit 1;
2018-01-30 16:00:11 +00:00
# (3) Check @PORT range #
2018-01-30 17:50:44 +00:00
MIN_PORT=1024;
MAX_PORT=49151;
2018-01-30 16:00:11 +00:00
test "$2" -gt "$MAX_PORT" -o "$2" -lt "$MIN_PORT" && echo "error: <port> must be between $MIN_PORT and $MAX_PORT" && exit 1;
# (4) Set argument explicit names #
2018-01-30 17:50:44 +00:00
POOL_KEY="$1";
POOL_PORT="$2";
2018-01-30 16:00:11 +00:00
2018-01-30 17:50:44 +00:00
# (5) init logger variables #
declare -A INPUT_PID; # will contain each bind-input pid
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# (6) Define on-exit routine #
LOOP_PID="";
2018-01-30 17:50:44 +00:00
on_exit(){
2018-01-31 19:55:11 +00:00
echo "killing 'pool' loop";
kill -INT $LOOP_PID 2>/dev/null;
echo "killing ${#INPUT_PID[@]} background 'bind-input'";
2018-01-30 17:50:44 +00:00
for logger_name in "${!INPUT_PID[@]}"; do
# kill each logger input
2018-01-31 19:55:11 +00:00
echo ".killing input@$logger_name (${INPUT_PID[$logger_name]})";
test ! -z "${INPUT_PID[$logger_name]}" && kill -INT ${INPUT_PID[$logger_name]} 2>/dev/null;
2018-01-30 17:50:44 +00:00
done;
2018-01-31 19:55:11 +00:00
echo "killing 'pool' input listener ($POOL_PID)";
kill -INT $POOL_PID 2>/dev/null;
2018-01-30 17:50:44 +00:00
2018-01-31 19:55:11 +00:00
exit 1;
2018-01-30 17:50:44 +00:00
}
2018-01-31 19:55:11 +00:00
# (2) Launch pool listener
2018-01-30 16:00:11 +00:00
#--------------------------------------------------------#
2018-01-30 17:50:44 +00:00
# (1) Bind-input #
echo "(.) listen $POOL_PORT";
2018-01-31 19:55:11 +00:00
( $ROOT/bind-input pool $POOL_PORT )&
POOL_PID=$!;
2018-01-30 17:50:44 +00:00
2018-01-30 16:00:11 +00:00
2018-01-30 17:50:44 +00:00
# (4) Launch pool manager
#--------------------------------------------------------#
2018-01-30 16:00:11 +00:00
# infinite listener
2018-01-30 21:54:47 +00:00
PORT="$MIN_PORT";
2018-01-31 19:55:11 +00:00
(
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
trap "on_exit;" HUP INT KILL TERM;
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
while true; do
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# Listen on port @POOL_PORT
MSG=`$ROOT/read pool`;
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# 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;
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# Extract ID - PORT
KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`";
LOGGER_NAME="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\2/'`";
LOGGER_HOST="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\3/'`";
LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`";
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# if ID does not match
test "$KEY" != "$POOL_KEY" && echo ' /!\ wrong id' && sleep .5 && continue;
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# create new listening socket
echo " (.) binding input for logger@$LOGGER_NAME ($LOGGER_HOST:$LOGGER_PORT)";
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# Find an available port
PORT="`expr $PORT + 1`";
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
while true; do
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# ignore pool port
test "$PORT" = "$POOL_PORT" && PORT="`expr $PORT + 1`" && continue;
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# if port not in use -> use it
ss -tl4 "( sport = :$PORT )" | grep "$PORT" >/dev/null 2>&1 || break;
# else try next port (+1)
PORT="`expr $PORT + 1`";
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
done;
2018-01-30 17:50:44 +00:00
2018-01-31 19:55:11 +00:00
# if already an input for this logger -> kill it
test ! -z "${INPUT_PID[$LOGGER_NAME]}" && kill -INT ${INPUT_PID[$LOGGER_NAME]};
2018-01-30 17:50:44 +00:00
2018-01-31 19:55:11 +00:00
# Bind-input for the logger at the free port found
( $ROOT/bind-input $LOGGER_NAME $PORT )&
2018-01-30 17:50:44 +00:00
2018-01-31 19:55:11 +00:00
# Store bind-input PID
INPUT_PID["$LOGGER_NAME"]=$!;
2018-01-30 17:50:44 +00:00
2018-01-31 19:55:11 +00:00
# Notify that input is active to MASTER
bash -c "exec 4<>/dev/tcp/$LOGGER_HOST/$LOGGER_PORT; echo -n \"$PORT\" >&4;";
2018-01-30 17:50:44 +00:00
2018-01-31 19:55:11 +00:00
done;
)&
2018-01-30 17:50:44 +00:00
LOOP_PID=$!;
2018-01-30 16:00:11 +00:00
2018-01-31 19:55:11 +00:00
# (3) Set exit management
#--------------------------------------------------------#
trap "kill -INT $LOOP_PID;" HUP INT KILL TERM;
2018-01-30 17:50:44 +00:00
wait $LOOP_PID;
2018-01-30 16:00:11 +00:00