📄 maincontainerimpl.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
//#MIDP_EXCLUDE_FILE
import java.util.Vector;
import jade.util.leap.Iterator;
import jade.util.leap.List;
import jade.util.leap.ArrayList;
import jade.util.leap.LinkedList;
import jade.util.leap.Map;
import jade.util.leap.HashMap;
import jade.core.behaviours.Behaviour;
import jade.core.event.PlatformEvent;
import jade.core.event.MTPEvent;
import jade.domain.AMSEventQueueFeeder;
import jade.domain.FIPANames;
import jade.domain.ams;
import jade.domain.df;
import jade.domain.FIPAAgentManagement.AMSAgentDescription;
import jade.domain.FIPAAgentManagement.AlreadyRegistered;
import jade.domain.FIPAAgentManagement.NotRegistered;
import jade.mtp.MTPException;
import jade.mtp.MTPDescriptor;
import jade.security.JADESecurityException;
import jade.security.JADEPrincipal;
import jade.security.Credentials;
import jade.util.InputQueue;
import jade.util.Logger;
/**
This class is a concrete implementation of the JADE main container,
providing runtime support to JADE agents, and the special, front
end container where the AMS and the Default DF can run.
This class cannot be instantiated from applications. Instead, the
<code>Runtime.createMainContainer(Profile p)</code> method must be
called.
@see Runtime#createMainContainer(Profile p);
@author Giovanni Rimassa - Universita' di Parma
@version $Date: 2007-06-11 11:35:39 +0200 (lun, 11 giu 2007) $ $Revision: 5964 $
*/
public class MainContainerImpl implements MainContainer, AgentManager {
/**
* Profile option that specifies the classes of agents that must be restarted in
* case of fault of the master Main Contaier
*/
private static final String REPLICATED_AGENTS = "jade_core_MainContainerImpl_replicatedagents";
// The two mandatory system agents.
private ams theAMS;
private df defaultDF;
private Map replicatedAgents = new HashMap();
private Vector replicatedAgentClasses;
private ContainerID localContainerID;
private PlatformManagerImpl myPlatformManager;
private CommandProcessor myCommandProcessor;
private List platformListeners = new LinkedList();
private List platformAddresses = new LinkedList();
private List agentTools = new LinkedList();
private ContainerTable containers = new ContainerTable();
private GADT platformAgents = new GADT();
private Logger myLogger = Logger.getMyLogger(getClass().getName());
public MainContainerImpl(Profile p, PlatformManagerImpl pm) throws ProfileException {
myCommandProcessor = p.getCommandProcessor();
replicatedAgentClasses = Specifier.parseList(p.getParameter(REPLICATED_AGENTS, ""), ';');
myPlatformManager = pm;
// The AMS must be instantiated before the installation of kernel services to
// avoid NullPointerException in case a service provides an AMS-behaviour
theAMS = new ams(this);
defaultDF = new df();
}
public PlatformManager getPlatformManager() {
return myPlatformManager;
}
void addLocalContainer(ContainerID cid) {
containers.addContainer(cid);
localContainerID = cid;
}
void removeLocalContainer(ContainerID cid) {
// Stop the Default DF
Agent systemAgent = defaultDF;
systemAgent.doDelete();
systemAgent.join();
systemAgent.resetToolkit();
// Stop the AMS
systemAgent = theAMS;
systemAgent.doDelete();
systemAgent.join();
systemAgent.resetToolkit();
removeListener(theAMS.getQueueFeeder());
}
void addRemoteContainer(ContainerID cid) {
containers.addContainer(cid);
// Notify listeners
fireAddedContainer(cid);
}
void removeRemoteContainer(ContainerID cid) {
// Eradicate all MTPs installed on the dead container (this
// requires that the container is still present in the
// Container Table)
removeAllMTPs(cid);
containers.removeContainer(cid);
// Eradicate all the entries for agents living on the dead container
removeAllAgents(cid);
// Notify listeners
fireRemovedContainer(cid);
}
void initSystemAgents(AgentContainer localContainer, boolean restarting) throws IMTPException, NotFoundException, JADESecurityException {
ContainerID cid = localContainer.getID();
NodeDescriptor dsc = getDescriptor(cid.getName());
// The owner of both the AMS and the DF is the owner of the main container.
JADEPrincipal cp = dsc.getOwnerPrincipal();
try {
((Agent) theAMS).setRestarting(restarting);
AID amsId = localContainer.getAMS();
// The AMS has NO initial credentials
localContainer.initAgent(amsId, theAMS, cp, null);
}
catch(Exception e) {
throw new IMTPException("Exception during AMS initialization", e);
}
try {
((Agent) defaultDF).setRestarting(restarting);
AID dfId = localContainer.getDefaultDF();
// The DF has NO initial credentials
localContainer.initAgent(dfId, defaultDF, cp, null);
}
catch(Exception e) {
throw new IMTPException("Exception during Default DF initialization", e);
}
}
// GC-MODIFY-18022007-START
// Start the AMS and the Default DF
void startSystemAgents(AgentContainer localContainer, AMSEventQueueFeeder feeder) throws IMTPException, NotFoundException, JADESecurityException {
if (feeder != null) {
theAMS.setQueueFeeder(feeder);
}
else {
theAMS.resetEvents(true);
}
AID amsId = localContainer.getAMS();
localContainer.powerUpLocalAgent(amsId);
theAMS.waitUntilStarted();
AID dfId = localContainer.getDefaultDF();
localContainer.powerUpLocalAgent(dfId);
defaultDF.waitUntilStarted();
}
// GC-MODIFY-18022007-END
void restartReplicatedAgents(AgentContainer localContainer) throws IMTPException, NotFoundException, JADESecurityException {
ContainerID cid = localContainer.getID();
NodeDescriptor dsc = getDescriptor(cid.getName());
// The owner of the replicated agents is the owner of the main container.
JADEPrincipal cp = dsc.getOwnerPrincipal();
Iterator it = replicatedAgents.keySet().iterator();
while (it.hasNext()) {
AID aid = (AID) it.next();
try {
String className = (String) replicatedAgents.get(aid);
if (className != null) {
myLogger.log(Logger.INFO, "Restarting replicated agent "+aid.getName());
Agent agent = (Agent)Class.forName(className).newInstance();
agent.setRestarting(true);
localContainer.initAgent(aid, agent, cp, null);
localContainer.powerUpLocalAgent(aid);
}
else {
myLogger.log(Logger.WARNING, "Missing class-name for replicated agent "+aid.getName()+". Cannot restart it");
}
}
catch(NameClashException nce) {
// This agent was not in the crashed master main, but in another backup main --> It is still alive
// --> Just do nothing
}
catch(Exception e) {
myLogger.log(Logger.SEVERE, "Exception restarting replicated agent "+aid.getName(), e);
}
}
}
/**
* Store the AID of a newly born agent if it has started on a remote Main Container
* and its class is one of those to be replicated
*/
private void checkReplication(AID aid, ContainerID cid) {
try {
String className = aid.getAllUserDefinedSlot().getProperty(AID.AGENT_CLASSNAME);
if (replicatedAgentClasses.contains(className) && (!cid.equals(localContainerID)) && getContainerNode(cid).getNode().hasPlatformManager()) {
replicatedAgents.put(aid, className);
}
}
catch (NotFoundException nfe) {
// Should never happen
nfe.printStackTrace();
}
}
void installAMSBehaviour(Behaviour b) {
theAMS.addBehaviour(b);
}
void uninstallAMSBehaviour(Behaviour b) {
theAMS.removeBehaviour(b);
}
/**
Notify the platform that an agent has just born on a container
*/
public void bornAgent(AID aid, ContainerID cid, JADEPrincipal principal, String ownership, boolean forceReplacement) throws NameClashException, NotFoundException {
AgentDescriptor ad = new AgentDescriptor(AgentDescriptor.NATIVE_AGENT);
ad.setContainerID(cid);
ad.setPrincipal(principal);
// Registration to the With Pages service
AMSAgentDescription amsd = new AMSAgentDescription();
amsd.setName(aid);
amsd.setOwnership(ownership);
amsd.setState(AMSAgentDescription.ACTIVE);
ad.setDescription(amsd);
AgentDescriptor old = platformAgents.put(aid, ad);
if(old != null) {
// There's already an agent with name 'name'
if (old.isNative()) {
// The old agent lives in the platform. Restor it and throw a NameClashException unless
// either we are requested to replace it or it is LATENT
if(forceReplacement) {
myLogger.log(Logger.WARNING, "Replacing dead agent "+aid.getName()+"...");
fireDeadAgent(old.getContainerID(), aid, false);
}
else {
if (!old.getDescription().getState().equals(AMSAgentDescription.LATENT) ) {
platformAgents.put(aid, old);
throw new NameClashException("Agent " + aid.getName() + " already present in the platform ");
}
}
}
else {
// The agent lives outside the platform (neither the forceReplacement flag nor the LATENT state apply in this case)
platformAgents.put(aid, old);
throw new NameClashException("Agent " + aid + " already registered to the platform ");
}
}
checkReplication(aid, cid);
// Notify listeners
fireBornAgent(cid, aid, ownership);
}
/**
Notify the platform that an agent has just died
*/
public void deadAgent(AID name, boolean containerRemoved) throws NotFoundException {
AgentDescriptor ad = platformAgents.acquire(name);
if(ad == null)
throw new NotFoundException("DeadAgent failed to find " + name);
ContainerID cid = ad.getContainerID();
platformAgents.remove(name);
replicatedAgents.remove(name);
// Notify listeners
fireDeadAgent(cid, name, containerRemoved);
}
/**
Notify the platform that an agent has just suspended
*/
public void suspendedAgent(AID name) throws NotFoundException {
AgentDescriptor ad = platformAgents.acquire(name);
if (ad == null)
throw new NotFoundException("SuspendedAgent failed to find " + name);
AMSAgentDescription amsd = ad.getDescription();
if (amsd != null) {
amsd.setState(AMSAgentDescription.SUSPENDED);
}
ContainerID cid = ad.getContainerID();
platformAgents.release(name);
// Notify listeners
fireSuspendedAgent(cid, name);
}
/**
Notify the platform that an agent has just resumed
*/
public void resumedAgent(AID name) throws NotFoundException {
AgentDescriptor ad = platformAgents.acquire(name);
if(ad == null)
throw new NotFoundException("ResumedAgent failed to find " + name);
AMSAgentDescription amsd = ad.getDescription();
if (amsd != null) {
amsd.setState(AMSAgentDescription.ACTIVE);
}
ContainerID cid = ad.getContainerID();
platformAgents.release(name);
// Notify listeners
fireResumedAgent(cid, name);
}
/**
Notify the platform that an agent has just moved
*/
public void movedAgent(AID agentID, ContainerID srcID, ContainerID destID) throws NotFoundException {
AgentDescriptor ad = platformAgents.acquire(agentID);
if (ad == null) {
throw new NotFoundException("Agent "+agentID.getName()+" not found in GADT");
}
ad.setContainerID((ContainerID)destID);
fireMovedAgent((ContainerID)srcID, (ContainerID)destID, agentID);
platformAgents.release(agentID);
}
/**
Notify the platform that an agent has just frozen
*/
public void frozenAgent(AID name, ContainerID bufferContainer) throws NotFoundException {
AgentDescriptor ad = platformAgents.acquire(name);
if (ad == null)
throw new NotFoundException("FrozenAgent failed to find " + name);
AMSAgentDescription amsd = ad.getDescription();
if (amsd != null) {
amsd.setState(AMSAgentDescription.SUSPENDED);
}
ContainerID cid = ad.getContainerID();
platformAgents.release(name);
// Notify listeners
fireFrozenAgent(cid, name, bufferContainer);
}
/**
Notify the platform that an agent has just thawed
*/
public void thawedAgent(AID name, ContainerID bufferContainer) throws NotFoundException {
AgentDescriptor ad = platformAgents.acquire(name);
if (ad == null)
throw new NotFoundException("ThawedAgent failed to find " + name);
AMSAgentDescription amsd = ad.getDescription();
if (amsd != null) {
amsd.setState(AMSAgentDescription.ACTIVE);
}
ContainerID cid = ad.getContainerID();
platformAgents.release(name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -