File: async_udp_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 (102 lines) | stat: -rwxr-xr-x 3,170 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
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
#!/usr/bin/env python
"""
Example of a UDP 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):
    """
    Function handler for /foo
    """
    print("foo_handler")
    print("  Got %s from %s" % (message, address))

class UDPReceiverApplication(object):
    """
    Example that receives UDP OSC messages.
    """
    def __init__(self, port):
        self.port = port
        self.receiver = dispatch.Receiver()
        self._server_port = reactor.listenUDP(self.port, async.DatagramServerProtocol(self.receiver))
        print("Listening on osc.udp://localhost:%s" % (self.port))
        self.receiver.addCallback("/foo", foo_handler)
        self.receiver.addCallback("/ping", self.ping_handler)
        self.receiver.addCallback("/quit", self.quit_handler)
        self.receiver.addCallback("/ham/egg", self.ham_egg_handler)
        self.receiver.addCallback("/*/egg", self.any_egg_handler)

        # Now, let's demonstrate how to use address nodes:
        # /cheese:
        self.cheese_node = dispatch.AddressNode("cheese")
        self.cheese_node.addCallback("*", self.cheese_handler)
        self.receiver.addNode("cheese", self.cheese_node)
        # /cheese/cheddar:
        self.cheddar_node = dispatch.AddressNode("cheddar")
        self.cheddar_node.addCallback("*", self.cheese_cheddar_handler)
        self.cheese_node.addNode("cheddar", self.cheddar_node)
        
        # fallback:
        self.receiver.fallback = self.fallback
    
    def cheese_handler(self, message, address):
        """
        Method handler for /ping
        """
        print("cheese_handler")
        print("  Got %s from %s" % (message, address))

    def cheese_cheddar_handler(self, message, address):
        """
        Method handler for /cheese/cheddar
        """
        print("cheese_cheddar_handler")
        print("  Got %s from %s" % (message, address))

    def any_egg_handler(self, message, address):
        """
        Method handler for /*/egg
        """
        print("any_egg_handler")
        print("  Got %s from %s" % (message, address))
    
    def fallback(self, message, address):
        """
        Fallback for any unhandled message
        """
        print("Fallback:")
        print("  Got %s from %s" % (message, address))

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

    def ham_egg_handler(self, message, address):
        """
        Method handler for /ham/egg
        """
        print("ham_egg_handler")
        print("  Got %s from %s" % (message, address))

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

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