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

📄 proposeinitiator.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   * This method is called every time an <code>accept-proposal</code>
   * message is received, which is not out-of-sequence according
   * to the protocol rules.
   * This default implementation does nothing; programmers might
   * wish to override the method in case they need to react to this event.
   * @param accept_proposal the received accept-proposal message
   **/
  protected void handleAcceptProposal(ACLMessage accept_proposal) {
  }

  /**
   * This method is called every time an <code>reject-proposal</code>
   * message is received, which is not out-of-sequence according
   * to the protocol rules.
   * This default implementation does nothing; programmers might
   * wish to override the method in case they need to react to this event.
   * @param reject_proposal the received reject-proposal message
   **/
  protected void handleRejectProposal(ACLMessage reject_proposal) {
  }

  /**
   * This method is called every time a <code>not-understood</code>
   * message is received, which is not out-of-sequence according
   * to the protocol rules.
   * This default implementation does nothing; programmers might
   * wish to override the method in case they need to react to this event.
   * @param notUnderstood the received not-understood message
   **/
  protected void handleNotUnderstood(ACLMessage notUnderstood) {
  }

  /**
   * This method is called every time a 
   * message is received, which is out-of-sequence according
   * to the protocol rules.
   * This default implementation does nothing; programmers might
   * wish to override the method in case they need to react to this event.
   * @param msg the received message
   **/
  protected void handleOutOfSequence(ACLMessage msg) {
  }

  /**
   * This method allows to register a user defined <code>Behaviour</code>
   * in the HANDLE_ALL_RESPONSES state.
   * This behaviour would override the homonymous method.
   * This method also set the 
   * data store of the registered <code>Behaviour</code> to the
   * DataStore of this current behaviour.
   * The registered behaviour can retrieve
   * the vector of ACLMessage objects, received as a response,
   * from the datastore at the <code>ALL_RESPONSES_KEY</code>
   * key.
   * @param b the Behaviour that will handle this state
   */
  public void registerHandleAllResponses(Behaviour b) {
    registerState(b, HANDLE_ALL_RESPONSES);
    b.setDataStore(getDataStore());
  }

  /**
   * This method allows to register a user defined <code>Behaviour</code>
   * in the HANDLE_ACCEPT_PROPOSAL state.
   * This behaviour would override the homonymous method.
   * This method also set the 
   * data store of the registered <code>Behaviour</code> to the
   * DataStore of this current behaviour.
   * The registered behaviour can retrieve
   * the <code>inform</code> ACLMessage object received
   * from the datastore at the <code>REPLY_KEY</code> key.
   * @param b the Behaviour that will handle this state
   */
  public void registerHandleAcceptProposal(Behaviour b) {
    registerState(b, HANDLE_ACCEPT_PROPOSAL);
    b.setDataStore(getDataStore());
  }

  /**
   * This method allows to register a user defined <code>Behaviour</code>
   * in the HANDLE_REJECT_PROPOSAL state.
   * This behaviour would override the homonymous method.
   * This method also set the 
   * data store of the registered <code>Behaviour</code> to the
   * DataStore of this current behaviour.
   * The registered behaviour can retrieve
   * the <code>inform</code> ACLMessage object received
   * from the datastore at the <code>REPLY_KEY</code> key.
   * @param b the Behaviour that will handle this state
   */
  public void registerHandleRejectProposal(Behaviour b) {
    registerState(b, HANDLE_REJECT_PROPOSAL);
    b.setDataStore(getDataStore());
  }


  /**
   * This method allows to register a user defined <code>Behaviour</code>
   * in the HANDLE_NOT_UNDERSTOOD state.
   * This behaviour would override the homonymous method.
   * This method also set the 
   * data store of the registered <code>Behaviour</code> to the
   * DataStore of this current behaviour.
   * The registered behaviour can retrieve
   * the <code>not-understood</code> ACLMessage object received
   * from the datastore at the <code>REPLY_KEY</code>
   * key.
   * @param b the Behaviour that will handle this state
   */
  public void registerHandleNotUnderstood(Behaviour b) {
    registerState(b, HANDLE_NOT_UNDERSTOOD);
    b.setDataStore(getDataStore());
  }

  /**
   * This method allows to register a user defined <code>Behaviour</code>
   * in the HANDLE_OUT_OF_SEQ state.
   * This behaviour would override the homonymous method.
   * This method also set the 
   * data store of the registered <code>Behaviour</code> to the
   * DataStore of this current behaviour.
   * The registered behaviour can retrieve
   * the <code>out of sequence</code> ACLMessage object received
   * from the datastore at the <code>REPLY_KEY</code>
   * key.
   * @param b the Behaviour that will handle this state
   */
  public void registerHandleOutOfSequence(Behaviour b) {
    registerState(b, HANDLE_OUT_OF_SEQ);
    b.setDataStore(getDataStore());
  }

  /**
   * This method is called when all the responses have been
   * collected or when the timeout is expired.
   * The used timeout is the minimum value of the slot <code>replyBy</code> 
   * of all the sent messages. 
   * By response message we intend here all the <code>accept-proposal,
   * reject-proposal, not-understood</code> received messages, which are not
   * not out-of-sequence according to the protocol rules.
   * This default implementation does nothing; programmers might
   * wish to override the method in case they need to react to this event
   * by analysing all the messages in just one call.
   * @param responses the Vector of ACLMessage objects that have been received 
   **/
  protected void handleAllResponses(Vector responses) {
  }

  /**
   * reset this behaviour by putting a null ACLMessage as message
   * to be sent
   **/
  public void reset(){
    reset(null);
  }

  /**
   * reset this behaviour
   * @param msg is the ACLMessage to be sent
   **/
  public void reset(ACLMessage msg){
    super.reset();
    replyReceiver.reset(null, MsgReceiver.INFINITE, getDataStore(),REPLY_KEY);
    initiation = msg;
    sessions.clear();
    allResponsesReceived = false;
  }

  /** 
   * Override the onStart() method to initialize the vectors that
   * will keep all the replies in the data store.
   */
  public void onStart() {
    initializeDataStore(initiation);
  }
    
  /** 
   * Override the setDataStore() method to initialize propagate this
   * setting to all children.
   */
  public void setDataStore(DataStore ds) {
    super.setDataStore(ds);
    Iterator it = getChildren().iterator();
    while (it.hasNext()) {
      Behaviour b = (Behaviour) it.next();
      b.setDataStore(ds);
    }
  }

    /**
       Create a new conversation identifier to begin a new
       interaction.
       @param msgs A vector of ACL messages. If the first one has a
       non-empty <code>:conversation-id</code> slot, its value is
       used, else a new conversation identifier is generated.
    */
  protected String createConvId(Vector msgs) {
    // If the conversation-id of the first message is set --> 
    // use it. Otherwise create a default one
    String convId = null;
    if (msgs.size() > 0) {
      ACLMessage msg = (ACLMessage) msgs.elementAt(0);
      if ((msg == null) || (msg.getConversationId() == null)) {
        convId = "C"+hashCode()+"_"+System.currentTimeMillis();
      }
      else {
        convId = msg.getConversationId();
      }
    }
    return convId;
  }

    //#APIDOC_EXCLUDE_BEGIN
  protected void adjustReplyTemplate(ACLMessage msg) {
    // If myAgent is among the receivers (strange case, but can happen)
    // then modify the replyTemplate to avoid intercepting the initiation
    // message msg as if it was a reply
    AID r = (AID) msg.getAllReceiver().next();
    if (myAgent.getAID().equals(r)) {
      replyTemplate = MessageTemplate.and(
                                          replyTemplate,
                                          MessageTemplate.not(MessageTemplate.MatchCustom(msg, true)));
    }
  }
    //#APIDOC_EXCLUDE_END


  /**
   * Inner class Session
   */
  private static class Session implements Serializable {
    // Session states
    static final int INIT = 0;
    static final int NEGATIVE_RESPONSE_RECEIVED = 2;
    static final int RESULT_NOTIFICATION_RECEIVED = 3;
		
    private int state = INIT;
	
    /**
       return true if the received performative is valid with respect to
       the current session state.
    */
    boolean update(int perf) {
	    switch (state) {
	    case INIT:
        switch (perf) {
        case ACLMessage.NOT_UNDERSTOOD:
          state = NEGATIVE_RESPONSE_RECEIVED;
          return true;
        case ACLMessage.REJECT_PROPOSAL:
        case ACLMessage.ACCEPT_PROPOSAL:
          state = RESULT_NOTIFICATION_RECEIVED;
          return true;
        default:
          return false;
        }
	    default:
        return false;
	    }
    }
	
    int getState() {
	    return state;
    }
	
    boolean isCompleted() {
	    return (state == NEGATIVE_RESPONSE_RECEIVED || state == RESULT_NOTIFICATION_RECEIVED);
    }
	
  } // End of inner class Session
}

⌨️ 快捷键说明

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