add broker class
This commit is contained in:
parent
348cb7effa
commit
0615be568d
47
Broker.py
Normal file
47
Broker.py
Normal 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:
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user