New year, big checkin
[jamaendo] / jamaui / postoffice.py
diff --git a/jamaui/postoffice.py b/jamaui/postoffice.py
new file mode 100644 (file)
index 0000000..88143fe
--- /dev/null
@@ -0,0 +1,35 @@
+# message central
+
+class PostOffice(object):
+    class Client(object):
+        def __init__(self):
+            self.tags = {}
+        def has_tag(self, tag):
+            return tag in self.tags
+        def notify(self, tags, data):
+            for tag in tags:
+                cb = self.tags.get(tag)
+                if cb:
+                    cb(data)
+        def register(self, tag, callback):
+            self.tags[tag] = callback
+
+    def __init__(self):
+        self.clients = {}
+
+    def notify(self, tags, data):
+        if not isinstance(tags, list):
+            tags = [tags]
+        for client in clients:
+            client.notify(tags, data)
+
+    def register(self, client_id, tag, callback):
+        client = self.clients.get(client_id)
+        if not client:
+            client = Client()
+            self.clients[client_id] = client
+        client.register(tag, callback)
+
+postoffice = PostOffice()
+
+