-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.py
330 lines (299 loc) · 10.9 KB
/
http_client.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
# test a Python client calling our cherrypy server main.py
# command shell commands:
# arp -a # inspect LAN - local area network
# ipconfig # summary of this computer's IP environment
# IP addresses that may be useful:
# 192.168.0.1 # router for LAN (password needed)
# https://stackoverflow.com/questions/11763976/python-http-client-json-request-and-response-how
from pprint import pprint
import http.client
import json
import threading
import inspect
from http_util import IP_ADDRESS, IP_PORT
last_index = 0
debugging = False
debug_in = False # in from server
debug_out = False # out to server
simulate_fifo_out = True # allows for single-computer mode bypassing the server
allow_single_computer = True
conn = None
server_started = False # we think that server started normally
use_server = True # allow program to work in single user mode
def start_server():
global conn, server_started, use_server
try:
conn = http.client.HTTPConnection(IP_ADDRESS, IP_PORT, timeout=100)
print(conn)
conn.request('GET', '/echo?text=whatever')
response = conn.getresponse().read()
assert response == b'whatever', 'Echo test failed.'
server_started = True
use_server = True
except Exception as e:
conn = None
server_fifo_started = False
use_server = False
print(f'Open connection: server is not responding.\n {e}')
if allow_single_computer:
print('Using "single-computer" mode.')
else:
print('Single-computer mode not allowed (http_client boolean)')
start_server()
def stop_server():
global use_server, server_started
HTTPResponse.closed()
conn.close()
use_server = False
server_started = False
def test_server():
# test that server is communicating, this called by game for display
global conn, use_server
try:
conn.request('GET', '/echo?text=server+is+working')
response = conn.getresponse().read()
except Exception as e:
conn = None
start_server()
if conn:
response = 'test_server restarted server.'
else:
response = f'test_server, exception found\n {e}'
else:
use_server = True
response = 'test_server, ok'
return response
server_lock = False
def lock_server():
global server_lock
if server_lock: return False
server_lock = True
return True
def wait_for_server_lock():
global server_lock
from time import time
t = time() + 2.0 # seconds
while not lock_server():
if time() > t:
server_lock = False
raise Exception('Could not lock server (server already locked).')
def unlock_server():
global server_lock
server_lock = False
def request_wait_print(mode, url):
global conn
conn.request(mode, url)
response = conn.getresponse().read()
print(response)
def reset_server_fifo():
# clear server fifos so as to start over and preserve memory
# clear fifos in chronological order (to prevent race conditions)
global conn, use_server
function_fifo_in = []
if use_server:
while not lock_server():
pass
try:
conn.request('GET', '/reset_fifo')
response = conn.getresponse().read()
except Exception as e:
use_server = False
print(f"reset_server_fifo: comm error: \n {e}")
unlock_server()
function_fifo_out = []
return use_server
def isnamedtupleinstance(x):
# https://stackoverflow.com/questions/2166818/how-to-check-if-an-object-is-an-instance-of-a-namedtuple
t = type(x)
b = t.__bases__
if len(b) != 1 or b[0] != tuple: return False
f = getattr(t, '_fields', None)
if not isinstance(f, tuple): return False
return all(type(n)==str for n in f)
def serialize_structure(value):
# expects a function parameter
# expects that param is in (class, container, simple type)
if type(value) in (int, float, str, dict, list, tuple):
value1 = value
elif isnamedtupleinstance(value):
fields = value._fields
value1 = {key: value[i] for i, key in enumerate(fields)}
value1.update({'_my_type_': 'namedtuple'})
elif type(value).__name__ in ('Event', 'Game', 'Board'):
value1 = value.__dict__
value1.update({'_my_type_': 'class'})
else:
raise Exception('Type is not known and cannot be converted '+
'to be JSON compatible')
return value1
def dict_to_class(d):
class MyObject:
def __init__(self, d):
for key, value in d.items():
setattr(self, key, value)
return MyObject(d)
def deserialize_structure(value):
# takes a single serialized param and deserializes in
if type(value) == dict and '_my_type_' in value and value['_my_type_']:
value1 = dict_to_class(value)
else:
value1 = value
return value1
def serialize_fifo_dict(d):
# expects a dictionary containing {funcname: params}
# expects that param elements in (class, container, simple type)
funcname = list(d.keys())[0]
params = d[funcname]
serialized_params = [serialize_structure(value) for value in params]
return {funcname: serialized_params}
def deserialize_fifo_dict(d):
# expects a serialized fifo dictionary containing {funcname: serialized_params}
funcname = list(d.keys())[0]
serialized_params = d[funcname]
params = [deserialize_structure(value) for value in serialized_params]
return {funcname: params}
def post_dictionary(d):
# send dictionary to the server fifo
global conn
js0 = json.dumps(d)
wait_for_server_lock()
try:
if debug_out: print('client sends POST: ' + js0)
headers = {'Content-type': 'application/json'}
conn.request('POST', '/fifo_in', js0, headers)
js = conn.getresponse().read()
response = json.loads(js)
if debug_out: print('client gets response to POST:', response)
finally:
unlock_server()
return response
def get_dictionary(index):
# get dictionary from fifo
global conn
# receive a dictionary from the server fifo
url = '/fifo_out/?index=' + str(index)
if not lock_server():
return None
try:
if debug_in: print('client sends GET request:', url)
conn.request('GET', url)
js = conn.getresponse().read()
response = json.loads(js)
if debug_in: print('client gets response to GET request:', response)
finally:
unlock_server()
return response
remote_functions = {} # map from function name to function
function_fifo_in = [] # function name and params to post to server
function_fifo_out = [] # function name and params to get from server
execute_mode = False # does the decorator re-route the command or execute?
def pass_server_respons_to_fifo_out():
# GET data from server and save in function_fifo_out
global last_index
index = last_index + 1
response = get_dictionary(index)
indexes = [int(x) for x in response.keys()]
last_index = max(last_index, max(indexes) if indexes else 0)
d = {int(k): response[k] for k in response}
d1 = {k: deserialize_fifo_dict(d[k]) for k in d}
function_fifo_out.append(d1)
def pass_fifo_in_to_server():
global function_fifo_in, function_fifo_out, last_index, use_server
# pop from function_fifo_in and POST to server
d0 = function_fifo_in.pop(0)
d1 = d0
d1 = serialize_fifo_dict(d0)
if debug_out: pprint(d1)
try:
post_dictionary(d1)
except Exception as e:
print(f'pass_fifo_in_to_server: unable to post data to server:\n {e}')
use_server = False
if simulate_fifo_out:
# simulate fifo_out based on fifo_in
response = {last_index + 1: d0}
function_fifo_out.append(response)
def check_server_for_function():
# checks and calls saved functions with their parameters
# does not filter out commands sent by this client
global conn, use_server, function_fifo_in, function_fifo_out, last_index
if use_server: # connection to server exists
try:
pass_server_respons_to_fifo_out()
except Exception as e:
print(f'check_server_for_data: server might not be running.\n {e}')
use_server = False
while function_fifo_out:
items = function_fifo_out.pop(0)
for key in items:
last_index = key
item = items[key]
function_name = list(item.keys())[0]
params = item[function_name]
foo = remote_functions[function_name]
foo(*params)
def send_to_server(func):
# A decorator function (@)
# saves function name and reference in a dict
# sends function name and parameters to server when "inner" is called
global server_started, use_server, function_fifo_in
if not server_started and use_server:
start_server()
# use function id if the func name is "inner", used in @fifo_run_queue
func_name = func.__name__ if func.__name__ != "inner" else str(hex(id(func)))
if func.__name__ not in remote_functions:
remote_functions.update({func_name: func})
def inner(*p):
global use_server
function_fifo_in.append({func_name: p})
if use_server:
try:
pass_fifo_in_to_server()
except Exception as e:
print(f'send_to_server: server might not be running:\n {e}')
use_server = False
#func(*p)
elif allow_single_computer:
func(*p)
return # this method does not support return values
return inner
if __name__ == "__main__":
debugging = True
debug_in = True
debug_out = True
echo_test = False
server_fifo_test = False
decorator_fifo_test = False
serialize_test = True
reset_server_fifo()
if echo_test:
request_wait_print('GET', '/')
request_wait_print('GET', '/echo')
request_wait_print('GET', '/echo?text=This+text%20should+echo')
if server_fifo_test:
last_index = 2
d = {33: (3,4,5)}
post_dictionary(d)
d1 = {44: (8,4,2)}
post_dictionary(d)
d2 = get_dictionary(last_index + 1)
indexs = [int(s) for s in d2.keys()]
last_index = max([last_index] + indexs)
print('last_index:', last_index)
if decorator_fifo_test:
@send_to_server
def divide(a, b): print(a/b)
divide(2, 3)
divide(5, 2)
check_server_for_function()
if serialize_test:
class Event:
def __init__(self, x, y, ch):
self.x, self.y, self.ch = x, y, ch
event = Event(200,250,'g')
@send_to_server
def print_event(event):
x, y, ch = event.x, event.y, event.ch
print(f"Event({x},{y},{ch})")
print_event(event)
check_server_for_function()