-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.py
executable file
·427 lines (316 loc) · 12.2 KB
/
http_server.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/python
import time
import json
import sys
import os
from collections import defaultdict, OrderedDict
try:
# python 2
from BaseHTTPServer import HTTPServer
from SocketServer import ThreadingMixIn
from SimpleHTTPServer import SimpleHTTPRequestHandler
except ImportError:
# python 3
from http.server import HTTPServer, SimpleHTTPRequestHandler
from socketserver import ThreadingMixIn
from redis_wrapper import (
RedisWrapper,
BUZZER_NOT_PRESSED,
BUZZER_PRESSED,
BUZZER_PRESSED_FAILED,
)
if __debug__:
import pdb
redis_con = RedisWrapper()
OP_HISTORY = []
OP_FUTURE = []
scoreboard_state = {}
client_buzzer_config = {}
def parse_config():
global config
global HTTP_HOST_NAME
global HTTP_PORT_NUMBER
global TEAM_LIST
global TEAM_CONFIG
global ADMIN_CONFIG
with open("./config.json") as flink:
config = json.loads(
flink.read(),
object_pairs_hook=OrderedDict,
)
HTTP_HOST_NAME = config['http']['hostname']
HTTP_PORT_NUMBER = config['http']['port']
ADMIN_CONFIG = config.get("admin", dict())
if isinstance(config['teams'], list):
TEAM_LIST = config['teams']
TEAM_CONFIG = dict()
# setup null config
for team_name in TEAM_LIST:
TEAM_CONFIG[team_name] = dict()
TEAM_CONFIG[team_name]["key"] = None
elif isinstance(config['teams'], dict):
TEAM_LIST = []
TEAM_CONFIG = dict()
for team_name, team_config in config['teams'].items():
TEAM_LIST.append(team_name)
TEAM_CONFIG[team_name] = team_config
all_teams_with_keys = all(item.get("key", None) for item in TEAM_CONFIG.values() )
all_team_with_no_keys = all(not item.get("key", None) for item in TEAM_CONFIG.values() )
if not (all_teams_with_keys or all_team_with_no_keys):
raise Exception("Not all teams have keys. Either all teams should have keys, or all teams should have NOT have them.")
if 'score_scale_factors' in ADMIN_CONFIG:
# Add unit scale factor to config within 0.1% of 1.00
# or 1.000 +- 0.001
tol = ADMIN_CONFIG['score_scale_factors'].get('tol',10**(-3)) # tolerance
has_unit_scale_factor = False
if (
'values' not in ADMIN_CONFIG['score_scale_factors'] or
not isinstance(ADMIN_CONFIG['score_scale_factors']['values'], list)
):
ADMIN_CONFIG['score_scale_factors']['values'] = []
for scale_factor in ADMIN_CONFIG['score_scale_factors']['values']:
assert isinstance(scale_factor, (int,float))
has_unit_scale_factor = has_unit_scale_factor or abs(scale_factor-1.0) <= tol
if not has_unit_scale_factor:
ADMIN_CONFIG['score_scale_factors']['values'].append(1.0)
ADMIN_CONFIG['score_scale_factors']['values'].sort()
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""
Handle requests in a separate thread.
*******************************************
Note: that ThreadingMixIn must come before HTTPServer
in the superclass list or this won't work as intended.
*******************************************
"""
class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
did_handel_request = False
if self.path == "/admin":
self.send_response(301)
self.send_header('Location','/admin.html')
self.end_headers()
did_handel_request = True
elif self.path == "/scoreboard":
self.send_response(301)
self.send_header('Location','/scoreboard.html')
self.end_headers()
did_handel_request = True
elif self.path == "/scoreboard/state":
self.send_response(200)
self.send_header('content-type','application/json')
self.end_headers()
self.wfile.write(json.dumps(scoreboard_state).encode())
did_handel_request = True
elif self.path == "/buzzer_config":
self.send_response(200)
self.send_header('content-type','application/json')
self.end_headers()
self.wfile.write(json.dumps(client_buzzer_config).encode())
did_handel_request = True
if not did_handel_request:
SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
data_string = self.rfile.read(int(self.headers['Content-Length']))
data = json.loads(data_string.decode())
print(data_string)
if data["userType"] == "player":
handle_player_post_request(self, data)
elif data["userType"] == "admin":
handle_admin_post_request(self, data)
else:
print("Not excepted userType")
self.end_headers()
HandlerClass = MyHandler
def handle_player_post_request(request, data):
if "team" not in data:
request.send_response(400)
if __debug__:
print("There is no team field in the data passed in.")
return
elif data["team"] not in TEAM_LIST:
request.send_response(400)
if __debug__:
print("This is not a valid team.")
return
elif client_buzzer_config["teams_expect_key"] and data["password"] != TEAM_CONFIG[data["team"]]["key"]:
request.send_response(403)
if __debug__:
print("Request not authorized.")
return
if not redis_con.get_is_buzzer_listening():
request.send_response(409)
if __debug__:
print("Someone else has already buzzed in.")
else:
team_buzzer_status = redis_con.get_buzzer(data["team"])
if team_buzzer_status == BUZZER_NOT_PRESSED:
# No one has buzzed in yet, including you
if __debug__:
print("You have buzzed in!")
redis_con.set_is_buzzer_listening(False)
redis_con.set_buzzer(data["team"], BUZZER_PRESSED)
update_board_state()
# GOOD REQUEST
request.send_response(200)
else:
request.send_response(409)
if __debug__:
if team_buzzer_status == BUZZER_PRESSED:
if __debug__:
print("You have already sucessfully buzzed in.")
elif team_buzzer_status == BUZZER_PRESSED_FAILED:
if __debug__:
print("You had your chance.")
else:
print("THIS SHOULD NEVER HAPPEN!")
def handle_admin_post_request(request, data):
try:
data["value"] = int(data["value"])
except:
data["value"] = 0
if client_buzzer_config["admin"]["key"] and data["password"] != ADMIN_CONFIG["key"]:
request.send_response(403)
print(data["password"],ADMIN_CONFIG["key"])
if __debug__:
print("Request not authorized.")
return
if "operation" not in data:
# Ignore
request.send_response(400)
if __debug__:
print("No admin operation was specified.")
return
request.send_response(200)
if data["operation"] == "buzzerListening":
set_to_listen = bool(data["value"])
if set_to_listen:
# We want to start listening
# Reinitialize the state
for team in TEAM_LIST:
redis_con.set_buzzer(team, BUZZER_NOT_PRESSED)
redis_con.set_is_buzzer_listening(True)
redis_con.set_is_question_listening(set_to_listen)
elif data["operation"] == "keepListening":
if redis_con.get_is_question_listening():
for team in TEAM_LIST:
if redis_con.get_buzzer(team) == BUZZER_PRESSED:
redis_con.set_buzzer(team, BUZZER_PRESSED_FAILED)
# Only begin listening after resetting the buzzer states for the teams
redis_con.set_is_buzzer_listening(True)
elif data["operation"] == "resetBuzzer":
if redis_con.get_is_question_listening():
for team in TEAM_LIST:
redis_con.set_buzzer(team, BUZZER_NOT_PRESSED)
# Only begin listening after resetting the buzzer states for the teams
redis_con.set_is_buzzer_listening(True)
elif data["operation"] in ["add","sub","set"]:
if "teams" not in data:
# Ignore
if __debug__:
print("There is no teams field in the data passed in.")
elif isinstance(data["teams"], list) and any(team_name not in TEAM_LIST for team_name in data["teams"]):
# Ignore
if __debug__:
print("This is not a valid team in the list passed.")
print(data["teams"])
for team in data["teams"]:
print(team, team in TEAM_LIST)
else:
handle_score_opperation(
teams=data["teams"],
operation=data["operation"],
value=data["value"],
)
elif data["operation"] == "undo":
handle_undo()
elif data["operation"] == "redo" :
handle_redo()
update_board_state()
def _handle_score_opperation_helper(teams,operation,value):
if operation == "add":
pass
elif operation == "sub":
value *= -1
elif operation == "set":
raise Exception()
else:
raise Exception()
for team in teams:
redis_con.increment_score(team, value)
def handle_score_opperation(teams,operation,value):
if operation == "set":
new_args = defaultdict(list)
for team in teams:
old_score = redis_con.get_score(team)
diff = value - old_score
new_operation = "add" if diff >= 0 else "sub"
diff = abs(diff)
new_args[diff,new_operation].append(team)
for (new_value,new_operation),teams in new_args.items():
handle_score_opperation(teams, new_operation, new_value)
return
_handle_score_opperation_helper(teams,operation, value)
OP_HISTORY.append( (teams,operation,value) )
while OP_FUTURE:
OP_FUTURE.pop()
def handle_undo():
team,operation,value = OP_HISTORY.pop()
OP_FUTURE.append((team,operation,value))
if operation in ["add","sub"]:
_handle_score_opperation_helper(team,operation, -value)
else:
raise Exception()
def handle_redo():
if len(OP_FUTURE)>0:
team,operation,value = OP_FUTURE.pop()
OP_HISTORY.append((team,operation,value))
_handle_score_opperation_helper(team,operation, value)
def update_board_state():
global scoreboard_state
state = {}
state["question"] = int(redis_con.get_is_question_listening())
for team in TEAM_LIST:
state[team] = {}
state[team]["score"] = redis_con.get_score(team)
state[team]["buzzer"] = int(redis_con.get_buzzer(team))
scoreboard_state = state
def create_buzzer_config_json():
global client_buzzer_config
assert all( " " not in team_name for team_name in TEAM_LIST)
assert all( "\t" not in team_name for team_name in TEAM_LIST)
assert all( "\n" not in team_name for team_name in TEAM_LIST)
temp = dict(config)
del temp['http']
if isinstance(config["teams"], dict):
temp["teams"] = TEAM_LIST
temp["teams_expect_key"] = all(item.get("key", None) for item in TEAM_CONFIG.values() )
if "admin" in config:
temp["admin"] = dict(config["admin"])
# set to bool, so that client admin page know to display key
temp["admin"]["key"] = bool(config["admin"]["key"])
else:
if not "admin" in config:
temp["admin"] = dict()
temp["admin"]["key"] = False
client_buzzer_config = temp
def start_http_server():
# HTTP SERVER SETUP
parse_config()
create_buzzer_config_json()
update_board_state()
HandlerClass.protocol_version = "HTTP/1.0"
httpd = ThreadedHTTPServer(
server_address=(HTTP_HOST_NAME,HTTP_PORT_NUMBER),
RequestHandlerClass=HandlerClass,
)
print(time.asctime(), "Server Starts - {}:{}".format(HTTP_HOST_NAME, HTTP_PORT_NUMBER))
try:
os.chdir("web/")
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print("")
print(time.asctime(), "Server Stops - {}:{}".format(HTTP_HOST_NAME, HTTP_PORT_NUMBER))
if __name__ == "__main__":
start_http_server()