def inreq(self, msg): corruuid = msg.correlationId dataindex = msg.header['dataindex'] if dataindex == "pubdata": newmsg = d6msg(key=msg.replyTo, replyTo=self.name, correlationId=msg.correlationId, type="rep", dataindex=dataindex, body=dataObject(self.pubdata)) self.send(newmsg) elif dataindex == "subdata": newmsg = d6msg(key=msg.replyTo, replyTo=self.name, correlationId=msg.correlationId, type="rep", dataindex=dataindex, body=dataObject(self.subdata)) self.send(newmsg) elif dataindex in self.pubdata: reply = d6msg(replyTo=self.name, type="rep", body=self.pubdata[dataindex].get(), srcmsg=msg) self.send(reply) elif hasattr(self, 'reqhandler'): self.pubdata[dataindex] = pubData(dataindex, msg.body) self.pubdata[dataindex].requesters[msg.replyTo] = corruuid self.reqhandler(msg)
def push(self, dataindex, key=""): if self.pubdata[dataindex].dataObject.data: if self.pubdata[dataindex].subscribers: #do = self.pubdata[dataindex].get() #logging.info("%s PUSH: %s" % (self.name, do.data)) if key: msg = d6msg(key=key, replyTo=self.name, correlationId= self.pubdata[dataindex] .subscribers[key]['correlationId'], type="pub", dataindex=dataindex, body=self.pubdata[dataindex].get()) self.send(msg) else: subscribers = self.pubdata[dataindex].getsubscribers() for subscriber in subscribers: if subscribers[subscriber]['type'] == "push": msg = d6msg(key=subscriber, replyTo=self.name, correlationId= subscribers[subscriber] ['correlationId'], type="pub", dataindex=dataindex, body=self.pubdata[dataindex].get()) self.send(msg) if self.pubdata[dataindex].requesters: if key: msg = d6msg(key=key, replyTo=self.name, correlationId= self.pubdata[dataindex].requesters[key], type="rep", dataindex=dataindex, body=self.pubdata[dataindex].get()) self.send(msg) del self.pubdata[dataindex].requesters[key] else: for requester in self.pubdata[dataindex].requesters.keys(): msg = d6msg(key=requester, replyTo=self.name, correlationId= self.pubdata[dataindex] .requesters[requester], type="rep", dataindex=dataindex, body=self.pubdata[dataindex].get()) self.send(msg) del self.pubdata[dataindex].requesters[requester]
def command( self, key, command, corrId="", callback=None, timer=30, args={}): msg = d6msg(key=key, replyTo=self.name, type="cmd", correlationId=corrId, dataindex=command, body=args) corruuid = msg.correlationId self.subdata[corruuid] = subData(key, command, corruuid, callback) self.send(msg) if timer: self.timerThreads.append( eventlet.spawn_after(timer, self.reqtimeout, corruuid))
def subscribe( self, key, dataindex, corrId="", callback=None, pull=False, interval=30, args={}): """Subscribe to a DATAINDEX for a given KEY.""" self.log_info("subscribed to %s with dataindex %s", key, dataindex) msg = d6msg(key=key, replyTo=self.name, correlationId=corrId, dataindex=dataindex, body=args) if pull: msg.type = 'pull' else: msg.type = 'sub' corruuid = msg.correlationId self.subdata[corruuid] = subData(key, dataindex, corruuid, callback) self.scheduuids.add(corruuid) self.schedule(msg, corruuid, interval) return corruuid
def request( self, key, dataindex, corrId="", callback=None, interval=0, timer=30, args={}): msg = d6msg(key=key, replyTo=self.name, correlationId=corrId, type="req", dataindex=dataindex, body=args) corruuid = msg.correlationId self.subdata[corruuid] = subData(key, dataindex, corruuid, callback) if interval: self.scheduuids.add(corruuid) self.schedule(msg, corruuid, interval, callback) else: self.send(msg) if timer: self.timerThreads.append( eventlet.spawn_after(timer, self.reqtimeout, corruuid))
def subscribe( self, key, dataindex, corrId="", callback=None, pull=False, interval=30, args={}): msg = d6msg(key=key, replyTo=self.name, correlationId=corrId, dataindex=dataindex, body=args) if pull: msg.type = 'pull' else: msg.type = 'sub' corruuid = msg.correlationId self.subdata[corruuid] = subData(key, dataindex, corruuid, callback) self.scheduuids.add(corruuid) self.schedule(msg, corruuid, interval) return corruuid
def unsubscribe(self, key="", dataindex="", corrId=""): """Unsubscribe self from DATAINDEX for KEY.""" self.log("unsubscribed to {} with dataindex {}".format( key, dataindex)) if corrId: if corrId in self.scheduuids: self.scheduuids.remove(corrId) if corrId in self.subdata: key = self.subdata[corrId].key dataindex = self.subdata[corrId].dataindex del self.subdata[corrId] msg = d6msg(key=key, replyTo=self.name, correlationId=corrId, type='unsub', dataindex=dataindex) self.send(msg) elif key and dataindex: for corruuid in self.subdata.keys(): if key == self.subdata[corruuid].key and \ dataindex == self.subdata[corruuid].dataindex: if corruuid in self.scheduuids: self.scheduuids.remove(corruuid) del self.subdata[corruuid] msg = d6msg(key=key, replyTo=self.name, correlationId=corruuid, type='unsub', dataindex=dataindex) self.send(msg) return
def unsubscribe(self, key="", dataindex="", corrId=""): if corrId: if corrId in self.scheduuids: self.scheduuids.remove(corrId) if corrId in self.subdata: key = self.subdata[corrId].key dataindex = self.subdata[corrId].dataindex del self.subdata[corrId] msg = d6msg(key=key, replyTo=self.name, correlationId=corrId, type='unsub', dataindex=dataindex) self.send(msg) elif key and dataindex: for corruuid in self.subdata.keys(): if key == self.subdata[corruuid].key and \ dataindex == self.subdata[corruuid].dataindex: if corruuid in self.scheduuids: self.scheduuids.remove(corruuid) del self.subdata[corruuid] msg = d6msg(key=key, replyTo=self.name, correlationId=corruuid, type='unsub', dataindex=dataindex) self.send(msg) return
def d6reload(self, msg): inargs = msg.body.data service = inargs['service'] newmsg = d6msg(key=service, replyTo=self.name, type="shut") self.send(newmsg) cbkwargs = {} cbkwargs['service'] = service self.waitForServiceToStop( service, callback=self.reloadStoppedService, cbkwargs=cbkwargs)
def inpull(self, msg): dataindex = msg.header['dataindex'] if dataindex in self.pubdata: reply = d6msg(replyTo=self.name, type="rep", body=self.pubdata[dataindex].get(), srcmsg=msg) self.send(reply) else: self.pubdata[dataindex] = pubData(dataindex, msg.body) self.subhandler(msg) self.pubdata[dataindex].addsubscriber( msg.replyTo, "pull", msg.correlationId)
def reply(self, dataindex, newdata="", delete=True): for requester in self.pubdata[dataindex].requesters: msg = d6msg(key=requester, replyTo=self.name, correlationId=self.pubdata[dataindex] .requesters[requester], type="rep", dataindex=self.pubdata[dataindex].dataindex) if newdata: msg.body = dataObject(newdata) else: msg.body = self.pubdata[dataindex].get() self.log_debug("REPLY body: %s", msg.body) self.send(msg) if delete: del self.pubdata[dataindex]
def push(self, dataindex, key="", type=None): """Send data for DATAINDEX and KEY to subscribers/requesters.""" self.log_debug("pushing dataindex %s to subscribers %s " "and requesters %s ", dataindex, self.pubdata[dataindex].subscribers, self.pubdata[dataindex].requesters) # bail out if there are no requesters/subscribers if (len(self.pubdata[dataindex].requesters) == 0 and len(self.pubdata[dataindex].subscribers) == 0): self.log_debug("no requesters/subscribers; not sending") return # give prepush hook chance to morph data if self.reserved_dataindex(dataindex): data = self.pubdata[dataindex].get() # bail out if no data to send if data is None: return else: # .get() returns dataObject data = self.prepush_processor(self.pubdata[dataindex].get().data, dataindex, type=type) # bail out if prepush hook said there's no data if data is None: return data = dataObject(data) # send to subscribers/requestors if self.pubdata[dataindex].subscribers: if key: msg = d6msg(key=key, replyTo=self.name, correlationId=self.pubdata[dataindex] .subscribers[key]['correlationId'], type="pub", dataindex=dataindex, body=data) self.send(msg) else: subscribers = self.pubdata[dataindex].getsubscribers() for subscriber in subscribers: if subscribers[subscriber]['type'] == "push": msg = d6msg(key=subscriber, replyTo=self.name, correlationId=subscribers[subscriber] ['correlationId'], type="pub", dataindex=dataindex, body=data) self.send(msg) if self.pubdata[dataindex].requesters: if key: msg = d6msg(key=key, replyTo=self.name, correlationId=self.pubdata[dataindex]. requesters[key], type="rep", dataindex=dataindex, body=self.pubdata[dataindex].get()) self.send(msg) del self.pubdata[dataindex].requesters[key] else: for requester in self.pubdata[dataindex].requesters.keys(): msg = d6msg(key=requester, replyTo=self.name, correlationId=self.pubdata[dataindex] .requesters[requester], type="rep", dataindex=dataindex, body=self.pubdata[dataindex].get()) self.send(msg) del self.pubdata[dataindex].requesters[requester]