forked from offtools/BLive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
125 lines (102 loc) · 3.29 KB
/
client.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# Script copyright (C) 2012 Thomas Achtner (offtools)
import sys
import types
#
# --- BLive OSC Client, used by blender:
#
# --- OSC Messages are send via Operators, with prefix "BLive_OT_osc_"
# --- These operators are defined in ops.py in the different module subfolders
from . import OSC
from .OSC import OSCClient, OSCMessage, OSCServer
class BLiveServer(OSCServer):
#~ def __new__(cls, *args, **kwargs):
#~ if not '_instance' in cls.__dict__:
#~ cls._instance = object.__new__(cls)
#~ return cls._instance
def __init__(self, ip="127.0.0.1", port=9900):
super().__init__((ip,port))
self.timeout = 0
def handle_timeout(self):
self.timed_out = True
def update(self):
self.timed_out = False
while not self.timed_out:
self.handle_request()
class BLiveServerSingleton(OSCServer):
def __new__(cls, *args, **kwargs):
if not '_instance' in cls.__dict__:
cls._instance = object.__new__(cls)
return cls._instance
def __init__(self, ip="127.0.0.1", port=9900):
if not '_init' in dir(self):
self._init = True
super().__init__((ip,port))
self.timeout = 0
def handle_timeout(self):
self.timed_out = True
def update(self):
self.timed_out = False
while not self.timed_out:
self.handle_request()
class BLiveClient(OSCClient):
def __new__(type, *args):
if not '_instance' in type.__dict__:
type._instance = object.__new__(type)
return type._instance
def __init__(self, server=None):
if not '_init' in dir(self):
super().__init__(server)
self._init = True
def quit(self):
if self.address():
super().send(OSCMessage("/quit"))
self.close()
def set_server(self, ip, port):
if not self.server:
self.setServer(BLiveServer(ip,port))
print("BLiveClient.set_server", self.server)
else:
print("BLive Error in Client set_server - server instance already exists")
def addMsgHandler(self, path, callback):
if self.server:
print("adding msghandler:", path, callback, self.server.callbacks.keys())
self.server.addMsgHandler( path, callback )
def connect(self, ip, port):
super().connect((ip,port))
self.send('/connect')
def send(self, path, *args):
if self.address():
try:
super().send(OSCMessage(path, args))
except ConnectionRefusedError as err:
print('[BLive] - connection to server lost')
self.close()
except TypeError:
print('[BLive] - connection to server lost')
self.close()
def update(self):
if self.server:
self.server.update()
def register():
print("client.register")
pass
def unregister():
print("client.unregister")
pass