class Adapter(object): def __init__(self): self.db = MongoClient()[DATABASE] self.mng = Messenger() self.mng.route("amqp:/*", "amqp://%s/$1" % AMQP_HOST) self.mng.start() self.mng.subscribe(SERVICE) def run(self): while (True): if self.mng.incoming < 1: self.mng.recv(1) if self.mng.incoming > 0: request = Message() self.mng.get(request) if request.reply_to: response = Message() response.address = request.reply_to response.correlation_id = request.correlation_id response.properties = {} response.properties['result'], response.body = self.process(request.properties, request.body) self.mng.put(response) self.mng.send() def process(self, properties, body): if 'collection' not in properties: return 'Error: collection not specified', None if 'command' not in properties: return 'Error: command not specified', None collection = properties['collection'] command = properties['command'] col = self.db[collection] if command == 'find': if body == None: cursor = col.find() elif body.__class__ == dict: cursor = col.find(body) elif body.__class__ == list: cursor = col.find(body[0], body[1]) answer = [] for i in range(cursor.count()): answer.append(cursor[i]) return 'OK', answer
class BusManager: def __init__(self): pass def SetHost(self, host, router): self.M = Messenger() self.M.start() self.M.timeout = 3 self.M.route("amqp:/*", "amqp://%s/$1" % host) if router: self.address = "amqp:/_topo/0/%s/$management" % router else: self.address = "amqp:/_local/$management" self.subscription = self.M.subscribe("amqp:/#") self.reply = self.subscription.address def Disconnect(self): self.M.stop() def GetObject(self, cls): request = Message() response = Message() request.address = self.address request.reply_to = self.reply request.correlation_id = 1 request.properties = {u'operation':u'GET', u'type':cls} request.body = {'attributeNames': []} self.M.put(request) self.M.send() self.M.recv() self.M.get(response) if response.properties['status-code'] != 200: raise Exception("Agent reports: %d %s" % (response.properties['status-code'], response.properties['status-description'])) entities = [] results = response.body for e in results: entities.append(AmqpEntity(e)) return entities