Now 'master' have a main 'input' for all port responses + now all seem to work each time .. (+fixed some minor things)

This commit is contained in:
xdrm-brackets 2018-02-01 16:06:40 +01:00
parent 3c6d8ec0fb
commit 5aeaab0bc5
2 changed files with 32 additions and 25 deletions

53
master
View File

@ -69,6 +69,9 @@ on_exit(){
echo "- killing main output";
kill -INT $MASTER_PID 2>/dev/null;
echo "- killing main input";
kill -INT $LIST_PID 2>/dev/null;
echo "- killing loop";
# kill -INT $LOOP_PID 2>/dev/null;
@ -76,6 +79,7 @@ on_exit(){
echo "- deleting tmp file";
rm $TMPFILE 2>/dev/null;
exit 1;
}
trap "on_exit;" HUP INT KILL TERM;
@ -91,6 +95,24 @@ echo -e "\e[32m+\e[0m connect to $POOL_HOST:$POOL_PORT";
$ROOT/bind-output master $POOL_HOST $POOL_PORT > $TMPFILE;
MASTER_PID="`cat $TMPFILE`";
# (2) Bind input (find free port first)
# Find an available port
LIST_PORT="$MIN_PORT";
while true; do
# if port not in use -> use it
ss -tl4 "( sport = :$LIST_PORT )" | grep "$LIST_PORT" >/dev/null 2>&1 || break;
# else try next port (+1)
PORT="`expr $LIST_PORT + 1`";
done;
# listen for pool response (port number)
echo -e "\e[32m+\e[0m listen $LIST_PORT";
$ROOT/bind-input master $LIST_PORT > $TMPFILE;
LIST_PID="`cat $TMPFILE`";
echo " + flush connection before using it";
# $ROOT/write master "";
@ -101,35 +123,18 @@ for logger_name in "${LOGGERS[@]}"; do
echo " + logger '$logger_name'";
# Find an available port
PORT="$MIN_PORT";
while true; do
# 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`";
done;
# listen for pool response (port number)
echo -e " \e[32m+\e[0m listen $PORT";
$ROOT/bind-input freeport $PORT > $TMPFILE;
FREEPORT_PID="`cat $TMPFILE`";
sleep 1;
sleep .5;
# 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:$LIST_PORT";
echo " + waiting for response";
RESP="";
timeout=8; # 4sec = 8 x .5sec
timeout=10; # 5sec = 10 x .5sec
while [ "$timeout" -gt "0" ]; do
RESP="`$ROOT/read freeport`";
RESP="`$ROOT/read master`";
# if empty answer, try again in .5 sec
if [ -z "$RESP" ]; then
@ -148,8 +153,6 @@ for logger_name in "${LOGGERS[@]}"; do
# closing listen socket
echo -e " + received '\e[33m$RESP\e[0m'";
echo -e " \e[31m+\e[0m stop listening on $PORT";
kill -INT $FREEPORT_PID 2>/dev/null;
# empty response -> ignore
test -z "$RESP" && echo " + no answer" && continue;
@ -173,12 +176,14 @@ done;
# When all loggers are bound, destroy pool connection
# 1. Send END msg to pool (but wait for it to manage last logger)
sleep 2;
$ROOT/write master "END${POOL_KEY}END";
# 2. Close pool connection
echo -e "\e[31m+\e[0m close connection to $POOL_HOST:$POOL_PORT ";
kill -INT $MASTER_PID 2>/dev/null;
echo -e "\e[31m+\e[0m stop listening on $LIST_PORT";
kill -INT $LIST_PID;
echo " + ${#LOGGER_PID[@]} loggers running in background";
for logger_name in "${!LOGGER_PID[@]}"; do

4
pool
View File

@ -105,9 +105,10 @@ POOL_PID="`cat $TMPFILE`";
# Find an available port
PORT="$MIN_PORT";
while true; do
# do not use same port as master (if on same machine)
test "$PORT" = "$LOGGER_PORT" && PORT="`expr $PORT + 1`" && continue;
# ignore already used ports
for used in ${USED_PORTS[*]}; do
test "$PORT" = "$used" && PORT="`expr $PORT + 1`" && continue;
@ -139,6 +140,7 @@ POOL_PID="`cat $TMPFILE`";
# Notify that input is active to MASTER
sleep 1;
echo " + sending '$PORT' to $LOGGER_HOST:$LOGGER_PORT";
bash -c "exec 4<>/dev/tcp/$LOGGER_HOST/$LOGGER_PORT; echo -n \"$PORT\" >&4;";