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

📄 maincontainerimpl.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		throw new NotRegistered();
	}
	
	/**
	 Searches the White Pages for agents whose description matches a given
	 template.
	 */
	public List amsSearch(AMSAgentDescription template, long maxResults) {
		List results = new ArrayList();
		AID[] ids = platformAgents.keys();
		for (int i = 0; i < ids.length; ++i) {
			try {
				AMSAgentDescription amsd = getAMSDescription(ids[i]); 
				if (match(template, amsd)) {
					results.add(amsd);
					if (results.size() >= maxResults) {
						break;
					}
				}
			}
			catch (NotFoundException nfe) {
				// The agent disappeared while we were looping. Ignore it
			}
		}
		return results;
	}
	
	/**
	 Return the IDs of all containers in the platform
	 */
	public ContainerID[] containerIDs() {
		return containers.names();
	}
	
	/**
	 Return the IDs of all agents in the platform
	 */
	public AID[] agentNames() {
		return platformAgents.keys();
	}
	
	/**
	 Return all MTPs in a given container
	 */
	public List containerMTPs(ContainerID cid) throws NotFoundException {
		return containers.getMTPs(cid);
	}
	
	/**
	 Return all agents living on a container
	 */
	public List containerAgents(ContainerID cid) throws NotFoundException {
		List agents = new ArrayList();
		AID[] allIds = platformAgents.keys();
		
		for (int i = 0; i < allIds.length; ++i) {
			AID    id = allIds[i];
			AgentDescriptor ad = platformAgents.acquire(id);
			if (ad != null) {
				ContainerID cid1 = ad.getContainerID();
				
				if (cid.equals(cid1)) {
					agents.add(id);
				} 
				platformAgents.release(id);
			}
		} 
		return agents;
	}
	
	public void toolAdded(AID tool) {
		synchronized(agentTools) {
			if(!agentTools.contains(tool)) {
				agentTools.add(tool);
			}
		}
	}
	
	public void toolRemoved(AID tool) {
		synchronized(agentTools) {
			agentTools.remove(tool);
		}
	}
	
	public AID[] agentTools() {
		synchronized(agentTools) {
			Object[] objs = agentTools.toArray();
			AID[] result = new AID[objs.length];
			for(int i = 0; i < result.length; i++) {
				result[i] = (AID)objs[i];
			}
			
			return result;
		}
	}
	
	/**
	 Return the ID of the container an agent lives in
	 */
	public ContainerID getContainerID(AID agentID) throws NotFoundException {
		AgentDescriptor ad = platformAgents.acquire(agentID);
		if(ad == null)
			throw new NotFoundException("getContainerID() failed to find agent " + agentID.getName());
		ContainerID result = ad.getContainerID();
		platformAgents.release(agentID);
		return result;
	}
	
	/**
	 Return the node a container is deployed at
	 */
	public NodeDescriptor getContainerNode(ContainerID cid) throws NotFoundException {
		return getDescriptor(cid.getName());
	}
	
	/**
	 Return the AMS description of an agent
	 */
	public AMSAgentDescription getAMSDescription(AID agentID) throws NotFoundException {
		AgentDescriptor ad = platformAgents.acquire(agentID);
		if(ad == null)
			throw new NotFoundException("getAMSDescription() failed to find agent " + agentID.getName());
		AMSAgentDescription amsd = ad.getDescription();
		platformAgents.release(agentID);
		return amsd;
	}
	
	/**
	 Add a listener of platform events
	 */
	public void addListener(AgentManager.Listener l) {
		platformListeners.add(l);
	}
	
	/**
	 Remove a listener of platform events
	 */
	public void removeListener(AgentManager.Listener l) {
		platformListeners.remove(l);
	}
	
	
	
	private NodeDescriptor getDescriptor(String name) throws NotFoundException {
		NodeDescriptor dsc = myPlatformManager.getDescriptor(name);
		if (dsc == null) {
			throw new NotFoundException("Node  " + name + " not found.");
		}
		else {
			return dsc;
		}
	}
	
	private boolean match(AMSAgentDescription templateDesc, AMSAgentDescription factDesc) {
		try {
			String o1 = templateDesc.getOwnership();
			if(o1 != null) {
				String o2 = factDesc.getOwnership();
				if((o2 == null) || (!o1.equalsIgnoreCase(o2)))
					return false;
			}
			
			String s1 = templateDesc.getState();
			if(s1 != null) {
				String s2 = factDesc.getState();
				if((s2 == null) || (!s1.equalsIgnoreCase(s2)))
					return false;
			}
			
			AID id1 = templateDesc.getName();
			if(id1 != null) {
				AID id2 = factDesc.getName();
				if((id2 == null) || (!matchAID(id1, id2)))
					return false;
			}
			
			return true;
		}
		catch (ClassCastException cce) {
			return false;
		}
	}
	
	// Helper method to match two Agent Identifiers
	private final boolean matchAID(AID template, AID fact) {
		// Match the GUID in the ':name' slot
		String templateName = template.getName();
		if(templateName != null) {
			String factName = fact.getName();
			if((factName == null) || (!templateName.equalsIgnoreCase(factName)))
				return false;
		}
		
		// Match the address sequence. See 'FIPA Agent Management Specification, Sect. 6.4.2.1'
		Iterator itTemplate = template.getAllAddresses();
		Iterator itFact = fact.getAllAddresses();
		
		// All the elements in the template sequence must appear in the
		// fact sequence, in the same order
		while(itTemplate.hasNext()) {
			String templateAddr = (String)itTemplate.next();
			
			// Search 'templateAddr' into the remaining part of the fact sequence
			boolean found = false;
			while(!found && itFact.hasNext()) {
				String factAddr = (String)itFact.next();
				found = templateAddr.equalsIgnoreCase(factAddr);
			}
			if(!found) // An element of the template does not appear in the fact sequence
				return false;
		}
		
		// Match the resolvers sequence. See 'FIPA Agent Management Specification, Sect. 6.4.2.1'
		itTemplate = template.getAllResolvers();
		itFact = fact.getAllResolvers();
		
		while(itTemplate.hasNext()) {
			AID templateRes = (AID)itTemplate.next();
			
			// Search 'templateRes' into the remaining part of the fact sequence
			boolean found = false;
			while(!found && itFact.hasNext()) {
				AID factRes = (AID)itFact.next();
				found = matchAID(templateRes, factRes); // Recursive call
			}
			if(!found) // An element of the template does not appear in the fact sequence
				return false;
		}
		
		return true;
	}
	
	/////////////////////////////////////////////////////////////////
	// Private methods to notify platform listeners of significant 
	// events.
	/////////////////////////////////////////////////////////////////
	private void fireAddedContainer(ContainerID cid) {
		PlatformEvent ev = new PlatformEvent(PlatformEvent.ADDED_CONTAINER, cid);
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.addedContainer(ev);
		}
	}
	
	private void fireRemovedContainer(ContainerID cid) {
		PlatformEvent ev = new PlatformEvent(PlatformEvent.REMOVED_CONTAINER, cid);
		
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.removedContainer(ev);
		}
	}
	
	private void fireBornAgent(ContainerID cid, AID agentID, String ownership) {
		PlatformEvent ev = new PlatformEvent(PlatformEvent.BORN_AGENT, agentID, cid, null, ownership);
		
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.bornAgent(ev);
		}
	}
	
	private void fireDeadAgent(ContainerID cid, AID agentID, boolean containerRemoved) {
		PlatformEvent ev = new PlatformEvent(PlatformEvent.DEAD_AGENT, agentID, cid, containerRemoved);
		
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.deadAgent(ev);
		}
	}
	
	private void fireSuspendedAgent(ContainerID cid, AID agentID) {
		PlatformEvent ev = new PlatformEvent(PlatformEvent.SUSPENDED_AGENT, agentID, cid);
		
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.suspendedAgent(ev);
		}
	}
	
	private void fireResumedAgent(ContainerID cid, AID agentID) {
		PlatformEvent ev = new PlatformEvent(PlatformEvent.RESUMED_AGENT, agentID, cid);
		
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.resumedAgent(ev);
		}
	}
	
	private void fireFrozenAgent(ContainerID cid, AID agentID, ContainerID bufferContainer) {
		PlatformEvent ev = new PlatformEvent(PlatformEvent.FROZEN_AGENT, agentID, cid, bufferContainer);
		
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.frozenAgent(ev);
		}
	}
	
	private void fireThawedAgent(ContainerID cid, AID agentID, ContainerID bufferContainer) {
		PlatformEvent ev = new PlatformEvent(PlatformEvent.THAWED_AGENT, agentID, cid, bufferContainer);
		
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.thawedAgent(ev);
		}
	}
	
	private void fireMovedAgent(ContainerID from, ContainerID to, AID agentID) {
		PlatformEvent ev = new PlatformEvent(agentID, from, to);
		
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.movedAgent(ev);
		}
	}
	
	private void fireAddedMTP(MTPDescriptor mtp, ContainerID cid) {
		String name = mtp.getName();
		String[] addrs = mtp.getAddresses();
		Channel ch = new Channel("FIXME: missing channel name", name, addrs[0]);
		MTPEvent ev = new MTPEvent(MTPEvent.ADDED_MTP, cid, ch);
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.addedMTP(ev);
		} 
	}
	
	private void fireRemovedMTP(MTPDescriptor mtp, ContainerID cid) {
		String name = mtp.getName();
		String[] addrs = mtp.getAddresses();
		Channel ch = new Channel("FIXME: missing channel name", name, addrs[0]);
		MTPEvent ev = new MTPEvent(MTPEvent.REMOVED_MTP, cid, ch);
		for(int i = 0; i < platformListeners.size(); i++) {
			AgentManager.Listener l = (AgentManager.Listener)platformListeners.get(i);
			l.removedMTP(ev);
		}
	}
	
	private void removeAllAgents(ContainerID cid) {
		String name = cid.getName();
		AID[] allIDs = platformAgents.keys();
		for(int i = 0; i < allIDs.length; i++) {
			AID aid = allIDs[i];
			AgentDescriptor ad = platformAgents.acquire(aid);
			if (ad != null) {
				ContainerID id = ad.getContainerID();
				if(CaseInsensitiveString.equalsIgnoreCase(id.getName(), name)) {
					String localName = aid.getLocalName();
					if (localName.equals(FIPANames.AMS) || localName.equals(FIPANames.DEFAULT_DF) || replicatedAgents.containsKey(aid)) {
						ad.getDescription().setState(AMSAgentDescription.LATENT);
						platformAgents.release(aid);
						// GC-ADD-18022007-START
						// Notify listeners 
						// fireDeadAgent(cid, aid, true);
						// GC-ADD-18022007-END
					}
					else {
						platformAgents.release(aid);
						try {
							deadAgent(aid, true);
						}
						catch(NotFoundException nfe) {
							nfe.printStackTrace();
						}
					}
				}
				else {
					platformAgents.release(aid);
				}
			}
		}
	}
	
	private void removeAllMTPs(ContainerID cid) {
		try {
			List l = containers.getMTPs(cid);
			Object[] objs = l.toArray();
			for(int i = 0; i < objs.length; i++) {
				MTPDescriptor mtp = (MTPDescriptor)objs[i];
				
				GenericCommand gCmd = new GenericCommand(jade.core.messaging.MessagingSlice.DEAD_MTP, jade.core.messaging.MessagingSlice.NAME, null);
				gCmd.addParam(mtp);
				gCmd.addParam(cid);
				myCommandProcessor.processOutgoing(gCmd);
				
			}
		}
		catch(NotFoundException nfe) {
			nfe.printStackTrace();
		}
	}
	
	public AgentDescriptor acquireAgentDescriptor(AID agentID) {
		return platformAgents.acquire(agentID);
	}
	
	public void releaseAgentDescriptor(AID agentID) {
		platformAgents.release(agentID);
	}
	
}

⌨️ 快捷键说明

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