X-Git-Url: http://git.maemo.org/git/?p=jamaendo;a=blobdiff_plain;f=jamaui%2Fpostoffice.py;h=a84158b1a57af60ce02dfdbd287a45b5ad749c7f;hp=078316c0dfc22866925fa7f3c266c819523c9a98;hb=ae451be237b4622abd934a611f5e2dd4d8aec883;hpb=facd2a5710fb05ac80d55ed8e1af8184fb496c02 diff --git a/jamaui/postoffice.py b/jamaui/postoffice.py index 078316c..a84158b 100644 --- a/jamaui/postoffice.py +++ b/jamaui/postoffice.py @@ -23,38 +23,45 @@ # # message central +from __future__ import with_statement import logging +import threading log = logging.getLogger(__name__) class PostOffice(object): def __init__(self): + self.lock = threading.RLock() self.tags = {} # tag -> [callback] def notify(self, tag, *data): - clients = self.tags.get(tag) - if clients: - for ref, client in clients: - client(*data) + with self.lock: + log.info("(%s %s)", tag, " ".join(str(x) for x in data)) + clients = self.tags.get(tag) + if clients: + for ref, client in clients: + client(*data) def connect(self, tag, ref, callback): - if not isinstance(tag, list): - tag = [tag] - for t in tag: - if t not in self.tags: - self.tags[t] = [] - clients = self.tags[t] - if callback not in clients: - clients.append((ref, callback)) + with self.lock: + if not isinstance(tag, list): + tag = [tag] + for t in tag: + if t not in self.tags: + self.tags[t] = [] + clients = self.tags[t] + if callback not in clients: + clients.append((ref, callback)) def disconnect(self, tag, ref): - if not isinstance(tag, list): - tag = [tag] - for t in tag: - if t not in self.tags: - self.tags[t] = [] - self.tags[t] = [(_ref, cb) for _ref, cb in self.tags[t] if _ref != ref] + with self.lock: + if not isinstance(tag, list): + tag = [tag] + for t in tag: + if t not in self.tags: + self.tags[t] = [] + self.tags[t] = [(_ref, cb) for _ref, cb in self.tags[t] if _ref != ref] postoffice = PostOffice()