📄 gridagent.java
字号:
/*
****************************************************************************************
* 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
*
* GridAgent.java
*
* Created on 11 March 2007, 12.00 by Giovanni Novelli
*
****************************************************************************************
*
* $Revision$
* $Id$
* $HeadURL$
*
*****************************************************************************************
*/
package net.sf.gap.agents;
import junit.framework.Assert;
import net.sf.gap.AbstractGAP;
import net.sf.gap.agents.gridlets.scheduling.RRScheduler;
import net.sf.gap.constants.AgentStates;
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 net.sf.gap.messages.impl.GridletReply;
import net.sf.gap.messages.impl.GridletRequest;
import eduni.simjava.Sim_event;
import eduni.simjava.Sim_system;
import gridsim.GridSim;
import gridsim.GridSimTags;
import gridsim.Gridlet;
import gridsim.IO_data;
/**
* This abstract class is mainly responsible in simulating basic behaviour of
* agents in relation to:
* <p>
* <ul>
* <li> Gridlets submission and monitoring by agents <ul/>
*
* @author Giovanni Novelli
*/
public abstract class GridAgent extends DFAgent {
/**
* This field incorporates gridlets' scheduler
*/
private RRScheduler scheduler;
/**
* Creates a new instance of GridAgent class
*
* @param name
* agent name
* @param agentSizeInBytes
* agent size in bytes
* @param trace_flag
* trace flag for GridSim
* @throws Exception
*/
public GridAgent(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();
this.setScheduler(new RRScheduler(this, 16));
}
/**
* The core method that handles communications among entities
*/
@Override
public void body() {
// Wait for a little while for about 3 seconds.
// This to give a time for GridResource entities to
// registerAgent their services to GIS
// (GridInformationService) entity.
super.gridSimHold(AbstractGAP.getStartTime());
Sim_event ev = new Sim_event();
while (AbstractGAP.isRunning()) {
super.sim_wait_for(Sim_system.SIM_ANY, 1.0, ev);
this.processEvent(ev);
while (super.sim_waiting() > 0) {
this.processEvents();
}
this.doSomething();
}
// Dispose DFAgent
this.dispose();
// //////////////////////////////////////////////////////
// shut down I/O ports
this.shutdownUserEntity();
this.terminateIOEntities();
// don't forget to close the file
if (this.getReport_() != null) {
this.writeHistory();
this.getReport_().finalWrite();
}
}
protected void doSomething() {}
/**
* Processes single event related to: - gridlets management - replying to
* requests related to the presence of gridlets - other
*
* @param ev
* Sim_event to be processed
*/
@Override
public void processEvent(Sim_event ev) {
super.processEvent(ev);
switch (ev.get_tag()) {
// Gridlets Management
case GridSimTags.GRIDLET_PAUSE:
case GridSimTags.GRIDLET_CANCEL:
case GridSimTags.GRIDLET_STATUS:
case Tags.GRIDLET_SUBMIT_REQ:
case Tags.GRIDLET_SUBMIT_REP:
this.manageGridlets(ev);
break;
case GridSimTags.GRIDLET_RETURN:
this.manageGridletReturn(ev);
break;
// Replying to requests related to the presence of gridlets
case Tags.HASGRIDLETS_REQUEST:
case Tags.HASGRIDLETS_REPLY:
AgentRequest agentRequest = AgentRequest.get_data(ev);
if (this.getScheduler().hasGridlets()) {
this.sendHASGRIDLETSACK(ev, agentRequest);
} else {
this.sendHASGRIDLETSNACK(ev, agentRequest);
}
break;
// other
default:
this.processOtherEvent(ev);
break;
}
}
/**
* @TODO Gridlets management to be fixed!!!!!
* @TODO Comments to be completed
* @param ev
*/
@Override
protected void manageGridlets(Sim_event ev) {
Gridlet gridlet;
GridletRequest gridletRequest;
gridletRequest = GridletRequest.get_data(ev);
switch (ev.get_tag()) {
// Cancels a Gridlet submitted in the GridResource entity
case GridSimTags.GRIDLET_CANCEL:
gridlet = gridletRequest.getGridlet();
if (this.getAgentState() == AgentStates.RUNNING) {
Gridlet canceledGridlet = this.getScheduler().gridletCancel(
gridlet);
if (canceledGridlet != null) {
this.sendACK(ev, gridletRequest, gridlet);
} else {
this.sendNACK(ev, gridletRequest, gridlet);
}
} else {
this.sendNACK(ev, gridletRequest, gridlet);
}
break;
case GridSimTags.GRIDLET_MOVE:
Assert.fail();
break;
case GridSimTags.GRIDLET_MOVE_ACK:
Assert.fail();
break;
// Pauses a Gridlet submitted in the GridResource entity
case GridSimTags.GRIDLET_PAUSE:
gridlet = gridletRequest.getGridlet();
if (this.getAgentState() == AgentStates.RUNNING) {
boolean paused = this.getScheduler().gridletPause(gridlet);
if (paused) {
this.sendACK(ev, gridletRequest, gridlet);
} else {
this.sendNACK(ev, gridletRequest, gridlet);
}
} else {
this.sendNACK(ev, gridletRequest, gridlet);
}
break;
case GridSimTags.GRIDLET_PAUSE_ACK:
Assert.fail();
break;
case GridSimTags.GRIDLET_RESUME:
Assert.fail();
break;
case GridSimTags.GRIDLET_RESUME_ACK:
Assert.fail();
break;
case GridSimTags.GRIDLET_STATUS:
Assert.fail();
break;
case GridSimTags.GRIDLET_SUBMIT:
Assert.fail();
break;
case GridSimTags.GRIDLET_SUBMIT_ACK:
Assert.fail();
break;
case Tags.GRIDLET_STATUS_REQ:
Assert.fail();
/*
* gridlet = gridletRequest.getGridlet(); if (this.getAgentState() ==
* AgentStates.RUNNING) { gridlet.setUserID(this.get_id());
* super.gridletStatus(gridlet, this.getResourceID());
* this.sendSTATUSACK(ev, gridletRequest, gridlet); } else {
* this.sendSTATUSNACK(ev, gridletRequest, gridlet); }
*/
break;
case Tags.GRIDLET_SUBMIT_REQ:
// Assert.fail();
gridlet = gridletRequest.getGridlet();
if (this.getAgentState() == AgentStates.RUNNING) {
gridlet.setUserID(this.get_id());
this.getScheduler().getGridletsBag().addRequest(gridletRequest);
boolean submitted = this.getScheduler().enque(gridlet);
if (submitted) {
if (this.getScheduler().gridletSubmit()) {
/*
* // Receiving gridlet back receivedGridlet =
* super.gridletReceive();
* gridletRequest.setReceivedGridlet(receivedGridlet);
*/
this.sendACK(ev, gridletRequest, gridlet);
} else {
System.out
.println("Problems in submitting a gridlet from "
+ this.get_name()
+ " to "
+ this.getGridElement().get_name());
this.sendNACK(ev, gridletRequest, gridlet);
}
} else {
System.out
.println("Problems in queueing of last gridlet on agent "
+ this.get_name());
this.sendNACK(ev, gridletRequest, gridlet);
}
} else {
this.sendNACK(ev, gridletRequest, gridlet);
}
break;
case Tags.GRIDLET_SUBMIT_REP:
Assert.fail();
break;
default:
break;
}
}
protected void manageGridletReturn(Sim_event ev) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -