def unsubscribe(self, key="", dataindex="", corrId=""): """Unsubscribe self from DATAINDEX for KEY.""" self.log_debug("unsubscribed to %s with dataindex %s", 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 = d6message.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 = d6message.d6msg( key=key, replyTo=self.name, correlationId=corruuid, type="unsub", dataindex=dataindex ) self.send(msg) return
def inreq(self, msg): corruuid = msg.correlationId dataindex = msg.header['dataindex'] if dataindex == "pubdata": newmsg = d6message.d6msg(key=msg.replyTo, replyTo=self.name, correlationId=msg.correlationId, type="rep", dataindex=dataindex, body=dataobj.dataObject(self.pubdata)) self.send(newmsg) elif dataindex == "subdata": newmsg = d6message.d6msg(key=msg.replyTo, replyTo=self.name, correlationId=msg.correlationId, type="rep", dataindex=dataindex, body=dataobj.dataObject(self.subdata)) self.send(newmsg) elif dataindex in self.pubdata: reply = d6message.d6msg(replyTo=self.name, type="rep", body=self.pubdata[dataindex].get(), srcmsg=msg) self.send(reply) elif hasattr(self, 'reqhandler'): self.pubdata[dataindex] = dataobj.pubData(dataindex, msg.body) self.pubdata[dataindex].requesters[msg.replyTo] = corruuid self.reqhandler(msg)
def inreq(self, msg): corruuid = msg.correlationId dataindex = msg.header["dataindex"] if dataindex == "pubdata": newmsg = d6message.d6msg( key=msg.replyTo, replyTo=self.name, correlationId=msg.correlationId, type="rep", dataindex=dataindex, body=dataobj.dataObject(self.pubdata), ) self.send(newmsg) elif dataindex == "subdata": newmsg = d6message.d6msg( key=msg.replyTo, replyTo=self.name, correlationId=msg.correlationId, type="rep", dataindex=dataindex, body=dataobj.dataObject(self.subdata), ) self.send(newmsg) elif hasattr(self, "reqhandler"): self.pubdata[dataindex] = dataobj.pubData(dataindex, msg.body) self.pubdata[dataindex].requesters[msg.replyTo] = corruuid self.reqhandler(msg) else: self.log_exception("Received a request but have no handler: %s", msg)
def request( self, key, dataindex, corrId="", callback=None, interval=0, timer=30, args={}): msg = d6message.d6msg(key=key, replyTo=self.name, correlationId=corrId, type="req", dataindex=dataindex, body=args) corruuid = msg.correlationId self.subdata[corruuid] = dataobj.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={}): """Subscribe to a DATAINDEX for a given KEY.""" self.log_info("subscribed to %s with dataindex %s", key, dataindex) msg = d6message.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] = dataobj.subData(key, dataindex, corruuid, callback) self.scheduuids.add(corruuid) self.schedule(msg, corruuid, interval) return corruuid
def command( self, key, command, corrId="", callback=None, timer=30, args={}): msg = d6message.d6msg(key=key, replyTo=self.name, type="cmd", correlationId=corrId, dataindex=command, body=args) corruuid = msg.correlationId self.subdata[corruuid] = dataobj.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_debug("subscribed to %s with dataindex %s", key, dataindex) msg = d6message.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] = dataobj.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 = d6message.d6msg(key=key, replyTo=self.name, correlationId=corrId, type="req", dataindex=dataindex, body=args) corruuid = msg.correlationId self.subdata[corruuid] = dataobj.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 unsubscribe(self, key="", dataindex="", corrId=""): """Unsubscribe self from DATAINDEX for KEY.""" self.log_debug("unsubscribed to %s with dataindex %s", 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 = d6message.d6msg(key=key, replyTo=self.name, correlationId=corrId, type='unsub', dataindex=dataindex) self.send(msg) elif key and dataindex: for corruuid in self.subdata.copy().keys(): # copy to avoid undefined behavior w changing dict during iter 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 = d6message.d6msg(key=key, replyTo=self.name, correlationId=corruuid, type='unsub', dataindex=dataindex) self.send(msg) return
def unsubscribe(self, key="", dataindex="", corrId=""): """Unsubscribe self from DATAINDEX for KEY.""" self.log_info("unsubscribed to %s with dataindex %s", 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 = d6message.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 = d6message.d6msg(key=key, replyTo=self.name, correlationId=corruuid, type='unsub', dataindex=dataindex) self.send(msg) return
def inpull(self, msg): # self.log_debug("received PULL msg: %s", msg) dataindex = msg.header["dataindex"] if dataindex in self.pubdata: reply = d6message.d6msg(replyTo=self.name, type="rep", body=self.pubdata[dataindex].get(), srcmsg=msg) self.send(reply) else: self.pubdata[dataindex] = dataobj.pubData(dataindex, msg.body) self.subhandler(msg) self.pubdata[dataindex].addsubscriber(msg.replyTo, "pull", msg.correlationId)
def d6reload(self, msg): inargs = msg.body.data service = inargs['service'] newmsg = d6message.d6msg(key=service, replyTo=self.name, type="shut") self.send(newmsg) cbkwargs = {} cbkwargs['service'] = service self.waitForServiceToStop(service, callback=self.reloadStoppedService, cbkwargs=cbkwargs)
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): self.log_debug("received PULL msg: %s", msg) dataindex = msg.header['dataindex'] if dataindex in self.pubdata: reply = d6message.d6msg(replyTo=self.name, type="rep", body=self.pubdata[dataindex].get(), srcmsg=msg) self.send(reply) else: self.pubdata[dataindex] = dataobj.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 = d6message.d6msg(key=requester, replyTo=self.name, correlationId=self.pubdata[dataindex] .requesters[requester], type="rep", dataindex=self.pubdata[dataindex].dataindex) if newdata: msg.body = dataobj.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 reply(self, dataindex, newdata="", delete=True): for requester in self.pubdata[dataindex].requesters: msg = d6message.d6msg( key=requester, replyTo=self.name, correlationId=self.pubdata[dataindex].requesters[requester], type="rep", dataindex=self.pubdata[dataindex].dataindex) if newdata: msg.body = dataobj.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 command(self, key, command, corrId="", callback=None, timer=30, args={}): msg = d6message.d6msg(key=key, replyTo=self.name, type="cmd", correlationId=corrId, dataindex=command, body=args) corruuid = msg.correlationId self.subdata[corruuid] = dataobj.subData(key, command, corruuid, callback) self.send(msg) if timer: self.timerThreads.append( eventlet.spawn_after(timer, self.reqtimeout, corruuid))
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 = dataobj.dataObject(data) # send to subscribers/requestors if self.pubdata[dataindex].subscribers: if key: msg = d6message.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": corId = subscribers[subscriber]["correlationId"] msg = d6message.d6msg( key=subscriber, replyTo=self.name, correlationId=corId, type="pub", dataindex=dataindex, body=data, ) self.send(msg) if self.pubdata[dataindex].requesters: if key: msg = d6message.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(): corId = self.pubdata[dataindex].requesters[requester] msg = d6message.d6msg( key=requester, replyTo=self.name, correlationId=corId, type="rep", dataindex=dataindex, body=self.pubdata[dataindex].get(), ) self.send(msg) del self.pubdata[dataindex].requesters[requester]
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 = dataobj.dataObject(data) # send to subscribers/requestors if self.pubdata[dataindex].subscribers: if key: msg = d6message.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": corId = subscribers[subscriber]['correlationId'] msg = d6message.d6msg(key=subscriber, replyTo=self.name, correlationId=corId, type="pub", dataindex=dataindex, body=data) self.send(msg) if self.pubdata[dataindex].requesters: if key: msg = d6message.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(): corId = self.pubdata[dataindex].requesters[requester] msg = d6message.d6msg(key=requester, replyTo=self.name, correlationId=corId, type="rep", dataindex=dataindex, body=self.pubdata[dataindex].get()) self.send(msg) del self.pubdata[dataindex].requesters[requester]