agentcontainerimpl.java

来自「java实现的P2P多agent中间件」· Java 代码 · 共 1,225 行 · 第 1/3 页

JAVA
1,225
字号
		AgentState from = AgentState.getInstance(oldState);
		AgentState to = AgentState.getInstance(newState);

		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.INFORM_STATE_CHANGED, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(agentID);
		cmd.addParam(from);
		cmd.addParam(to);
		// No security check is meaningful on this action --> don't even set the Credentials

		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if (ret instanceof Throwable) {
				((Throwable) ret).printStackTrace();
			}
		}
	}

	public void handleEnd(AID agentID) {
		GenericCommand cmd = new GenericCommand(jade.core.management.AgentManagementSlice.INFORM_KILLED, jade.core.management.AgentManagementSlice.NAME, null);
		cmd.addParam(agentID);
		// Set the credentials of the terminating agent
		initCredentials(cmd, agentID);

		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if (ret instanceof Throwable) {
				((Throwable) ret).printStackTrace();
			}
		}
	}

	/* FIXME: Needed due to the Persistence Service being an add-on
	 public void handleSave(AID agentID, String repository) throws ServiceException, NotFoundException, IMTPException {
	 GenericCommand cmd = new GenericCommand("Save-Myself", "jade.core.persistence.Persistence", null);
	 cmd.addParam(agentID);
	 cmd.addParam(repository);
	 // Set the credentials of the agent to be saved
	  initCredentials(cmd, agentID);

	  Object ret = myCommandProcessor.processOutgoing(cmd);
	  if (ret != null) {
	  if (ret instanceof ServiceException) {
	  throw ((ServiceException) ret);
	  }
	  else if (ret instanceof NotFoundException) {
	  throw ((NotFoundException) ret);
	  }
	  else if (ret instanceof IMTPException) {
	  throw ((IMTPException) ret);
	  }
	  else if (ret instanceof Throwable) {
	  ((Throwable) ret).printStackTrace();
	  }
	  }
	  }*/

	// FIXME: Needed due to the Persistence Service being an add-on
	/*public void handleReload(AID agentID, String repository) throws ServiceException, NotFoundException, IMTPException {
	 GenericCommand cmd = new GenericCommand("Reload-Myself", "jade.core.persistence.Persistence", null);
	 cmd.addParam(agentID);
	 cmd.addParam(repository);
	 // Set the credentials of the agent to be reloaded
	  initCredentials(cmd, agentID);

	  Object ret = myCommandProcessor.processOutgoing(cmd);
	  if (ret != null) {
	  if (ret instanceof ServiceException) {
	  throw ((ServiceException) ret);
	  }
	  else if (ret instanceof NotFoundException) {
	  throw ((NotFoundException) ret);
	  }
	  else if (ret instanceof IMTPException) {
	  throw ((IMTPException) ret);
	  }
	  else if (ret instanceof Throwable) {
	  ((Throwable) ret).printStackTrace();
	  }
	  }
	  }*/

	// FIXME: Needed due to the Persistence Service being an add-on
	/*public void handleFreeze(AID agentID, String repository, ContainerID bufferContainer) throws ServiceException, NotFoundException, IMTPException {
	 GenericCommand cmd = new GenericCommand("Freeze-Myself", "jade.core.persistence.Persistence", null);
	 cmd.addParam(agentID);
	 cmd.addParam(repository);
	 cmd.addParam(bufferContainer);
	 // Set the credentials of the agent to be frozen
	  initCredentials(cmd, agentID);

	  Object ret = myCommandProcessor.processOutgoing(cmd);
	  if (ret != null) {
	  if (ret instanceof ServiceException) {
	  throw ((ServiceException) ret);
	  }
	  else if (ret instanceof NotFoundException) {
	  throw ((NotFoundException) ret);
	  }
	  else if (ret instanceof IMTPException) {
	  throw ((IMTPException) ret);
	  }
	  else if (ret instanceof Throwable) {
	  ((Throwable) ret).printStackTrace();
	  }
	  }
	  }*/

	public void setPlatformAddresses(AID id) {
		GenericCommand cmd = new GenericCommand(jade.core.messaging.MessagingSlice.SET_PLATFORM_ADDRESSES, jade.core.messaging.MessagingSlice.NAME, null);
		cmd.addParam(id);
		// No security check is meaningful on this action --> don't even set the Credentials

		Object ret = myCommandProcessor.processOutgoing(cmd);
		if (ret != null) {
			if (ret instanceof Throwable) {
				((Throwable) ret).printStackTrace();
			}
		}
	}

	public AID getAMS() {
		return (AID)theAMS.clone();
	}

	public AID getDefaultDF() {
		return (AID)theDefaultDF.clone();
	}

	public String getProperty(String key, String aDefault) {
		return myProfile.getParameter(key, aDefault);
	}

	//#MIDP_EXCLUDE_BEGIN
	public Properties getBootProperties(){
		return myProfile.getBootProperties();
	}
	//#MIDP_EXCLUDE_END

	public ServiceHelper getHelper(Agent a, String serviceName) throws ServiceException {
		try {

			// Retrieve the service
			Service s = myServiceFinder.findService(serviceName);
			if(s == null) {
				throw new ServiceNotActiveException(serviceName);
			}

			return s.getHelper( a );
		}
		catch (IMTPException imtpe) {
			throw new ServiceException(" ServiceHelper could not be created for: " + serviceName, imtpe);
		}
	}


	// Private and package scoped methods



	/**
	 */
	public String getPlatformID() {
		return AID.getPlatformID();
	}

	public Agent addLocalAgent(AID id, Agent a) {
		a.setToolkit(this);
		//#MIDP_EXCLUDE_BEGIN
		// Initialize the agent message queue after the toolkit is set and before the agent is inserted in the LADT
		a.initMessageQueue();
		//#MIDP_EXCLUDE_END
		return localAgents.put(id, a);
	}

	public void powerUpLocalAgent(AID agentID) throws NotFoundException {
		Agent instance = localAgents.acquire(agentID);
		if (instance == null) {
			throw new NotFoundException("powerUpLocalAgent() failed to find agent "+agentID.getName());
		}
		int type = (agentID.equals(theAMS) || agentID.equals(theDefaultDF) ? ResourceManager.SYSTEM_AGENTS : ResourceManager.USER_AGENTS);
		Thread t = myResourceManager.getThread(type, agentID.getLocalName(), instance);
		instance.powerUp(agentID, t);
		localAgents.release(agentID);
	}

	public void removeLocalAgent(AID id) {
		localAgents.remove(id);
	}

	public Agent acquireLocalAgent(AID id) {
		return localAgents.acquire(id);
	}

	public void releaseLocalAgent(AID id) {
		localAgents.release(id);
	}

	public boolean isLocalAgent(AID id) {
		return localAgents.contains(id);
	}
	
	public AID[] agentNames() {
		return localAgents.keys();
	}

	//#MIDP_EXCLUDE_BEGIN
	public void fillListFromMessageQueue(List messages, Agent a) {
		MessageQueue mq = a.getMessageQueue();
		synchronized(mq) {
			mq.copyTo(messages);
		}
	}
	//#MIDP_EXCLUDE_END

	//#MIDP_EXCLUDE_BEGIN
	public void fillListFromReadyBehaviours(List behaviours, Agent a) {

		Scheduler s = a.getScheduler();

		// (Mutual exclusion with Scheduler.add(), remove()...)
		synchronized (s) {
			Iterator it = s.readyBehaviours.iterator();
			while (it.hasNext()) {
				Behaviour b = (Behaviour) it.next();
				behaviours.add(new BehaviourID(b));
			}

		}
	}
	//#MIDP_EXCLUDE_END

	//#MIDP_EXCLUDE_BEGIN
	public void fillListFromBlockedBehaviours(List behaviours, Agent a) {

		Scheduler s = a.getScheduler();

		// (Mutual exclusion with Scheduler.add(), remove()...)
		synchronized (s) {
			Iterator it = s.blockedBehaviours.iterator();
			while (it.hasNext()) {
				Behaviour b = (Behaviour) it.next();
				behaviours.add(new BehaviourID(b));
			}
		}
	}
	//#MIDP_EXCLUDE_END

	//#MIDP_EXCLUDE_BEGIN
	/*public void commitMigration(Agent instance) {
	 instance.doGone();
	 localAgents.remove(instance.getAID());
	 }*/
	//#MIDP_EXCLUDE_END

	//#MIDP_EXCLUDE_BEGIN
	/*public void abortMigration(Agent instance) {
	 instance.doExecute();
	 }*/
	//#MIDP_EXCLUDE_END

	public void addAddressToLocalAgents(String address) {
		Agent[] allLocalAgents = localAgents.values();

		// Add the address to the AIDs of all local agents
		for(int j = 0; j < allLocalAgents.length; j++) {
			allLocalAgents[j].addPlatformAddress(address);
		}

		// Add the new addresses to the AMS and Default DF AIDs
		theAMS.addAddresses(address);
		theDefaultDF.addAddresses(address);
	}

	public void removeAddressFromLocalAgents(String address) {
		Agent[] allLocalAgents = localAgents.values();

		// Remove the address from the AIDs of all local agents
		for(int j = 0; j < allLocalAgents.length; j++) {
			allLocalAgents[j].removePlatformAddress(address);
		}

		// Remove the address from the AIDs of the AMS and the Default DF
		theAMS.removeAddresses(address);
		theDefaultDF.removeAddresses(address);
	}

	public boolean postMessageToLocalAgent(ACLMessage msg, AID receiverID) {
		Agent receiver = localAgents.acquire(receiverID);
		if(receiver == null) {
			return false;
		}
		receiver.postMessage(msg);
		localAgents.release(receiverID);

		return true;
	}

	public ContainerID getID() {
		return myID;
	}

	public MainContainer getMain() {
		//#MIDP_EXCLUDE_BEGIN
		return myMainContainer;
		//#MIDP_EXCLUDE_END
		/*#MIDP_INCLUDE_BEGIN
		 return null;
		 #MIDP_INCLUDE_END*/
	}

	public ServiceManager getServiceManager() {
		return myServiceManager;
	}

	public ServiceFinder getServiceFinder() {
		return myServiceFinder;
	}

	// Utility method to start a kernel service
	protected ServiceDescriptor startService(String name, boolean activateIt) throws ServiceException {

		try {
			Class svcClass = Class.forName(name);
			Service svc = (Service)svcClass.newInstance();
			svc.init(this, myProfile);
			ServiceDescriptor dsc = new ServiceDescriptor(svc.getName(), svc);

			if (activateIt) {
				myServiceManager.activateService(dsc);
				svc.boot(myProfile);
			}
			return dsc;
		}
		catch(ServiceException se) {
			// Let it through
			throw se;
		}
		catch(Throwable t) {
			throw new ServiceException("An error occurred during service activation", t);
		}
	}

	protected void stopService(String name) throws ServiceException {
		try {
			myServiceManager.deactivateService(name);
		}
		catch(ServiceException se) {
			// Let it through
			throw se;
		}
		catch(Throwable t) {
			throw new ServiceException("An error occurred during service deactivation", t);
		}
	}

	// GC-MODIFY-18022007-START
	//#MIDP_EXCLUDE_BEGIN
	public void becomeLeader(AMSEventQueueFeeder feeder) {
		try {
			myMainContainer.initSystemAgents(this, true);
			myMainContainer.startSystemAgents(this, feeder);
			myMainContainer.restartReplicatedAgents(this);
			myProfile.setParameter(Profile.LOCAL_SERVICE_MANAGER, "false");
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	//#MIDP_EXCLUDE_END
	// GC-MODIFY-18022007-END


	//#ALL_EXCLUDE_BEGIN
	//FIXME: These methods have been added to support
	// PlatformListener registration from the In-process-interface
	// with minimum effort. They will possibly be removed in a
	// future (more general) implementation
	public void addPlatformListener(AgentManager.Listener l) throws ClassCastException {
		AgentManager m = (AgentManager) myMainContainer;
		m.addListener(l);
	}

	public void removePlatformListener(AgentManager.Listener l) throws ClassCastException {
		AgentManager m = (AgentManager) myMainContainer;
		m.removeListener(l);
	}
	//#ALL_EXCLUDE_END

	private void initCredentials(Command cmd, AID id) {
		//#MIDP_EXCLUDE_BEGIN
		Agent agent = localAgents.acquire(id);
		if (agent != null) {
			try {
				CredentialsHelper ch = (CredentialsHelper) agent.getHelper("jade.core.security.Security");
				cmd.setPrincipal(ch.getPrincipal());
				cmd.setCredentials(ch.getCredentials());
			}
			catch (ServiceException se) {
				// The security plug-in is not there. Just ignore it
			}
		}
		localAgents.release(id);
		//#MIDP_EXCLUDE_END
	}

}

⌨️ 快捷键说明

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