Magentix2
2.1.1
|
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 () |
This class implements the FIPA-Contract-Net interaction protocol, Role Responder
Definition at line 14 of file FIPAContractNetResponder.java.
es.upv.dsic.gti_ia.architecture.FIPAContractNetResponder.FIPAContractNetResponder | ( | QueueAgent | _agent, |
MessageTemplate | _template | ||
) |
Creates a new FIPA-Contract-Net interaction protocol, rol responder.
agent | is the reference to the Agent Object |
template | is 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); }
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; } } }
Returns the agent.
Definition at line 56 of file FIPAContractNetResponder.java.
{ return this.myAgent; }
int es.upv.dsic.gti_ia.architecture.FIPAContractNetResponder.getState | ( | ) | [package] |
Definition at line 61 of file FIPAContractNetResponder.java.
{
return this.state;
}
void es.upv.dsic.gti_ia.architecture.FIPAContractNetResponder.handleAcceptProposal | ( | ACLMessage | cfp, |
ACLMessage | propose, | ||
ACLMessage | accept | ||
) | [protected] |
This method is called when ACCEPT-PROPOSAL is received from the initiator.
cfp | initial CFP message |
propose | the PROPOSE message sent back as reply to the initial CFP message |
accept | the received ACCEPT_PROPOSAL message. |
Definition at line 337 of file FIPAContractNetResponder.java.
{ }
void es.upv.dsic.gti_ia.architecture.FIPAContractNetResponder.handleRejectProposal | ( | ACLMessage | cfp, |
ACLMessage | propose, | ||
ACLMessage | reject | ||
) | [protected] |
This method is called when REJECT-PROPOSAL is received from the initiator.
cfp | initial CFP message |
propose | the PROPOSE message sent back as reply to the initial CFP message |
reject | the received REJECT_PROPOSAL message |
Definition at line 320 of file FIPAContractNetResponder.java.
{ }
ACLMessage es.upv.dsic.gti_ia.architecture.FIPAContractNetResponder.prepareResponse | ( | ACLMessage | cfp | ) | throws NotUnderstoodException, RefuseException [protected] |
This method is called when the initiator's message is received that matches the message template passed in the constructor.
cfp | initial CFP message |
NotUnderstoodException | |
RefuseException |
Definition at line 284 of file FIPAContractNetResponder.java.
{
return null;
}
ACLMessage es.upv.dsic.gti_ia.architecture.FIPAContractNetResponder.prepareResultNotification | ( | ACLMessage | cfp, |
ACLMessage | propose, | ||
ACLMessage | accept | ||
) | throws FailureException [protected] |
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.
cfp | initial CFP message |
propose | propose message |
accept | accept message |
FailureException |
Definition at line 303 of file FIPAContractNetResponder.java.
{
return null;
}
Definition at line 26 of file FIPAContractNetResponder.java.