📄 agentcontainerimpl.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;
//#APIDOC_EXCLUDE_FILE
import jade.util.leap.Iterator;
import jade.util.leap.List;
import jade.util.leap.ArrayList;
import jade.util.leap.Properties;
import jade.util.Logger;
import jade.lang.acl.ACLMessage;
import jade.core.behaviours.Behaviour;
import jade.core.messaging.GenericMessage;
import jade.core.management.AgentManagementSlice;
//#MIDP_EXCLUDE_BEGIN
import jade.domain.AMSEventQueueFeeder;
//#MIDP_EXCLUDE_END
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.InternalError;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.mtp.MTPDescriptor;
import jade.mtp.TransportAddress;
import jade.security.JADESecurityException;
import jade.security.CredentialsHelper;
import jade.security.JADEPrincipal;
import jade.security.Credentials;
/**
This class is a concrete implementation of the JADE agent
container, providing runtime support to JADE agents.
This class cannot be instantiated from applications. Instead, the
<code>Runtime.createAgentContainer(Profile p)</code> method must be called.
@see Runtime#createAgentContainer(Profile)
@author Giovanni Rimassa - Universita' di Parma
@author Jerome Picault - Motorola Labs
@author Giovanni Caire - TILAB
@version $Date: 2008-05-05 13:12:54 +0200 (lun, 05 mag 2008) $ $Revision: 6030 $
*/
class AgentContainerImpl implements AgentContainer, AgentToolkit {
public static final String ENABLE_MONITOR = "jade_core_AgentContainerImpl_enablemonitor";
public static final String MONITOR_AGENT_NAME = "monitor-%C";
public static final String MONITOR_AGENT_CLASS = "jade.core.ContainerMonitorAgent";
private Logger myLogger = Logger.getMyLogger(this.getClass().getName());
// Local agents, indexed by agent name
protected LADT localAgents;
// The Profile defining the configuration of this Container
protected Profile myProfile;
// The Command Processor through which all the vertical commands in this container will pass
protected CommandProcessor myCommandProcessor;
//#MIDP_EXCLUDE_BEGIN
// The agent platform this container belongs to
protected MainContainerImpl myMainContainer; // FIXME: It should go away
//#MIDP_EXCLUDE_END
// The IMTP manager, used to access IMTP-dependent functionalities
protected IMTPManager myIMTPManager;
// The platform Service Manager
private ServiceManager myServiceManager;
// The platform Service Finder
private ServiceFinder myServiceFinder;
// The Object managing Thread resources in this container
private ResourceManager myResourceManager;
protected ContainerID myID;
protected NodeDescriptor myNodeDescriptor;
// These are only used at bootstrap-time to initialize the local
// NodeDescriptor. Further modifications take no effect
protected JADEPrincipal ownerPrincipal;
protected Credentials ownerCredentials;
private AID theAMS;
private AID theDefaultDF;
// This is used to avoid killing this container just after its creation and
// possibly before the monitoring PING is received. In that case in fact the Main Container does
// not deregister it.
private long creationTime = -1;
// Default constructor
AgentContainerImpl() {
}
// Package scoped constructor, so that only the Runtime
// class can actually create a new Agent Container.
AgentContainerImpl(Profile p) {
myProfile = p;
localAgents = new LADT(16);
}
//#MIDP_EXCLUDE_BEGIN
/////////////////////////////////////////////////
// Support for the in-process interface section
/////////////////////////////////////////////////
jade.wrapper.AgentContainer getContainerController() {
return getContainerController(myNodeDescriptor.getOwnerPrincipal(), myNodeDescriptor.getOwnerCredentials());
}
public jade.wrapper.AgentContainer getContainerController(JADEPrincipal principal, Credentials credentials) {
return new jade.wrapper.AgentContainer(getContainerProxy(principal, credentials), this, getPlatformID());
}
/**
Return a proxy that allows making requests to the local container
as if they were received from the main. This allows dealing
uniformly with local and remote requests.
Local requests occurs at bootstrap and following calls to the
in-process interface.
*/
private jade.wrapper.ContainerProxy getContainerProxy(final JADEPrincipal principal, final Credentials credentials) {
return new jade.wrapper.ContainerProxy() {
GenericCommand dummyCmd = new GenericCommand(null, null, null);
{
dummyCmd.setPrincipal(principal);
dummyCmd.setCredentials(credentials);
}
public void createAgent(AID id, String className, Object[] args) throws Throwable {
// Do as if it was a remote call from the main to allows
// security checks to take place if needed
AgentManagementSlice target = (AgentManagementSlice) getProxyToLocalSlice(AgentManagementSlice.NAME);
target.createAgent(id, className, args, principal, null, AgentManagementSlice.CREATE_ONLY, dummyCmd);
}
public void killContainer() throws Throwable {
// Do as if it was a remote call from the main to allows
// security checks to take place if needed
AgentManagementSlice target = (AgentManagementSlice) getProxyToLocalSlice(AgentManagementSlice.NAME);
// FIXME: set Principal and Credentials
target.exitContainer();
}
public MTPDescriptor installMTP(String address, String className) throws Throwable {
// Do as if it was a remote call from the main to allows
// security checks to take place if needed
jade.core.messaging.MessagingSlice target = (jade.core.messaging.MessagingSlice) getProxyToLocalSlice(jade.core.messaging.MessagingSlice.NAME);
// FIXME: set Principal and Credentials
return target.installMTP(address, className);
}
public void uninstallMTP(String address) throws Throwable {
// Do as if it was a remote call from the main to allows
// security checks to take place if needed
jade.core.messaging.MessagingSlice target = (jade.core.messaging.MessagingSlice) getProxyToLocalSlice(jade.core.messaging.MessagingSlice.NAME);
// FIXME: set Principal and Credentials
target.uninstallMTP(address);
}
public void suspendAgent(AID id) throws Throwable {
// Do as if it was a remote call from the main to allows
// security checks to take place if needed
jade.core.management.AgentManagementSlice target = (jade.core.management.AgentManagementSlice) getProxyToLocalSlice(jade.core.management.AgentManagementSlice.NAME);
// FIXME: set Principal and Credentials
target.changeAgentState(id, Agent.AP_SUSPENDED);
}
public void activateAgent(AID id) throws Throwable {
// Do as if it was a remote call from the main to allows
// security checks to take place if needed
jade.core.management.AgentManagementSlice target = (jade.core.management.AgentManagementSlice) getProxyToLocalSlice(jade.core.management.AgentManagementSlice.NAME);
// FIXME: set Principal and Credentials
target.changeAgentState(id, Agent.AP_ACTIVE);
}
public void killAgent(AID id) throws Throwable {
// Do as if it was a remote call from the main to allows
// security checks to take place if needed
jade.core.management.AgentManagementSlice target = (jade.core.management.AgentManagementSlice) getProxyToLocalSlice(jade.core.management.AgentManagementSlice.NAME);
target.killAgent(id, dummyCmd);
}
public void moveAgent(AID id, Location where) throws Throwable {
// Do as if it was a remote call from the main to allows
// security checks to take place if needed
jade.core.mobility.AgentMobilitySlice target = (jade.core.mobility.AgentMobilitySlice) getProxyToLocalSlice(jade.core.mobility.AgentMobilitySlice.NAME);
// FIXME: set Principal and Credentials
target.moveAgent(id, where);
}
public void cloneAgent(AID id, Location where, String newName) throws Throwable {
// Do as if it was a remote call from the main to allows
// security checks to take place if needed
jade.core.mobility.AgentMobilitySlice target = (jade.core.mobility.AgentMobilitySlice) getProxyToLocalSlice(jade.core.mobility.AgentMobilitySlice.NAME);
// FIXME: set Principal and Credentials
target.copyAgent(id, where, newName);
}
private SliceProxy getProxyToLocalSlice(String serviceName) throws Throwable {
Service svc = myServiceFinder.findService(serviceName);
return (SliceProxy) myIMTPManager.createSliceProxy(serviceName, svc.getHorizontalInterface(), myIMTPManager.getLocalNode());
}
};
}
////////////////////////////////////////////////////////
// END of support for the in-process interface section
////////////////////////////////////////////////////////
//#MIDP_EXCLUDE_END
/**
Issue an INFORM_CREATED vertical command.
Note that the Principal, if any, is that of the
owner of the newly born agent, while the Credentials, if any, are a set
additional initial credentials to be attached to the newly born agent.
The SecurityService, if active,
will create a new Principal for the newly born agent and will initialize its
credentials as the union of the initial credential and the ownership
certificate.
*/
public void initAgent(AID agentID, Agent instance,
JADEPrincipal ownerPrincipal, Credentials initialCredentials)
throws NameClashException, IMTPException, NotFoundException, JADESecurityException {
// Replaces wildcards
agentID.setName(JADEManagementOntology.adjustAgentName(agentID.getName(), new String[] {myID.getName()}));
// Setting the AID and toolkit here is redundant, but
// allows services to retrieve their agent helper correctly
// when processing the INFORM_CREATED command.
instance.setAID(agentID);
instance.setToolkit(this);
GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.INFORM_CREATED, jade.core.management.AgentManagementSlice.NAME, null);
cmd.addParam(agentID);
cmd.addParam(instance);
cmd.addParam( ownerPrincipal );
cmd.addParam( initialCredentials );
Object ret = myCommandProcessor.processOutgoing(cmd);
if (ret != null) {
if (ret instanceof NameClashException) {
throw ((NameClashException) ret);
}
else if (ret instanceof IMTPException) {
throw ((IMTPException) ret);
}
else if (ret instanceof NotFoundException) {
throw ((NotFoundException) ret);
}
else if (ret instanceof JADESecurityException) {
throw ((JADESecurityException) ret);
}
else if (ret instanceof Throwable) {
((Throwable) ret).printStackTrace();
}
}
}
public NodeDescriptor getNodeDescriptor() {
return myNodeDescriptor;
}
protected void init() throws IMTPException, ProfileException {
myCommandProcessor = myProfile.getCommandProcessor();
//#J2ME_EXCLUDE_BEGIN
// main-host option takes precendence over dectect-main
if (myProfile.getBooleanProperty(Profile.DETECT_MAIN, false) && !myProfile.getBooleanProperty(Profile.MAIN_HOST, false)) {
// FIXME check correctness of cast to ProfileImpl
MainDetectionManager.detect((ProfileImpl)myProfile);
}
//#J2ME_EXCLUDE_END
try {
// Create and initialize the IMTPManager
myIMTPManager = myProfile.getIMTPManager();
myIMTPManager.initialize(myProfile);
//#J2ME_EXCLUDE_BEGIN
if (myProfile.getBooleanProperty(Profile.DETECT_MAIN, true) && ((ProfileImpl)myProfile).isMain()) {
try {
MainDetectionManager.export((ProfileImpl)myProfile, myIMTPManager);
}
catch (ProfileException pe) {
if ("true".equalsIgnoreCase(myProfile.getBootProperties().getProperty(Profile.DETECT_MAIN))) {
// The detect-main option was explicitly set to true in the boot properties --> let the exception through
throw pe;
}
else {
// The detect-main option was NOT explicitly specified in the boot properties --> just print a warning
myLogger.log(Logger.WARNING, "Automatic main-detection mechanism initialization failed ("+pe.getMessage()+"). Mechanism disabled!");
}
}
}
//#J2ME_EXCLUDE_END
}
finally {
if (myProfile.getBooleanProperty(Profile.DUMP_OPTIONS, false)) {
myLogger.log(Logger.INFO, "Startup options dump:\n"+myProfile);
}
}
// Get the Service Manager and the Service Finder
myServiceManager = myProfile.getServiceManager();
myServiceFinder = myProfile.getServiceFinder();
// Attach CommandProcessor and ServiceManager to the local node
BaseNode localNode = (BaseNode) myIMTPManager.getLocalNode();
localNode.setCommandProcessor(myCommandProcessor);
localNode.setServiceManager(myServiceManager);
//#MIDP_EXCLUDE_BEGIN
myMainContainer = myProfile.getMain();
//#MIDP_EXCLUDE_END
// This string will be used to build the GUID for every agent on
// this platform.
AID.setPlatformID(myServiceManager.getPlatformName());
// Build the Agent IDs for the AMS and for the Default DF.
theAMS = new AID(FIPANames.AMS, AID.ISLOCALNAME);
theDefaultDF = new AID(FIPANames.DEFAULT_DF, AID.ISLOCALNAME);
// Create the ResourceManager
myResourceManager = myProfile.getResourceManager();
// Initialize the Container ID
TransportAddress addr = (TransportAddress) myIMTPManager.getLocalAddresses().get(0);
myID = new ContainerID(myProfile.getParameter(Profile.CONTAINER_NAME, PlatformManager.NO_NAME), addr);
myNodeDescriptor = new NodeDescriptor(myID, myIMTPManager.getLocalNode());
}
/**
Add the node to the platform with the basic services
*/
protected void startNode() throws IMTPException, ProfileException, ServiceException, JADESecurityException, NotFoundException {
// Initialize all services (without activating them)
List services = new ArrayList();
ServiceDescriptor dsc = startService("jade.core.management.AgentManagementService", false);
dsc.setMandatory(true);
services.add(dsc);
//#MIDP_EXCLUDE_BEGIN
dsc = startService("jade.core.messaging.MessagingService", false);
//#MIDP_EXCLUDE_END
/*#MIDP_INCLUDE_BEGIN
dsc = startService("jade.core.messaging.LightMessagingService", false);
#MIDP_INCLUDE_END*/
dsc.setMandatory(true);
services.add(dsc);
List l = myProfile.getSpecifiers(Profile.SERVICES);
myProfile.setSpecifiers(Profile.SERVICES, l); // Avoid parsing services twice
Iterator serviceSpecifiers = l.iterator();
while(serviceSpecifiers.hasNext()) {
Specifier s = (Specifier) serviceSpecifiers.next();
String serviceClass = s.getClassName();
boolean isMandatory = false;
if ( s.getArgs() != null ) {
isMandatory = CaseInsensitiveString.equalsIgnoreCase( (String) s.getArgs()[0], "true" );
}
try {
dsc = startService(serviceClass, false);
dsc.setMandatory(isMandatory);
services.add(dsc);
}
catch (ServiceException se) {
if (isMandatory) {
throw se;
}
else {
myLogger.log(Logger.WARNING,"Exception initializing service " + serviceClass + " : " + se.toString());
se.printStackTrace();
}
}
}
// Register with the platform
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -