forked from stufisher/LiveOSC2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LO2OSC.py
70 lines (48 loc) · 2.08 KB
/
LO2OSC.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
import OSC
import socket
import sys
import errno
class LO2OSC(object):
@staticmethod
def set_log(func):
LO2OSC.log_message = func
@staticmethod
def release_attributes():
LO2OSC.log_message = None
def __init__(self, remotehost = '127.0.0.1', remoteport=9000, localhost='127.0.0.1', localport=9001):
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._socket.setblocking(0)
self._local_addr = (localhost, localport)
self._remote_addr = (remotehost, remoteport)
self._socket.bind(self._local_addr)
self.log_message('LiveOSC2 starting on: ' + str(self._local_addr) + ', remote addr: '+ str(self._remote_addr))
self._callback_manager = OSC.CallbackManager()
self._callback_manager.add('/live/set_peer', self._set_peer)
def send(self, address, msg):
oscmsg = OSC.OSCMessage(address, msg)
self._socket.sendto(oscmsg.getBinary(), self._remote_addr)
def send_message(self, message):
self._socket.sendto(message.getBinary(), self._remote_addr)
def process(self):
try:
while 1:
self._data, self._addr = self._socket.recvfrom(65536)
try:
self._callback_manager.handle(self._data, self._addr)
except Exception, e:
self.log_message('LiveOSC: error handling message ' + str(e))
self.send('/live/error', (str(sys.exc_info())))
except Exception, e:
#self.log_message('LiveOSC: Error: '+str(e))
err, msg = e
if err != errno.EAGAIN:
self.log_message('LiveOSC: error handling message ' + str(error) + ' ' + str(msg))
def shutdown(self):
self._socket.close()
def _set_peer(self, msg, source):
host = msg[2]
if host == '':
host = source[0]
port = msg[3]
self.log_message('LiveOSC2: reconfigured to send to ' + host + ':' + str(port))
self._remote_addr = (host, port)