-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrest_server.py
More file actions
485 lines (418 loc) · 18.3 KB
/
rest_server.py
File metadata and controls
485 lines (418 loc) · 18.3 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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
from bottle import default_app, route, debug, static_file, response, request
import os
import subprocess
import time
import json
import signal
import socket
from datetime import datetime
import __builtin__
#import BaseDEVS, DomainBehavior, DomainStructure
__builtin__.__dict__['DEVS_DIR_PATH_DICT'] = {}
from param import *
### global variables
global_running_sim = {}
global_simu_id = 0
# the decorator
def enable_cors(fn):
def _enable_cors(*args, **kwargs):
# set CORS headers
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
if request.method != 'OPTIONS':
# actual request; reply with the actual response
return fn(*args, **kwargs)
return _enable_cors
############################################################################
#
# Functions
#
############################################################################
def getYAMLFile(name):
""" Get yaml file with json format
"""
### list of yaml filename in yaml_path_dir directory
filenames = [entry for entry in os.listdir(yaml_path_dir)]
return dict([(name, open(os.path.join(yaml_path_dir, name), 'r').read())])\
if name in filenames else {}
def getYAMLFiles():
""" Get all yamls files in yaml_path_dir
"""
return dict([(entry, open(os.path.join(yaml_path_dir, entry), 'r').read())\
for entry in os.listdir(yaml_path_dir)\
if entry.endswith('.yaml')])
def getYAMLFilenames():
""" Get all yamls file names in yaml_path_dir
"""
return dict([(entry, {'last modified':str(time.ctime(os.path.getmtime(os.path.join(yaml_path_dir, entry)))), 'size':str(os.path.getsize(os.path.join(yaml_path_dir, entry))*0.001)+' ko'})\
for entry in os.listdir(yaml_path_dir)\
if entry.endswith('.yaml')])
def group(lst, n):
"""group([0,3,4,10,2,3], 2) => [(0,3), (4,10), (2,3)]
Group a list into consecutive n-tuples. Incomplete tuples are
discarded e.g.
>>> group(range(10), 3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
"""
return zip(*[lst[i::n] for i in range(n)])
def getJointJs(d):
""" return the json for JOIN
"""
### get param coming from url
name = request.params.name
### list of ConnectinShape, CodeBlock, ContainerBlock
docs = []
l = d[name].split('\r\n')
for i,raw_doc in enumerate(l):
if raw_doc and ("!!python/object:" in raw_doc or 'label: !!python/unicode' in raw_doc):
docs.append(raw_doc)
### return list of tuples of connected models
return str(group(map(lambda b: b.split(' ')[-1], filter(lambda a: 'label' in a, docs)),2))
def send_via_socket(simu_name, data):
""" send data string to the simulation identified by simu_name
"""
try:
socket_id = global_running_sim[simu_name]['socket_id']
#socket_address = '\0' + socket_id
#comm_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
#comm_socket.connect(socket_address)
comm_socket.connect(('localhost', 5555))
comm_socket.sendall(data)
status = comm_socket.recv(1024)
comm_socket.close()
except:
comm_socket.close()
raise
return status
############################################################################
#
# Routes
#
############################################################################
@route('/img/<filepath:path>')
def server_img(filepath):
return static_file(filepath, root=os.path.join(current_path,'static','img'))
############################################################################
@route('/dsp/<filepath:path>')
def server_dsp(filepath):
return static_file(filepath, root=os.path.join(current_path,'static','dsp'))
############################################################################
@route('/')
@enable_cors
def serve_homepage():
return static_file('index.html', root=os.path.join(current_path, 'static'))
############################################################################
@route('/info', method=['OPTIONS', 'GET'])
@enable_cors
def recipes_info():
""" Get server info
"""
from platform import python_version
data = {'devsimpy-version':os.path.basename(os.path.dirname(devsimpy_nogui)),
'devsimpy-libraries': filter(lambda a: a not in [".directory", "Basic", "__init__.py"], os.listdir(os.path.join(os.path.dirname(devsimpy_nogui), 'Domain'))),
'python-version': python_version(),
### TODO read __init__.py to build plugins list
'devsimpy-plugins':filter(lambda a: a not in ["__init__.py"], os.listdir(os.path.join(os.path.dirname(devsimpy_nogui), 'plugins'))),
'url-server':url_server,
'machine-server': subprocess.check_output("uname -m", shell=True),
'os-server': subprocess.check_output("uname -o", shell=True),
'machine-version-server': subprocess.check_output("uname -v", shell=True)
}
return data
############################################################################
@route('/yaml', method=['OPTIONS', 'GET'])
@enable_cors
def recipes_yaml():
""" Get yaml description
"""
if 'all' in request.params:
data = getYAMLFiles()
elif 'name' in request.params:
data = getYAMLFile(request.params.name)
elif 'filenames' in request.params:
data = getYAMLFilenames()
else:
data = {}
return { "success": data!={} and data!=[], "content": data }
############################################################################
### POST body example :
### {"filename":"test.yaml",
### "model":"RandomGenerator_0",
### "args":{"maxStep":"1",
### "maxValue":"100",
### "minStep":"1",
### "minValue":"0",
### "start":"0"}}
@route('/yaml/save', method=['OPTIONS', 'POST'])
@enable_cors
def save_yaml():
""" Update yaml file from devsimpy-mob
"""
# read POST data as JSON
data = request.json
# update filename to absolute path
data['filename'] = os.path.join(yaml_path_dir, data['filename'])
# Get the expected input string from JSON model and args data
dataS1 = json.dumps(data)
dataS2 = str(dataS1.replace("\"","'"))
# perform update (blocking operation)
cmd = ["python2.7", devsimpy_nogui, "-update", dataS2]
output = subprocess.check_output(cmd) #empty
return {'success':True, 'output':output}
############################################################################
### xxx.pythonanywhere.com/yaml/labels?name=test1.yaml
@route('/yaml/labels', method=['OPTIONS', 'GET'])
@enable_cors
def labels_yaml():
""" get the model blocks list from yaml
"""
# get the models names (blocking operation)
cmd = ["python2.7", devsimpy_nogui, "-models", os.path.join(yaml_path_dir, request.params.name)]
output = subprocess.check_output(cmd) # output is JSON, rstrip('\r\n') makes it easier to read by human
return {'success':True, 'output':eval(output.rstrip('\r\n'))}
############################################################################
### xxx.pythonanywhere.com/json?name=test1.yaml
@route('/json', method=['OPTIONS', 'GET'])
@enable_cors
def recipes_json():
"""
"""
### get param coming from url
name_param = request.params.name
### if no time limit and .dsp or .yaml file, we can simulate
if name_param.endswith(('.dsp', '.yaml')):
dsp_file = os.path.join(dsp_path_dir if name_param.endswith('.dsp') else yaml_path_dir, name_param)
### command to be executed
cmd = ["python2.7", devsimpy_nogui, dsp_file,"-json"]
### transformation completed (output is json format)
output = subprocess.check_output(cmd)
else:
### generation failed
output = {'success':False, 'info': "file does not exist!"}
return output
############################################################################
### simulate the model identified by its dsp or yaml file
### /simulate?name=test.dsp&time=10
### or /simulate?name=test.yaml&time=10
@route('/simulate', method=['OPTIONS', 'GET'])
@enable_cors
def simulate():
"""
"""
### Check that the given model name is valid
model_filename = request.params.name
path = dsp_path_dir if model_filename.endswith('.dsp') else yaml_path_dir
abs_model_filename = os.path.join(path, model_filename)
if not os.path.exists(abs_model_filename):
return {'success':False, 'info': "file does not exist! "+ abs_model_filename}
### Check that the given simulation duration is valid
sim_duration = request.params.time
if sim_duration in ('ntl', 'inf'):
sim_duration = "10000000"
if not sim_duration.isdigit():
return {'success':False, 'info': "time must be digit!"}
### Delete old result files .dat
for name in filter(lambda fn: fn.endswith('.dat') and fn.split('_')[0] == os.path.splitext(model_filename)[0], os.listdir(path)):
os.remove(os.path.join(path, name))
### Create simulation name
model_name = model_filename.split('.')[0]
global global_simu_id
global_simu_id += 1
simu_name = model_name + '_' + str(global_simu_id)
### Launch simulation
### NB : Don't set shell=True because then it is not possible to interact with the process inside the shell
socket_id = "celinebateaukessler."+simu_name # has to be unique
#--> TODO replace with DEVS+username+simu_name
# using the user name as a prefix is a convention on PythonAnywhere
cmd = ['python2.7', devsimpy_nogui, abs_model_filename, str(sim_duration), socket_id]
fout = open(simu_name+'.out', 'w+') # where simulation execution report will be written
process = subprocess.Popen(cmd, stdout=fout, stderr=subprocess.STDOUT, close_fds=True)
#output = subprocess.check_output(cmd)
#return output
# Call to Popen is non-blocking, BUT, the child process inherits the file descriptors from the parent,
# and that will include the web app connection to the WSGI server,
# so the process needs to run and finish before the connection is released and the server notices that the request is finished.
# This is solved by passing close_fds=True to Popen
global_running_sim[simu_name] = {'process':process, 'output_name':simu_name+'.out', 'socket_id':socket_id}
# TODO could be stored in a DB except for the process : stored in session variable or as a scheduled task (cf PythonAnywhere rules)
return {'success': True, 'pid': os.getpid(), 'simulation_name':simu_name}
############################################################################
### /modify
### example POST body : {"simulation_name":"test", "modelID":"A2", "paramName":"maxValue", "paramValue":"50"}
@route('/modify', method=['POST'])
@enable_cors
def modify():
data = request.json
simu_name = data['simulation_name']
if (global_running_sim.has_key(simu_name)):
global_running_sim[simu_name]['process'].poll()
if (global_running_sim[simu_name]['process'].returncode != None):
return {'success':False, 'info':"simulation " + simu_name + " is not in progress"}
#if (global_running_sim[simu_name]['paused'] == False): #TODO
# return {'success':False, 'info':"simulation " + simu_name + " is not paused"}
del data['simulation_name']
data['date'] = datetime.strftime(datetime.today(), "%Y-%m-%d %H:%M:%S")
status = send_via_socket(simu_name, json.dumps(data))
return {'status':status}
else:
return {'success':False, 'info':"no simulation in progress is named " + simu_name}
############################################################################
### /result?name=test # TODO rename to status
@route('/result', method=['OPTIONS', 'GET'])
@enable_cors
def result():
simu_name = request.params.name
if not global_running_sim.has_key(simu_name):
return {'success':False, 'info':"no simulation is named " + simu_name}
else:
global_running_sim[simu_name]['process'].poll()
if (global_running_sim[simu_name]['process'].returncode == None):
return {'success':True, 'info':"simulation " + simu_name + " is running"}
else:
fout=open(global_running_sim[simu_name]['output_name'], 'r')
output = ""
for line in fout:
output = output + line
fout.close()
#TODO del dict_proc_sim when ...?
return output#{'success':True, 'report':output}
############################################################################
### /process_pause?name=test.yaml
@route('/process_pause', method=['OPTIONS', 'GET'])
@enable_cors
def process_pause():
"""
"""
simu_name = request.params.name
if not global_running_sim.has_key(simu_name):
return {'success':False, 'info':"no simulation is named " + simu_name}
global_running_sim[simu_name]['process'].poll()
if (global_running_sim[simu_name]['process'].returncode != None):
return {'success':False, 'info':"simulation " + simu_name + " is finished"}
else:
global_running_sim[simu_name]['process'].send_signal(signal.SIGSTOP)
return {'success':True, 'info':"simulation " + simu_name + " is paused"}
############################################################################
### /pause?name=test.yaml
### suspends the simulation thread but not the wrapping process
@route('/pause', method=['OPTIONS', 'GET'])
@enable_cors
def pause():
"""
"""
simu_name = request.params.name
if not global_running_sim.has_key(simu_name):
return {'success':False, 'info':"no simulation is named " + simu_name}
global_running_sim[simu_name]['process'].poll()
if (global_running_sim[simu_name]['process'].returncode != None):
return {'success':False, 'info':"simulation " + simu_name + " is finished"}
else:
status = send_via_socket(simu_name, 'SUSPEND')
return {'success':True, 'status':status}
############################################################################
### /process_resume?name=test.yaml
@route('/process_resume', method=['OPTIONS', 'GET'])
@enable_cors
def process_resume():
"""
"""
simu_name = request.params.name
if not global_running_sim.has_key(simu_name):
return {'success':False, 'info':"no simulation is named " + simu_name}
global_running_sim[simu_name]['process'].poll()
if (global_running_sim[simu_name]['process'].returncode != None):
return {'success':False, 'info':"simulation " + simu_name + " is finished"}
else:
global_running_sim[simu_name]['process'].send_signal(signal.SIGCONT)
return {'success':True, 'info':"simulation " + simu_name + " is resumed"}
############################################################################
### /resume?name=test.yaml
@route('/resume', method=['OPTIONS', 'GET'])
@enable_cors
def resume():
"""
"""
simu_name = request.params.name
if not global_running_sim.has_key(simu_name):
return {'success':False, 'info':"no simulation is named " + simu_name}
global_running_sim[simu_name]['process'].poll()
if (global_running_sim[simu_name]['process'].returncode != None):
return {'success':False, 'info':"simulation " + simu_name + "is finished"}
else:
status = send_via_socket(simu_name, 'RESUME')
return {'success':True, 'status':status}
############################################################################
### /kill?name=test.yaml
@route('/kill', method=['OPTIONS', 'GET'])
@enable_cors
def kill():
"""
"""
simu_name = request.params.name
if not global_running_sim.has_key(simu_name):
return {'success':False, 'info':"no simulation is named " + simu_name}
global_running_sim[simu_name]['process'].poll()
if (global_running_sim[simu_name]['process'].returncode != None):
return {'success':True, 'info':"simulation " + simu_name + " is finished"}
else:
fout=open(global_running_sim[simu_name]['output_name'], 'r')
fout.close()
global_running_sim[simu_name]['process'].send_signal(signal.SIGKILL)
return {'success':True, 'info':"simulation " + simu_name + " is killed"}
############################################################################
### /pause?name=result.dat
@route('/plot', method=['OPTIONS', 'GET'])
@enable_cors
def plot():
"""
"""
filename = request.params.name
# Build the diagram data as :
# - 1 list of labels (X or Time axis) called category TBC : what if time delta are not constant???
# - 1 list of values (Y or Amplitude axis) called data
data = []
category = []
with open(os.path.join(yaml_path_dir, filename)) as fp:
for line in fp:
a,b = line.split(" ")
category.append({'label':a})
data.append({'value':b.rstrip('\r\n')})
result = {
"chart": {
"caption": filename,
"subCaption": "",
"xAxisName": "Time",
"yAxisName": "Amplitude",
"showValues": "0",
"numberPrefix": "",
"showBorder": "0",
"showShadow": "0",
"bgColor": "#ffffff",
"paletteColors": "#008ee4",
"showCanvasBorder": "0",
"showAxisLines": "0",
"showAlternateHGridColor": "0",
"divlineAlpha": "100",
"divlineThickness": "1",
"divLineIsDashed": "1",
"divLineDashLen": "1",
"divLineGapLen": "1",
"lineThickness": "3",
"flatScrollBars": "1",
"scrollheight": "10",
"numVisiblePlot": "10",
"showHoverEffect": "1"
}}
result.update({"categories": [{'category':category}], 'dataset': [{'data':data}]})
return result
############################################################################
#
# Application definition
#
############################################################################
debug(True)
application = default_app()
if __name__ == "__main__":
from paste import httpserver
httpserver.serve(application, host=url_server, port=port_server)