Exemple #1
0
    def eBindBlockingListener(self, sQueueName, lBindingKeys=None):
        """
        """
        if self.oListenerChannel is None:
            if lBindingKeys is None:
                lBindingKeys = ['#']
            self.oCreateConnection()
            oChannel = self.oConnection.channel()

            oChannel.exchange_declare(
                exchange=self.sExchangeName,
                passive=False,
                # auto_delete=True,
                type='topic')
            # oResult = oChannel.queue_declare(exclusive=True)
            # self.oListenerQueueName = oResult.method.queue
            # I don't think we want exclusive here:
            # we could have more than one listener,
            # and we could have one listening for retvals...
            oResult = oChannel.queue_declare(queue=sQueueName, exclusive=False)
            self.oListenerQueueName = sQueueName
            for sBindingKey in lBindingKeys:
                oChannel.queue_bind(
                    exchange=self.sExchangeName,
                    queue=sQueueName,
                    routing_key=sBindingKey,
                )
            time.sleep(0.1)
            self.oListenerChannel = oChannel
            vDebug("Bound listener channel " + str(id(oChannel)))
Exemple #2
0
    def oCreateConnection(self):
        import pika
        global oCONNECTION
        if not self.oConnection:
            try:
                oConnection = pika.BlockingConnection(self.oParameters)
                assert oConnection, "oCreateConnection: no oConnection created"
                self.oConnection = oConnection
                oCONNECTION = oConnection
                vDebug("Created connection " + str(id(oConnection)))
            except Exception as e:
                #     raise exceptions.ProbableAuthenticationError
                oLOG.exception("Error in oCreateConnection " + str(e))
                raise

        return self.oConnection
Exemple #3
0
    def eBindBlockingSpeaker(self):
        """
        We are going to use our Speaker channel as a broadcast
        channel for ticks, so we will set it up as a "topic".
        """
        if self.oSpeakerChannel is None:
            self.oCreateConnection()
            oChannel = self.oConnection.channel()

            oChannel.exchange_declare(
                exchange=self.sExchangeName,
                passive=False,
                # auto_delete=True,
                type='topic')

            time.sleep(0.1)
            self.oSpeakerChannel = oChannel
            vDebug("Bound speaker channel " + str(id(oChannel)))
Exemple #4
0
    def eSendOnSpeaker(self, sType, sMess):
        if sType not in lKNOWN_TOPICS:
            sRetval = "eSendOnSpeaker: oSpeakerChannel unhandled topic " + sMess
            vError(sRetval)
            return sRetval
        if self.oSpeakerChannel is None:
            self.eBindBlockingSpeaker()

        assert self.oSpeakerChannel, "eSendOnSpeaker: oSpeakerChannel is null"
        assert self.oConnection, "eSendOnSpeaker: oConnection is null"

        # we will break the sChartId up into dots from the underscores
        # That way the end consumer can look at the feed selectively
        sPublishingKey = sType + '.' + self.sChartId.replace('_', '.')

        self.oSpeakerChannel.basic_publish(exchange=self.sExchangeName,
                                           routing_key=sPublishingKey,
                                           body=sMess,
                                           mandatory=False,
                                           immediate=False,
                                           properties=self.oProperties)
        vDebug("eSendOnSpeaker: sent " + sMess)
        return ""