Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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]
Ejemplo n.º 3
0
    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))
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    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))
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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]
Ejemplo n.º 12
0
    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]