This commit is contained in:
Wolfgang Hottgenroth 2016-06-25 15:46:27 +02:00
commit 6c6f6c7e62

47
Broker.py Normal file
View File

@ -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: