Magentix2  2.1.1
es.upv.dsic.gti_ia.architecture.FIPAContractNetResponder Class Reference
Collaboration diagram for es.upv.dsic.gti_ia.architecture.FIPAContractNetResponder:

List of all members.

Public Member Functions

 FIPAContractNetResponder (QueueAgent _agent, MessageTemplate _template)
QueueAgent getQueueAgent ()
void action ()

Public Attributes

QueueAgent myAgent

Protected Member Functions

ACLMessage prepareResponse (ACLMessage cfp) throws NotUnderstoodException, RefuseException
ACLMessage prepareResultNotification (ACLMessage cfp, ACLMessage propose, ACLMessage accept) throws FailureException
void handleRejectProposal (ACLMessage cfp, ACLMessage propose, ACLMessage reject)
void handleAcceptProposal (ACLMessage cfp, ACLMessage propose, ACLMessage accept)

Package Functions

int getState ()

Detailed Description

This class implements the FIPA-Contract-Net interaction protocol, Role Responder

Author:
Joan Bellver Faus, GTI-IA, DSIC, UPV
Version:
2009.9.07

Definition at line 14 of file FIPAContractNetResponder.java.


Constructor & Destructor Documentation

Creates a new FIPA-Contract-Net interaction protocol, rol responder.

Parameters:
agentis the reference to the Agent Object
templateis a MessageTemplate, will serve as a filter for receiving the right message

Definition at line 45 of file FIPAContractNetResponder.java.

                                                                                      {
                myAgent = _agent;
                template = _template;
                this.monitor = myAgent.addMonitor(this);

        }

Member Function Documentation

Runs the state machine with the communication protocol

Definition at line 69 of file FIPAContractNetResponder.java.

                             {

                switch (state) {
                case WAITING_MSG_STATE: {
                        ACLMessage request = myAgent.receiveACLMessage(template, 1);

                        if (request != null) {

                                this.cfp = request;
                                state = PREPARE_RESPONSE_STATE;
                        } else {

                                monitor.waiting();//waiting for a new messages

                        }
                        break;
                }
                case PREPARE_RESPONSE_STATE: {
                        ACLMessage request = this.cfp;
                        ACLMessage response = null;
                        state = SEND_RESPONSE_STATE;
                        try {
                                response = prepareResponse(request);
                        } catch (NotUnderstoodException nue) {
                                response = request.createReply();
                                response.setContent(nue.getMessage());
                                response.setPerformative(ACLMessage.NOT_UNDERSTOOD);

                        } catch (RefuseException re) {

                                response = request.createReply();
                                response.setContent(re.getMessage());
                                response.setPerformative(ACLMessage.REFUSE);

                        }

                        this.propose = response;
                        break;
                }
                case SEND_RESPONSE_STATE: {
                        ACLMessage response = this.propose;

                        if (response != null) {

                                response = arrangeMessage(this.cfp, response);
                                response.setSender(myAgent.getAid());
                                //If the message is for a agent Jade
                                if (response.getReceiver() != null) {
                                        if (response.getReceiver(0).protocol.equals("http")) {
                                                name = response
                                                                .getReceiver()
                                                                .name_all()
                                                                .substring(
                                                                                0,
                                                                                response
                                                                                                .getReceiver()
                                                                                                .name_all()
                                                                                                .indexOf(
                                                                                                                "@",
                                                                                                                response
                                                                                                                                .getReceiver()
                                                                                                                                .name_all()
                                                                                                                                .indexOf(
                                                                                                                                                "@") + 1));
                                                if (response.getReceiver().port.indexOf(":") != -1)
                                                {
                                                 port = response.getReceiver().port
                                                                .substring(response.getReceiver().port
                                                                                .indexOf(":") + 1, response
                                                                                .getReceiver().port
                                                                                .indexOf("/", 10));
                                                }
                                                else
                                                {
                                                        port = response.getReceiver().port
                                                        .substring(0, response
                                                                        .getReceiver().port
                                                                        .indexOf("/"));
                                                        
                                                }
                                        
                                                
                                                response.getReceiver().name = name;
                                                response.getReceiver().port = port;

                                        }
                                }
                                myAgent.send(response);
                                if (response.getPerformativeInt() == ACLMessage.PROPOSE)
                                        state = RECEIVE_MSG_STATE;
                                else
                                        //If the performative is refuse then we cancel the protocol.
                                        state = RESET_STATE;
                        }

                        break;

                }

                case RECEIVE_MSG_STATE: {
                        
                        //configure a new template to wait only the response for the agent wich we send a message 
                        MessageTemplate template2 = new MessageTemplate(
                                        InteractionProtocol.FIPA_CONTRACT_NET);
                        template2.addConversation(this.propose.getConversationId());
                        template2.add_receiver(this.propose.getReceiver());
                        ACLMessage secondReply = myAgent.receiveACLMessage(template2, 0);

                        
                        //waiting to the agent accepts the second proposal
                        if (secondReply != null) {

                                switch (secondReply.getPerformativeInt()) {
                                case ACLMessage.REJECT_PROPOSAL: {
                                        this.reject = secondReply;
                                        state = RESET_STATE;
                                        handleRejectProposal(this.cfp, this.propose, this.reject);
                                        break;

                                }
                                case ACLMessage.ACCEPT_PROPOSAL: {
                                        this.accept = secondReply;
                                        state = PREPARE_RES_NOT_STATE;
                                        handleAcceptProposal(this.cfp, this.propose, this.accept);
                                        break;

                                }
                                }
                                break;
                        } else {
                                this.monitor.waiting();
                                state = RECEIVE_MSG_STATE;
                                break;
                        }
                }

                case PREPARE_RES_NOT_STATE: {

                        state = SEND_RESULT_NOTIFICATION_STATE;

                        ACLMessage resNotification = null;

                        try {
                                resNotification = prepareResultNotification(this.cfp,
                                                this.propose, this.accept);
                        } catch (FailureException fe) {

                                resNotification = cfp.createReply();

                                resNotification.setContent(fe.getMessage());
                                resNotification.setPerformative(ACLMessage.FAILURE);
                        }

                        this.resNofificationmsg = resNotification;
                        break;
                }
                case SEND_RESULT_NOTIFICATION_STATE: {
                        state = RESET_STATE;
                        ACLMessage resNotification = this.resNofificationmsg;
                        if (resNotification != null) {

                                ACLMessage receiveMsg = arrangeMessage(this.accept,
                                                resNotification);
                                receiveMsg.setSender(myAgent.getAid());
                                if (receiveMsg.getReceiver() != null) {
                                        if (receiveMsg.getReceiver(0).protocol.equals("http")) {

                                                receiveMsg.getReceiver().name = name;
                                                receiveMsg.getReceiver().port = port;
                                        }
                                }
                                
                                myAgent.send(receiveMsg);

                        }

                        break;

                }
                case RESET_STATE: {

                        state = WAITING_MSG_STATE;
                        this.cfp = null;
                        this.accept = null;
                        this.reject = null;
                        this.propose = null;
                        this.resNofificationmsg = null;
                        break;
                }

                }

        }

Definition at line 61 of file FIPAContractNetResponder.java.

                        {
                return this.state;
        }

This method is called when ACCEPT-PROPOSAL is received from the initiator.

Parameters:
cfpinitial CFP message
proposethe PROPOSE message sent back as reply to the initial CFP message
acceptthe received ACCEPT_PROPOSAL message.

Definition at line 337 of file FIPAContractNetResponder.java.

                                           {

        }

This method is called when REJECT-PROPOSAL is received from the initiator.

Parameters:
cfpinitial CFP message
proposethe PROPOSE message sent back as reply to the initial CFP message
rejectthe received REJECT_PROPOSAL message

Definition at line 320 of file FIPAContractNetResponder.java.

                                           {

        }

This method is called when the initiator's message is received that matches the message template passed in the constructor.

Parameters:
cfpinitial CFP message
Returns:
Exceptions:
NotUnderstoodException
RefuseException

Definition at line 284 of file FIPAContractNetResponder.java.

                                                                       {
                return null;
        }

This method is called after the response has been sent and only when one of the following two cases arise: the response was an agree message OR no response message was sent.

Parameters:
cfpinitial CFP message
proposepropose message
acceptaccept message
Returns:
Exceptions:
FailureException

Definition at line 303 of file FIPAContractNetResponder.java.

                                                                                       {
                return null;
        }

Member Data Documentation


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables