Commit 5d2ed4f0 authored by Admin's avatar Admin

Added TUIO server class.

parent 7f609252
#!/usr/bin/env python
from OSC import OSCServer
from logger import Logger
class TuioServer2D(Logger):
_tuio_address = ('localhost', 3333)
def __init__(self, handler_obj, **kwargs):
super(TuioServer2D, self).__init__(**kwargs)
for handler in ('point_down', 'point_up', 'point_move'):
if not hasattr(handler_obj, handler):
raise RuntimeError('Handler "%s" is not defined.' % handler)
# OSC server that listens to incoming TUIO events
self.server = OSCServer(self.__class__._tuio_address)
#self.server.addDefaultHandlers()
self.server.addMsgHandler('/tuio/2Dobj', self._receive)
self.server.addMsgHandler('/tuio/2Dcur', self._receive)
self.server.addMsgHandler('/tuio/2Dblb', self._receive)
# List of alive seddion id's
self.alive = set()
# List of session id's of points that have generated a 'point_down'
# event
self.down = set()
self.handler_obj = handler_obj
def _receive(self, addr, tags, data, source):
surface = addr[8:]
self.log('Received message <surface=%s tags="%s" data=%s source=%s>' \
% (surface, tags, data, source), 2)
msg_type = data[0]
if msg_type == 'alive':
alive = set(data[1:])
released = self.alive - alive
self.alive = alive
self.down -= released
self.down -= released
if released:
self.log('Released %s.' % ', '.join(map(str, released)), 2)
for sid in released:
self.handler_obj.point_up(sid)
elif msg_type == 'set':
sid, x, y = data[1:4]
if sid not in self.alive:
raise ValueError('Point with sid "%d" is not alive.' % sid)
# Check if 'point_down' has already been triggered. If so, trigger
# a 'point_move' event instead
if sid in self.down:
self.log('Moved %d to (%s, %s).' % (sid, x, y), 2)
self.handler_obj.point_move(sid, x, y)
else:
self.log('Down %d at (%s, %s).' % (sid, x, y), 2)
self.handler_obj.point_down(sid, x, y)
self.down.add(sid)
def start(self):
self.log('Starting OSC server')
self.server.serve_forever()
def stop(self):
self.log('Stopping OSC server')
self.server.close()
if __name__ == '__main__':
import sys
class Handler(Logger):
def point_down(self, sid, x, y):
self.log('Point down: sid=%d (%s, %s)' % (sid, x, y))
def point_up(self, sid):
self.log('Point up: sid=%d' % sid)
def point_move(self, sid, x, y):
self.log('Point move: sid=%d (%s, %s)' % (sid, x, y))
v = 1 if len(sys.argv) < 2 else int(sys.argv[1])
server = TuioServer2D(Handler(verbose=v), verbose=v)
server.start()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment