X-Git-Url: http://git.maemo.org/git/?p=jamaendo;a=blobdiff_plain;f=jamaui%2Fpostoffice.py;h=9453176f4b0adbab72afa7f27c7e641e5a8af7ff;hp=009c8846e51536f82b864067518c48ec7625f49a;hb=19020e8dae378875c8bfcd7e573ca5e91b1dfa10;hpb=99b9e55a7165a70a2e2f8846a214246d49faf9d0 diff --git a/jamaui/postoffice.py b/jamaui/postoffice.py index 009c884..9453176 100644 --- a/jamaui/postoffice.py +++ b/jamaui/postoffice.py @@ -23,35 +23,36 @@ # # message central +import logging + +log = logging.getLogger(__name__) + 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) + self.tags = {} # tag -> [callback] + + def notify(self, tag, *data): + clients = self.tags.get(tag) + if clients: + #log.debug("(%s %s) -> [%s]", + # tag, + # " ".join(str(x) for x in data), + # " ".join(repr(x) for x,_ in clients)) + for ref, client in clients: + client(*data) + + def connect(self, tag, ref, callback): + if tag not in self.tags: + self.tags[tag] = [] + clients = self.tags[tag] + if callback not in clients: + clients.append((ref, callback)) + + def disconnect(self, tag, ref): + if tag not in self.tags: + self.tags[tag] = [] + self.tags[tag] = [(_ref, cb) for _ref, cb in self.tags[tag] if _ref != ref] postoffice = PostOffice()