📄 agentmanagementservice.java
字号:
/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A.
GNU Lesser General Public License
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation,
version 2.1 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*****************************************************************/
package jade.core.management;
import jade.core.HorizontalCommand;
import jade.core.Specifier;
import jade.core.VerticalCommand;
import jade.core.GenericCommand;
import jade.core.Service;
import jade.core.BaseService;
import jade.core.ServiceException;
import jade.core.Sink;
import jade.core.Filter;
import jade.core.Node;
import jade.core.Profile;
import jade.core.Agent;
import jade.core.AgentState;
import jade.core.AID;
import jade.core.ContainerID;
import jade.core.AgentContainer;
import jade.core.MainContainer;
import jade.core.ProfileException;
import jade.core.IMTPException;
import jade.core.NameClashException;
import jade.core.NotFoundException;
import jade.core.behaviours.Behaviour;
import jade.security.Credentials;
import jade.security.JADEPrincipal;
import jade.security.JADESecurityException;
import jade.util.Logger;
import jade.util.leap.Properties;
//#J2ME_EXCLUDE_BEGIN
import java.io.IOException;
import java.io.File;
//#J2ME_EXCLUDE_END
import java.util.Vector;
/**
The JADE service to manage the basic agent life cycle: creation,
destruction, suspension and resumption.
@author Giovanni Rimassa - FRAMeTech s.r.l.
*/
public class AgentManagementService extends BaseService {
public static final String NAME = AgentManagementSlice.NAME;
// Class properties names
public static final String CLASS_NAME = "name";
public static final String CLASS_CODE = "code";
public static final String CLASS_STATE = "state";
/**
The path where to search agent jar files
*/
public static final String AGENTS_PATH = "jade_core_management_AgentManagementService_agentspath";
private static final String[] OWNED_COMMANDS = new String[] {
AgentManagementSlice.REQUEST_CREATE,
AgentManagementSlice.REQUEST_KILL,
AgentManagementSlice.REQUEST_STATE_CHANGE,
AgentManagementSlice.INFORM_CREATED,
AgentManagementSlice.INFORM_KILLED,
AgentManagementSlice.INFORM_STATE_CHANGED,
AgentManagementSlice.KILL_CONTAINER,
AgentManagementSlice.ADD_TOOL,
AgentManagementSlice.REMOVE_TOOL
};
public void init(AgentContainer ac, Profile p) throws ProfileException {
super.init(ac, p);
myContainer = ac;
//#J2ME_EXCLUDE_BEGIN
// Initialize the code locator
agentsPath = p.getParameter(AGENTS_PATH, ".");
codeLocator = new CodeLocator();
//#J2ME_EXCLUDE_END
}
public String getName() {
return AgentManagementSlice.NAME;
}
public Class getHorizontalInterface() {
try {
return Class.forName(AgentManagementSlice.NAME + "Slice");
}
catch(ClassNotFoundException cnfe) {
return null;
}
}
public Service.Slice getLocalSlice() {
return localSlice;
}
public Filter getCommandFilter(boolean direction) {
return null;
}
public Sink getCommandSink(boolean side) {
if(side == Sink.COMMAND_SOURCE) {
return senderSink;
}
else {
return receiverSink;
}
}
public String[] getOwnedCommands() {
return OWNED_COMMANDS;
}
public void removeLocalAgent(AID target) {
myContainer.removeLocalAgent(target);
}
//#J2ME_EXCLUDE_BEGIN
public CodeLocator getCodeLocator() {
return codeLocator;
}
//#J2ME_EXCLUDE_END
// This inner class handles the messaging commands on the command
// issuer side, turning them into horizontal commands and
// forwarding them to remote slices when necessary.
private class CommandSourceSink implements Sink {
public void consume(VerticalCommand cmd) {
try {
String name = cmd.getName();
if(name.equals(AgentManagementSlice.REQUEST_CREATE)) {
handleRequestCreate(cmd);
}
else if(name.equals(AgentManagementSlice.REQUEST_KILL)) {
handleRequestKill(cmd);
}
else if(name.equals(AgentManagementSlice.REQUEST_STATE_CHANGE)) {
handleRequestStateChange(cmd);
}
else if(name.equals(AgentManagementSlice.INFORM_CREATED)) {
handleInformCreated(cmd);
}
else if(name.equals(AgentManagementSlice.INFORM_KILLED)) {
handleInformKilled(cmd);
}
else if(name.equals(AgentManagementSlice.INFORM_STATE_CHANGED)) {
handleInformStateChanged(cmd);
}
else if(name.equals(AgentManagementSlice.KILL_CONTAINER)) {
handleKillContainer(cmd);
}
else if(name.equals(AgentManagementSlice.ADD_TOOL)) {
handleAddTool(cmd);
}
else if(name.equals(AgentManagementSlice.REMOVE_TOOL)) {
handleRemoveTool(cmd);
}
}
catch (Throwable t) {
cmd.setReturnValue(t);
}
}
// Vertical command handler methods
private void handleRequestCreate(VerticalCommand cmd) throws IMTPException, JADESecurityException, NotFoundException, NameClashException, ServiceException {
Object[] params = cmd.getParams();
String name = (String)params[0];
String className = (String)params[1];
Object[]args = (Object[])params[2];
ContainerID cid = (ContainerID)params[3];
JADEPrincipal owner = (JADEPrincipal) params[4];
Credentials initialCredentials = (Credentials) params[5];
if(myLogger.isLoggable(Logger.CONFIG))
myLogger.log(Logger.CONFIG,"Source Sink consuming command REQUEST_CREATE. Name is "+name);
MainContainer impl = myContainer.getMain();
if(impl != null) {
AID agentID = new AID(name, AID.ISLOCALNAME);
AgentManagementSlice targetSlice = (AgentManagementSlice)getSlice(cid.getName());
if (targetSlice != null) {
try {
targetSlice.createAgent(agentID, className, args, owner, initialCredentials, AgentManagementSlice.CREATE_AND_START, cmd);
}
catch(IMTPException imtpe) {
// Try to get a newer slice and repeat...
targetSlice = (AgentManagementSlice)getFreshSlice(cid.getName());
targetSlice.createAgent(agentID, className, args, owner, initialCredentials, AgentManagementSlice.CREATE_AND_START, cmd);
}
}
else {
throw new NotFoundException("Container "+cid.getName()+" not found");
}
}
else {
// Do nothing for now, but could also route the command to the main slice, thus enabling e.g. AMS replication
}
}
private void handleRequestKill(VerticalCommand cmd) throws IMTPException, JADESecurityException, NotFoundException, ServiceException {
Object[] params = cmd.getParams();
AID agentID = (AID)params[0];
//log("Source Sink consuming command REQUEST_KILL. Name is "+agentID.getName(), 3);
if(myLogger.isLoggable(Logger.CONFIG))
myLogger.log(Logger.CONFIG,"Source Sink consuming command REQUEST_KILL. Name is "+agentID.getName());
MainContainer impl = myContainer.getMain();
if(impl != null) {
ContainerID cid = impl.getContainerID(agentID);
// Note that since getContainerID() succeeded, targetSlice can't be null
AgentManagementSlice targetSlice = (AgentManagementSlice)getSlice(cid.getName());
try {
targetSlice.killAgent(agentID, cmd);
}
catch(IMTPException imtpe) {
// Try to get a newer slice and repeat...
targetSlice = (AgentManagementSlice)getFreshSlice(cid.getName());
targetSlice.killAgent(agentID, cmd);
}
}
else {
// Do nothing for now, but could also route the command to the main slice, thus enabling e.g. AMS replication
}
}
private void handleRequestStateChange(VerticalCommand cmd) throws IMTPException, JADESecurityException, NotFoundException, ServiceException {
Object[] params = cmd.getParams();
AID agentID = (AID)params[0];
AgentState as = (AgentState)params[1];
int newState = Agent.AP_MIN;
if(as.equals(jade.domain.FIPAAgentManagement.AMSAgentDescription.SUSPENDED)) {
newState = Agent.AP_SUSPENDED;
}
else if(as.equals(jade.domain.FIPAAgentManagement.AMSAgentDescription.WAITING)) {
newState = Agent.AP_WAITING;
}
else if(as.equals(jade.domain.FIPAAgentManagement.AMSAgentDescription.ACTIVE)) {
newState = Agent.AP_ACTIVE;
}
MainContainer impl = myContainer.getMain();
if(impl != null) {
ContainerID cid = impl.getContainerID(agentID);
// Note that since getContainerID() succeeded, targetSlice can't be null
AgentManagementSlice targetSlice = (AgentManagementSlice)getSlice(cid.getName());
try {
targetSlice.changeAgentState(agentID, newState);
}
catch(IMTPException imtpe) {
// Try to get a newer slice and repeat...
targetSlice = (AgentManagementSlice)getFreshSlice(cid.getName());
targetSlice.changeAgentState(agentID, newState);
}
}
else {
// Do nothing for now, but could also route the command to the main slice, thus enabling e.g. AMS replication
}
}
private void handleInformCreated(VerticalCommand cmd) throws IMTPException, NotFoundException, NameClashException, JADESecurityException, ServiceException {
Object[] params = cmd.getParams();
AID target = (AID)params[0];
Agent instance = (Agent)params[1];
if(myLogger.isLoggable(Logger.CONFIG)) {
myLogger.log(Logger.CONFIG,"Source Sink consuming command INFORM_CREATED. Name is "+target.getName());
}
initAgent(target, instance, cmd);
}
private void handleInformKilled(VerticalCommand cmd) throws IMTPException, NotFoundException, ServiceException {
Object[] params = cmd.getParams();
AID target = (AID)params[0];
//log("Source Sink consuming command INFORM_KILLED. Name is "+target.getName(), 3);
if(myLogger.isLoggable(Logger.CONFIG))
myLogger.log(Logger.CONFIG,"Source Sink consuming command INFORM_KILLED. Name is "+target.getName());
// Remove CodeLocator entry.
//#J2ME_EXCLUDE_BEGIN
codeLocator.removeAgent(target);
//#J2ME_EXCLUDE_END
// Remove the dead agent from the LADT of the container
removeLocalAgent(target);
// Notify the main container through its slice
AgentManagementSlice mainSlice = (AgentManagementSlice)getSlice(MAIN_SLICE);
try {
mainSlice.deadAgent(target, cmd);
}
catch(IMTPException imtpe) {
// Try to get a newer slice and repeat...
mainSlice = (AgentManagementSlice)getFreshSlice(MAIN_SLICE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -