-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmvrx_protocol.py
508 lines (434 loc) · 18.7 KB
/
mvrx_protocol.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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# Copyright vanous
#
# This file is part of BlenderDMX.
#
# BlenderDMX is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# BlenderDMX is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import time
import traceback
# from dmx import bl_info as application_info
import uuid as py_uuid
import bpy
from .logging import DMX_Log
from .mvrxchange import mvrx_tcp_client as mvrx_client
from .mvrxchange import mvrx_tcp_server as mvrx_server
from .mvrxchange import mvrx_ws_client as mvrx_ws_client
from .mvrxchange.mvrx_message import mvrx_message
# Instances and callbacks for the MVR TCP client and servers
class DMX_MVR_X_Client:
"""This is a blender specific instance of the MVR-xchange client
(mvrx_protocol-mvrxchange_client-client) TCP connection"""
_instance = None
def __init__(self):
super(DMX_MVR_X_Client, self).__init__()
self._dmx = bpy.context.scene.dmx
self.client = None
self.selected_client = None
prefs = bpy.context.preferences.addons[__package__].preferences
application_uuid = prefs.get(
"application_uuid", str(py_uuid.uuid4())
) # must never be 0
if self._dmx.mvrx_per_project_station_uuid:
application_uuid = self._dmx.project_application_uuid
self.application_uuid = application_uuid.upper()
# print("bl info", application_info) # TODO: use this in the future
@staticmethod
def tcp_client_callback(data):
# TODO: rework this from a keyword based parsing to message Type based parsing
station_uuid = ""
if "StationUUID" in data:
station_uuid = data["StationUUID"]
if "Commits" in data and station_uuid != "":
DMX_MVR_X_Client._instance._dmx.createMVR_Commits(
data["Commits"], station_uuid
)
if "Type" in data and station_uuid != "":
if data["Type"] == "MVR_COMMIT":
DMX_MVR_X_Client._instance._dmx.createMVR_Commits([data], station_uuid)
if "FileUUID" in data and station_uuid != "":
DMX_MVR_X_Client._instance._dmx.createMVR_Commits([data], station_uuid)
if "Provider" in data and station_uuid != "":
provider = data["Provider"]
station_name = None
if "StationName" in data:
station_name = data["StationName"]
DMX_MVR_X_Client._instance._dmx.updateMVR_Client(
provider=provider, station_uuid=station_uuid, station_name=station_name
)
if "file_downloaded" in data:
DMX_MVR_X_Client._instance._dmx.fetched_mvr_downloaded_file(
data["file_downloaded"]
)
msg_type = data.get("Type", "")
msg_ok = data.get("OK", "")
msg_message = data.get("Message", "")
dmx = bpy.context.scene.dmx
if msg_type == "MVR_JOIN_RET" and msg_ok is False:
DMX_Log.log.error("MVR-xchange client refused our connection")
dmx.mvrx_enabled = False
if msg_type == "MVR_REQUEST_RET" and msg_ok is False:
DMX_Log.log.error("MVR-xchange file request declined")
commit = DMX_MVR_X_Client._instance.client.commit
if commit:
dmx.request_failed_mvr_downloaded_file(commit)
@staticmethod
def create_self_request_commit(mvr_commit):
"""used when requesting commit ourselves just by mvr_request"""
uuid = mvr_commit["FileUUID"]
station_uuid = mvr_commit["StationUUID"]
DMX_MVR_X_Client._instance._dmx.createMVR_Commits([mvr_commit], station_uuid)
clients = bpy.context.window_manager.dmx.mvr_xchange.mvr_xchange_clients
for client in clients:
if client.station_uuid == station_uuid:
for mvr_commit in client.commits:
if mvr_commit.commit_uuid == uuid:
mvr_commit.self_requested = True
return mvr_commit
@staticmethod
def request_file(commit):
if not DMX_MVR_X_Client._instance:
return
if DMX_MVR_X_Client._instance.client:
dmx = bpy.context.scene.dmx
ADDON_PATH = dmx.get_addon_path()
path = os.path.join(
ADDON_PATH, "assets", "mvrs", f"{commit.commit_uuid.upper()}.mvr"
)
DMX_Log.log.debug(f"path {path}")
try:
if not DMX_MVR_X_Client._instance.client.running:
DMX_MVR_X_Client.connect()
DMX_MVR_X_Client._instance.client.request_file(commit, path)
except Exception as e:
DMX_Log.log.debug(f"problem requesting file {e}")
return
DMX_Log.log.info("Requesting file")
@staticmethod
def send_commit(commit):
if not DMX_MVR_X_Client._instance:
return
if DMX_MVR_X_Client._instance.client:
try:
DMX_Log.log.debug(f"re-joining")
if not DMX_MVR_X_Client._instance.client.running:
DMX_MVR_X_Client.connect()
DMX_MVR_X_Client._instance.client.send_commit(commit)
except Exception as e:
DMX_Log.log.debug(f"problem re_joining {e}")
return
@staticmethod
def re_join():
if not DMX_MVR_X_Client._instance:
return
if DMX_MVR_X_Client._instance.client:
try:
DMX_Log.log.debug(f"re-joining")
if not DMX_MVR_X_Client._instance.client.running:
DMX_MVR_X_Client.connect()
DMX_MVR_X_Client._instance.client.join_mvr()
except Exception as e:
DMX_Log.log.debug(f"problem re_joining {e}")
return
@staticmethod
def connect():
if not DMX_MVR_X_Client._instance:
return
try:
client = DMX_MVR_X_Client._instance.selected_client
DMX_Log.log.info(
f"Connecting to MVR-xchange client {client.ip_address} {client.port}"
)
DMX_MVR_X_Client._instance.client = mvrx_client.client(
client.ip_address,
client.port,
timeout=0,
callback=DMX_MVR_X_Client.tcp_client_callback,
application_uuid=DMX_MVR_X_Client._instance.application_uuid,
)
except Exception as e:
DMX_Log.log.error(f"Cannot connect to host {e}")
return
DMX_MVR_X_Client._instance.client.start()
DMX_Log.log.debug("thread started")
@staticmethod
def join(client):
DMX_MVR_X_Client._instance = DMX_MVR_X_Client()
DMX_MVR_X_Client._instance.selected_client = client
DMX_MVR_X_Client.connect()
DMX_MVR_X_Client._instance.client.join_mvr()
# TODO do only if we get OK ret
DMX_Log.log.info("Joining")
@staticmethod
def disable():
if DMX_MVR_X_Client._instance:
if DMX_MVR_X_Client._instance.client:
DMX_MVR_X_Client._instance.client.stop()
DMX_MVR_X_Client._instance = None
DMX_Log.log.info("Disabling MVR client")
@staticmethod
def leave():
if DMX_MVR_X_Client._instance:
if DMX_MVR_X_Client._instance.client:
DMX_MVR_X_Client.connect()
DMX_MVR_X_Client._instance.client.leave_mvr()
time.sleep(0.3)
DMX_MVR_X_Client._instance.client.stop()
DMX_MVR_X_Client._instance = None
DMX_Log.log.info("Disabling MVR")
class DMX_MVR_X_Server:
"""This is an instance of the blender specific mvr_xchange TCP server
(mvrx_protocol-mvrxchange_server-server) for incoming connections"""
_instance = None
def __init__(self):
super(DMX_MVR_X_Server, self).__init__()
self._dmx = bpy.context.scene.dmx
self.server = None
prefs = bpy.context.preferences.addons[__package__].preferences
application_uuid = prefs.get(
"application_uuid", str(py_uuid.uuid4())
) # must never be 0
if self._dmx.mvrx_per_project_station_uuid:
application_uuid = self._dmx.project_application_uuid
self.application_uuid = application_uuid.upper()
# print("bl info", application_info) # TODO: use this in the future
@staticmethod
def tcp_server_callback(json_data, data):
# TODO: rework this from a keyword based parsing to message Type based parsing
DMX_Log.log.debug(("callback", json_data, data, type(json_data), type(data)))
addr, port = data.addr
provider = ""
station_name = ""
station_uuid = ""
if "StationUUID" in json_data:
station_uuid = json_data["StationUUID"]
if "Commits" in json_data and station_uuid != "":
DMX_MVR_X_Server._instance._dmx.createMVR_Commits(
json_data["Commits"], station_uuid
)
if "FileUUID" in json_data and station_uuid != "":
DMX_MVR_X_Server._instance._dmx.createMVR_Commits([json_data], station_uuid)
if "Type" in json_data and station_uuid != "":
if json_data["Type"] == "MVR_COMMIT":
DMX_MVR_X_Server._instance._dmx.createMVR_Commits(
[json_data], station_uuid
)
@staticmethod
def enable():
if DMX_MVR_X_Server._instance:
return
DMX_MVR_X_Server._instance = DMX_MVR_X_Server()
try:
DMX_MVR_X_Server._instance.server = mvrx_server.server(
callback=DMX_MVR_X_Server.tcp_server_callback,
uuid=DMX_MVR_X_Server._instance.application_uuid,
)
except Exception as e:
DMX_Log.log.error(f"Cannot connect to host {e}")
return
DMX_MVR_X_Server._instance.server.start()
@staticmethod
def get_port():
if DMX_MVR_X_Server._instance:
return DMX_MVR_X_Server._instance.server.get_port()
@staticmethod
def disable():
if DMX_MVR_X_Server._instance:
if DMX_MVR_X_Server._instance.server:
DMX_MVR_X_Server._instance.server.stop()
DMX_MVR_X_Server._instance = None
DMX_Log.log.info("Disabling MVR")
class DMX_MVR_X_WS_Client:
"""Websocket client"""
_instance = None
def __init__(self):
super(DMX_MVR_X_WS_Client, self).__init__()
self._dmx = bpy.context.scene.dmx
self.client = None
self.server_url = None
prefs = bpy.context.preferences.addons[__package__].preferences
application_uuid = prefs.get(
"application_uuid", str(py_uuid.uuid4())
) # must never be 0
if self._dmx.mvrx_per_project_station_uuid:
application_uuid = self._dmx.project_application_uuid
self.application_uuid = application_uuid.upper()
# print("bl info", application_info) # TODO: use this in the future
@staticmethod
def ws_client_callback(data):
# TODO: rework this from a keyword based parsing to message Type based parsing
DMX_Log.log.debug(("websocket data", data))
station_uuid = ""
if "StationUUID" in data:
station_uuid = data["StationUUID"]
if "Commits" in data and station_uuid != "":
DMX_MVR_X_WS_Client._instance._dmx.createMVR_WS_Commits(
data["Commits"], station_uuid
)
if "Type" in data and station_uuid != "":
if data["Type"] == "MVR_COMMIT":
DMX_MVR_X_WS_Client._instance._dmx.createMVR_WS_Commits(
[data], station_uuid
)
if "FileUUID" in data and station_uuid != "":
DMX_MVR_X_WS_Client._instance._dmx.createMVR_WS_Commits(
[data], station_uuid
)
if "Provider" in data and station_uuid != "":
provider = data["Provider"]
station_name = None
if "StationName" in data:
station_name = data["StationName"]
DMX_MVR_X_WS_Client._instance._dmx.updateMVR_Client(
provider=provider,
station_uuid=station_uuid,
station_name=station_name,
service_name="",
ip_address="0",
port=0,
)
if "file_downloaded" in data:
DMX_MVR_X_WS_Client._instance._dmx.fetched_mvr_downloaded_ws_file(
data["file_downloaded"]
)
msg_type = data.get("Type", "")
msg_ok = data.get("OK", "")
msg_message = data.get("Message", "")
dmx = bpy.context.scene.dmx
if msg_type == "MVR_JOIN_RET" and msg_ok is False:
DMX_Log.log.error("MVR-xchange client refused our connection")
dmx.mvrx_enabled = False
if msg_type == "MVR_REQUEST_RET" and msg_ok is False:
DMX_Log.log.error("MVR-xchange file request declined")
commit = DMX_MVR_X_WS_Client._instance.client.commit
if commit:
dmx.request_failed_mvr_downloaded_ws_file(commit)
if "Type" in data:
if data["Type"] == "MVR_REQUEST":
dmx = bpy.context.scene.dmx
local_path = dmx.get_addon_path()
file_uuid = data["FileUUID"]
if not file_uuid:
shared_commits = (
bpy.context.window_manager.dmx.mvr_xchange.shared_commits
)
if len(shared_commits):
last_commit = shared_commits[-1]
file_uuid = last_commit.commit_uuid
DMX_Log.log.debug("Sharing last version")
ADDON_PATH = dmx.get_addon_path()
file_path = os.path.join(
ADDON_PATH, "assets", "mvrs", f"{file_uuid.upper()}.mvr"
)
DMX_Log.log.debug("sending file")
if not os.path.exists(file_path):
DMX_Log.log.error(
"MVR file for sending via MVR-xchange does not exist"
)
DMX_MVR_X_WS_Client._instance.client.send(
mvrx_message.create_message(
"MVR_REQUEST_RET",
ok=False,
nok_reason="Requested file does not exist",
)
)
return
chunk_size = 8192
with open(file_path, "br") as f:
buffer = f.read(chunk_size)
DMX_MVR_X_WS_Client._instance.client.send(buffer)
buffer = f.read(chunk_size)
while buffer:
DMX_MVR_X_WS_Client._instance.client.send(buffer)
buffer = f.read(chunk_size)
@staticmethod
def create_self_request_commit(mvr_commit):
"""used when requesting commit ourselves just by mvr_request"""
uuid = mvr_commit["FileUUID"]
station_uuid = mvr_commit["StationUUID"]
DMX_MVR_X_Server._instance._dmx.createMVR_Commits([mvr_commit], station_uuid)
clients = bpy.context.window_manager.dmx.mvr_xchange.mvr_xchange_clients
for client in clients:
if client.station_uuid == station_uuid:
for mvr_commit in client.commits:
if mvr_commit.commit_uuid == uuid:
mvr_commit.self_requested = True
return mvr_commit
@staticmethod
def request_file(commit):
if not DMX_MVR_X_WS_Client._instance:
return
if DMX_MVR_X_WS_Client._instance.client:
dmx = bpy.context.scene.dmx
ADDON_PATH = dmx.get_addon_path()
path = os.path.join(
ADDON_PATH, "assets", "mvrs", f"{commit.commit_uuid.upper()}.mvr"
)
DMX_Log.log.debug(f"path {path}")
try:
DMX_MVR_X_WS_Client._instance.client.request_file(commit, path)
except Exception as e:
DMX_Log.log.debug(f"problem requesting file {e}")
return
DMX_Log.log.info("Requesting file")
@staticmethod
def re_join():
if not DMX_MVR_X_WS_Client._instance:
return
if DMX_MVR_X_WS_Client._instance.client:
try:
DMX_Log.log.debug(f"re-joining")
DMX_MVR_X_WS_Client._instance.client.join_mvr()
except Exception as e:
DMX_Log.log.debug(f"problem re_joining {e}")
return
@staticmethod
def connect():
if not DMX_MVR_X_WS_Client._instance:
return
try:
url = DMX_MVR_X_WS_Client._instance.server_url
DMX_Log.log.info(f"Connecting to MVR-xchange client {url}")
DMX_MVR_X_WS_Client._instance.client = mvrx_ws_client.WebSocketClient(
url,
callback=DMX_MVR_X_WS_Client.ws_client_callback,
application_uuid=DMX_MVR_X_WS_Client._instance.application_uuid,
)
except Exception as e:
DMX_Log.log.error(f"Cannot connect to host {e}")
return
DMX_MVR_X_WS_Client._instance.client.start()
DMX_Log.log.debug("thread started")
@staticmethod
def join(server_url):
DMX_MVR_X_WS_Client._instance = DMX_MVR_X_WS_Client()
DMX_MVR_X_WS_Client._instance.server_url = server_url
DMX_MVR_X_WS_Client.connect()
# we send the mvr join automatically in the connected event
@staticmethod
def disable():
if DMX_MVR_X_WS_Client._instance:
if DMX_MVR_X_WS_Client._instance.client:
DMX_MVR_X_WS_Client._instance.client.stop()
DMX_MVR_X_WS_Client._instance = None
DMX_Log.log.info("Disabling MVR")
@staticmethod
def leave():
if DMX_MVR_X_WS_Client._instance:
if DMX_MVR_X_WS_Client._instance.client:
# DMX_MVR_X_WS_Client.connect()
DMX_MVR_X_WS_Client._instance.client.leave_mvr()
time.sleep(0.1)
DMX_MVR_X_WS_Client._instance.client.stop()
DMX_MVR_X_WS_Client._instance = None
DMX_Log.log.info("Disabling MVR")