예제 #1
0
        def do_long_job_callback(question):

            request_calc = ACLMessage()
            request_calc.set_content(question)
            request_calc.add_receiver(self.calculator_aid)

            while True:
                try:
                    response_calc = yield from self.consult_behavior.send_request(
                        request_calc)
                    display_message(
                        self.aid.name,
                        f'I received INFORM: {response_calc.content} from {response_calc.sender.name}'
                    )
                except FipaProtocolComplete:
                    break

            def more_long_job():
                # Massive calculations part II
                display_message(self.aid.name, f'Doing second long job')
                time.sleep(1.25)

                return response_calc.content

            def more_long_job_callback(result):
                # There is still a reference to the incoming request
                display_message(self.aid.name, f'Calling callback')
                reply_inform = message.create_reply()
                reply_inform.set_content(f'The result is: {result}')
                self.inform_behavior.send_inform(reply_inform)

                print('END OF PROTOCOL')

            # Another blocking method in other thread, this time using callback
            defer_to_thread(more_long_job, more_long_job_callback)
예제 #2
0
    def on_request(self, message):
        content = message.content
        display_message(
            self.aid.name,
            f'I received REQUEST: {content} from {message.sender.name}')

        reply_agree = message.create_reply()
        reply_agree.set_content('OK, I`ll do it, wait for me!')
        self.inform_behavior.send_agree(reply_agree)

        def do_long_job():
            # Massive calculations part I
            display_message(self.aid.name, f'Doing long job')
            time.sleep(1.5)
            question = f'{randint(1, 50)}+{randint(100, 150)}'

            return question

        @AgentSession.session
        def do_long_job_callback(question):

            request_calc = ACLMessage()
            request_calc.set_content(question)
            request_calc.add_receiver(self.calculator_aid)

            while True:
                try:
                    response_calc = yield from self.consult_behavior.send_request(
                        request_calc)
                    display_message(
                        self.aid.name,
                        f'I received INFORM: {response_calc.content} from {response_calc.sender.name}'
                    )
                except FipaProtocolComplete:
                    break

            def more_long_job():
                # Massive calculations part II
                display_message(self.aid.name, f'Doing second long job')
                time.sleep(1.25)

                return response_calc.content

            def more_long_job_callback(result):
                # There is still a reference to the incoming request
                display_message(self.aid.name, f'Calling callback')
                reply_inform = message.create_reply()
                reply_inform.set_content(f'The result is: {result}')
                self.inform_behavior.send_inform(reply_inform)

                print('END OF PROTOCOL')

            # Another blocking method in other thread, this time using callback
            defer_to_thread(more_long_job, more_long_job_callback)

        # Blocking method, must be called from another thread
        defer_to_thread(do_long_job, do_long_job_callback)
    def on_cfp(self, message):
        display_message(
            self.aid.name,
            f'I received CFP: {message.content} from {message.sender.name}'
        )
        reply = message.create_reply()

        if randint(0, 1000) > 750:
            # Refuse
            self.contract_net.send_refuse(reply)
            return

        # Propose
        reply.set_content(str(randint(0, 1000)))

        try:
            accept_proposal = yield from self.contract_net.send_propose(reply)
            display_message(
                self.aid.name,
                f'I received ACCEPT-PROPOSAL: {accept_proposal.content} from {accept_proposal.sender.name}'
            )
            # I'mma do some job in another thread
            def job():
                display_message(
                    self.aid.name,
                    f"I'mma do my job (5 seconds)"
                )
                time.sleep(5)
                return 10*int(reply.content)

            def job_callback(value):
                inform_message = accept_proposal.create_reply()
                inform_message.set_content(value)
                # Use reactor thread to send final results
                self.contract_net.send_inform(inform_message)

            defer_to_thread(job, job_callback)

        except FipaRejectProposalHandler as h:
            reject_proposal = h.message
            display_message(
                self.aid.name,
                f'I received REJECT-PROPOSAL: {reject_proposal.content} from {reject_proposal.sender.name}'
            )
예제 #4
0
    def handle_inform(self, message):
        content = json.loads(message.content)
        if content['type'] == 'ENERGY_BUYED':
            display_message(
                self.agent.aid.localname,
                'Energy Buyed from utility: {:03.2f} kW'.format(
                    content['qtd']))
        elif content['type'] == 'PRICES':
            display_message(
                self.agent.aid.name,
                'Prices received from utility {}'.format(content['prices']))

            # processo de otimizacao estocastica aqui
            utility_prices = content['prices']
            defer_to_thread(call_pyomo, self.optimal_value)

            answer = message.create_reply()
            answer.set_performative(ACLMessage.REQUEST)
            content = {'type': 'BUY_ENERGY', 'qtd': random.uniform(10, 20)}
            answer.set_content(json.dumps(content))
            self.agent.send(answer)
            display_message(
                self.agent.aid.name,
                'Buying {:03.2f} kW from utility'.format(content['qtd']))