diff --git a/Broker.py b/Broker.py new file mode 100644 index 0000000..b238ce6 --- /dev/null +++ b/Broker.py @@ -0,0 +1,47 @@ +''' +Created on 06.03.2016 + +@author: wn +''' + + +import Queue + +class BrokerException(Exception): pass + +class BrokerDuplicateIdException(BrokerException): pass + +class BrokerNotSubscribedException(BrokerException): pass + +class BrokerOverflowException(BrokerException): pass + + +class Broker(object): + def __init__(self): + self.outQueues = {} + self.inQueue = Queue.Queue + + def subscribe(self, name): + if self.outQueues.has_key(name): + raise BrokerDuplicateIdException() + self.outQueues[name] = Queue.Queue() + + def unsubscribe(self, name): + try: + del self.outQueues[name] + except KeyError: + raise BrokerNotSubscribedException + + def get(self, name): + try: + return self.outQueues[name].get() + except KeyError: + raise BrokerNotSubscribedException + + def put(self, msg): + try: + for q in self.outQueues.values(): + q.put_nowait(msg) + except Queue.Full: + + \ No newline at end of file