47 lines
1.0 KiB
Python
47 lines
1.0 KiB
Python
![]() |
'''
|
||
|
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:
|
||
|
|
||
|
|