-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
239 lines (168 loc) · 5.71 KB
/
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
from flask import Flask
import socketio
from rich import print
from rich.traceback import install
from planager.ToolLibrary import ToolLibrary
from planager.Toolchain import Toolchain
from planager.logging import message, error, debug
install()
app = Flask(__name__)
TOOL_LIBRARY_PATH = "tools"
tool_library = ToolLibrary(tool_library_path=TOOL_LIBRARY_PATH)
tool_library.build_index()
ASYNC_MODE = "eventlet"
ORIGINS = "*"
ENGINEIO_LOGGER = False
LOGGER = False
BUFFER = 10000000
static_files = {"/": "./dist/"} # Static files are served from the dist folder
sio = socketio.Server(
cors_allowed_origins=ORIGINS,
async_mode=ASYNC_MODE,
logger=LOGGER,
engineio_logger=ENGINEIO_LOGGER,
max_http_buffer_size=BUFFER,
)
app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app, static_files=static_files)
def send_toolchain_info(info):
sio.emit("toolchain_info", info)
@sio.event
def connect(sid, environ, auth):
message("Connected to ", sid)
@sio.event
def disconnect(sid):
message("Disconnected from ", sid)
@sio.on("*")
def handle_message(event, sid, data):
debug("Socket " + sid + " received unhandled event type: " + event)
message("Message: ", data)
@sio.event
def new_toolchain(sid):
debug("Creating a new toolchain!")
new_toolchain = Toolchain(socket=sio)
sio.save_session(sid, {"toolchain": new_toolchain})
@sio.event
def get_toolchain(sid):
"""Gets the toolchain stored in the session.
Checks to see if there is a toolchain in the session. If not, creates and
returns a new toolchain.
Returns:
JSON: The JSON specification for the toolchain.
"""
with sio.session(sid) as session:
if "toolchain" in session:
return session.get("toolchain").toJSON()
return {}
@sio.event
def get_toolchain_info(sid):
"""Gets the toolchain stored in the session.
Checks to see if there is a toolchain in the session. If not, creates and
returns a new toolchain.
Returns:
JSON: The JSON specification for the toolchain.
"""
with sio.session(sid) as session:
if "toolchain" in session:
return session.get("toolchain").info()
return {}
@sio.event
def set_toolchain(sid, toolchain_config):
"""Creates a new Toolchain from a configuration file and adds it to the
session.
Returns:
dict: toolchain JSON formatting
"""
with sio.session(sid) as session:
new_toolchain = Toolchain(
tool_library=tool_library, src=toolchain_config, socket=sio
)
session["toolchain"] = new_toolchain
return new_toolchain.toJSON()
@sio.event
def clear_toolchain(sid):
"""Removes the current toolchain from the session
Returns:
dict: Dictionary containing the result message.
"""
session.pop("toolchain", None)
new_toolchain = Toolchain()
session["toolchain"] = new_toolchain
session.modified = True
return {"message": "OK"}
@sio.event
def get_tool_library(sid):
"""Endpoint for retreiving the tool library.
Returns:
dict: A dictionary of categories of the tools in the tool library.
"""
return tool_library.get_tools()
@sio.event
def add_tool(sid, req):
"""Adds a tool to the current toolchain.
Retrieves the current toolchain from the session and calls its add_tool()
method, passing in the tool name from the request JSON.
Returns:
JSON: a json version of the tool that was created.
"""
tool_class = tool_library.get_tool_class(req["category"], req["tool"])
if not tool_class:
error("Error! Could not find that tool!")
return
session = sio.get_session(sid)
try:
new_tool = session.get("toolchain").add_tool(tool_class)
except BaseException as err:
error(f"Error adding tool: Unexpected {err=}, {type(err)=}")
raise
send_toolchain_info(session.get("toolchain").info())
return new_tool.toJSON()
@sio.event
def remove_tool(sid, tool_id):
"""Removes the specified tool from the toolchain.
Removes the specified tool from the toolchain and returns the tool that was
removed.
"""
with sio.session(sid) as session:
removed_tool = session.get("toolchain").remove_tool(tool_id)
return removed_tool.toJSON()
@sio.event
def add_pipe(sid, pipe_info):
"""Adds a pipe between two tools in the current toolchain.
Unpacks the request JSON containing a dictionary containing origin_tool_id,
origin_port_id, destination_tool_id, and destination_port_id. These are
passed to the toolchains's add_pipe method.
Returns:
dict: the data about the link that was created
"""
with sio.session(sid) as session:
session.get("toolchain").add_pipe(
pipe_info["origin_tool_id"],
pipe_info["origin_port_id"],
pipe_info["destination_tool_id"],
pipe_info["destination_port_id"],
)
message("PLUMBING: ", "Pipe hooked up.")
return pipe_info
@sio.event
def remove_pipe(sid):
"""Removes a pipe between two tools/ports in the toolchain"""
pass
@sio.event
def update_tool_coordinates(sid, msg):
with sio.session(sid) as session:
session.get("toolchain").update_tool_coordinates(
msg["tool_id"], msg["coordinates"]
)
return {"msg": "ok"}
@sio.event
def update_view_coordinates(sid, msg):
# TODO: How can we separate out the client info (like view and tool coords) from the
# toolchain info?
with sio.session(sid) as session:
session.get("toolchain").update_tool_coordinates(
msg["tool_id"], msg["coordinates"]
)
return {"msg": "ok"}
if __name__ == "__main__":
import eventlet
eventlet.wsgi.server(eventlet.listen(("", 5000)), app)