-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.sh
executable file
·85 lines (64 loc) · 1.55 KB
/
server.sh
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
80
81
82
83
84
85
#!/bin/bash
# server script
#trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
#function to delete server.pipe when script is shut down
rm server.pipe
exit 0
}
mkfifo server.pipe
while true; do
# read input from server pipe and store in array
read -a array < server.pipe
if [ "${#array[@]}" -gt 1 ]; then
idArr=("${array[0]}") # this slice contains the user id
id="${idArr[*]}"
reqArr=("${array[1]}") # this slice contains the request
req="${reqArr[*]}"
argsArr=("${array[@]:2}") # this slice contains the arguments
args="${argsArr[*]}"
# first element of array is command we want, subsequent elements are parameters
case "$req" in
create_database)
./P.sh "$args"
echo "$(./create_database.sh $args)" > "$id".pipe
./V.sh "$args"
;;
create_table)
./P.sh "$args"
echo "$(./create_table.sh $args)" > "$id".pipe
./V.sh "$args"
;;
insert)
./P.sh "$args"
echo "$(./insert.sh $args)" > "$id".pipe
./V.sh "$args"
;;
select)
./P.sh "$args"
echo "$(./select.sh $args)" > "$id".pipe
./V.sh "$args"
;;
replace)
./P.sh "$args"
echo "$(./replace.sh $args)" > "$id".pipe
./V.sh "$args"
;;
*)
echo "Error: bad request" > "$id".pipe
exit 1
esac
elif [ "${#array[@]}" -eq 1 ]; then
case "${array[0]}" in
shutdown)
# kill the process and delete pipe if shutdown is input by user
echo "shutting down server.." > "$id".pipe
ctrl_c
;;
*)
echo "Error: bad request" > "$id".pipe
exit 1
esac
fi
done