-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.sh
179 lines (171 loc) · 5.53 KB
/
control.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
# Configuration
declare -a servers=("127.0.0.1:2010" "127.0.0.1:2020" "127.0.0.1:2030" "127.0.0.1:2040" "127.0.0.1:2050")
# Helper Functions
send_get_request() {
local url=$1
curl -X GET "$url"
echo
}
send_post_request() {
local url=$1
local json=$2
curl -X POST -H "Content-Type: application/json" -d "$json" "$url"
echo
}
increment_port() {
local address=$1
local ip=${address%:*}
local port=${address#*:}
echo "$ip:$((port + 1))"
}
print_help() {
echo "Commands:"
echo "g <idx> - Get health status"
echo "s <idx> - Get node status"
echo "j <from_idx> <to_idx> - Join nodes"
echo "l <idx> - Node leaves"
echo "k <idx> - Kill node"
echo "r <idx> - Revive node"
echo "acq <idx> <resource> - Acquire resource"
echo "rel <idx> <resource> - Release resource"
echo "det <idx> - Start detection"
echo "wait <idx> <target_idx> - Wait for message"
echo "active <idx> - Set active"
echo "passive <idx> - Set passive"
echo "delay <idx> <ms> - Set delay"
echo "h - Help"
echo "q - Quit"
}
print_servers() {
echo "Available nodes:"
for i in "${!servers[@]}"; do
echo "$i: ${servers[$i]}"
done
}
# Main Loop
while true; do
echo -e "\nEnter command (h for help):"
print_servers
read -r cmd arg1 arg2
case $cmd in
h)
print_help
;;
q)
exit 0
;;
g)
if [[ -z "${servers[$arg1]}" ]]; then
echo "Invalid node index"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/health"
send_get_request "$url"
;;
s)
if [[ -z "${servers[$arg1]}" ]]; then
echo "Invalid node index"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/status"
send_get_request "$url"
;;
j)
if [[ -z "${servers[$arg1]}" || -z "${servers[$arg2]}" ]]; then
echo "Invalid node indices"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/joinother"
json="{\"address\": \"${servers[$arg2]}\"}"
send_post_request "$url" "$json"
;;
l)
if [[ -z "${servers[$arg1]}" ]]; then
echo "Invalid node index"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/leave"
send_post_request "$url"
;;
k)
if [[ -z "${servers[$arg1]}" ]]; then
echo "Invalid node index"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/kill"
send_post_request "$url"
;;
r)
if [[ -z "${servers[$arg1]}" ]]; then
echo "Invalid node index"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/revive"
send_post_request "$url"
;;
acq)
if [[ -z "${servers[$arg1]}" || -z "$arg2" ]]; then
echo "Usage: acq <node_idx> <resource>"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/acquire"
json="{\"resource\": \"$arg2\"}"
send_post_request "$url" "$json"
;;
rel)
if [[ -z "${servers[$arg1]}" || -z "$arg2" ]]; then
echo "Usage: rel <node_idx> <resource>"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/release"
json="{\"resource\": \"$arg2\"}"
send_post_request "$url" "$json"
;;
det)
if [[ -z "${servers[$arg1]}" ]]; then
echo "Invalid node index"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/detection/start"
send_post_request "$url"
;;
wait)
if [[ -z "${servers[$arg1]}" || -z "${servers[$arg2]}" ]]; then
echo "Invalid node indices"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/waitForMessage"
json="{\"address\": \"${servers[$arg2]}\"}"
send_post_request "$url" "$json"
;;
active)
if [[ -z "${servers[$arg1]}" ]]; then
echo "Invalid node index"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/setActive"
send_post_request "$url"
;;
passive)
if [[ -z "${servers[$arg1]}" ]]; then
echo "Invalid node index"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/setPassive"
send_post_request "$url"
;;
delay)
if [[ -z "${servers[$arg1]}" || -z "$arg2" ]]; then
echo "Usage: delay <node_idx> <milliseconds>"
continue
fi
url="http://$(increment_port "${servers[$arg1]}")/delay"
json="{\"delay_ms\": $arg2}"
send_post_request "$url" "$json"
;;
*)
echo "Invalid command. Use 'h' for help."
;;
esac
done