Example #1
0
    def __init__(self, host, port, session, main_ams=True, debug=False):
        self.ams_aid = AID('ams@' + str(host) + ':' + str(port))
        super(AMS, self).__init__(self.ams_aid, debug=debug)
        self.ams = {'name': str(host), 'port': str(port)}
        super(AMS, self).update_ams(self.ams)
        self.host = host
        self.port = port
        self.session = session
        self.main_ams = main_ams

        self.agents_conn_time = dict()
        self.comport_ident = PublisherBehaviour(self)

        # message to check the connection.
        message = ACLMessage(ACLMessage.REQUEST)
        message.set_protocol(ACLMessage.FIPA_REQUEST_PROTOCOL)
        self.add_all(message)
        message.set_content('CONNECTION')
        message.set_system_message(is_system_message=True)

        self.comport_conn_verify = CompConnectionVerify(self, message)
        self.comport_send_conn_messages = ComportSendConnMessages(
            self, message, 4.0)
        self.comport_conn_verify_timed = ComportVerifyConnTimed(self, 10.0)
        self.comport_conn_verify_reg = CompVerifyRegister(self)

        self.system_behaviours.append(self.comport_ident)
        self.system_behaviours.append(self.comport_conn_verify)
        self.system_behaviours.append(self.comport_send_conn_messages)
        self.system_behaviours.append(self.comport_conn_verify_timed)
        self.system_behaviours.append(self.comport_conn_verify_reg)
        self.on_start()
Example #2
0
 def notify(self):
     message = ACLMessage(ACLMessage.INFORM)
     message.set_protocol(ACLMessage.FIPA_SUBSCRIBE_PROTOCOL)
     message.set_content(dumps(self.agent.agentInstance.table))
     message.set_system_message(is_system_message=True)
     self.STATE = 0
     super(PublisherBehaviour, self).notify(message)
Example #3
0
 def update_ams(self, ams):
     super(Agent, self).update_ams(ams)
     message = ACLMessage(ACLMessage.SUBSCRIBE)
     message.set_protocol(ACLMessage.FIPA_SUBSCRIBE_PROTOCOL)
     ams_aid = AID('ams@' + self.ams['name'] + ':' + str(self.ams['port']))
     message.add_receiver(ams_aid)
     message.set_content('IDENT')
     message.set_system_message(is_system_message=True)
     self.comport_ident = SubscribeBehaviour(self, message)
     self.system_behaviours.append(self.comport_ident)
Example #4
0
    def react(self, message):
        super(Agent, self).react(message)

        # sends the received message to AMS
        # building of the message to be sent to AMS.
        _message = ACLMessage(ACLMessage.INFORM)
        ams_aid = AID('ams@' + self.ams['name'] + ':' + str(self.ams['port']))
        _message.add_receiver(ams_aid)
        _message.set_content(dumps({'ref': 'MESSAGE', 'message': message}))
        _message.set_system_message(is_system_message=True)
        self.send(_message)
Example #5
0
    def react(self, message):
        super(Agent, self).react(message)

        if 'ams' not in message.sender.name and 'sniffer' not in self.aid.name:
            # sends the received message to Sniffer
            # building of the message to be sent to Sniffer.
            _message = ACLMessage(ACLMessage.INFORM)
            sniffer_aid = AID('sniffer@' + self.sniffer['name'] + ':' +
                              str(self.sniffer['port']))
            _message.add_receiver(sniffer_aid)
            _message.set_content(dumps({'ref': 'MESSAGE', 'message': message}))
            _message.set_system_message(is_system_message=True)
            self.send(_message)
Example #6
0
    def handle_subscribe(self, message):

        sender = message.sender

        if sender in self.agent.agentInstance.table.values():
            display_message(self.agent.aid.name,
                            'Failure when Identifying agent ' + sender.name)

            # prepares the answer message
            reply = message.create_reply()
            reply.set_content(
                'There is already an agent with this identifier. Please, choose another one.'
            )
            # sends the message
            self.agent.send(reply)
        else:
            # registers the agent in the database.

            a = AgentModel(name=sender.localname,
                           session_id=self.agent.session.id,
                           date=datetime.now(),
                           state='Active')
            db.session.add(a)
            db.session.commit()

            # registers the agent in the table of agents
            self.agent.agentInstance.table[sender.name] = sender
            # registers the agent as a subscriber in the protocol.
            self.register(message.sender)
            # registers the agent in the table of time.
            self.agent.agents_conn_time[message.sender.name] = datetime.now()

            display_message(
                self.agent.aid.name,
                'Agent ' + sender.name + ' successfully identified.')

            # prepares and sends answer messages to the agent
            reply = message.create_reply()
            reply.set_performative(ACLMessage.AGREE)
            reply.set_content('Agent successfully identified.')
            self.agent.send(reply)

            # prepares and sends the update message to
            # all registered agents.
            message = ACLMessage(ACLMessage.INFORM)
            message.set_protocol(ACLMessage.FIPA_SUBSCRIBE_PROTOCOL)
            message.set_content(dumps(self.agent.agentInstance.table))
            message.set_system_message(is_system_message=True)
            self.notify(message)
Example #7
0
 def __init__(self, aid, user_login, session_name, session):
     super(ValidadeUserAgent, self).__init__(aid=aid, debug=True)
     self.user_login = user_login
     self.session_name = session_name
     self.session= session
     super(ValidadeUserAgent,self).update_ams(session.ams)
     message = ACLMessage(ACLMessage.REQUEST)
     message.set_protocol(ACLMessage.FIPA_REQUEST_PROTOCOL)
     content = dumps({
     'ref': 'REGISTER',
     'content': {'user_login': self.user_login, 'session_name': self.session_name}})
     message.set_content(content)
     ams_aid = AID('ams@' + self.ams['name'] + ':' + str(self.ams['port']))
     message.add_receiver(ams_aid)
     message.set_system_message(is_system_message=True)
     self.comport_register_user = CompRegisterUser(self, message, self.session)
     self.system_behaviours.append(self.comport_register_user)
Example #8
0
    def react(self, message):

        if message.system_message:
            for system_behaviour in self.system_behaviours:
                system_behaviour.execute(message)
        else:
            for behaviour in self.behaviours:
                #display_message(self.aid.getLocalName(), f"Sending message {message} to Behaviour: {behaviour}")
                behaviour.execute(message)

        if 'ams' not in message.sender.name and 'sniffer' not in self.aid.name:
            # sends the received message to Sniffer
            # building of the message to be sent to Sniffer.
            _message = ACLMessage(ACLMessage.INFORM)
            sniffer_aid = AID('sniffer@' + self.sniffer['name'] + ':' +
                              str(self.sniffer['port']))
            _message.add_receiver(sniffer_aid)
            _message.set_content(dumps({'ref': 'MESSAGE', 'message': message}))
            _message.set_system_message(is_system_message=True)
            self.send(_message)
Example #9
0
    def react(self, message):
        """Summary
        
        Parameters
        ----------
        message : TYPE
            Description
        """
        super(Agent, self).react(message)

        if 'ams' not in message.sender.name and 'sniffer' not in self.aid.name:
            # sends the received message to Sniffer
            # building of the message to be sent to Sniffer.
            _message = ACLMessage(ACLMessage.INFORM)
            sniffer_aid = AID('sniffer@' + self.sniffer['name'] + ':' +
                              str(self.sniffer['port']))
            _message.add_receiver(sniffer_aid)
            _message.set_content(dumps({'ref': 'MESSAGE', 'message': message}))
            _message.set_system_message(is_system_message=True)
            self.send(_message)

        with self.messages_lock:
            if not self.ignore_ams or not message.system_message:
                self.messages.appendleft(message)