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

📄 dfagent.java

📁 网格agent平台(GAP ,Grid AgentsPlatform)开发包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 *
	 * @param agentRequest
	 *            an instance of AgentRequest class specifying parameters the
	 *            agent to kill
	 * @return an instance of AgentReply class
	 *
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 * @see net.p2pgrid.gap.agents.messages.AgentReply
	 */
	protected AgentReply killAgent(AgentRequest agentRequest) {
		return this.requestToAgent(agentRequest, Tags.AGENT_KILL_REQ);
	}

	/**
	 * This method is responsible to send a delayed KILL request to an existing
	 * agent
	 *
	 * @param agentRequest
	 *            an instance of AgentRequest class specifying parameters the
	 *            agent to kill while waiting for its gridlets
	 * @return an instance of AgentReply class
	 *
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 * @see net.p2pgrid.gap.agents.messages.AgentReply
	 */
	protected AgentReply killWaitAgent(AgentRequest agentRequest) {
		return this.requestToAgent(agentRequest, Tags.AGENT_KILLAWAIT_REQ);
	}

	/**
	 * This method is responsible to send a MOVE request to an existing agent
	 *
	 * @param agentRequest
	 *            an instance of AgentRequest class specifying parameters the
	 *            agent to move
	 * @param moveToResourceID
	 *            the destination resource ID for the migration
	 * @return an instance of AgentReply class
	 *
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 * @see net.p2pgrid.gap.agents.messages.AgentReply
	 */
	protected AgentReply moveAgent(AgentRequest agentRequest,
			int moveToResourceID) {
		agentRequest.setDst_moveToresID(moveToResourceID);
		return this.requestToAgent(agentRequest, Tags.AGENT_MOVE_REQ);
	}

	/**
	 * This method is responsible to send any request related to agents
	 * lifecycle to an existing agent Allowed requests: RUN, PAUSE, RESUME,
	 * KILL, delayed KILL, MOVE
	 *
	 * @param request
	 *            an instance of AgentRequest specifying parameters for any
	 *            request related to agent's lifecycle
	 * @param tag
	 *            the tag specifying the requested action
	 * @return an instance of AgentReply class
	 *
	 * @see eduni.simjava.Sim_type_p
	 * @see eduni.simjava.Sim_event
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 * @see net.p2pgrid.gap.agents.messages.AgentReply
	 */
	private AgentReply requestToAgent(AgentRequest request, int tag) {
		// Unique request ID
		int requestID = request.getRequestID();
		// Unique conversation (request/reply) ID
		int reqrepID = request.getReqrepID();
		// Event send time
		double evsend_time;

		// Destination agent's entity type
		int agentType = request.getDst_entityType();

		// Sets agent request transfer size
		int SIZE;
		if (tag == Tags.AGENT_RUN_REQ) {
			SIZE = request.getDst_agentSize();
		} else {
			// @TODO tune fixed SIZE for messages between agents
			SIZE = 500;
		}

		// if requested action is RUN, PAUSE, RESUME, KILL, delayed KILL
		if ((tag == Tags.AGENT_RUN_REQ) || (tag == Tags.AGENT_KILL_REQ)
				|| (tag == Tags.AGENT_KILLAWAIT_REQ)
				|| (tag == Tags.AGENT_PAUSE_REQ)
				|| (tag == Tags.AGENT_RESUME_REQ)) {
			// Sends agent's request to destination agent indirectly
			// through its Grid Element resource ID that acts as
			// DFAgent Middleware
			// @TODO for a better SoC DFAgent Middleware should be separated
			// from Grid Element in order to use GridSim entities without
			// requiring a class like AbstractGridElement
			super.send(super.output, GridSimTags.SCHEDULE_NOW, tag,
					new IO_data(request, SIZE, request.getDst_resID()));
		} else {
			// @TODO verify possible alternative tags (should be only
			// Tags.AGENT_MOVE_REQ, added an Assert for this goal
			Assert.assertEquals(tag == Tags.AGENT_MOVE_REQ, true);
			// Sends agent's request directly to destination agent
			super.send(super.output, GridSimTags.SCHEDULE_NOW, tag,
					new IO_data(request, SIZE, request.getDst_agentID()));
		}
		// Sets event's send time to current simulation's clock
		evsend_time = GridSim.clock();
		// Prepares a message for simulation's output
		// @TODO Note that an improvement of current GAP library should pass
		// through a global revision of its simulation's output that, i.e.,
		// could rely upon existing libraries like log4j
		String msg = String
				.format(
						"%1$f %2$d %3$s --> AM_%4$s::REQUEST %6$s (%7$s AID %9$d) %5$s AM_%8$s",
						evsend_time, reqrepID, this.get_name(), AbstractAgent
								.getEntityName(request.getDst_resID()),
						EntityTypes.toString(request.getDst_entityType()), Tags
								.toString(tag), AbstractAgent
								.getEntityName(request.getDst_agentID()),
						AbstractAgent.getEntityName(request
								.getDst_moveToresID()), request.getDst_AID());
		this.write(msg);

		// This segment of the code is responsible for
		// - syncronously receiving the reply to the agent's request
		// - parsing such reply
		// - return such reply as method's result
		// Note: As a convention in GAP library all tags' replies have a value
		// equal to tag's request value plus one

		// Create an instance of Sim_type_p class
		// It's a predicate to select events with specific tags
		// In this case the reply tag related to the request tag
		Sim_type_p ptag = new Sim_type_p(tag + 1);
		// Creates a new instance of Sim_event class
		Sim_event ev = new Sim_event();
		// Get the first event waiting in the entity's deferred queue
		// or if there are none, wait for an event to arrive
		// Event tag is that of related agent's reply that
		// the agent is syncronously waiting for
		super.sim_get_next(ptag, ev);
		// Extracts an instance of AgentReply class from the received event
		AgentReply agentReply = AgentReply.get_data(ev);

		// Checks that:
		// - the request ID, encapsulated in agent's reply is the
		// same of that agent's request
		// - the request tag, encapsulated in agent's reply is the
		// same of that agent's request
		// - the reply tag is really associated to the request tag
		Assert.assertEquals(requestID, agentReply.getRequestID());
		Assert.assertEquals(tag, agentReply.getRequestTAG());
		Assert.assertEquals(ev.get_tag(), tag + 1);

		// Sets event's receive time to current simulation's clock
		double evrecv_time = GridSim.clock();
		// Prepares a message for simulation's output
		msg = String
				.format(
						"%1$f %2$d %3$s <-- AM_%4$s::%7$s %8$s (%6$s AID %10$d) %5$s AM_%9$s",
						evrecv_time, reqrepID, this.get_name(), AbstractAgent
								.getEntityName(agentReply.getRequest()
										.getDst_resID()), EntityTypes
								.toString(agentType), AbstractAgent
								.getEntityName(request.getDst_agentID()),
						agentReply.isOk(), Tags.toString(tag), AbstractAgent
								.getEntityName(request.getDst_moveToresID()),
						request.getDst_AID());
		this.write(msg);
		return agentReply;
	}

	/**
	 * This methos is responsible for sending ACK/NACK through requesting
	 * agent's DFAgent Middleware
	 *
	 * @param ev
	 *            received event
	 * @param agentRequest
	 *            agent's request
	 * @param flag
	 *            boolean flag indicating success or failure of the request
	 *
	 * @see eduni.simjava.Sim_event
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 * @see net.p2pgrid.gap.agents.messages.AgentReply
	 */
	private void sendACKNACK(Sim_event ev, AgentRequest agentRequest,
			boolean flag) {
		AgentReply agentReply;

		int SIZE = 500;

		int replyToID = 0;
		replyToID = agentRequest.getDst_resID();
		agentReply = new AgentReply(ev.get_tag(), flag, agentRequest);
		super.send(this.output, GridSimTags.SCHEDULE_NOW, ev.get_tag() + 1,
				new IO_data(agentReply, SIZE, replyToID));
	}

	/**
	 * This methos is responsible for sending ACK through requesting agent's
	 * DFAgent Middleware
	 *
	 * @param ev
	 * @param agentRequest
	 *
	 * @see eduni.simjava.Sim_event
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 */
	protected void sendACK(Sim_event ev, AgentRequest agentRequest) {
		this.sendACKNACK(ev, agentRequest, true);
	}

	/**
	 * This methos is responsible for sending NACK through requesting agent's
	 * resource ID (its agent middleware)
	 *
	 * @param ev
	 * @param agentRequest
	 *
	 * @see eduni.simjava.Sim_event
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 */
	private void sendNACK(Sim_event ev, AgentRequest agentRequest) {
		this.sendACKNACK(ev, agentRequest, false);
	}

	/**
	 * This method is responsible for syncronously notifying to DF service which
	 * is located on DFAgent Platform, all agent's requests related to: -
	 * REGISTER - DEREGISTER - PAUSE - RESUME
	 *
	 * @param originalRequest
	 * @param dftag
	 *
	 * @see eduni.simjava.Sim_type_p
	 * @see eduni.simjava.Sim_event
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 * @see net.p2pgrid.gap.agents.messages.AgentReply
	 */
	private void notifyDFService(AgentRequest originalRequest, int dftag) {
		AgentRequest agentRequest;
		AgentReply agentReply;

		int SIZE = 500;

		agentRequest = new AgentRequest(originalRequest.getSrc_ID(),
				originalRequest.getSrc_resID(), originalRequest
						.getSrc_agentHistory(), originalRequest.getDst_resID(),
				this.get_id(), this.getEntityType(),
				this.getAgentSizeInBytes(), originalRequest.getDst_resID(),
				originalRequest.getDst_AID());

		int requestID = agentRequest.getRequestID();
		int reqrepID = agentRequest.getReqrepID();

		super.send(this.output, GridSimTags.SCHEDULE_NOW, dftag, new IO_data(
				agentRequest, SIZE, this.getAgentPlatform().get_id()));

		double evsend_time = GridSim.clock();
		String msg = String
				.format(
						"%1$f %2$d %3$s --> DFService::REQUEST %6$s (%7$s AID %8$d) %4$s AM_%5$s",
						evsend_time, reqrepID, this.get_name(), EntityTypes
								.toString(originalRequest.getDst_entityType()),
						AbstractAgent.getEntityName(originalRequest
								.getDst_resID()), Tags.toString(dftag),
						AbstractAgent.getEntityName(originalRequest
								.getDst_agentID()), agentRequest.getDst_AID());
		this.write(msg);

		Sim_type_p tag = new Sim_type_p(dftag + 1);
		Sim_event ev = new Sim_event();
		super.sim_get_next(tag, ev); // only look for this type of ack
		agentReply = AgentReply.get_data(ev);
		Assert.assertEquals(requestID, agentReply.getRequestID());
		Assert.assertEquals(dftag, agentReply.getRequestTAG());
		Assert.assertEquals(dftag + 1, ev.get_tag());
		double evrecv_time = GridSim.clock();
		msg = String
				.format(
						"%1$f %2$d %3$s <-- DFService::DFSERVICE_ACK %6$s (%7$s AID %8$d) %4$s AM_%5$s",
						evrecv_time, reqrepID, this.get_name(), EntityTypes
								.toString(originalRequest.getDst_entityType()),
						AbstractAgent.getEntityName(originalRequest
								.getDst_resID()), Tags.toString(dftag),
						AbstractAgent.getEntityName(originalRequest
								.getDst_agentID()), originalRequest
								.getDst_AID());
		this.write(msg);
	}
}

⌨️ 快捷键说明

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