File: async_tcp_receiver.py

package info (click to toggle)
python-txosc 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 304 kB
  • sloc: python: 1,857; makefile: 8
file content (52 lines) | stat: -rwxr-xr-x 1,476 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
"""
Example of a TCP txosc receiver with Twisted.

This example is in the public domain.
"""
from twisted.internet import reactor
from txosc import osc
from txosc import dispatch
from txosc import async

def foo_handler(message, address):
    """
    Single function handler.
    """
    print("Got %s from %s" % (message, address))

class TCPReceiverApplication(object):
    """
    Example that receives UDP OSC messages.
    """
    def __init__(self, port):
        self.port = port
        self.receiver = dispatch.Receiver()
        self.receiver.addCallback("/foo", foo_handler)
        self.receiver.addCallback("/ping", self.ping_handler)
        self.receiver.addCallback("/quit", self.quit_handler)
        self.receiver.setFallback(self.fallback)
        self._server_port = reactor.listenTCP(self.port, async.ServerFactory(self.receiver))
        print("Listening on osc.tcp://127.0.0.1:%s" % (self.port))

    def ping_handler(self, message, address):
        """
        Method handler.
        """
        print("Got %s from %s" % (message, address))

    def quit_handler(self, message, address):
        """
        Quits the application.
        """
        print("Got %s from %s" % (message, address))
        reactor.stop()
        print("Goodbye.")

    def fallback(self, message, address):
        print("Got %s from %s" % (message, address))

if __name__ == "__main__":
    app = TCPReceiverApplication(17779)
    reactor.run()