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

📄 maincontainerimpl.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			if (dsc != null) {
				if (dsc.getParentNode() != null) {
					shutdownContainer(targetID, "Container", requesterPrincipal, requesterCredentials);
					cnt++;
				}
			}
			else {
				// A zombie container. Just remove it from the container table
				removeRemoteContainer(targetID);
			}
		}
		
		if (cnt > 0 && myLogger.isLoggable(Logger.FINER)) {
			myLogger.log(Logger.FINER, "Containers on child nodes shutdown completed.");
		}
		
		// Then kill all remaining peripheral containers  
		cnt = 0;
		allContainers = containers.names();
		for(int i = 0; i < allContainers.length; i++) {
			ContainerID targetID = allContainers[i];
			NodeDescriptor dsc = myPlatformManager.getDescriptor(targetID.getName());
			if (dsc != null) {
				if (!dsc.getNode().hasPlatformManager()) {
					shutdownContainer(targetID, "Container", requesterPrincipal, requesterCredentials);
					cnt++;
				}
			}
			else {
				// A zombie container. Just remove it from the container table
				removeRemoteContainer(targetID);
			}
		}
		
		if (cnt > 0 && myLogger.isLoggable(Logger.FINER)) {
			myLogger.log(Logger.FINER, "Peripheral containers shutdown completed.");
		}
		
		// Then kill all auxiliary nodes not holding containers 
		myPlatformManager.shutdown();
		
		// Then kill all other main containers
		cnt = 0;
		allContainers = containers.names();
		for(int i = 0; i < allContainers.length; i++) {
			ContainerID targetID = allContainers[i];
			if(!targetID.equals(localContainerID)) {
				shutdownContainer(targetID, "Main Container", requesterPrincipal, requesterCredentials);
				cnt++;
			}
		}
		
		if (cnt > 0 && myLogger.isLoggable(Logger.FINER)) {
			myLogger.log(Logger.FINER, "Backup Main Containers shutdown completed.");
		}
		
		// Finally, kill the local container
		try {
			if (myLogger.isLoggable(Logger.FINEST)) {
				myLogger.log(Logger.FINEST, "Killing local node "+localContainerID.getName());
			}
			killContainer(localContainerID, requesterPrincipal, requesterCredentials);
			
			// Make sure all containers are succesfully removed from the table...
			boolean removed = containers.waitUntilEmpty(5000);
			if (removed) {
				if (myLogger.isLoggable(Logger.FINEST)) {
					myLogger.log(Logger.FINEST, "Local node shutdown completed.");
				}
			}
		}
		catch(NotFoundException nfe) {
			// Ignore the exception as we are removing a non-existing container
			myLogger.log(Logger.FINE, "Container " + localContainerID.getName() + " does not exist. Ignoring...");
		}
		catch(UnreachableException ue) {
			myLogger.log(Logger.WARNING, "Cannot kill container " + localContainerID.getName() + ": Unreachable. "+ue);
		}
		catch(JADESecurityException se) {
			// Let it through
			throw se;
		}
		catch(Throwable t) {
			myLogger.log(Logger.WARNING, "Cannot kill container " + localContainerID.getName() + ": Unexpected error. "+t);
		}
	}
	
	private void shutdownContainer(ContainerID targetID, String type, JADEPrincipal requesterPrincipal, Credentials requesterCredentials) throws JADESecurityException {
		try {
			if (myLogger.isLoggable(Logger.FINEST)) {
				myLogger.log(Logger.FINEST, "Killing "+type+" "+targetID.getName());
			}
			killContainer(targetID, requesterPrincipal, requesterCredentials);
			boolean removed = containers.waitForRemoval(targetID, 5000);
			if (removed) {
				if (myLogger.isLoggable(Logger.FINEST)) {
					myLogger.log(Logger.FINEST, type+" "+targetID.getName()+" shutdown completed");
				}
				return;
			}
		}
		catch(NotFoundException nfe) {
			// Ignore the exception as we are removing a non-existing container
			myLogger.log(Logger.FINE, "Container " + targetID.getName() + " does not exist. Ignoring...");
		}
		catch(UnreachableException ue) {
			myLogger.log(Logger.WARNING, "Cannot kill container " + targetID.getName() + ": Unreachable.");
		}
		catch(JADESecurityException se) {
			// Let it through
			throw se;
		}
		catch(Throwable t) {
			myLogger.log(Logger.WARNING, "Cannot kill container " + targetID.getName() + ": Unexpected error. "+t);
		}
		
		// If we get here either killContainer() threw an exception or the container did not terminate.
		// Just remove it from the container table
		removeRemoteContainer(targetID);
	}
	
	/** 
	 Install a new MTP on a given container
	 */
	public MTPDescriptor installMTP(String address, ContainerID cid, String className) throws NotFoundException, UnreachableException, MTPException {
		
		GenericCommand cmd = new GenericCommand(jade.core.messaging.MessagingSlice.INSTALL_MTP, jade.core.messaging.MessagingSlice.NAME, null);
		cmd.addParam(address);
		cmd.addParam(cid);
		cmd.addParam(className);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if(ret instanceof NotFoundException) {
				throw (NotFoundException)ret;
			}
			else if (ret instanceof IMTPException) {
				throw new UnreachableException("", (IMTPException) ret);
			}
			else if (ret instanceof MTPException) {
				throw (MTPException)ret;
			}
			else if (ret instanceof Throwable) {
				// In methods called by the AMS to serve agents requests we throw
				// a RuntimeException that will result in a FAILURE message sent
				// back to the requester
				throw new RuntimeException(((Throwable) ret).getMessage());
			}
		}
		
		MTPDescriptor dsc = (MTPDescriptor)ret;
		/***
		 System.out.println("--- New MTP ---");
		 System.out.println("Name: " + dsc.getName());
		 System.out.println("Addresses: ");
		 for(int i = 0; i < dsc.getAddresses().length; i++) {
		 System.out.println("[" + dsc.getAddresses()[i] + "]");
		 }
		 System.out.println("Supported Protocols: ");
		 for(int i = 0; i < dsc.getSupportedProtocols().length; i++) {
		 System.out.println("[" + dsc.getSupportedProtocols()[i] + "]");
		 }
		 ***/
		
		
		return dsc;
	}
	
	/** 
	 Uninstall an MTP on a given container
	 */
	public void uninstallMTP(String address, ContainerID cid) throws NotFoundException, UnreachableException, MTPException {
		
		GenericCommand cmd = new GenericCommand(jade.core.messaging.MessagingSlice.UNINSTALL_MTP, jade.core.messaging.MessagingSlice.NAME, null);
		cmd.addParam(address);
		cmd.addParam(cid);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if(ret instanceof NotFoundException) {
				throw (NotFoundException)ret;
			}
			else if (ret instanceof IMTPException) {
				throw new UnreachableException("", (IMTPException) ret);
			}
			else if (ret instanceof MTPException) {
				throw (MTPException)ret;
			}
			else if (ret instanceof Throwable) {
				// In methods called by the AMS to serve agents requests we throw
				// a RuntimeException that will result in a FAILURE message sent
				// back to the requester
				throw new RuntimeException(((Throwable) ret).getMessage());
			}
		}
	}
	
	/**
	 Change the ownership of an agent
	 // FIXME: implement or remove
	  */
	public void take(final AID agentID, final String username, final byte[] password) throws NotFoundException, UnreachableException, JADESecurityException {
	}
	
	
	/**
	 Activate sniffing on a given agent
	 */
	public void sniffOn(AID snifferName, List toBeSniffed) throws NotFoundException, UnreachableException  {
		GenericCommand cmd = new GenericCommand(jade.core.event.NotificationSlice.SNIFF_ON, jade.core.event.NotificationSlice.NAME, null);
		cmd.addParam(snifferName);
		cmd.addParam(toBeSniffed);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if(ret instanceof NotFoundException) {
				throw (NotFoundException)ret;
			}
			else if (ret instanceof IMTPException) {
				throw new UnreachableException("", (IMTPException) ret);
			}
			else if (ret instanceof Throwable) {
				// In methods called by the AMS to serve agents requests we throw
				// a RuntimeException that will result in a FAILURE message sent
				// back to the requester
				throw new RuntimeException(((Throwable) ret).getMessage());
			}
		}
		
	}
	
	/**
	 Deactivate sniffing on a given agent
	 */
	public void sniffOff(AID snifferName, List notToBeSniffed) throws NotFoundException, UnreachableException {
		GenericCommand cmd = new GenericCommand(jade.core.event.NotificationSlice.SNIFF_OFF, jade.core.event.NotificationSlice.NAME, null);
		cmd.addParam(snifferName);
		cmd.addParam(notToBeSniffed);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if(ret instanceof NotFoundException) {
				throw (NotFoundException)ret;
			}
			else if (ret instanceof IMTPException) {
				throw new UnreachableException("", (IMTPException) ret);
			}
			else if (ret instanceof Throwable) {
				// In methods called by the AMS to serve agents requests we throw
				// a RuntimeException that will result in a FAILURE message sent
				// back to the requester
				throw new RuntimeException(((Throwable) ret).getMessage());
			}
		}
	}
	
	/**
	 Activate debugging on a given agent
	 */
	public void debugOn(AID debuggerName, List toBeDebugged) throws NotFoundException, UnreachableException {
		GenericCommand cmd = new GenericCommand(jade.core.event.NotificationSlice.DEBUG_ON, jade.core.event.NotificationSlice.NAME, null);
		cmd.addParam(debuggerName);
		cmd.addParam(toBeDebugged);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if(ret instanceof NotFoundException) {
				throw (NotFoundException)ret;
			}
			else if (ret instanceof IMTPException) {
				throw new UnreachableException("", (IMTPException) ret);
			}
			else if (ret instanceof Throwable) {
				// In methods called by the AMS to serve agents requests we throw
				// a RuntimeException that will result in a FAILURE message sent
				// back to the requester
				throw new RuntimeException(((Throwable) ret).getMessage());
			}
		}
	}
	
	/**
	 Deactivate debugging on a given agent
	 */
	public void debugOff(AID debuggerName, List notToBeDebugged) throws NotFoundException, UnreachableException {
		GenericCommand cmd = new GenericCommand(jade.core.event.NotificationSlice.DEBUG_OFF, jade.core.event.NotificationSlice.NAME, null);
		cmd.addParam(debuggerName);
		cmd.addParam(notToBeDebugged);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if(ret instanceof NotFoundException) {
				throw (NotFoundException)ret;
			}
			else if (ret instanceof IMTPException) {
				throw new UnreachableException("", (IMTPException) ret);
			}
			else if (ret instanceof Throwable) {
				// In methods called by the AMS to serve agents requests we throw
				// a RuntimeException that will result in a FAILURE message sent
				// back to the requester
				throw new RuntimeException(((Throwable) ret).getMessage());
			}
		}
	}
	
	/**
	 Register an agent to the White Pages service of this platform
	 */
	public void amsRegister(AMSAgentDescription dsc) throws AlreadyRegistered, JADESecurityException {
		// Mandatory slots have already been checked
		AID agentID = dsc.getName();
		
		AgentDescriptor ad = platformAgents.acquire(agentID);
		if (ad == null) {
			System.out.println("No descriptor found for agent "+agentID);
			// This is a foreign agent registering to this platform
			ad = new AgentDescriptor(AgentDescriptor.FOREIGN_AGENT);
			ad.setDescription(dsc);
			platformAgents.put(agentID, ad);
		}
		else {
			if (ad.getDescription() == null) {
				System.out.println("Descriptor with null AMSD found for agent "+agentID);
				// This is an agent living in the platform that had previously deregistered 
				ad.setDescription(dsc);
				platformAgents.release(agentID);
			}
			else {
				System.out.println("Descriptor with NON null AMSD found for agent "+agentID);
				// This agent is already registered --> Exception
				platformAgents.release(agentID);
				throw new AlreadyRegistered();
			}
		}
	}
	
	/**
	 Deregister an agent from the White Pages service of this platform
	 */
	public void amsDeregister(AMSAgentDescription dsc) throws NotRegistered, JADESecurityException {
		// Mandatory slots have already been checked
		AID agentID = dsc.getName();
		
		AgentDescriptor ad = platformAgents.acquire(agentID);
		if (ad != null) {
			if (ad.getDescription() != null) {
				if (ad.isNative()) {
					// This is an agent living in the platform --> just clear its registration
					ad.setDescription(null);
					platformAgents.release(agentID);
				}
				else {
					// This is a foreign agent --> remove the descriptor completely
					platformAgents.remove(agentID);
				}
				return;
			}
		}
		// This agent was not registered --> Exception
		throw new NotRegistered();
	}
	
	/**
	 Modify the registration of an agent to the White Pages service of 
	 this platform.
	 If the modification implies a change in the agent state (and the agent
	 lives in the platform) --> force that change 
	 If the modification implies a change in the agent ownership (and the agent
	 lives in the platform) --> force that change 
	 */
	public void amsModify(AMSAgentDescription dsc) throws NotRegistered, NotFoundException, UnreachableException, JADESecurityException {
		// Mandatory slots have already been checked
		AID agentID = dsc.getName();
		
		AgentDescriptor ad = platformAgents.acquire(agentID);
		if (ad != null) {
			AMSAgentDescription oldDsc = ad.getDescription();
			if (oldDsc != null) {
				ad.setDescription(dsc);
				String newState = dsc.getState();
				String newOwnership = dsc.getOwnership();
				if (newOwnership == null) {
					newOwnership = oldDsc.getOwnership();
				}
				platformAgents.release(agentID);
				if (ad.isNative()) {
					// This is an agent living in the platform --> if necessary
					// force changes in agent state and ownership 
					if (AMSAgentDescription.SUSPENDED.equals(newState) && !AMSAgentDescription.SUSPENDED.equals(oldDsc.getState())) {
						suspend(agentID);
					}
					if (AMSAgentDescription.ACTIVE.equals(newState) && !AMSAgentDescription.ACTIVE.equals(oldDsc.getState())) {
						activate(agentID);
					}
					if (newOwnership != null && newOwnership != oldDsc.getOwnership()) {
						/*byte[] password = Agent.extractPassword(newOwnership);
						 String username = Agent.extractUsername(newOwnership);
						 take(agentID, username, password);*/
					}
				}
				return;
			}
		}
		// This agent was not registered --> Exception

⌨️ 快捷键说明

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