diff options
author | Jesse Luehrs <doy@tozt.net> | 2014-09-14 23:37:56 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2014-09-14 23:37:56 -0400 |
commit | c6633dae0e1886ac415dcb74fa0b2bde1ea48e66 (patch) | |
tree | ef27b560671ac1f7c1cac7ec91c027e8bff020ef /pubsub.py | |
parent | 033673ac065a189bc611af689c964a7f75a3a183 (diff) | |
download | python-termcast-server-c6633dae0e1886ac415dcb74fa0b2bde1ea48e66.tar.gz python-termcast-server-c6633dae0e1886ac415dcb74fa0b2bde1ea48e66.zip |
introduce a pubsub system for communication
Diffstat (limited to 'pubsub.py')
-rw-r--r-- | pubsub.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/pubsub.py b/pubsub.py new file mode 100644 index 0000000..6b860e0 --- /dev/null +++ b/pubsub.py @@ -0,0 +1,17 @@ +class Publisher(object): + def __init__(self): + self.subscribers = [] + + def subscribe(self, who): + if who not in self.subscribers: + self.subscribers.append(who) + + def unsubscribe(self, who): + if who in self.subscribers: + self.subscribers.remove(who) + + def publish(self, message, *args): + for subscriber in self.subscribers: + method = "msg_" + message + if hasattr(subscriber, method): + getattr(subscriber, method)(*args) |