From ffa7aac0a1618b5040e853f9def26cfeab011a27 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 27 Jan 2018 18:57:57 +0100 Subject: [PATCH] First commit: all works but problem with 'sync' have to wait 2sec... --- launch | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ read | 16 +++++++++ trigger | 10 ++++++ write | 13 ++++++++ 4 files changed, 141 insertions(+) create mode 100755 launch create mode 100755 read create mode 100755 trigger create mode 100755 write diff --git a/launch b/launch new file mode 100755 index 0000000..c14a0a0 --- /dev/null +++ b/launch @@ -0,0 +1,102 @@ +#!/bin/bash + + +# (1) Primary setup +#--------------------------------------------------------# +# (1) Get current ABS directory +ROOT=$(dirname `realpath $0`); + +# (2) Check required arguments +test $# -lt 4 && echo -e "ERR: Missing arguments\n\nARGUMENTS:\n\tname\t\tThe name of the machine\n\ttarget_host\tThe hostname (or ip) of the target\n\tout_port\tThe port you want to use to send\n\tin_port\t\tThe port the peer uses to listen\n\ttrigger_port\tOptional param. Used to trigger the peer connection\n" && exit 1; + +# (3) Manage optional param # +TRIGGER=""; +test $# -ge 5 && TRIGGER="$5"; + + +# (2) Secondary setup +#--------------------------------------------------------# +# (1) Reset configuration file +test -f $ROOT/config && rm $ROOT/config; + +# (2) Create buffer paths +BUF_IN="/tmp/buf_in_$4"; +BUF_OUT="/tmp/buf_out_$2_$3"; + +# (3) Flush buffers +echo -ne "" > $BUF_IN; +echo -ne "" > $BUF_OUT; + + +# (3) Launch INPUT server +#--------------------------------------------------------# +# (1) Start the script in background +( + + infail=0; + + # kill script after 10 failures + while [ $infail -lt 10 ]; do + + # main command + nc -lp $4 >> $BUF_IN && infail=0 || infail=`expr $infail + 1`; + + # try to kill proccess that uses port if failed and such proccess exists + if [ $infail -gt 0 ]; then + LISTEN_PID=`lsof -n -i4TCP:$4 | grep LISTEN | awk '{print $2}'`; + test -z "$LISTEN_PID" && kill -9 $LISTEN_PID; + fi; + + done; + +)& + +# (2) Store the background proccess PID # +IN_PID=$!; + + + + + +################################ +# IF SET, SEND TRIGGER TO PEER # +################################ +test ! -z "$TRIGGER" && bash -c "exec 3<>/dev/tcp/$2/$TRIGGER"; + +################################## +# WAIT 2s FOR THE PEER TO LISTEN # +################################## +sleep 2; + + + + + +# (4) Launch OUTPUT server +#--------------------------------------------------------# +# (1) Launch output server +( + + outfail=0; + + # stop after 20 failed attempts + while [ $outfail -lt 20 ]; do + + # main command + tail -f $BUF_OUT | nc $2 $3 && outfail=0 || outfail=`expr $outfail + 1`; + + sleep 1; # can sleep because the buffer is never flushed + + done& + +)& + +# (2) Store the background proccess PID # +OUT_PID=$!; + + + +# (5) Store the config file for 'write' + 'send' to work properly +#--------------------------------------------------------# +# (1) Store config file in 'config_$NAME' +echo -e "#!/bin/bash\nBUF_IN=\"$BUF_IN\"; BUF_OUT=\"$BUF_OUT\";\nIN_PID=$IN_PID; OUT_PID=$OUT_PID;\n" > $ROOT/config_$1; diff --git a/read b/read new file mode 100755 index 0000000..05066c8 --- /dev/null +++ b/read @@ -0,0 +1,16 @@ +#!/bin/bash + +# (0) Get current ABS directory +ROOT=$(dirname `realpath $0`); + +# (1) Check arguments +test $# -lt 1 && echo "ERR: Missing arguments (name)" && exit 1; + +# (2) load configuration file +test -f $ROOT/config_$1 && source $ROOT/config_$1 || ( echo "ERR: No such name" && exit 1 ) || exit 1; + +# (3) Read content from file +cat $BUF_IN; + +# (4) Flush buffer +echo -ne "" > $BUF_IN; diff --git a/trigger b/trigger new file mode 100755 index 0000000..c4ff82b --- /dev/null +++ b/trigger @@ -0,0 +1,10 @@ +#!/bin/bash + +# (0) Get current ABS directory +ROOT=$(dirname `realpath $0`); + +# (1) Check arguments +test $# -lt 1 && echo "ERR: Missing arguments (trigger_port)" && exit 1; + +# (2) Wait for triggering by peer +nc -lp $1; diff --git a/write b/write new file mode 100755 index 0000000..e6c542b --- /dev/null +++ b/write @@ -0,0 +1,13 @@ +#!/bin/bash + +# (0) Get current ABS directory +ROOT=$(dirname `realpath $0`); + +# (1) Check arguments +test $# -lt 2 && echo "ERR: Missing arguments (name, message)" && exit 1; + +# (2) load configuration file +test -f $ROOT/config_$1 && source $ROOT/config_$1 || ( echo "ERR: No such name" && exit 1 ) || exit 1; + +# (3) Write content to file +echo -ne "$2" >> $BUF_OUT;