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

📄 abstractuser.java

📁 网格agent平台(GAP ,Grid AgentsPlatform)开发包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 ****************************************************************************************
 * Copyright ? Giovanni Novelli
 * All Rights Reserved.
 ****************************************************************************************
 *
 * Title:        AbstractGAP Simulator
 * Description:  AbstractGAP (Grid Agents Platform) Toolkit for Modeling and Simulation
 *               of Mobile Agents on Grids
 * License:      GPL - http://www.gnu.org/copyleft/gpl.html
 *
 * AbstractUser.java
 *
 * Created on 17 August 2006, 12.00 by Giovanni Novelli
 *
 ****************************************************************************************
 *
 * $Revision$
 * $Id$
 * $HeadURL$
 *
 *****************************************************************************************
 */

package net.sf.gap.users;

import eduni.simjava.Sim_event;
import eduni.simjava.Sim_system;
import eduni.simjava.Sim_type_p;
import gridsim.GridSim;
import gridsim.GridSimTags;
import gridsim.Gridlet;
import gridsim.IO_data;
import gridsim.datagrid.DataGridUser;
import gridsim.net.FIFOScheduler;
import gridsim.net.Link;
import gridsim.util.SimReport;
import junit.framework.Assert;
import net.sf.gap.AbstractGAP;
import net.sf.gap.agents.predicates.Predicate;
import net.sf.gap.constants.EntityTypes;
import net.sf.gap.constants.Tags;
import net.sf.gap.grid.AbstractVirtualOrganization;
import net.sf.gap.messages.impl.AgentReply;
import net.sf.gap.messages.impl.AgentRequest;
import net.sf.gap.messages.impl.GISReply;
import net.sf.gap.messages.impl.GISRequest;
import net.sf.gap.messages.impl.GridletReply;
import net.sf.gap.messages.impl.GridletRequest;
import net.sf.gap.messages.impl.PingReply;
import net.sf.gap.messages.impl.PingRequest;
import net.sf.gap.messages.impl.ServicesListReply;
import net.sf.gap.messages.impl.ServicesListRequest;

/**
 *
 * @author Giovanni Novelli
 */
public abstract class AbstractUser extends DataGridUser {
	private boolean traceFlag;

	private SimReport report_; // logs every events

	private FIFOScheduler userSched;

	private AbstractVirtualOrganization virtualOrganization;

	private int entityType;

	/**
	 * Creates a new instance of AbstractUser
	 */
	public AbstractUser(String name, Link link, int entityType,
			boolean trace_flag) throws Exception {
		super(name, link);
		this.setEntityType(entityType);
		this.setUserSched(new FIFOScheduler("sched_" + name));
	}

	protected GISReply requestToGIS() {
		int SIZE = 500;
		double evsend_time = 0;
		GISRequest request = new GISRequest(this.get_id(), this.get_id());
		int requestID = request.getRequestID();
		int reqrepID = request.getReqrepID();
		super.send(super.output, GridSimTags.SCHEDULE_NOW, Tags.GIS_REQUEST,
				new IO_data(request, SIZE, super.getEntityId("GISService")));
		evsend_time = GridSim.clock();
		String msg = String.format("%1$f %2$d %3$s --> GAP GIS_REQUEST",
				evsend_time, reqrepID, this.get_name());
		this.write(msg);

		Sim_event ev = new Sim_event();
		Predicate predicate = new Predicate(Tags.GIS_REPLY);
		super.sim_get_next(predicate, ev); // only look for this type of ack
		GISReply reply = GISReply.get_data(ev);
		Assert.assertEquals(requestID, reply.getRequestID());
		Assert.assertEquals(Tags.GIS_REQUEST, reply.getRequestTAG());
		Assert.assertEquals(Tags.GIS_REPLY, ev.get_tag());
		double evrecv_time = GridSim.clock();
		msg = String.format("%1$f %2$d %3$s <-- GAP GIS_REPLY\n", evrecv_time,
				reqrepID, this.get_name(), reply.getGIS());
		this.write(msg);
		return reply;
	}

	protected ServicesListReply requestServicesList() {
		int SIZE = 500;
		double evsend_time = 0;
		ServicesListRequest request = new ServicesListRequest(this.get_id(),
				this.get_id());
		int requestID = request.getRequestID();
		int reqrepID = request.getReqrepID();
		super.send(super.output, GridSimTags.SCHEDULE_NOW,
				Tags.AP_SERVICES_LIST_REQ, new IO_data(request, SIZE, this
						.getVirtualOrganization().getPlatform().get_id()));
		evsend_time = GridSim.clock();
		String msg = String.format(
				"%1$f %2$d %3$s --> %4$s SERVICES_LIST_REQUEST", evsend_time,
				reqrepID, this.get_name(), this.getVirtualOrganization()
						.getPlatform().get_name());
		this.write(msg);

		Sim_event ev = new Sim_event();
		Predicate predicate = new Predicate(Tags.AP_SERVICES_LIST_REP);
		super.sim_get_next(predicate, ev); // only look for this type of ack
		ServicesListReply reply = ServicesListReply.get_data(ev);
		Assert.assertEquals(requestID, reply.getRequestID());
		Assert.assertEquals(Tags.AP_SERVICES_LIST_REQ, reply.getRequestTAG());
		Assert.assertEquals(Tags.AP_SERVICES_LIST_REP, ev.get_tag());
		double evrecv_time = GridSim.clock();
		msg = String.format("%1$f %2$d %3$s <-- %4$s SERVICES_LIST_REPLY %5$s",
				evrecv_time, reqrepID, this.get_name(), this
						.getVirtualOrganization().getPlatform().get_name(),
				reply.getServicesList());
		this.write(msg);
		return reply;
	}

	protected PingReply requestPing(int src_id, int dst_id) {
		int SIZE = 500;
		double evsend_time = 0;
		PingRequest request = new PingRequest(this.get_id(), this.get_id(),
				src_id, dst_id);
		int requestID = request.getRequestID();
		int reqrepID = request.getReqrepID();
		super.send(super.output, GridSimTags.SCHEDULE_NOW, Tags.PING_REQ,
				new IO_data(request, SIZE, src_id));
		evsend_time = GridSim.clock();
		String msg = String.format(
				"%1$f %2$d %3$s --> AM_%4$s PING_REQUEST AM_%5$s", evsend_time,
				reqrepID, this.get_name(), super.getEntityName(src_id), super
						.getEntityName(dst_id));
		this.write(msg);

		Sim_event ev = new Sim_event();
		Predicate predicate = new Predicate(Tags.PING_REP);
		super.sim_get_next(predicate, ev); // only look for this type of ack
		PingReply reply = PingReply.get_data(ev);
		Assert.assertEquals(requestID, reply.getRequestID());
		Assert.assertEquals(Tags.PING_REQ, reply.getRequestTAG());
		Assert.assertEquals(Tags.PING_REP, ev.get_tag());
		double evrecv_time = GridSim.clock();
		msg = String.format("%1$f %2$d %3$s <-- AM_%4$s PING_REQUEST AM_%5$s",
				evrecv_time, reqrepID, this.get_name(), super
						.getEntityName(src_id), super.getEntityName(dst_id));
		this.write(msg);
		return reply;
	}

	public AgentReply submitAgent(int agentEntityType, int agentResourceID,
			int SIZE) {
		AgentRequest agentRequest = null;
		agentRequest = new AgentRequest(this.get_id(), this.get_id(), null,
				agentResourceID, -1, agentEntityType, SIZE, agentResourceID,
				EntityTypes.NOBODY);
		return this.runAgent(agentRequest);
	}

	private AgentReply runAgent(AgentRequest agentRequest) {
		return this.requestToAgent(agentRequest, Tags.AGENT_RUN_REQ);
	}

	public AgentReply pauseAgent(AgentRequest agentRequest) {
		return this.requestToAgent(agentRequest, Tags.AGENT_PAUSE_REQ);
	}

	public AgentReply resumeAgent(AgentRequest agentRequest) {
		return this.requestToAgent(agentRequest, Tags.AGENT_RESUME_REQ);
	}

	public AgentReply killAgent(AgentRequest agentRequest) {
		return this.requestToAgent(agentRequest, Tags.AGENT_KILL_REQ);
	}

	public AgentReply killWaitAgent(AgentRequest agentRequest) {
		return this.requestToAgent(agentRequest, Tags.AGENT_KILLAWAIT_REQ);
	}

	public AgentReply moveAgent(AgentRequest agentRequest, int moveToResourceID) {
		agentRequest.setDst_moveToresID(moveToResourceID);
		return this.requestToAgent(agentRequest, Tags.AGENT_MOVE_REQ);
	}

	public abstract void processOtherEvent(Sim_event ev);

	protected void processEvent(Sim_event ev) {
		int tag = ev.get_tag();
		switch (tag) {
		default:
			this.processOtherEvent(ev);
			break;
		}
	}

	protected void processEvents() {
		Sim_event ev = new Sim_event();

		super.sim_get_next(ev);
		this.processEvent(ev);
	}

	public GridletReply submitGridletToAgent(int dst_agentID, int dst_resID,
			Gridlet gridlet) {
		GridletRequest request = new GridletRequest(this.get_id(), this
				.get_id(), dst_agentID, dst_resID, gridlet);
		int requestID = request.getRequestID();
		int reqrepID = request.getReqrepID();
		super.send(super.output, GridSimTags.SCHEDULE_NOW,
				Tags.GRIDLET_SUBMIT_REQ, new IO_data(request, gridlet
						.getGridletFileSize(), request.getDst_agentID()));

		Sim_type_p ptag = new Sim_type_p(Tags.GRIDLET_SUBMIT_REP);
		Sim_event ev = new Sim_event();
		super.sim_get_next(ptag, ev); // only look for this type of ack
		GridletReply reply = GridletReply.get_data(ev);

		Assert.assertEquals(requestID, reply.getRequestID());
		Assert.assertEquals(Tags.GRIDLET_SUBMIT_REQ, reply.getRequestTAG());
		Assert.assertEquals(Tags.GRIDLET_SUBMIT_REP, ev.get_tag());
		double evrecv_time = GridSim.clock();
		if (reply.isOk()) {
			String msg = String
					.format(
							"%1$f %2$d %3$s <-- GOT GRIDLET_REPLY for Gridlet %4$d of Gridlet %5$d with result TRUE on AM_%6$s",
							evrecv_time, reqrepID, this.get_name(), reply
									.getReceivedGridlet().getGridletID(), reply
									.getRequest().getGridlet().getGridletID(),
							super.getEntityName(reply.getRequest()
									.getDst_resID()));
			this.write(msg);
		} else {
			String msg = String
					.format(
							"%1$f %2$d %3$s <-- GOT GRIDLET_REPLY for Gridlet %4$d of Gridlet %5$d with result FALSE on AM_%6$s",
							evrecv_time, reqrepID, this.get_name(), reply
									.getReceivedGridlet().getGridletID(), reply
									.getRequest().getGridlet().getGridletID(),
							super.getEntityName(reply.getRequest()
									.getDst_resID()));
			this.write(msg);
		}
		return reply;
	}

	public GridletReply askGridletStatusToAgent(int dst_agentID, int dst_resID,
			Gridlet gridlet) {
		GridletRequest request = new GridletRequest(this.get_id(), this
				.get_id(), dst_agentID, dst_resID, gridlet);
		int requestID = request.getRequestID();
		int reqrepID = request.getReqrepID();
		super.send(super.output, GridSimTags.SCHEDULE_NOW,
				Tags.GRIDLET_STATUS_REQ, new IO_data(request, gridlet
						.getGridletFileSize(), request.getDst_agentID()));

		Sim_type_p ptag = new Sim_type_p(Tags.GRIDLET_STATUS_REP);
		Sim_event ev = new Sim_event();
		super.sim_get_next(ptag, ev); // only look for this type of ack

⌨️ 快捷键说明

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