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

📄 contractnetinitiator.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			if (s.update(perf)) {
	    	// The reply is compliant to the protocol 
				Vector all = (Vector) getDataStore().get(step == 1 ? ALL_RESPONSES_KEY : ALL_RESULT_NOTIFICATIONS_KEY);
				all.addElement(reply);
				ret = true;
			}
			if (s.isCompleted()) {
				sessions.remove(inReplyTo);
			}
		}
		return ret;
  }
  
  /**
     Check the status of the sessions after the reception of the last reply
     or the expiration of the timeout
   */    
  protected int checkSessions(ACLMessage reply) {
		if (skipNextRespFlag) {
			sessions.clear();
		}
		
		int ret = (step == 1 ? ALL_RESPONSES_RECEIVED : ALL_RESULT_NOTIFICATIONS_RECEIVED);
    if (reply != null) {
    	if (sessions.size() > 0) {
    		// If there are still active sessions we haven't received
    		// all responses/result_notifications yet
		    ret = -1;
  		}
    }
  	else {
    	// Timeout has expired or we were interrupted --> clear all remaining sessions
  		sessions.clear();
  	}
  	if (ret != -1) {
  		step++;
  	}
	  return ret;
  }
  
	private String[] toBeReset = null;
		
  /**
   */
  protected String[] getToBeReset() {
  	if (toBeReset == null) {
			toBeReset = new String[] {
				HANDLE_PROPOSE, 
				HANDLE_REFUSE,
				HANDLE_NOT_UNDERSTOOD,
				HANDLE_INFORM,
				HANDLE_FAILURE,
				HANDLE_OUT_OF_SEQ
			};
  	}
  	return toBeReset;
  }
  //#APIDOC_EXCLUDE_END
    
  /**
   * This method must return the vector of ACLMessage objects to be
   * sent. It is called in the first state of this protocol.
   * This default implementation just returns the ACLMessage object (a CFP)
   * passed in the constructor. Programmers might prefer to override
   * this method in order to return a vector of CFP objects for 1:N conversations
   * or also to prepare the messages during the execution of the behaviour.
   * @param cfp the ACLMessage object passed in the constructor
   * @return a Vector of ACLMessage objects. The value of the slot
   * <code>reply-with</code> is ignored and regenerated automatically
   * by this class.
   **/    
  protected Vector prepareCfps(ACLMessage cfp) {
		Vector v = new Vector(1);
		v.addElement(cfp);
		return v;
  }
    
  /**
   * This method is called every time a <code>propose</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 propose the received propose message
   * @param acceptances the list of ACCEPT/REJECT_PROPOSAL to be sent back.
   * This list can be filled step by step redefining this method, or
   * it can be filled at once
   * redefining the handleAllResponses method.
   **/
  protected void handlePropose(ACLMessage propose, Vector acceptances) {
  }

  /**
   * This method is called every time a <code>refuse</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 refuse the received refuse message
   **/
  protected void handleRefuse(ACLMessage refuse) {
  }

  /**
   * This method is called every time a <code>inform</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 inform the received inform message
   **/
  protected void handleInform(ACLMessage inform) {
  }
    
  /**
   * 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>propose, not-understood,
   * refuse</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 
   * @param acceptances the list of ACCEPT/REJECT_PROPOSAL to be sent back.
   * This list can be filled at once redefining this method, or step by step 
   * redefining the handlePropose method.
   **/
  protected void handleAllResponses(Vector responses, Vector acceptances) {
  }
    
  /**
   * This method is called when all the result notification messages 
   * have been
   * collected. 
   * By result notification message we intend here all the <code>inform, 
   * failure</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 resultNodifications the Vector of ACLMessage object received 
   **/
  protected void handleAllResultNotifications(Vector resultNotifications) {
  }
    
    
  /**
     This method allows to register a user-defined <code>Behaviour</code>
     in the PREPARE_CFPS 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.
     It is responsibility of the registered behaviour to put the
     Vector of ACLMessage objects to be sent 
     into the datastore at the <code>ALL_CFPS_KEY</code>
     key.
     @param b the Behaviour that will handle this state
   */
  public void registerPrepareCfps(Behaviour b) {
    registerPrepareInitiations(b);
  }
    
  /**
     This method allows to register a user defined <code>Behaviour</code>
     in the HANDLE_PROPOSE 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>propose</code> ACLMessage object received
     from the datastore at the <code>REPLY_KEY</code>
     key and the <code>Vector</code> of ACCEPT/REJECT_PROPOSAL to be 
     sent back at the <code>ALL_ACCEPTANCES_KEY</code>
     @param b the Behaviour that will handle this state
   */
  public void registerHandlePropose(Behaviour b) {
		registerState(b, HANDLE_PROPOSE);
		b.setDataStore(getDataStore());
  }
     
  /**
     This method allows to register a user defined <code>Behaviour</code>
     in the HANDLE_REFUSE 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>refuse</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 registerHandleRefuse(Behaviour b) {
		registerState(b, HANDLE_REFUSE);
		b.setDataStore(getDataStore());
  }
    
  /**
     This method allows to register a user defined <code>Behaviour</code>
     in the HANDLE_INFORM 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 registerHandleInform(Behaviour b) {
		registerState(b, HANDLE_INFORM);
		b.setDataStore(getDataStore());
  }
  
    /**
       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 and the <code>Vector</code> of ACCEPT/REJECT_PROPOSAL to be 
     	 sent back at the <code>ALL_ACCEPTANCES_KEY</code>
       @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_ALL_RESULT_NOTIFICATIONS 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 result notification,
       from the datastore at the <code>ALL_RESULT_NOTIFICATIONS_KEY</code>
       key.
       @param b the Behaviour that will handle this state
    */
    public void registerHandleAllResultNotifications(Behaviour b) {
	registerLastState(b, HANDLE_ALL_RESULT_NOTIFICATIONS);
	b.setDataStore(getDataStore());
    }
    
  /**
     This method can be called (typically within the handlePropose() method)
     to skip all responses that have not been received yet.
   */
  public void skipNextResponses() {
  	skipNextRespFlag = true;
  }
  
  protected void reinit() {
		step = 1;
		skipNextRespFlag = false;
		super.reinit();
  }

  //#APIDOC_EXCLUDE_BEGIN
  /**
     Initialize the data store. 
   **/
  protected void initializeDataStore(ACLMessage msg){
  	super.initializeDataStore(msg);
		DataStore ds = getDataStore();
		Vector l = new Vector();
		ds.put(ALL_RESPONSES_KEY, l);
		l = new Vector();
		ds.put(ALL_RESULT_NOTIFICATIONS_KEY, l);
		l = new Vector();
		ds.put(ALL_ACCEPTANCES_KEY, l);
  }
  //#APIDOC_EXCLUDE_END
        
  
  protected ProtocolSession getSession(ACLMessage msg, int sessionIndex) {
  	if (msg.getPerformative() == ACLMessage.CFP) {
  		return new Session(1);
  	}
  	else if (msg.getPerformative() == ACLMessage.ACCEPT_PROPOSAL) {
  		return new Session(2);
  	}
  	else {
  		return null;
  	}
  }
  		
  /**
     Inner class Session
   */
  class Session implements ProtocolSession, Serializable {
  	// Session states
  	static final int INIT = 0;
  	static final int REPLY_RECEIVED = 1;
		
		private int state = INIT;
		private int step;
		
		public Session(int step) {
			this.step = step;
		}
		
		public String getId() {
			return null;
		}
		
		/** Return true if received ACLMessage is consistent with the protocol */
		public boolean update(int perf) {
			if (state == INIT) {
	    	if (step == 1) {
					switch (perf) {
					case ACLMessage.PROPOSE:
					case ACLMessage.REFUSE:
					case ACLMessage.NOT_UNDERSTOOD:
					case ACLMessage.FAILURE:
		    		state = REPLY_RECEIVED;
		    		return true;
					default:
		    		return false;
					}
				}
				else {
					switch (perf) {
					case ACLMessage.INFORM:
					case ACLMessage.NOT_UNDERSTOOD:
					case ACLMessage.FAILURE:
		    		state = REPLY_RECEIVED;
		    		return true;
					default:
		    		return false;
					}
				}
	    }
	    else {
	    	return false;
	    }
		}
					
	
		public int getState() {
	    return state;
		}
	
		public boolean isCompleted() {
	    return (state == REPLY_RECEIVED);
		}
  } // End of inner class Session
    
}
	
		

⌨️ 快捷键说明

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