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

📄 agentmanagementservice.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			myContainer.releaseLocalAgent(agentID);
		}
		
		private void changeAgentState(AID agentID, int newState) throws IMTPException, NotFoundException {
			Agent a = myContainer.acquireLocalAgent(agentID);
			
			if(a == null)
				throw new NotFoundException("Change-Agent-State failed to find " + agentID);
			
			if(newState == Agent.AP_SUSPENDED) {
				a.doSuspend();
			}
			else if(newState == Agent.AP_WAITING) {
				a.doWait();
			}
			else if(newState == Agent.AP_ACTIVE) {
				int oldState = a.getState();
				if(oldState == Agent.AP_SUSPENDED) {
					a.doActivate();
				}
				else {
					a.doWake();
				}
			}
			
			myContainer.releaseLocalAgent(agentID);
		}
		
		private void bornAgent(AID name, ContainerID cid, JADEPrincipal principal, Credentials credentials) throws NameClashException, NotFoundException {
			MainContainer impl = myContainer.getMain();
			if(impl != null) {
				// Retrieve the ownership from the credentials
				String ownership = "NONE";
				if (credentials != null) {
					JADEPrincipal ownerPr = credentials.getOwner();
					if (ownerPr != null) {
						ownership = ownerPr.getName();
					}
				}
				try {
					// If the name is already in the GADT, throws NameClashException
					impl.bornAgent(name, cid, principal, ownership, false);
				}
				catch(NameClashException nce) {
					//#CUSTOMJ2SE_EXCLUDE_BEGIN
					try {
						ContainerID oldCid = impl.getContainerID(name);
						if (oldCid != null) {
							Node n = impl.getContainerNode(oldCid).getNode();
							
							// Perform a non-blocking ping to check...
							n.ping(false);
							
							// Ping succeeded: rethrow the NameClashException
							throw nce;
						}
						else {
							// The old agent is registered with the AMS, but does not live in the platform --> cannot check if it still exists
							throw nce;
						}
					}
					catch(NameClashException nce2) {
						// This is the re-thrown NameClashException --> let it through
						throw nce2;
					}
					catch(Exception e) {
						// Either the old agent disappeared in the meanwhile or the Ping failed: forcibly replace the old agent...
						impl.bornAgent(name, cid, principal, ownership, true);
					}
					//#CUSTOMJ2SE_EXCLUDE_END
					/*#CUSTOMJ2SE_INCLUDE_BEGIN
					 try {
					 //System.out.println("Replacing old agent "+name.getName());
					  if(myLogger.isLoggable(Logger.INFO))
					  myLogger.log(Logger.INFO,"Replacing old agent "+name.getName());
					  dyingAgents.add(name);
					  ((jade.core.AgentManager) impl).kill(name, principal, credentials);
					  waitUntilDead(name);
					  impl.bornAgent(name, cid, principal, ownership, false);
					  }
					  catch (Exception e) {
					  dyingAgents.remove(name);
					  impl.bornAgent(name, cid, principal, ownership, true);
					  }
					  #CUSTOMJ2SE_INCLUDE_END*/
				}
			}
		}
		
		/*#CUSTOMJ2SE_INCLUDE_BEGIN
		 private jade.util.leap.List dyingAgents = new jade.util.leap.ArrayList();
		 
		 private void waitUntilDead(AID id) {
		 synchronized (dyingAgents) {
		 while (dyingAgents.contains(id)) {
		 try {
		 dyingAgents.wait();
		 }
		 catch (Exception e) {}
		 }
		 }
		 }
		 
		 private void notifyDead(AID id) {
		 synchronized (dyingAgents) {
		 dyingAgents.remove(id);
		 dyingAgents.notifyAll();
		 }
		 }
		 #CUSTOMJ2SE_INCLUDE_END*/
		
		private void deadAgent(AID name) throws NotFoundException {
			MainContainer impl = myContainer.getMain();
			if(impl != null) {
				impl.deadAgent(name, false);
				/*#CUSTOMJ2SE_INCLUDE_BEGIN
				 notifyDead(name);
				 #CUSTOMJ2SE_INCLUDE_END*/
			}
		}
		
		private void suspendedAgent(AID name) throws NotFoundException {
			MainContainer impl = myContainer.getMain();
			if(impl != null) {
				impl.suspendedAgent(name);
			}
		}
		
		private void resumedAgent(AID name) throws NotFoundException {
			MainContainer impl = myContainer.getMain();
			if(impl != null) {
				impl.resumedAgent(name);
			}
		}
		
		private void exitContainer() {
			myContainer.shutDown();
		}

		
	} // End of CommandTargetSink class
	
	
	
	/**
	 Inner mix-in class for this service: this class receives
	 commands from the service <code>Sink</code> and serves them,
	 coordinating with remote parts of this service through the
	 <code>Service.Slice</code> interface.
	 */
	private class ServiceComponent implements Service.Slice {
		
		// Implementation of the Service.Slice interface
		
		public Service getService() {
			return AgentManagementService.this;
		}
		
		public Node getNode() throws ServiceException {
			try {
				return AgentManagementService.this.getLocalNode();
			}
			catch(IMTPException imtpe) {
				throw new ServiceException("Problem in contacting the IMTP Manager", imtpe);
			}
		}
		
		public VerticalCommand serve(HorizontalCommand cmd) {
			VerticalCommand result = null;
			try {
				String cmdName = cmd.getName();
				Object[] params = cmd.getParams();
				
				if(cmdName.equals(AgentManagementSlice.H_CREATEAGENT)) {
					GenericCommand gCmd = new GenericCommand(AgentManagementSlice.REQUEST_CREATE, AgentManagementSlice.NAME, null);
					AID agentID = (AID)params[0];
					String className = (String)params[1];
					Object[] arguments = (Object[])params[2];
					JADEPrincipal owner = (JADEPrincipal)params[3];
					Credentials initialCredentials = (Credentials)params[4];
					Boolean startIt = (Boolean) params[5];
					gCmd.addParam(agentID);
					gCmd.addParam(className);
					gCmd.addParam(arguments);
					gCmd.addParam(owner);
					gCmd.addParam(initialCredentials);
					gCmd.addParam(startIt);
					
					result = gCmd;
				}
				else if(cmdName.equals(AgentManagementSlice.H_KILLAGENT)) {
					GenericCommand gCmd = new GenericCommand(AgentManagementSlice.REQUEST_KILL, AgentManagementSlice.NAME, null);
					AID agentID = (AID)params[0];
					gCmd.addParam(agentID);
					
					result = gCmd;
				}
				else if(cmdName.equals(AgentManagementSlice.H_CHANGEAGENTSTATE)) {
					GenericCommand gCmd = new GenericCommand(AgentManagementSlice.REQUEST_STATE_CHANGE, AgentManagementSlice.NAME, null);
					AID agentID = (AID)params[0];
					Integer newState = (Integer)params[1];
					gCmd.addParam(agentID);
					gCmd.addParam(newState);
					
					result = gCmd;
				}
				else if(cmdName.equals(AgentManagementSlice.H_BORNAGENT)) {
					GenericCommand gCmd = new GenericCommand(AgentManagementSlice.INFORM_CREATED, AgentManagementSlice.NAME, null);
					AID agentID = (AID)params[0];
					ContainerID cid = (ContainerID)params[1];
					gCmd.addParam(agentID);
					gCmd.addParam(cid);
					
					result = gCmd;
				}
				else if(cmdName.equals(AgentManagementSlice.H_DEADAGENT)) {
					GenericCommand gCmd = new GenericCommand(AgentManagementSlice.INFORM_KILLED, AgentManagementSlice.NAME, null);
					AID agentID = (AID)params[0];
					gCmd.addParam(agentID);
					
					result = gCmd;
				}
				else if(cmdName.equals(AgentManagementSlice.H_SUSPENDEDAGENT)) {
					GenericCommand gCmd = new GenericCommand(AgentManagementSlice.INFORM_STATE_CHANGED, AgentManagementSlice.NAME, null);
					AID agentID = (AID)params[0];
					gCmd.addParam(agentID);
					gCmd.addParam(jade.domain.FIPAAgentManagement.AMSAgentDescription.SUSPENDED);
					gCmd.addParam("*");
					
					result = gCmd;
				}
				else if(cmdName.equals(AgentManagementSlice.H_RESUMEDAGENT)) {
					GenericCommand gCmd = new GenericCommand(AgentManagementSlice.INFORM_STATE_CHANGED, AgentManagementSlice.NAME, null);
					AID agentID = (AID)params[0];
					gCmd.addParam(agentID);
					gCmd.addParam(jade.domain.FIPAAgentManagement.AMSAgentDescription.ACTIVE);
					gCmd.addParam(jade.domain.FIPAAgentManagement.AMSAgentDescription.SUSPENDED);
					
					result = gCmd;
				}
				else if(cmdName.equals(AgentManagementSlice.H_EXITCONTAINER)) {
					GenericCommand gCmd = new GenericCommand(AgentManagementSlice.KILL_CONTAINER, AgentManagementSlice.NAME, null);
					
					result = gCmd;
				}
				
			}
			catch(Throwable t) {
				cmd.setReturnValue(t);
			}
			return result;
		}
		
	} // End of AgentManagementSlice class
	
	
	
	private void initAgent(AID target, Agent instance, VerticalCommand vCmd) throws IMTPException, JADESecurityException, NameClashException, NotFoundException, ServiceException {
		// Connect the new instance to the local container
		Agent old = myContainer.addLocalAgent(target, instance);
		if (instance == old) {
			// This is a re-addition of an existing agent to a recovered main container (FaultRecoveryService)
			old = null;
		}
		
		try {
			// Notify the main container through its slice
			AgentManagementSlice mainSlice = (AgentManagementSlice)getSlice(MAIN_SLICE);
			
			// We propagate the class-name to the main, but we don't want to keep it in the actual agent AID. 
			AID cloned = (AID) target.clone();
			cloned.addUserDefinedSlot(AID.AGENT_CLASSNAME, instance.getClass().getName());
			try {
				mainSlice.bornAgent(cloned, myContainer.getID(), vCmd);
			}
			catch(IMTPException imtpe) {
				// Try to get a newer slice and repeat...
				mainSlice = (AgentManagementSlice)getFreshSlice(MAIN_SLICE);
				mainSlice.bornAgent(cloned, myContainer.getID(), vCmd);
			}
			customize(instance);
		}
		catch(NameClashException nce) {
			removeLocalAgent(target);
			if(old != null) {
				myContainer.addLocalAgent(target, old);
			}
			throw nce;
		}
		catch(IMTPException imtpe) {
			removeLocalAgent(target);
			throw imtpe;
		}
		catch(NotFoundException nfe) {
			removeLocalAgent(target);
			throw nfe;
		}
		catch(JADESecurityException ae) {
			removeLocalAgent(target);
			throw ae;
		}
	}
	
	private void customize(Agent agent) {
		try {
			Behaviour amfServer = (Behaviour) Class.forName("jade.amf.AttributeManagementServer").newInstance();
			agent.addBehaviour(amfServer);
		}
		catch (Exception e) {
			// The AMF code is not in the classpath --> Just do nothing
		}
	}
	
	
	// The concrete agent container, providing access to LADT, etc.
	private AgentContainer myContainer;
	
	// The local slice for this service
	private final ServiceComponent localSlice = new ServiceComponent();
	
	// The command sink, source side
	private final CommandSourceSink senderSink = new CommandSourceSink();
	
	// The command sink, target side
	private final CommandTargetSink receiverSink = new CommandTargetSink();
	
	//#J2ME_EXCLUDE_BEGIN
	private String agentsPath = null;
	private CodeLocator codeLocator;
	//#J2ME_EXCLUDE_END
	
	// Work-around for PJAVA compilation
	protected Service.Slice getFreshSlice(String name) throws ServiceException {
		return super.getFreshSlice(name);
	}
}

⌨️ 快捷键说明

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