예제 #1
0
파일: redis.py 프로젝트: up9inc/mockintosh
    def consume(self) -> None:
        host, port = self.consumers[0].actor.service.address.split(':')
        while True:
            if self.stop:
                break

            try:
                queue = RedisSMQ(host=host, port=port, qname=self.consumers[0].topic)

                if any(consumer.enable_topic_creation for consumer in self.consumers):
                    try:
                        queue.createQueue(delay=0).vt(INFINITE_QUEUE_VISIBILITY).execute()
                    except QueueAlreadyExists:
                        pass

                try:
                    msg = queue.receiveMessage().exceptions(False).execute()

                    if msg:
                        self.consume_message(
                            key=None,
                            value=msg['message'],
                            headers={}
                        )
                except AttributeError:
                    pass

                queue.quit()
            except RedisConnectionError:
                logging.warning('Couldn\'t establish a connection to Redis instance at %s:%s', host, port)

            time.sleep(1)
예제 #2
0
    def handle(self, *args, **options):

        bic = options['destination'][0]

        # Setup the queue where the router delivering a packet from
        # waiting.

        qname = "{}_{}".format(bic, "send")
        queue = RedisSMQ(host="127.0.0.1", qname=qname)

        # Receive a payment packet
        try:
            msg = queue.receiveMessage().execute()
            # Process payload from YAML
            packet = yaml.safe_load(msg['message'])
            self.success("Payment packet received {}".format(
                self.format_payment(packet)))
            queue.deleteMessage(id=msg['id']).execute()
        except NoMessageInQueue:
            self.notice("No payment packets for {} ".format(bic))
예제 #3
0
    def handle(self, *args, **options):

        # Load the configuration

        self.success("Booting the router by reading configuration...")

        # FIXME: Configuration needs to be better deployed (and not hardwired)
        with open("csm.yaml") as fh:
            config = yaml.load(fh.read(), Loader=yaml.FullLoader)

        bics = [x['bic'] for x in config['participants']]
        self.success("Found PSPs with BICs: {}".format(", ".join(bics)))

        # Setup queues for all the PSPs

        self.success("Setting up interface for each PSP...")

        for psp in config['participants']:
            bic = psp['bic']
            name = psp['name']
            for direction in ['send', 'recv']:
                qname = "{}_{}".format(bic, direction)
                queue = RedisSMQ(host="127.0.0.1", qname=qname)
                try:
                    queue.createQueue(delay=0, vt=20, quiet=True).execute()
                except QueueAlreadyExists:
                    pass
                self.QUEUES.setdefault(bic, {})
                self.QUEUES[bic][direction] = queue
            self.success("Interface set up for {} ({})".format(
                bic, name))

        # Start event loop trying to read messages from the different queues

        # FIXME: This is completely naive way to do this, but it is
        # intentional and will be switched over to Kafka at a later
        # stage.

        self.success("Listening for payment packets...")

        while True:
            for bic, queues in self.QUEUES.items():
                # Receive a payment packet
                try:
                    queue = queues['recv']
                    msg = queue.receiveMessage().execute()
                    # Process payload from YAML
                    packet = yaml.safe_load(msg['message'])
                    self.success("Payment packet received: {}".format(
                        self.format_payment(packet)))
                    queue.deleteMessage(id=msg['id']).execute()
                except NoMessageInQueue:
                    self.notice("No payment packets for {} [{}]".format(
                        bic, time.asctime()))
                    continue

                # Authorise a payment packet; if not authorised just
                # drop the packet.

                # FIXME: The payment packet should be an object and we
                # should have methods for routing etc around that. [Or
                # maybe not as we have a routing service for the
                # routing. But the payment packet should certainly be
                # an object.]

                routserv = RoutingService()

                if not routserv.authorise(packet):
                    # FIXME: Non-authorised packets should be returned
                    # to sender. The router would need to have more in
                    # the payment packet to describe what a returned
                    # packet is. Therefore we will need to have
                    # unified packet types.

                    self.success("Payment packet authorisation failed: {}".format(
                        routserv.format_payment(packet)))

                    continue  # we just drop the non-authorised packet

                self.success("Payment packet authorisation succeeded: {}".format(
                    routserv.format_payment(packet)))

                # Route the packet by finding out what the destination
                # interface is.

                destination_bic = routserv.route(packet)

                if not destination_bic:
                    self.error("No destination for payment packet {}".format(
                               routserv.format_payment(packet)))
                    continue

                self.success("Routing payment packet to destination: {}".format(
                        routserv.format_payment(packet)))

                # Pass the message along to the destination BIC.

                qname = "{}_{}".format(destination_bic, "send")
                queue = RedisSMQ(host="127.0.0.1", qname=qname)

                message_id = queue.sendMessage().message(
                    yaml.safe_dump(packet)).execute()

                self.success("Payment packet sent: {}".format(
                    routserv.format_payment(packet)))

            time.sleep(1)  # just so we don't use _all_ CPU