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

📄 maincontainerimpl.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		
		// Notify listeners
		fireThawedAgent(cid, name, bufferContainer);
	}
	
	
	/**
	 Notify the platform that a new MTP has become active on a given container
	 */
	public void newMTP(MTPDescriptor mtp, ContainerID cid) throws IMTPException {
		try {
			String[] mtpAddrs = mtp.getAddresses();
			String mtpAddress = mtpAddrs[0];
			platformAddresses.add(mtpAddress);
			containers.addMTP(cid, mtp);
			
			// Update the AMS-descriptions of all registered agents living in the platform
			AID[] allIds = platformAgents.keys();
			for (int i = 0; i < allIds.length; ++i) {
				AgentDescriptor ad = platformAgents.acquire(allIds[i]);
				AMSAgentDescription dsc = ad.getDescription();	
				if (dsc != null && ad.isNative()) {
					AID id = dsc.getName();
					id.addAddresses(mtpAddress);
				} 
				platformAgents.release(allIds[i]);
			}
			
			// Notify listeners (typically the AMS)
			fireAddedMTP(mtp, cid);
		}
		catch(NotFoundException nfe) {
			System.out.println("Error: the container " + cid.getName() + " was not found.");
		}
	}
	
	/**
	 Notify the platform that an MTP is no longer active on a given container
	 */
	public void deadMTP(MTPDescriptor mtp, ContainerID cid) throws IMTPException {
		try {
			String[] mtpAddrs = mtp.getAddresses();
			String mtpAddress = mtpAddrs[0];
			platformAddresses.remove(mtpAddress);
			containers.removeMTP(cid, mtp);
			
			// Update the AMS-descriptions of all agents living in the platform
			AID[] allIds = platformAgents.keys();
			for (int i = 0; i < allIds.length; ++i) {
				AgentDescriptor ad = platformAgents.acquire(allIds[i]);
				AMSAgentDescription dsc = ad.getDescription();	
				if (ad.isNative()) {
					AID id = dsc.getName();
					id.removeAddresses(mtpAddress);
				} 
				platformAgents.release(allIds[i]);
			}
			
			// Notify listeners (typically the AMS)
			fireRemovedMTP(mtp, cid);
		}
		catch(NotFoundException nfe) {
			System.out.println("Error: the container " + cid.getName() + " was not found.");
		}
	}
	
	
	//////////////////////////////////////////////////////////////////////
	// AgentManager interface implementation.
	// These methods are called by the AMS to execute the actions that can 
	// be requested by agents in the platform.
	//////////////////////////////////////////////////////////////////////
	
	public void addTool(AID tool) {
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.ADD_TOOL, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(tool);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if (ret instanceof Throwable) {
				((Throwable) ret).printStackTrace();
			}
		}
	}
	
	public void removeTool(AID tool) {
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.REMOVE_TOOL, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(tool);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if (ret instanceof Throwable) {
				((Throwable) ret).printStackTrace();
			}
		}
	}
	
	
	/**
	 Create an agent on a given container
	 @see AgentManager#create(String agentName, String className, String arguments[], ContainerID cid, String ownership, CertificateFolder certs) throws UnreachableException, JADESecurityException, NotFoundException
	 */
	public void create(String name, String className, Object args[], ContainerID cid, JADEPrincipal owner, Credentials initialCredentials, JADEPrincipal requesterPrincipal, Credentials requesterCredentials) throws UnreachableException, JADESecurityException, NotFoundException, NameClashException {
		
		// Get the container where to create the agent
		// If it is not specified, assume it is the Main
		if (cid == null || cid.getName() == null) {
			cid = localContainerID;
		}
		
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.REQUEST_CREATE, jade.core.management.AgentManagementSlice.NAME, null);
		
		cmd.addParam(name);
		cmd.addParam(className);
		cmd.addParam(args);
		cmd.addParam(cid);
		cmd.addParam(owner);
		cmd.addParam(initialCredentials);
		cmd.setPrincipal(requesterPrincipal);
		cmd.setCredentials(requesterCredentials);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if(ret instanceof NotFoundException) {
				throw (NotFoundException)ret;
			}
			else if (ret instanceof NameClashException) {
				throw (NameClashException)ret;
			}
			else if (ret instanceof IMTPException) {
				throw new UnreachableException("", (IMTPException) ret);
			}
			else if (ret instanceof JADESecurityException) {
				throw (JADESecurityException)ret;
			}
			else if (ret instanceof Throwable) {
				((Throwable) ret).printStackTrace();
				// 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());
			}
		}
	}
	
	
	/**
	 Kill an agent wherever it is
	 */
	public void kill(AID agentID, JADEPrincipal requesterPrincipal, Credentials requesterCredentials) throws NotFoundException, UnreachableException, JADESecurityException {
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.REQUEST_KILL, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(agentID);
		cmd.setPrincipal(requesterPrincipal);
		cmd.setCredentials(requesterCredentials);
		
		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 JADESecurityException) {
				throw (JADESecurityException)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());
			}
		}
	}
	
	/**
	 Suspend an agent wherever it is
	 */
	public void suspend(final AID agentID) throws NotFoundException, UnreachableException, JADESecurityException {
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.REQUEST_STATE_CHANGE, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(agentID);
		cmd.addParam(AgentState.getInstance(Agent.AP_SUSPENDED));
		
		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 JADESecurityException) {
				throw (JADESecurityException)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());
			}
		}
	}
	
	/**
	 Resume an agent wherever it is
	 */
	public void activate(final AID agentID) throws NotFoundException, UnreachableException, JADESecurityException {
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.REQUEST_STATE_CHANGE, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(agentID);
		cmd.addParam(AgentState.getInstance(Agent.AP_ACTIVE));
		
		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 JADESecurityException) {
				throw (JADESecurityException)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());
			}
		}
	}
	
	
	/**
	 Put an agent in the WAITING state wherever it is
	 */
	public void wait(AID agentID, String password) throws NotFoundException, UnreachableException {
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.REQUEST_STATE_CHANGE, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(agentID);
		cmd.addParam(AgentState.getInstance(Agent.AP_WAITING));
		
		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());
			}
		}
	}
	
	/**
	 Wake-up an agent wherever it is
	 */
	public void wake(AID agentID, String password) throws NotFoundException, UnreachableException {
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.REQUEST_STATE_CHANGE, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(agentID);
		cmd.addParam(AgentState.getInstance(Agent.AP_ACTIVE));
		
		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());
			}
		}
	}
	
	/**
	 Move an agent to a given destination
	 */
	public void move(AID agentID, Location where) throws NotFoundException, UnreachableException, JADESecurityException {
		
		ContainerID to = (ContainerID)where;
		
		// Just check whether the destination exists
		getDescriptor(to.getName());
		
		GenericCommand cmd = new GenericCommand(jade.core.mobility.AgentMobilityHelper.REQUEST_MOVE, jade.core.mobility.AgentMobilitySlice.NAME, null);
		cmd.addParam(agentID);
		cmd.addParam(where);
		
		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 JADESecurityException) {
				throw (JADESecurityException)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());
			}
		}
		
	}
	
	
	/**
	 Clone an agent to a given destination
	 */
	public void copy(AID agentID, Location where, String newName) throws NotFoundException, NameClashException, UnreachableException, JADESecurityException {
		ContainerID to = (ContainerID)where;
		
		// Just check whether the destination exists
		getDescriptor(to.getName());
		
		GenericCommand cmd = new GenericCommand(jade.core.mobility.AgentMobilityHelper.REQUEST_CLONE, jade.core.mobility.AgentMobilitySlice.NAME, null);
		cmd.addParam(agentID);
		cmd.addParam(where);
		cmd.addParam(newName);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if(ret != null) {
			if(ret instanceof NotFoundException) {
				throw (NotFoundException)ret;
			}
			else if(ret instanceof NameClashException) {
				throw (NameClashException)ret;
			}
			else if (ret instanceof IMTPException) {
				throw new UnreachableException("", (IMTPException) ret);
			}
			else if(ret instanceof JADESecurityException) {
				throw (JADESecurityException)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());
			}
		}
		
	}
	
	/** 
	 Kill a given container
	 */
	public void killContainer(ContainerID cid, JADEPrincipal requesterPrincipal, Credentials requesterCredentials) throws NotFoundException, UnreachableException, JADESecurityException {
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.KILL_CONTAINER, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(cid);
		cmd.setPrincipal(requesterPrincipal);
		cmd.setCredentials(requesterCredentials);
		
		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if(ret instanceof NotFoundException) {
				throw (NotFoundException)ret;
			}
			else if (ret instanceof JADESecurityException) {
				throw (JADESecurityException)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(ret.toString());
			}
		}
		
	}
	
	/**
	 Shut down the whole platform
	 **/
	public void shutdownPlatform(JADEPrincipal requesterPrincipal, Credentials requesterCredentials) throws JADESecurityException {
		
		if (myLogger.isLoggable(Logger.FINE)) {
			myLogger.log(Logger.FINE, "Shutting down agent platform.");
		}
		
		// FIXME: Here we probably need to issue a KILL_PLATFORM VCommand for security check.
		// In facts, even if the requester does not have the permission to kill the whole platform
		// auxiliary nodes are killed in any case
		
		// First kill all containers held by child nodes 
		int cnt = 0;
		ContainerID[] allContainers = containers.names();
		for(int i = 0; i < allContainers.length; i++) {
			ContainerID targetID = allContainers[i];
			NodeDescriptor dsc = myPlatformManager.getDescriptor(targetID.getName());

⌨️ 快捷键说明

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