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

📄 dfagent.java

📁 网格agent平台(GAP ,Grid AgentsPlatform)开发包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 ****************************************************************************************
 * Copyright ? Giovanni Novelli
 * All Rights Reserved.
 ****************************************************************************************
 *
 * Title:        GAP Simulator
 * Description:  GAP (Grid Agents Platform) Toolkit for Modeling and Simulation
 *               of Mobile Agents on Grids
 * License:      GPL - http://www.gnu.org/copyleft/gpl.html
 *
 * DFAgent.java
 *
 * Created on 9 March 2007, 12.00 by Giovanni Novelli
 *
 ****************************************************************************************
 *
 * $Revision$
 * $Id$
 * $HeadURL$
 *
 *****************************************************************************************
 */
package net.sf.gap.agents;

import junit.framework.Assert;
import net.sf.gap.agents.history.AgentHistory;
import net.sf.gap.agents.history.AgentHistoryEntry;
import net.sf.gap.constants.AgentStates;
import net.sf.gap.constants.EntityTypes;
import net.sf.gap.constants.Tags;
import net.sf.gap.grid.components.AbstractGridElement;
import net.sf.gap.messages.impl.AgentReply;
import net.sf.gap.messages.impl.AgentRequest;
import eduni.simjava.Sim_event;
import eduni.simjava.Sim_type_p;
import gridsim.GridSim;
import gridsim.GridSimTags;
import gridsim.IO_data;

/**
 * This abstract class is mainly responsible in simulating basic behaviour of
 * agents in relation to:
 * <p>
 * <ul>
 * <li> GridAgent Platform (Directory Facilitator)
 *
 * <li> GridAgent LifeCycle over Grid's CEs/SEs
 *
 * <li> GridAgent's Mobility <ul/>
 *
 * @author Giovanni Novelli
 */
public abstract class DFAgent extends AbstractAgent {

	/**
	 * DFAgent constructor
	 *
	 * @param ge
	 *            AbstractGridElement associated to an agent
	 * @param name
	 *            DFAgent's name
	 * @param agentSizeInBytes
	 *            DFAgent's transfer size in bytes
	 * @param trace_flag
	 *            Flag used to enable(true)/disable(false) tracing
	 * @throws java.lang.Exception
	 *
	 * @see net.sf.gap.grid.components.AbstractGridElement
	 */
	public DFAgent(AbstractGridElement ge, String name, int agentSizeInBytes,
			boolean trace_flag) throws Exception {
		super(ge, name, agentSizeInBytes, trace_flag);
	}

	/**
	 * DFAgent's Initialization
	 *
	 * @throws java.lang.Exception
	 */
	@Override
	public void initialize() throws Exception {
		super.initialize();
	}

	/**
	 * @return true if there are gridlets carried by agent, false otherwise
	 */
	abstract protected boolean hasGridlets();

	/**
	 * @param agentRequest
	 */
	abstract protected void onWaitingGridlets(AgentRequest agentRequest);

	/**
	 * @param ev
	 */
	abstract protected void manageGridlets(Sim_event ev);

	/**
	 * @param ev
	 *            Sim_event to be processed by any child class
	 */
	@Override
	abstract public void processOtherEvent(Sim_event ev);

	/**
	 * Updates GridAgent's LifeCycle
	 * <p>
	 * It receives requests of the following types, checking if it is allowed
	 * according to permitted transition table:
	 * <p>
	 * <ul>
	 * <li> agent's activation ZOMBIE-->RUNNING
	 * <li> agent's killing RUNNING-->ZOMBIE
	 * <li> agent's pausing RUNNING-->PAUSED
	 * <li> agent's resuming PAUSED-->RUNNING <ul/>
	 * <p>
	 * Notifies the Directory Facilitator on the GridAgent Platform about state
	 * transition
	 * <p>
	 * Sends an ACK or a NACK about the success or failure of the request to the
	 * originator of it.
	 *
	 *
	 * @param ev
	 *            Sim_event containing the request
	 * @see eduni.simjava.Sim_event
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 */
	@Override
	protected void manageLifecycle(Sim_event ev) {
		AgentRequest agentRequest = AgentRequest.get_data(ev);

		// If this agent has gridlets and has received a KILL request
		// then send a NACK and do nothing
		if (this.hasGridlets() && ev.get_tag() == Tags.AGENT_KILL_REQ) {
			this.sendNACK(ev, agentRequest);

			// If this agent has gridlets and has received a delayed
			// KILL request then
		} else if (this.hasGridlets()
				&& ev.get_tag() == Tags.AGENT_KILLAWAIT_REQ) {
			// if agent's state is congruent with the request
			if (this.isGoodState(ev)) {
				// update agent's internals
				this.update(ev, agentRequest);
				// notify DF about agent's state change
				this.notifyDF(ev, agentRequest);
				// wait until the end of current gridlets
				// Waiting current gridlets is done at last because agent
				// won't be available anyway as delayed KILL keeps same
				// semanthics of KILL even if with greater latency
				this.onWaitingGridlets(agentRequest);
			}
			// otherwise, if this agent has NOT gridlets then
		} else {
			// if agent's state is congruent with the request
			if (this.isGoodState(ev)) {
				// update agent's internals
				this.update(ev, agentRequest);
				// notify DF about agent's state change
				this.notifyDF(ev, agentRequest);
				// send ACK
				this.sendACK(ev, agentRequest);
				// otherwise, if agent's state is NOT congruent with the
				// request
			} else {
				// send NACK
				this.sendNACK(ev, agentRequest);
			}
		}
	}

	/**
	 * Moves an agent from a Grid Element to another Grid Element
	 * <p>
	 * Notifies the Directory Facilitator on the GridAgent Platform about
	 * migration
	 * <p>
	 * Keeps AID on new Grid Element
	 * <p>
	 * Manages migration failures
	 * <p>
	 * Sends an ACK or a NACK about the success or failure of the request
	 *
	 * @param ev
	 *            Sim_event containing the request
	 *
	 * @see eduni.simjava.Sim_event
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 * @see net.p2pgrid.gap.agents.messages.AgentReply
	 * @see net.sf.gap.agents.history.AgentHistory
	 * @see net.sf.gap.agents.history.AgentHistoryEntry
	 */
	@Override
	protected void manageMigration(Sim_event ev) {
		// Extracts AgentRequest from ev
		AgentRequest agentRequest = AgentRequest.get_data(ev);
		// if agent's state is congruent with the request
		if (this.isGoodState(ev)) {
			// Stores in local variable SIZE the transfer size needed by
			// migration
			int SIZE = agentRequest.getDst_agentSize();
			// Stores in local variable oldaid agent's AID
			int oldaid = this.getAID();
			// Submits an agent to the destination Grid Element; such agent has:
			// - same entity type
			// - same AID
			// - current agent's history
			// - same SIZE
			// Submission could be successful or not
			AgentReply agentReply = this.submitAgent(agentRequest
					.getDst_entityType(), agentRequest.getDst_moveToresID(),
					SIZE, this.getAID(), this.getAgentHistory());
			// If agent's submission is successful then
			if (agentReply.isOk()) {
				// Putting agent in local zombies list
				// 1. Checks if agent is NOT in the zombies list of its original
				// Grid Element
				Assert.assertEquals(this.gridElement.getLocalDirectory()
						.getZombieAgents().contains(this.get_id()), false);
				/*
				 * @TODO Fix it This is a TRICK Missing message exchange with
				 * AbstractGridElement
				 */
				// 2. Updates local zombies list adding agent to zombies
				this.gridElement.getLocalDirectory().update(agentRequest,
						Tags.AGENT_MOVE_REQ);
				// 3. Checks if agent is in the zombies list of its original
				// Grid Element
				Assert.assertEquals(this.gridElement.getLocalDirectory()
						.getZombieAgents().contains(this.get_id()), true);

				// Notifies Directory Facilitator that original agent must be
				// DEREGISTERED
				this.notifyDFService(agentRequest, Tags.DF_DEREGISTER_REQ);
				// Sets agent's state to ZOMBIE
				this.setAgentState(AgentStates.ZOMBIE);
				// Sets agent's entity type to AGENT_ZOMBIE
				this.setEntityType(EntityTypes.AGENT_ZOMBIE);
				// Resets agent's transfer SIZE to 0 bytes
				this.setAgentSizeInBytes(0);
				// Detach AID from zombied agent
				this.detachAID();
				// Creates an empty agent's history entry
				AgentHistoryEntry agentHistoryEntry = new AgentHistoryEntry(
						AbstractAgent.clock(), this.get_name(), this
								.getAgentState(), this.get_id(), this.getAID(),
						this.getEntityType(), this.getResourceID());
				// Attach such history entry to zombied agent's history
				this.getAgentHistory().add(agentHistoryEntry);
				// Stores new (should be the same of the original) AID in local
				// variable newaid
				int newaid = agentReply.getRequest().getDst_agentID();
				// Checks that AID hasn't changed with migration
				Assert.assertEquals(oldaid == newaid, true);
				// Puts new AID in original request
				agentRequest.setDst_agentID(newaid);
				// Puts new resource ID in original request
				agentRequest.setDst_resID(agentRequest.getDst_moveToresID());
				// Sends ACK and updated request to its originator
				this.sendACK(ev, agentRequest);
				// otherwise, if agent's submission was NOT successful
			} else {
				// Sends NACK and original request to its originator
				this.sendNACK(ev, agentRequest);
			}
			// otherwise, if agent's state is congruent with the request
		} else {
			// Sends NACK and original request to its originator
			this.sendNACK(ev, agentRequest);
		}
	}

	/**
	 * This method allows an agent to submit a new agent to a Grid Element
	 *
	 * @param agentEntityType
	 *            new agent's entity type
	 * @param agentResourceID
	 *            destination Grid Element's resource ID
	 * @param SIZE
	 *            new agent's transfer size in bytes
	 * @return an instance of class AgentReply
	 *
	 * @see net.p2pgrid.gap.agents.messages.AgentRequest
	 * @see net.p2pgrid.gap.agents.messages.AgentReply
	 */
	protected AgentReply submitAgent(int agentEntityType, int agentResourceID,
			int SIZE) {
		AgentRequest agentRequest = null;
		// Creates a new instance of AgentRequest where:
		// - actor ID is the ID of this agent
		// - resource ID is the resource ID of this agent

⌨️ 快捷键说明

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