Example #1
0
 async def run(self):
     for successor in self.agent.successors:
         message = _prepare_respawn_notification(successor)
         await self.send(message)
         if (message.sent):
             self.agent.respawn_after_breakdown = False
             AgentActivityLogger._log(
                 "Agent sends notification about its rebirth to successors")
 async def recover(self, predecessor):
     print(">>>>>>>>>")
     AgentActivityLogger._log(
         dict(action="agent_recovery",
              actor=self.owning_agent,
              recovering_actor=predecessor))
     agent_id = AgentUsernameToIdMapper.agent_username_to_id[predecessor]
     agent = self.agent_factory.create_agent(agent_id, True)
     await agent.start()
Example #3
0
    def remove_entry_from_sent_messages_registry(self, sender_id = None, thread_id = None):

        entries_ids_to_remove = []
        include_all_threads = True if thread_id is None else False

        for i in range(len(self.sent_messages_registry)):
            temp_sender_id = AgentUsernameToIdMapper.agent_username_to_id[str(self.sent_messages_registry[i]["sender"])]
            if sender_id == temp_sender_id and \
                    (include_all_threads is True or self.sent_messages_registry[i]["thread"].id == thread_id):
                entries_ids_to_remove.append(i)

        listSize = len(entries_ids_to_remove)

        for i in range(listSize):
            j = listSize - i - 1
            AgentActivityLogger._log("Deleted entry of {0} for thread {1} from {2} "
                                     .format(self.username, self.sent_messages_registry[entries_ids_to_remove[j]]["thread"].id,
                                    self.sent_messages_registry[entries_ids_to_remove[j]]["sender"]))
            del self.sent_messages_registry[entries_ids_to_remove[j]]
    async def run(self):
        list_size = len(self.agent.sent_messages_registry)
        for i in range(list_size):
            respawned_agent_fullname = "{0}@{1}".format(
                self.respawned_agent.localpart, self.respawned_agent.domain)
            if self.agent.sent_messages_registry[i][
                    "sender"] == respawned_agent_fullname:
                message_thread_str = self.agent.sent_messages_registry[i][
                    "thread"].ToJson()
                message = _prepare_message(
                    respawned_agent_fullname,
                    dict(id=123,
                         body=self.agent.sent_messages_registry[i]["body"],
                         thread=message_thread_str))
                await self.send(message)
                if message.sent:
                    AgentActivityLogger._log(
                        "Retransmitted message sent to {0} from {1}".format(
                            self.respawned_agent, self.agent.jid))

        await asyncio.sleep(randint(3, 10))
    async def run(self):
        try:
            msg = await self.receive(timeout=10)
            agent_id = AgentUsernameToIdMapper.agent_username_to_id[str(self.jid)]
            if msg is not None:
                sender_id = AgentUsernameToIdMapper.agent_username_to_id[str(msg.sender)]


                received_thread = MessageThread(jsonStr=msg.thread)

                if msg.thread is None and msg.body == "respawn_notification":
                    self.agent.resend_missing_messages(msg.sender)

                elif received_thread.message_thread_type == MessageThreadType.RootComponentProduction:

                    AgentActivityLogger._log(
                        dict(msg_type="receive", msg_id=msg.metadata["message_id"], sender=sender_id, receiver=agent_id,
                             thread=msg.thread, body=msg.body))

                    message_thread_id = received_thread.id

                    index = -1
                    for i, thread in enumerate(self.agent.message_thread_counter_list):
                        if thread.thread_id == message_thread_id:
                            index = i
                            break

                    if index != -1:
                        AgentActivityLogger._log("Counter of thread {0} for agent {1} decreased to {2}"
                            .format(received_thread.id, '1', str(self.agent.message_thread_counter_list[index].getCounterValue()-1)))
                        self.agent.remove_entry_from_sent_messages_registry(sender_id, received_thread.id)
                        if self.agent.message_thread_counter_list[index].decreaseCounter():
                            del self.agent.message_thread_counter_list[index]
                            AgentActivityLogger._log("Thread with id {0} removed from thread list of agent {1}"
                                                     .format(received_thread.id, '1'))
                            AgentActivityLogger._log("CAR CREATED")
            else:
                print("{}: I did not received any message".format(agent_id))
        except Exception as e:
            print("exception in ", self.jid)
            traceback.print_exc(e)
Example #6
0
 async def run(self):
     self.exit_code = "Agent killed manually"
     AgentActivityLogger._log("######## about to kill agent {}".format(self.agent.jid))
     await self.agent.stop()
    async def run(self):
        if len(self.agent.predecessors) == 0:
            print("no neigbhours in", self.jid)
            await asyncio.sleep(100)

        thread_id = str(uuid.uuid4())
        message_thread = MessageThread(
            thread_id,
            message_thread_type=MessageThreadType.RootComponentProduction,
            message_direction=MessageDirection.Downward)
        message_thread_str = message_thread.ToJson()

        message_thread_counter = MessageThreadCounter(thread_id)
        AgentActivityLogger._log(
            "Thread with id {0} added to thread list of agent {1}".format(
                thread_id, '1'))

        for predecessor in self.agent.predecessors:
            body = json.dumps(self.workflow.get_ingredients("car"))
            message = _prepare_message(
                predecessor, dict(id=123, body=body,
                                  thread=message_thread_str))

            receiver_id = AgentUsernameToIdMapper.agent_username_to_id[str(
                predecessor)]
            agent_id = AgentUsernameToIdMapper.agent_username_to_id[str(
                self.jid)]

            receiver_part = self.graph.nodes[receiver_id]["part"][0]
            agent_part = self.graph.nodes[agent_id]["part"][0]

            if receiver_part in self.workflow.get_base_materials():
                index = 1
            else:
                index = self.workflow.get_complex_ingredients_for_part_with_quantities(
                    agent_part).get(receiver_part)

            for i in range(index):
                await self.send(message)
                if (message.sent):
                    self.agent.sent_messages_registry.append(
                        dict(sender=predecessor,
                             thread=message_thread,
                             body=body))
                    AgentActivityLogger._log("response added to registry")
                    message_thread_counter.increaseCounter()
                    AgentActivityLogger._log(
                        "Counter of thread {0} for agent {1} increased to {2}".
                        format(thread_id, '1',
                               str(message_thread_counter.getCounterValue())))

            AgentActivityLogger._log(
                dict(msg_type="send",
                     msg_id=message.metadata["message_id"],
                     sender=agent_id,
                     receiver=receiver_id,
                     thread=message_thread_str,
                     body=message.body))

        if (message_thread_counter.getCounterValue() != 0):
            self.agent.message_thread_counter_list.append(
                message_thread_counter)

        await asyncio.sleep(randint(3, 10))
Example #8
0
    async def process_msg(self, msg):
        sender_id = AgentUsernameToIdMapper.agent_username_to_id[str(
            msg.sender)]

        received_thread = MessageThread(jsonStr=msg.thread)

        if msg.thread is None and msg.body == "respawn_notification":
            if self.agent.was_ever_revived:
                # we are also respawned, so to avoid copies, we should skip this step
                self.agent.was_ever_revived = False
                self.agent.respawn_after_breakdown = False
            else:
                self.agent.resend_missing_messages(msg.sender)
        else:
            AgentActivityLogger._log(
                dict(msg_type="receive",
                     msg_id=msg.metadata["message_id"],
                     sender=sender_id,
                     receiver=self.agent_id,
                     thread=msg.thread,
                     body=msg.body))

            if received_thread.message_direction == MessageDirection.Downward:
                message_thread_counter = MessageThreadCounter(
                    received_thread.id)
                AgentActivityLogger._log(
                    "Thread with id {0} added to thread list of agent {1}".
                    format(received_thread.id, str(self.agent_id)))

                body = json.dumps(
                    self.workflow.get_ingredients(self.produced_component))
                for predecessor in self.agent.predecessors:
                    message = _prepare_message(
                        predecessor, dict(id=123, body=body,
                                          thread=msg.thread))

                    receiver_id = AgentUsernameToIdMapper.agent_username_to_id[
                        str(predecessor)]
                    agent_id = AgentUsernameToIdMapper.agent_username_to_id[
                        str(self.jid)]

                    receiver_part = self.graph.nodes[receiver_id]["part"][0]
                    agent_part = self.graph.nodes[agent_id]["part"][0]

                    if receiver_part in self.workflow.get_base_materials():
                        index = 1
                    else:
                        index = self.workflow.get_complex_ingredients_for_part_with_quantities(
                            agent_part).get(receiver_part)

                    for i in range(index):
                        await self.send(message)
                        if message.sent:
                            self.agent.sent_messages_registry.append(
                                dict(sender=predecessor,
                                     thread=received_thread,
                                     body=body))
                            AgentActivityLogger._log(
                                "response added to registry")
                            message_thread_counter.increaseCounter()
                            AgentActivityLogger._log(
                                "Counter of thread {0} for agent {1} increased to {2}"
                                .format(
                                    message_thread_counter.thread_id,
                                    self.agent_id,
                                    str(message_thread_counter.getCounterValue(
                                    ))))
                    AgentActivityLogger._log(
                        dict(msg_type="send",
                             msg_id=msg.metadata["message_id"],
                             sender=self.agent_id,
                             receiver=receiver_id,
                             thread=msg.thread,
                             body=body))

                if message_thread_counter.getCounterValue() != 0:
                    self.agent.message_thread_counter_list.append(
                        message_thread_counter)
                    pass

            else:
                index = -1
                for i, thread in enumerate(
                        self.agent.message_thread_counter_list):
                    if thread.thread_id == received_thread.id:
                        index = i
                        break

                if index != -1:
                    AgentActivityLogger._log(
                        "Counter of thread {0} for agent {1} decreased to {2}".
                        format(
                            received_thread.id, self.agent_id,
                            str(self.agent.message_thread_counter_list[index].
                                getCounterValue() - 1)))

                    self.agent.remove_entry_from_sent_messages_registry(
                        sender_id, received_thread.id)
                    if self.agent.message_thread_counter_list[
                            index].decreaseCounter():
                        del self.agent.message_thread_counter_list[index]
                        AgentActivityLogger._log(
                            "Thread with id {0} removed from thread list of agent {1}"
                            .format(received_thread.id, str(self.agent_id)))

                        for successor in self.agent.successors:
                            agent_id = AgentUsernameToIdMapper.agent_username_to_id[
                                str(self.jid)]
                            msg_body = self.graph.nodes[agent_id]["part"][0]

                            AgentActivityLogger._log("PART: " + msg_body +
                                                     " CREATED")
                            message = _prepare_message(
                                successor,
                                dict(id=123, body=msg_body, thread=msg.thread))
                            receiver_id = AgentUsernameToIdMapper.agent_username_to_id[
                                str(successor)]
                            await self.send(message)
                            AgentActivityLogger._log(
                                dict(msg_type="send",
                                     msg_id=msg.metadata["message_id"],
                                     sender=self.agent_id,
                                     receiver=receiver_id,
                                     thread=msg.thread,
                                     body=msg.body))