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

📄 notificationservice.java

📁 java实现的P2P多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.core.event;

//#J2ME_EXCLUDE_FILE

import jade.core.HorizontalCommand;
import jade.core.VerticalCommand;
import jade.core.Service;
import jade.core.BaseService;
import jade.core.Sink;
import jade.core.Filter;
import jade.core.Node;

import jade.core.Agent;
import jade.core.AgentContainer;
import jade.core.MainContainer;
import jade.core.AID;
import jade.core.ContainerID;
import jade.core.BehaviourID;
import jade.core.AgentState;
import jade.core.Channel;
import jade.core.Profile;
import jade.core.ProfileException;
import jade.core.ServiceException;
import jade.core.IMTPException;
import jade.core.NotFoundException;
import jade.core.ServiceHelper;

import jade.core.messaging.GenericMessage;

import jade.core.behaviours.Behaviour;

import jade.lang.acl.ACLMessage;
import jade.security.JADEPrincipal;
import jade.tools.ToolNotifier;

import jade.util.SynchList;
import jade.util.leap.Iterator;
import jade.util.leap.Map;
import jade.util.leap.HashMap;
import jade.util.leap.List;
import jade.util.leap.LinkedList;

/**
 
 The JADE service to manage the event notification subsystem installed
 on the platform.
 
 @author Giovanni Rimassa - FRAMeTech s.r.l.
 
 */
public class NotificationService extends BaseService {
	/**
	 The name of this service.
	 */
	public static final String NAME = "jade.core.event.Notification";
	
	private static final String[] OWNED_COMMANDS = new String[] {
		NotificationSlice.SNIFF_ON,
		NotificationSlice.SNIFF_OFF,
		NotificationSlice.DEBUG_ON,
		NotificationSlice.DEBUG_OFF,
		NotificationSlice.NOTIFY_POSTED,
		NotificationSlice.NOTIFY_RECEIVED,
		NotificationSlice.NOTIFY_CHANGED_AGENT_STATE,
		NotificationSlice.NOTIFY_CHANGED_AGENT_PRINCIPAL,
		NotificationSlice.NOTIFY_BEHAVIOUR_ADDED,
		NotificationSlice.NOTIFY_BEHAVIOUR_REMOVED,
		NotificationSlice.NOTIFY_CHANGED_BEHAVIOUR_STATE	
	};
	
	
	// The special name of an auxiliary thread used to avoid deadlock when debugging the AMS
	private final static String AMS_DEBUG_HELPER = "AMS-debug-helper";
	
	// The concrete agent container, providing access to LADT, etc.
	private AgentContainer myContainer;
	
	// The local slice for this service
	private ServiceComponent localSlice;
	private Sink sourceSink = new NotificationSourceSink();
	private Filter outgoingFilter = new NotificationOutgoingFilter();
	private Filter incomingFilter = new NotificationIncomingFilter();
	private NotificationHelper helper = new NotificationHelperImpl();
	
	// The list of all listeners of ACL messaging related events (uses RW-locking)
	private SynchList messageListeners = new SynchList();
	
	// The list of all listeners of agent life cycle events (uses RW-locking)
	private SynchList agentListeners = new SynchList();
	
	// The list of all listeners of container events (uses RW-locking)
	private SynchList containerListeners = new SynchList();
	
	// This maps a debugged agent into the list of debuggers that are 
	// currently debugging it. It is used to know when an agent is no longer
	// debugged by any debugger and behaviour event generation can be turned off.
	private Map debuggers = new HashMap();
	
	
	public void init(AgentContainer ac, Profile p) throws ProfileException {
		super.init(ac, p);		
		myContainer = ac;
		
		// Create a local slice
		localSlice = new ServiceComponent();	
	}
	
	public String getName() {
		return NotificationSlice.NAME;
	}
	
	public Class getHorizontalInterface() {
		return NotificationSlice.class;
	}
	
	public Slice getLocalSlice() {
		return localSlice;
	}
	
	public Filter getCommandFilter(boolean direction) {
		if (direction == Filter.OUTGOING) {
			return outgoingFilter;
		}
		else {
			return incomingFilter;
		}
	}
	
	public Sink getCommandSink(boolean side) {
		if (side == Sink.COMMAND_SOURCE) {
			return sourceSink;
		}
		else {
			return null;
		}
	}
	
	public ServiceHelper getHelper(Agent a) throws ServiceException {
		return helper;
	}
	
	public String[] getOwnedCommands() {
		return OWNED_COMMANDS;
	}

	
	/**
	 * Inner class NotificationSourceSink
	 */
	private class NotificationSourceSink implements Sink {
	    public void consume(VerticalCommand cmd) {
			try {
				String name = cmd.getName();
				if(name.equals(NotificationSlice.SNIFF_ON)) {
					handleSniffOn(cmd);
				}
				if(name.equals(NotificationSlice.SNIFF_OFF)) {
					handleSniffOff(cmd);
				}
				else if(name.equals(NotificationSlice.DEBUG_ON)) {
					handleDebugOn(cmd);
				}
				else if(name.equals(NotificationSlice.DEBUG_OFF)) {
					handleDebugOff(cmd);
				}
				else if(name.equals(NotificationSlice.NOTIFY_POSTED)) {
					handleNotifyPosted(cmd);
				}
				else if(name.equals(NotificationSlice.NOTIFY_RECEIVED)) {
					handleNotifyReceived(cmd);
				}
				else if(name.equals(NotificationSlice.NOTIFY_CHANGED_AGENT_PRINCIPAL)) {
					handleNotifyChangedAgentPrincipal(cmd);
				}
				else if(name.equals(NotificationSlice.NOTIFY_BEHAVIOUR_ADDED)) {
					handleNotifyAddedBehaviour(cmd);
				}
				else if(name.equals(NotificationSlice.NOTIFY_BEHAVIOUR_REMOVED)) {
					handleNotifyRemovedBehaviour(cmd);
				}
				else if(name.equals(NotificationSlice.NOTIFY_CHANGED_BEHAVIOUR_STATE)) {
					handleNotifyChangedBehaviourState(cmd);
				}
			}
			catch(Throwable t) {
				cmd.setReturnValue(t);
			}
		}
				
		private void handleSniffOn(VerticalCommand cmd) throws IMTPException, ServiceException, NotFoundException {
			Object[] params = cmd.getParams();
			AID sniffer = (AID)params[0];
			List targets = (List)params[1];
			
			MainContainer impl = myContainer.getMain();
			if(impl != null) {		
				// Activate sniffing each element of the list
				Iterator it = targets.iterator();
				while(it.hasNext()) {
					AID target = (AID)it.next();
					ContainerID cid = impl.getContainerID(target);
					
					NotificationSlice slice = (NotificationSlice)getSlice(cid.getName());
					try {
						slice.sniffOn(sniffer, target);
					}
					catch(IMTPException imtpe) {
						// Try to get a newer slice and repeat...
						slice = (NotificationSlice)getFreshSlice(cid.getName());
						slice.sniffOn(sniffer, target);
					}
				}
			}
			else {
				// Do nothing for now, but could also route the command to the main slice, thus enabling e.g. AMS replication
			}
		}
		
		private void handleSniffOff(VerticalCommand cmd) throws IMTPException, ServiceException, NotFoundException {
			Object[] params = cmd.getParams();
			AID sniffer = (AID)params[0];
			List targets = (List)params[1];
			
			MainContainer impl = myContainer.getMain();
			if(impl != null) {
				// Deactivate sniffing each element of the list
				Iterator it = targets.iterator();
				while(it.hasNext()) {
					AID target = (AID)it.next();
					ContainerID cid = impl.getContainerID(target);
					
					NotificationSlice slice = (NotificationSlice)getSlice(cid.getName());
					try {
						slice.sniffOff(sniffer, target);
					}
					catch(IMTPException imtpe) {
						// Try to get a newer slice and repeat...
						slice = (NotificationSlice)getFreshSlice(cid.getName());
						slice.sniffOff(sniffer, target);
					}
				}
			}
			else {
				// Do nothing for now, but could also route the command to the main slice, thus enabling e.g. AMS replication
			}
		}
		
		private void handleDebugOn(VerticalCommand cmd) throws IMTPException, ServiceException, NotFoundException {
			Object[] params = cmd.getParams();
			AID introspector = (AID)params[0];
			List targets = (List)params[1];
			
			MainContainer impl = myContainer.getMain();
			if(impl != null) {
				// Activate debugging each element of the list
				Iterator it = targets.iterator();
				while(it.hasNext()) {
					AID target = (AID)it.next();
					ContainerID cid = impl.getContainerID(target);
					
					NotificationSlice slice = (NotificationSlice)getSlice(cid.getName());
					try {
						slice.debugOn(introspector, target);
					}
					catch(IMTPException imtpe) {
						// Try to get a newer slice and repeat...
						slice = (NotificationSlice)getFreshSlice(cid.getName());
						slice.debugOn(introspector, target);
					}
				}
			}
			else {
				// Do nothing for now, but could also route the command to the main slice, thus enabling e.g. AMS replication
			}
		}
		
		private void handleDebugOff(VerticalCommand cmd) throws IMTPException, ServiceException, NotFoundException {
			Object[] params = cmd.getParams();
			AID introspector = (AID)params[0];
			List targets = (List)params[1];
			
			MainContainer impl = myContainer.getMain();
			if(impl != null) {
				// Deactivate debugging each element of the list
				Iterator it = targets.iterator();
				while(it.hasNext()) {
					AID target = (AID)it.next();
					ContainerID cid = impl.getContainerID(target);
					
					NotificationSlice slice = (NotificationSlice)getSlice(cid.getName());
					try {
						slice.debugOff(introspector, target);
					}
					catch(IMTPException imtpe) {
						// Try to get a newer slice and repeat...
						slice = (NotificationSlice)getFreshSlice(cid.getName());
						slice.debugOff(introspector, target);
					}

⌨️ 快捷键说明

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