-
Notifications
You must be signed in to change notification settings - Fork 0
/
Builder.py
105 lines (87 loc) · 2.84 KB
/
Builder.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
from flask import Flask, request, g
import os
import json
from flask import Response
from flask import jsonify
from classes.Topology import Topology
app = Flask(__name__)
topology = None
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
static_folder_root = os.path.dirname(os.path.abspath(__file__))
ASSETS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
@app.route('/static/<path:filename>')
def serveStatic(filename):
root_dir = os.path.dirname(os.getcwd())
return send_from_directory(os.path.join(root_dir, 'static', 'js'), filename)
@app.route("/")
def index():
return app.send_static_file('index.html')
@app.route("/getSavedTopo")
def getSavedTopo():
try:
f = open('config.json', 'r')
data = json.load(f)
except:
data = {}
return jsonify(data)
@app.route("/getParams")
def getParams():
global topology
return topology.getParams(request.args.get('id'))
@app.route("/postParams", methods=['POST'])
def postParams():
global topology
return topology.setParams(request.form['id'], json.loads(request.form['config']))
@app.route("/postAddNode", methods=['POST'])
def postAddNode():
global topology
return topology.addNode(request.form['type'], request.form['x'], request.form['y'])
@app.route("/postDelNode", methods=['POST'])
def postDelNode():
global topology
return topology.delNode(request.form['id'])
@app.route("/postSaveTopo", methods=['POST'])
def postSaveTopo():
config = request.form['config']
try:
f = open('config.json', 'w')
f.write(config)
f.close()
except:
f.close()
return 'success'
@app.route("/postAddLink", methods=['POST'])
def postAddLink():
global topology
return topology.setLink(request.form['firstId'], request.form['secondId'])
@app.route("/postDelLink", methods=['POST'])
def postDelLink():
global topology
return topology.delLink(request.form['firstId'], request.form['secondId'])
@app.route("/getPing", methods=['GET'])
def getPing():
global topology
try:
return topology.ping(request.args.get('sender'), request.args.get('receiver'))
except:
return 'Internal error'
@app.route("/shutdown", methods=['POST'])
def shutdown():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
global topology
if request.form['param'] == 'clear':
for node in topology.nameToNode.keys():
if node.startswith('S') or node.startswith('H') or node.startswith('R'):
topology.delNode(node)
else:
topology.stop()
func()
return Response('Server shutting down...', mimetype='text/plain')
if __name__ == "__main__":
topology = Topology(topo=None)
topology.start()
app.run(host='0.0.0.0')