-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosc_tcp_server.py
114 lines (93 loc) · 3.35 KB
/
osc_tcp_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
# This file is a derivation of work on - and as such shares the same
# licence as - Linux Show Player
#
# Linux Show Player:
# Copyright 2012-2022 Francesco Ceruti <ceppofrancy@gmail.com>
#
# This file:
# Copyright 2022 s0600204
#
# Linux Show Player 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.
#
# Linux Show Player 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 Linux Show Player. If not, see <http://www.gnu.org/licenses/>.
import logging
from threading import Lock
from liblo import ServerError, ServerThread, TCP
from lisp.core.signal import Signal
from lisp.core.util import get_lan_ip
from lisp.ui.ui_utils import translate
logger = logging.getLogger(__name__)
class OscTcpServer:
def __init__(self, port):
self._port = port
self._srv = None
self._running = False
self._lock = Lock()
self._methods = []
self.new_message = Signal()
@property
def port(self):
return self._port
@port.setter
def port(self, port):
self._port = port
self.stop()
self.start()
@property
def ip(self):
return get_lan_ip()
@property
def url(self):
return self._srv.url if self._running else None
def is_running(self):
return self._running
def register_method(self, callback, path=None, types=None):
self._methods.append((callback, path, types))
def start(self):
if self._running:
return
try:
self._srv = ServerThread(self._port, TCP)
for method in self._methods:
self._srv.add_method(method[1], method[2], method[0])
self._srv.add_method(None, None, self.new_message.emit, self._srv)
self._srv.start()
self._running = True
logger.info(
translate(
"OscServerInfo", "OSC server started at {}"
).format(self._srv.url)
)
except ServerError:
logger.exception(
translate("OscServerError", "Cannot start OSC server")
)
def stop(self):
if self._srv is not None:
with self._lock:
if self._running:
self._srv.stop()
self._running = False
self._srv.free()
logger.info(translate("OscServerInfo", "OSC server stopped"))
def send(self, address, path, *args):
if not address.hostname:
return False
with self._lock:
if self._running:
try:
self._srv.send(address, path, *args)
except OSError: # "Broken Pipe"
# It appears we can ignore this, as subsequent messages still get sent,
# but not catching it causes LiSP to crash to desktop.
logger.warning(f"Hiccup in connection when sending message.")
return True