⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 twophinitiator.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A.

GNU Lesser General Public License

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation,
version 2.1 of the License.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*****************************************************************/

package jade.proto;

//#J2ME_EXCLUDE_FILE

import jade.core.behaviours.*;
import jade.core.*;
import jade.lang.acl.*;
import java.util.Vector;

/**
 * Class description
 * @author Elena Quarantotto - TILAB
 * @author Giovanni Caire - TILAB
 */
public class TwoPhInitiator extends FSMBehaviour {
    /* FSM states names */
    public static final String PH0_STATE = "Ph0";
    public static final String PH1_STATE = "Ph1";
    public static final String PH2_STATE = "Ph2";
    private static final String DUMMY_FINAL = "Dummy-final";
    private static final String TEMP = "__temp";

    private boolean logging = true; // @todo REMOVE IT!!!!!!!!!!! 
    private boolean currentLogging = true; // @todo REMOVE IT!!!!!!!!!!! 

    /**
     * Constructs a <code>TwoPhInitiator</code> behaviour.
     * @param a The agent performing the protocol.
     * @param cfp The message that must be used to initiate the protocol.
     * Notice that the default implementation of the <code>prepareCfps</code> method
     * returns an array composed of that message only.
     */
    public TwoPhInitiator(Agent a, ACLMessage cfp) {
        this(a, cfp, new DataStore());
    }

    /**
     * Constructs a <code>TwoPhInitiator</code> behaviour.
     * @param a The agent performing the protocol.
     * @param cfp The message that must be used to initiate the protocol.
     * Notice that the default implementation of the <code>prepareCfps</code> method
     * returns an array composed of that message only.
     * @param ds <code>DataStore</code> that will be used by this <code>TwoPhInitiator</code>.
     */
    public TwoPhInitiator(Agent a, ACLMessage cfp, DataStore ds) {
        super(a);
        setDataStore(ds);
        // Register the FSM transitions specific to the Two-Phase-Commit protocol 
        registerTransition(PH0_STATE, PH1_STATE, ACLMessage.QUERY_IF);
        registerTransition(PH0_STATE, PH2_STATE, ACLMessage.REJECT_PROPOSAL);
        registerTransition(PH0_STATE, DUMMY_FINAL, -1);
        registerTransition(PH0_STATE, PH0_STATE, ACLMessage.CFP, new String[] {PH0_STATE});

        registerTransition(PH1_STATE, PH2_STATE, ACLMessage.ACCEPT_PROPOSAL);
        registerTransition(PH1_STATE, PH2_STATE, ACLMessage.REJECT_PROPOSAL);
        registerTransition(PH1_STATE, DUMMY_FINAL, -1); // fix
        
        // Create and register the states specific to the Two-Phase-Commit protocol
        Behaviour b;

        /* PH0_STATE activated for the first time. It sends cfps messages and wait
        for a propose (operation completed), a failure (operation failed) or
        expiration of timeout. */
        b = new TwoPh0Initiator(myAgent, cfp, TEMP, ds) {

            protected Vector prepareCfps(ACLMessage cfp) {
                return TwoPhInitiator.this.prepareCfps(cfp);
            }

            protected void handlePropose(ACLMessage propose) {
                TwoPhInitiator.this.handlePropose(propose);
            }

            protected void handleFailure(ACLMessage failure) {
                TwoPhInitiator.this.handleFailure(failure);
            }

            protected void handleNotUnderstood(ACLMessage notUnderstood) {
                TwoPhInitiator.this.handleNotUnderstood(notUnderstood);
            }

            protected void handleOutOfSequence(ACLMessage msg) {
                TwoPhInitiator.this.handleOutOfSequence(msg);
            }

            protected void handleAllResponses(Vector responses, Vector proposes,
                                              Vector pendings, Vector nextPhMsgs) {
                TwoPhInitiator.this.handleAllPh0Responses(responses, proposes, pendings, nextPhMsgs);
            }
        };
        registerFirstState(b, PH0_STATE);

        /* PH1_STATE activated if phase 0 succeded (all propose in phase 0). It
        sends queryIf messages and wait for a confirm (receiver prepared), a
        disconfirm (receiver aborted), an inform (receiver not changed) or
        expiration of timeout. */
        b = new TwoPh1Initiator(myAgent, null, TEMP, ds)  {
			    protected void initializeDataStore(ACLMessage msg) {
        		// Use the QUERY_IF messages prepared in previous phase
        		Vector v = (Vector) getDataStore().get(TEMP);
        		getDataStore().put(ALL_QUERYIFS_KEY, v);
			    	super.initializeDataStore(msg);
        	}
        	
            protected void handleConfirm(ACLMessage confirm) {
                TwoPhInitiator.this.handleConfirm(confirm);
            }

            protected void handleDisconfirm(ACLMessage disconfirm) {
                TwoPhInitiator.this.handleDisconfirm(disconfirm);
            }

            protected void handleInform(ACLMessage inform) {
                TwoPhInitiator.this.handlePh1Inform(inform);
            }

            protected void handleFailure(ACLMessage failure) {
                TwoPhInitiator.this.handleFailure(failure);
            }

            protected void handleNotUnderstood(ACLMessage notUnderstood) {
                TwoPhInitiator.this.handleNotUnderstood(notUnderstood);
            }

            protected void handleOutOfSequence(ACLMessage msg) {
                TwoPhInitiator.this.handleOutOfSequence(msg);
            }

            protected void handleAllResponses(Vector responses, Vector confirms, Vector disconfirms,
                                              Vector informs, Vector pendings, Vector nextPhMsgs) {
                TwoPhInitiator.this.handleAllPh1Responses(responses, confirms, disconfirms, informs, pendings, nextPhMsgs);
            }
        };
        registerState(b, PH1_STATE);

        /* PH2_STATE activated when phase 0 fails (some failure or expiration
        of timeout), phase 1 fails (some disconfirm or expiration of timeout) or
        phase 1 succeds (no disconfirms). In the first and third case it sends
        reject-proposal; in the second case it sends accept-proposal. */
        b = new TwoPh2Initiator(myAgent, null, ds) {
			    protected void initializeDataStore(ACLMessage msg) {
        		// Use the acceptance messages prepared in previous phase
        		Vector v = (Vector) getDataStore().get(TEMP);
        		getDataStore().put(ALL_ACCEPTANCES_KEY, v);
			    	super.initializeDataStore(msg);
        	}
        	
            protected void handleInform(ACLMessage inform) {
                TwoPhInitiator.this.handlePh2Inform(inform);
            }

            protected void handleOldResponse(ACLMessage old) {
                TwoPhInitiator.this.handleOldResponse(old);
            }

            protected void handleFailure(ACLMessage failure) {
                TwoPhInitiator.this.handleFailure(failure);
            }

            protected void handleNotUnderstood(ACLMessage notUnderstood) {
                TwoPhInitiator.this.handleNotUnderstood(notUnderstood);
            }

            protected void handleOutOfSequence(ACLMessage msg) {
                TwoPhInitiator.this.handleOutOfSequence(msg);
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -