⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servicemanagerimpl.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************
 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.mtp.TransportAddress;
import jade.security.JADESecurityException;

import jade.util.leap.Map;
import jade.util.leap.HashMap;
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 Rimassa - FRAMeTech s.r.l.
 @author Giovanni Caire - TILAB
 */
public class ServiceManagerImpl implements ServiceManager, ServiceFinder {
	private IMTPManager myIMTPManager;
	private CommandProcessor myCommandProcessor;
	private PlatformManager myPlatformManager;
	private boolean invalidPlatformManager;
	private String platformName;
	private Node localNode;
	private NodeDescriptor localNodeDescriptor;
	private Map localServices;
	private Map backupManagers;
	
	private boolean terminating = false;

	private jade.util.Logger myLogger;

	/**
	 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.
	 */
	ServiceManagerImpl(Profile p, PlatformManager pm) throws ProfileException {
		myCommandProcessor = p.getCommandProcessor();
		myIMTPManager = p.getIMTPManager();
		myPlatformManager = pm;
		invalidPlatformManager = false;
		localServices = new HashMap(5);
		backupManagers = new HashMap(1);

		myLogger = Logger.getMyLogger(getClass().getName());
	}

	// Implementation of the ServiceManager interface

	public String getPlatformName() throws IMTPException {
		if (platformName == null) {
			try {
				platformName = myPlatformManager.getPlatformName();
			} catch (IMTPException imtpe) {
				if (reconnect()) {
					platformName = myPlatformManager.getPlatformName();
				} else {
					throw imtpe;
				}
			}
		}
		return platformName;
	}

	public synchronized void addAddress(String addr) throws IMTPException {
		myLogger.log(Logger.INFO, "Adding PlatformManager address " + addr);

		if (invalidPlatformManager || !addr.equals(myPlatformManager.getLocalAddress())) {
			backupManagers.put(addr, myIMTPManager.getPlatformManagerProxy(addr));
			if (invalidPlatformManager) {
				reconnect();
			}
		}
	}

	public synchronized void removeAddress(String addr) throws IMTPException {
		myLogger.log(Logger.INFO, "Removing PlatformManager address " + addr);

		backupManagers.remove(addr);
		if (compareAddresses(addr, myPlatformManager.getLocalAddress())) {
			reconnect();
		}
	}

	private boolean compareAddresses(String addr1, String addr2) {
		//#MIDP_EXCLUDE_BEGIN
		try {
			TransportAddress ta1 = myIMTPManager.stringToAddr(addr1);
			TransportAddress ta2 = myIMTPManager.stringToAddr(addr2);
			if (CaseInsensitiveString.equalsIgnoreCase(ta1.getProto(), ta2.getProto())) {
				if (CaseInsensitiveString.equalsIgnoreCase(ta1.getPort(), ta2.getPort())) {
					if (Profile.compareHostNames(ta1.getHost(), ta2.getHost())) {
						return true;
					}
				}				
			}
			return false;
		}
		catch (Exception e) {
			// If we can't parse the addresses, just compare them as strings
			return CaseInsensitiveString.equalsIgnoreCase(addr1, addr2);
		}
		//#MIDP_EXCLUDE_END
		/*#MIDP_INCLUDE_BEGIN
		return CaseInsensitiveString.equalsIgnoreCase(addr1, addr2);
		#MIDP_INCLUDE_END*/
	}

	public String getLocalAddress() throws IMTPException {
		return myPlatformManager.getLocalAddress();
	}

	public void addNode(NodeDescriptor desc, ServiceDescriptor[] services) throws IMTPException, ServiceException, JADESecurityException {
		localNodeDescriptor = desc;
		localNode = desc.getNode();
		try {
			// Install all services locally
			Vector ss = new Vector(services != null ? services.length : 0);
			if (services != null) {
				for (int i = 0; i < services.length; ++i) {
					try {
						installServiceLocally(services[i]);
						ss.addElement(services[i]);
					}
					catch (Exception e) {
						if (services[i].isMandatory()) {
							throw e;
						}
						else {
					  		myLogger.log(Logger.WARNING,"Exception installing service " + services[i].getService() + ". " + e);
					  		e.printStackTrace();
						}
					}
				}
			}

			// Notify the platform manager. Get back a valid name and assign
			// it to both the node, the node descriptor and the container (if any)
			String name = null;
			try {
				name = myPlatformManager.addNode(desc, ss, false);
			} catch (IMTPException imtpe) {
				if (reconnect()) {
					name = myPlatformManager.addNode(desc, ss, false);
				} else {
					throw imtpe;
				}
			}
			adjustName(name);
		} catch (IMTPException imtpe2) {
			throw imtpe2;
		} catch (ServiceException se) {
			throw se;
		} catch (JADESecurityException ae) {
			throw ae;
		} catch (Throwable t) {
			throw new ServiceException("Unexpected error activating node", t);
		}
	}

	public void removeNode(NodeDescriptor desc) throws IMTPException, ServiceException {
		// Do not notify the platform manager. The node termination will cause the deregistration...

		terminating = true;
		
		// Uninstall all services locally
		Object[] names = localServices.keySet().toArray();
		for (int i = 0; i < names.length; i++) {
			try {
				String svcName = (String) names[i];
				uninstallServiceLocally(svcName);
			} catch (IMTPException imtpe) {
				// This should never happen, because it's a local call...
				imtpe.printStackTrace();
			}
		}

		//#MIDP_EXCLUDE_BEGIN
		// If this node was exporting a PlatformManager, unexport it
		if (desc.getNode().hasPlatformManager()) {
			myIMTPManager.unexportPlatformManager(myPlatformManager);
		}
		//#MIDP_EXCLUDE_END
	}

	public void activateService(ServiceDescriptor desc) throws IMTPException, ServiceException {
		try {
			// Install the service locally
			installServiceLocally(desc);
			// Notify the platform manager (add a slice for this service on this node)
			try {
				myPlatformManager.addSlice(desc, localNodeDescriptor, false);
			} catch (IMTPException imtpe) {
				if (reconnect()) {
					myPlatformManager.addSlice(desc, localNodeDescriptor, false);
				} else {
					throw imtpe;
				}
			}
		} catch (IMTPException imtpe2) {
			// Undo the local service installation
			uninstallServiceLocally(desc.getName());
			// Rethrow the exception
			throw imtpe2;
		}
	}

	public void deactivateService(String name) throws IMTPException, ServiceException {
		ServiceDescriptor desc = (ServiceDescriptor) localServices.get(name);
		if (desc != null) {
			// Notify the platform manager (remove the slice for this service on this node)
			try {
				myPlatformManager.removeSlice(name, localNode.getName(), false);
			} 
			catch (IMTPException imtpe) {
				if (reconnect()) {
					myPlatformManager.removeSlice(name, localNode.getName(), false);
				} 
				else {
					throw imtpe;
				}
			}
	
			// Uninstall the service locally
			uninstallServiceLocally(name);
		}
	}

	/////////////////////////////////////////////////
	// ServiceFinder interface
	/////////////////////////////////////////////////

	public Service findService(String key) throws IMTPException, ServiceException {
		Service svc = null;
		ServiceDescriptor svcDsc = (ServiceDescriptor) localServices.get(key);
		if (svcDsc != null) {
			svc = svcDsc.getService();
		}
		return svc;
	}

	public Service.Slice findSlice(String serviceKey, String sliceKey) throws IMTPException, ServiceException {
		Service.Slice slice = null;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -