-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain
More file actions
executable file
·80 lines (63 loc) · 1.4 KB
/
main
File metadata and controls
executable file
·80 lines (63 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#! /bin/bash
set -f
DBFILE='/var/lib/jenkins/workspace/auto_get_into_pool/auto/efficiencyPromotion/auto_get_into_pool/resource/dbfile.db'
PORT=5000
function ret() {
echo "$@" >> response
}
function log() {
timestamp="$(date +'%D %T')"
echo "[${timestamp}] $@" >> log
}
function clear() {
for pid in $(cat pid); do
kill -9 ${pid}
done
pids="$(ps -ef | grep 'tailf' | grep -v 'grep' | awk '{print $2}')"
for pid in ${pids}; do
kill -9 ${pid}
done
}
function main(){
>pid
>request
>response
nc -lk $PORT > request < <(tailf response) &
echo $! >> pid
while read line ; do
ret "$(react ${line})"
log "[req] $line, [res] $(tail -1 response)"
done < <(tailf request)
}
function react() {
req="$@"
consumed=0
if [[ "${req}" == "use db" ]]; then
echo 1 > dbflag
consumed=1
echo "OK"
elif [[ "$req" == "quit" ]]; then
echo 0 > dbflag
consumed=1
echo "Close DB"
fi
if [[ $consumed -eq 1 ]] ; then
return
fi
if [[ $(<dbflag) -eq 1 ]]; then
sqlite3 "${DBFILE}" 2>&1 <<<"$req"
return $?
fi
echo "hell $req"
}
function usage() {
echo "Usage: $0 dbfile port"
}
trap clear SIGINT SIGTERM
if [[ "$1" == "-h"]]; then
usage
return 0
fi
[[ -n "$1" ]] && DBFILE="$1"
[[ -n "$2" ]] && PORT="$2"
main "$@"