📄 platformmanagerimpl.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 jade.core.behaviours.Behaviour;
import jade.security.JADESecurityException;
import jade.mtp.TransportAddress;
import jade.util.leap.Map;
import jade.util.leap.HashMap;
import jade.util.leap.List;
import jade.util.leap.ArrayList;
import jade.util.leap.Iterator;
import jade.util.Logger;
import java.util.Vector;
/**
The <code>ServiceManagerImpl</code> class is the actual
implementation of JADE platform <i>Service Manager</i> and
<i>Service Finder</i> components. It holds a set of services and
manages them.
@author Giovanni Caire - TILAB
@author Giovanni Rimassa - FRAMeTech s.r.l.
*/
public class PlatformManagerImpl implements PlatformManager {
private IMTPManager myIMTPManager;
private CommandProcessor myCommandProcessor;
// FIXME: The association between MainContainer and PlatformManagerImpl need be clarified...
private MainContainerImpl myMain;
private Map nodes;
private Map services;
private Map replicas;
private Map monitors;
private String localAddr;
private String platformID;
// These variables hold progressive numbers just used to name new nodes.
// By convention, nodes hosting containers with a local copy of the Platform Manager are called
// Main-Container-<N>, hosting containers whereas nodes without their own Platform Manager are
// called Container-<M>.
// Nodes not hosting containers are called Aux-Node-<K>
private int containerNo = 1;
private int mainContainerNo = 0;
private int nodeNo = 1;
private Logger myLogger = Logger.getMyLogger(getClass().getName());
/**
Inner class ServiceEntry (package scoped for debugging purpose)
*/
class ServiceEntry {
public ServiceEntry(Service s) {
myService = s;
slices = new HashMap();
}
public void addSlice(String name, Service.Slice s, Node n) {
SliceEntry e = new SliceEntry(s, n);
slices.put(name, e);
}
public Service.Slice removeSlice(String name) {
SliceEntry e = (SliceEntry) slices.remove(name);
if (e == null) {
return null;
} else {
return e.getSlice();
}
}
public Vector getSlices() {
Iterator sliceEntries = slices.values().iterator();
Vector result = new Vector();
while (sliceEntries.hasNext()) {
SliceEntry e = (SliceEntry) sliceEntries.next();
result.addElement(e.getSlice());
}
return result;
}
public Service.Slice getSlice(String name) {
SliceEntry e = (SliceEntry) slices.get(name);
if (e == null) {
return null;
} else {
return e.getSlice();
}
}
public Node[] getNodes() {
Object[] sliceEntries = slices.values().toArray();
Node[] result = new Node[sliceEntries.length];
for (int i = 0; i < result.length; i++) {
SliceEntry e = (SliceEntry) sliceEntries[i];
result[i] = e.getNode();
}
return result;
}
public Node getNode(String name) {
SliceEntry e = (SliceEntry) slices.get(name);
if (e == null) {
return null;
} else {
return e.getNode();
}
}
public void setService(Service svc) {
myService = svc;
}
public Service getService() {
return myService;
}
// For debugging purpose only
Map getSlicesMap() {
return slices;
}
private Service myService;
private Map slices;
} // End of inner class ServiceEntry class
/**
Inner class SliceEntry (package-scoped for debugging purpose)
*/
class SliceEntry {
public SliceEntry(Service.Slice s, Node n) {
mySlice = s;
myNode = n;
}
public Service.Slice getSlice() {
return mySlice;
}
public Node getNode() {
return myNode;
}
private Service.Slice mySlice;
private Node myNode;
} // End of inner class SliceEntry class
/**
Constructs a new Service Manager implementation complying with
a given JADE profile. This constructor is package-scoped, so
that only the JADE kernel is allowed to create a new Service
Manager implementation.
@param p The platform profile describing how the JADE platform
is to be configured.
*/
PlatformManagerImpl(Profile p) throws ProfileException {
myCommandProcessor = p.getCommandProcessor();
myIMTPManager = p.getIMTPManager();
myMain = new MainContainerImpl(p, this);
nodes = new HashMap();
services = new HashMap();
replicas = new HashMap();
monitors = new HashMap();
platformID = p.getParameter(Profile.PLATFORM_ID, null);
if (platformID == null || platformID.equals("")) {
try {
// Build the PlatformID using the local host and port
List l = myIMTPManager.getLocalAddresses();
TransportAddress localAddr = (TransportAddress) l.get(0);
platformID = localAddr.getHost() + ":" + localAddr.getPort() + "/JADE";
} catch (Exception e) {
throw new ProfileException("Can't set PlatformID");
}
}
}
MainContainerImpl getMain() {
return myMain;
}
public void setPlatformName(String name) throws IMTPException {
platformID = name;
}
// Implementation of the PlatformManager interface
public String getPlatformName() throws IMTPException {
return platformID;
}
public String getLocalAddress() {
return localAddr;
}
public void setLocalAddress(String addr) {
localAddr = addr;
}
/**
@param dsc The Descriptor of the new Node
@param services The services currently installed on the new Node
@param propagated Flag indicating whether the new-node event
was a propagated event within the replication mechanism
*/
public String addNode(NodeDescriptor dsc, Vector nodeServices, boolean propagated) throws IMTPException, ServiceException, JADESecurityException {
String newName = localAddNode(dsc, nodeServices, propagated);
if (!propagated) {
broadcastAddNode(dsc, nodeServices);
}
return newName;
}
// This may throw IMTPException since localAddSlice() throws IMTPException
private String localAddNode(NodeDescriptor dsc, Vector nodeServices, boolean propagated) throws IMTPException, ServiceException, JADESecurityException {
Node node = dsc.getNode();
// If the node hosts a Container, adjust node name
adjustName(dsc, node);
// Issue a NEW_NODE vertical command
if (!propagated) {
// In this case we issue the command before adding the node
// for authorization purposes
issueNewNodeCommand(dsc);
}
// Add the new node
if (isLocalNode(node)) {
// If this is the local node it surely host a container (the main). Remove it
myMain.addLocalContainer(dsc.getContainer());
}
else {
if (myLogger.isLoggable(Logger.INFO)) {
myLogger.log(Logger.INFO, "Adding node <" + dsc.getName() + "> to the platform");
}
// If the node hosts a container add it as a remote container
if (dsc.getContainer() != null) {
myMain.addRemoteContainer(dsc.getContainer());
}
}
// Add the node to the global node list
nodes.put(dsc.getName(), dsc);
// Add all service slices
// Do not broadcast since this information is already conveied when broadcasting the add-node event
if (nodeServices != null) {
for (int i = 0; i < nodeServices.size(); ++i) {
ServiceDescriptor service = (ServiceDescriptor) nodeServices.elementAt(i);
localAddSlice(service, dsc, propagated);
}
}
// Start monitoring the new node if necessary
if (!isLocalNode(node) && !propagated) {
// Start monitoring
monitor(dsc);
}
// Return the name given to the new node
return node.getName();
}
private void broadcastAddNode(NodeDescriptor dsc, Vector nodeServices) throws ServiceException {
// Avoid concurrent modification exception
Object[] rr = replicas.values().toArray();
for (int i = 0; i < rr.length; ++i) {
PlatformManager replica = (PlatformManager) rr[i];
try {
replica.addNode(dsc, nodeServices, true);
} catch (IMTPException imtpe) {
// Zombie replica. Will be removed soon. Just ignore it
} catch (JADESecurityException ae) {
// Should never happen since this is a propagated info
ae.printStackTrace();
}
}
}
public void removeNode(NodeDescriptor dsc, boolean propagated) throws IMTPException, ServiceException {
localRemoveNode(dsc, propagated);
// If this is the local node the node termination will cause the deregistration...
if (!propagated && !isLocalNode(dsc.getNode())) {
broadcastRemoveNode(dsc);
}
}
private void localRemoveNode(NodeDescriptor dsc, boolean propagated) throws ServiceException {
dsc = adjustDescriptor(dsc);
Node node = dsc.getNode();
// Remove all the slices corresponding to the removed node
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -