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

📄 rma.java

📁 JADE(JAVA Agent开发框架)是一个完全由JAVA语言开发的软件,它简化了多Agent系统的实现。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************
 JADE - Java Agent DEvelopment Framework is a framework to develop
 multi-agent systems in compliance with the FIPA specifications.
 Copyright (C) 2000 CSELT S.p.A.
 
 GNU Lesser General Public License
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation,
 version 2.1 of the License.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the
 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA  02111-1307, USA.
 *****************************************************************/

package jade.tools.rma;

import java.io.StringReader;
import java.io.StringWriter;
import java.io.BufferedReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.InputStreamReader;

import jade.util.leap.List;
import jade.util.leap.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import jade.util.leap.Iterator;
import java.net.URL;
import jade.util.Logger;

import jade.core.*;
import jade.core.behaviours.*;

import jade.domain.FIPAAgentManagement.*;
import jade.domain.JADEAgentManagement.*;
import jade.domain.introspection.*;
import jade.domain.persistence.*;
import jade.domain.mobility.*;
import jade.domain.FIPANames;
import jade.gui.AgentTreeModel;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import jade.content.onto.basic.Result;
import jade.content.onto.basic.Action;

import jade.proto.SimpleAchieveREInitiator;

import jade.tools.ToolAgent;
import jade.security.JADEPrincipal;
import jade.security.SDSIName;


/**
 <em>Remote Management Agent</em> agent. This class implements
 <b>JADE</b> <em>RMA</em> agent. <b>JADE</b> applications cannot use
 this class directly, but interact with it through <em>ACL</em>
 message passing. Besides, this agent has a <em>GUI</em> through
 which <b>JADE</b> Agent Platform can be administered.
 
 
 @author Giovanni Rimassa - Universita' di Parma
 @version $Date: 2006-08-30 10:57:51 +0200 (mer, 30 ago 2006) $ $Revision: 5893 $
 
 */
public class rma extends ToolAgent {
	
	private APDescription myPlatformProfile;
	
	// Sends requests to the AMS
	private class AMSClientBehaviour extends SimpleAchieveREInitiator {
		
		private String actionName;
		
		
		public AMSClientBehaviour(String an, ACLMessage request) {
			super(rma.this, request);
			actionName = an;
		}
		
		
		protected void handleNotUnderstood(ACLMessage reply) {
			myGUI.showErrorDialog("NOT-UNDERSTOOD received by RMA during " + actionName, reply);
		}
		
		protected void handleRefuse(ACLMessage reply) {
			myGUI.showErrorDialog("REFUSE received during " + actionName, reply);
		}
		
		protected void handleAgree(ACLMessage reply) {
			if(logger.isLoggable(Logger.FINE))
				logger.log(Logger.FINE,"AGREE received"+reply);
		}
		
		protected void handleFailure(ACLMessage reply) {
			myGUI.showErrorDialog("FAILURE received during " + actionName, reply);
		}
		
		protected void handleInform(ACLMessage reply) {
			if(logger.isLoggable(Logger.FINE))
				logger.log(Logger.FINE,"INFORM received"+reply);
		}
		
	} // End of AMSClientBehaviour class
	
	
	private class handleAddRemotePlatformBehaviour extends AMSClientBehaviour{
		
		public handleAddRemotePlatformBehaviour(String an, ACLMessage request){
			super(an,request);
			
		}
		
		protected void handleInform(ACLMessage msg){
			if(logger.isLoggable(Logger.FINE))
				logger.log(Logger.FINE,"arrived a new APDescription");
			try{
				AID sender = msg.getSender();
				Result r =(Result)getContentManager().extractContent(msg);
				
				Iterator i = r.getItems().iterator();
				APDescription APDesc = (APDescription)i.next();
				if(APDesc != null){
					myGUI.addRemotePlatformFolder();
					myGUI.addRemotePlatform(sender,APDesc);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
	}//end handleAddRemotePlatformBehaviour
	
	private class handleRefreshRemoteAgentBehaviour extends AMSClientBehaviour{
		
		private APDescription platform;
		
		public handleRefreshRemoteAgentBehaviour(String an, ACLMessage request,APDescription ap){
			super(an,request);
			platform = ap;
			
		}
		
		protected void handleInform(ACLMessage msg){
			if(logger.isLoggable(Logger.FINE))
				logger.log(Logger.FINE,"arrived a new agents from a remote platform");
			try{
				AID sender = msg.getSender();
				Result r = (Result)getContentManager().extractContent(msg);
				Iterator i = r.getItems().iterator();
				myGUI.addRemoteAgentsToRemotePlatform(platform,i);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
	}//end handleAddRemotePlatformBehaviour
	
	
	private SequentialBehaviour AMSSubscribe = new SequentialBehaviour();
	
	private transient MainWindow myGUI;
	
	private String myContainerName;
	
	class RMAAMSListenerBehaviour extends AMSListenerBehaviour {
		protected void installHandlers(Map handlersTable) {
			
			// Fill the event handler table.
			
			handlersTable.put(IntrospectionVocabulary.META_RESETEVENTS, new EventHandler() {
				public void handle(Event ev) {
					ResetEvents re = (ResetEvents)ev;
					myGUI.resetTree();
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.ADDEDCONTAINER, new EventHandler() {
				public void handle(Event ev) {
					AddedContainer ac = (AddedContainer)ev;
					ContainerID cid = ac.getContainer();
					String name = cid.getName();
					String address = cid.getAddress();
					try {
						InetAddress addr = InetAddress.getByName(address);
						myGUI.addContainer(name, addr);
					}
					catch(UnknownHostException uhe) {
						myGUI.addContainer(name, null);
					}
				}
			});
			
			
			handlersTable.put(IntrospectionVocabulary.REMOVEDCONTAINER, new EventHandler() {
				public void handle(Event ev) {
					RemovedContainer rc = (RemovedContainer)ev;
					ContainerID cid = rc.getContainer();
					String name = cid.getName();
					myGUI.removeContainer(name);
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.BORNAGENT, new EventHandler() {
				public void handle(Event ev) {
					BornAgent ba = (BornAgent)ev;
					ContainerID cid = ba.getWhere();
					String container = cid.getName();
					AID agent = ba.getAgent();
					myGUI.addAgent(container, agent, ba.getState(), ba.getOwnership());
					if (agent.equals(getAID()))
						myContainerName = container;
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.DEADAGENT, new EventHandler() {
				public void handle(Event ev) {
					DeadAgent da = (DeadAgent)ev;
					ContainerID cid = da.getWhere();
					String container = cid.getName();
					AID agent = da.getAgent();
					myGUI.removeAgent(container, agent);
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.SUSPENDEDAGENT, new EventHandler() {
				public void handle(Event ev) {
					SuspendedAgent sa = (SuspendedAgent)ev;
					ContainerID cid = sa.getWhere();
					String container = cid.getName();
					AID agent = sa.getAgent();
					myGUI.modifyAgent(container, agent, AMSAgentDescription.SUSPENDED, null);
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.RESUMEDAGENT, new EventHandler() {
				public void handle(Event ev) {
					ResumedAgent ra = (ResumedAgent)ev;
					ContainerID cid = ra.getWhere();
					String container = cid.getName();
					AID agent = ra.getAgent();
					myGUI.modifyAgent(container, agent, AMSAgentDescription.ACTIVE, null);
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.FROZENAGENT, new EventHandler() {
				public void handle(Event ev) {
					FrozenAgent fa = (FrozenAgent)ev;
					String oldContainer = fa.getWhere().getName();
					String newContainer = fa.getBufferContainer().getName();
					AID agent = fa.getAgent();
					myGUI.modifyFrozenAgent(oldContainer, newContainer, agent);
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.THAWEDAGENT, new EventHandler() {
				public void handle(Event ev) {
					ThawedAgent ta = (ThawedAgent)ev;
					String oldContainer = ta.getWhere().getName();
					String newContainer = ta.getBufferContainer().getName();
					AID agent = ta.getAgent();
					myGUI.modifyThawedAgent(oldContainer, newContainer, agent);
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.CHANGEDAGENTOWNERSHIP, new EventHandler() {
				public void handle(Event ev) {
					ChangedAgentOwnership cao = (ChangedAgentOwnership)ev;
					ContainerID cid = cao.getWhere();
					String container = cid.getName();
					AID agent = cao.getAgent();
					myGUI.modifyAgent(container, agent, null, cao.getTo());
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.MOVEDAGENT, new EventHandler() {
				public void handle(Event ev) {
					MovedAgent ma = (MovedAgent)ev;
					AID agent = ma.getAgent();
					ContainerID from = ma.getFrom();
					ContainerID to = ma.getTo();
					myGUI.moveAgent(from.getName(), to.getName(), agent);
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.ADDEDMTP, new EventHandler() {
				public void handle(Event ev) {
					AddedMTP amtp = (AddedMTP)ev;
					String address = amtp.getAddress();
					ContainerID where = amtp.getWhere();
					myGUI.addAddress(address, where.getName());
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.REMOVEDMTP, new EventHandler() {
				public void handle(Event ev) {
					RemovedMTP rmtp = (RemovedMTP)ev;
					String address = rmtp.getAddress();
					ContainerID where = rmtp.getWhere();
					myGUI.removeAddress(address, where.getName());
				}
			});
			
			//handle the APDescription provided by the AMS
			handlersTable.put(IntrospectionVocabulary.PLATFORMDESCRIPTION, new EventHandler(){
				public void handle(Event ev){
					PlatformDescription pd = (PlatformDescription)ev;
					APDescription APdesc = pd.getPlatform();
					myPlatformProfile = APdesc;
					myGUI.refreshLocalPlatformName(myPlatformProfile.getName());
				}
			});
			
		}
	} // END of inner class RMAAMSListenerBehaviour
	
	
	/**
	 This method starts the <em>RMA</em> behaviours to allow the agent
	 to carry on its duties within <em><b>JADE</b></em> agent platform.
	 */
	protected void toolSetup() {
		logger = Logger.getMyLogger(getName());
		
		// Register the supported ontologies
		getContentManager().registerOntology(MobilityOntology.getInstance());
		getContentManager().registerOntology(PersistenceOntology.getInstance());
		
		// Send 'subscribe' message to the AMS
		AMSSubscribe.addSubBehaviour(new SenderBehaviour(this, getSubscribe()));
		
		// Handle incoming 'inform' messages
		AMSSubscribe.addSubBehaviour(new RMAAMSListenerBehaviour());
		
		// Schedule Behaviour for execution
		addBehaviour(AMSSubscribe);
		
		// Show Graphical User Interface
		myGUI = new MainWindow(this);
		myGUI.ShowCorrect();	
	}
	
	/**
	 Cleanup during agent shutdown. This method cleans things up when
	 <em>RMA</em> agent is destroyed, disconnecting from <em>AMS</em>
	 agent and closing down the platform administration <em>GUI</em>.
	 */
	protected void toolTakeDown() {
		send(getCancel());
		if (myGUI != null) {
			// The following call was removed as it causes a threading
			// deadlock with join. Its also not needed as the async
			// dispose will do it.
			// myGUI.setVisible(false);
			myGUI.disposeAsync();
		}
	}
	
	protected void beforeMove() {
		super.beforeMove();
		
		myGUI.disposeAsync();
		send(getCancel());
	}
	
	protected void afterMove() {

⌨️ 快捷键说明

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