def receive_topic_messages(self, ch, method, properties, body):
        """
        Standard interface for the pika module for the agent to get messages
        :param ch:
        :param method:
        :param properties:
        :param body:
        :return:
        """
        body = body.decode("utf-8")
        if "admin" in body:
            print("Hey, Admin!")

        try:
            command = method.routing_key.split(".")
            if not command:
                data = json.loads(body)
                print("My special task: " + str(data))
            elif len(command):
                if command[1] == "get_scales":
                    print("Preparing and sending my scales")
                    message = pack_msg_json(level=Level.info, body={"scale": ["A", "B", "C"]})
                    self.send_message(message=message, routing_key=self.make_routing_key(" ", "info"))
            elif command[1] == "get_estimates":
                print("Preparing and sending my estimates")

        except ValueError:
            if Message.kill_everyone in body:
                print("Bye!")
                sys.exit(-1)
    def receive_message(self, ch, method, properties, body):

        """
        Standard interface for the pika module for the agent to get messages
        :param ch:
        :param method:
        :param properties:
        :param body:
        :return:
        """
        body = body.decode("utf-8")
        if "admin" in body:
            print("Hey, Admin!")

        try:
            data = json.loads(body)
            if data.get(Message.new_id, -1) > -1:
                self.ID = data.get(Message.new_id, 0)
                print("Wow, new id: " + str(self.ID))

                data = {Message.info: Message.approve_enumeration}
                # data[Message.info] = "I am now enumerated. So, check the connection with my routing_key"
                data["id"] = self.ID
                message = pack_msg_json(level=Level.info, body=data)
                self.send_message(message=message, routing_key=self.make_routing_key(" ", "info"))

                self.change_routing_key()
                self.channel.stop_consuming()

            print("In receiving info " + str(data))
        except ValueError:
            if Message.new_id in body:
                if self.ID == -1:
                    self.ID = body.split()[-1]
                    print("My ID: " + str(self.ID))
                    message = pack_msg_json(level=Level.info)
                    self.send_message(message=message, routing_key=self.make_routing_key(" ", "info"))
                return
            elif Message.kill_everyone in body:
                print("Bye!")
                sys.exit(0)
    def __init__(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
        self.channel = self.connection.channel()
        self.result = self.channel.queue_declare(exclusive=True, durable=True)
        self.queue_name = self.result.method.queue
        self.routing_key = "routing_key"  # '.'.join([str(self.ID), "report", "update_energy"])

        try:
            self.ID
        except AttributeError:
            data = {Message.new_member: ""}
            self.send_message(message=data, routing_key=Message.new_member)
        else:
            self.ID = int(1)
            data = {Message.info: Message.approve_enumeration}
            data[Message.info] = "I am now enumerated. So, check the connection with my routing_key"
            message = pack_msg_json(level=Level.info, body=data)
            self.send_message(message=message, routing_key=self.make_routing_key(" ", "info"))

        self.begin_listen(queue_name=self.queue_name)
示例#4
0
    def receive_topic_messages(self, ch, method, properties, body):
        """
        Standard interface for the pika module for the agent to get messages
        :param ch:
        :param method:
        :param properties:
        :param body:
        :return:
        """
        body = body.decode("utf-8")
        if "admin" in body:
            print("Hey, Admin!")

        try:
            command = method.routing_key.split('.')
            # if not command:
            data = {}
            if (body):
                data = json.loads(body)
                print("My special task: " + str(data))
            if len(command) > 0:
                if command[1] == Task.set_options:
                    print("Got options and experts participating. Saved them")
                    try:
                        options_from_center = data["alternatives"]
                    except KeyError:
                        raise UnexpectedMessageError("Expected to get alternatives")
                    else:
                        self.alternatives = options_from_center

                        # finally decide what is the scale for our estimates
                        self.set_linguistic_set()

                        message = pack_msg_json(level=Level.info, body={"info": Task.set_options})
                        self.send_message(message=message, routing_key=self.make_routing_key(" ", "info"))
                elif command[1] == Task.get_estimates:
                    print("Preparing and sending my estimates and the whole scale")
                    self.calculate_estimates()
                    message = pack_msg_json(level=Level.info,
                                            body={Message.info: Task.get_estimates,
                                                  "data": self.estimates,
                                                  "linguistic_set_size": self.linguistic_set.size})
                    self.send_message(message=message, routing_key=self.make_routing_key(" ", "info"))
                elif command[1] == Task.set_community_best:
                    print("Got community best")
                    try:
                        best_from_server = TwoTuple.from_json(data[Message.best_alternative])
                        best_from_server_id = data[Message.best_alternative_id]
                    except KeyError:
                        raise UnexpectedMessageError("Expected to get best alternative")
                    else:

                        tuple_estimates = [TwoTuple.from_string(i) for i in self.estimates]
                        personal_best = tuple_estimates[best_from_server_id]
                        if personal_best < best_from_server:
                            satisfaction = "not_satisfied"
                        elif personal_best == best_from_server:
                            satisfaction = "satisfied"
                        else:
                            satisfaction = "over_satisfied"
                        message = pack_msg_json(level=Level.info,
                                                body={Message.info: Task.set_community_best, Message.satisfaction: satisfaction})
                        self.send_message(message=message, routing_key=self.make_routing_key(" ", "info"))
                elif command[1] == Task.finish_game:
                    print("I leave the room")
                    message = pack_msg_json(level=Level.info, body={"info": Task.finish_game})
                    self.send_message(message=message, routing_key=self.make_routing_key(" ", "info"))
                    sys.exit(0)
            else:
                print("I got some unknown task.")

        except ValueError:
            if Message.kill_everyone in body:
                print("Bye!")
                sys.exit(-1)
        except UnexpectedMessageError as e:
            print("Need to notify the central that there is something wrong with him. " + e.message)