persistentdeliveryservice.java

来自「JADE(JAVA Agent开发框架)是一个完全由JAVA语言开发的软件,它简」· Java 代码 · 共 680 行 · 第 1/2 页

JAVA
680
字号
/*****************************************************************
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.messaging;

//#J2ME_EXCLUDE_FILE

import java.io.IOException;

import jade.core.ServiceFinder;

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

import jade.core.AgentContainer;
import jade.core.AID;
import jade.core.Profile;
import jade.core.ProfileException;
import jade.core.IMTPException;

import jade.lang.acl.ACLMessage;
import jade.domain.FIPAAgentManagement.Envelope;

import jade.util.Logger;



/**

   The JADE service to manage the persistent storage of undelivered
   ACL messages installed on the platform.

   @author Giovanni Rimassa - FRAMeTech s.r.l.

*/
public class PersistentDeliveryService extends BaseService {
  /**
     This constant is the name of the property whose value contains
     the name of the application-specific class that will be used by the
     PersistentDeliveryService on the local container as a filter for 
     undelivered ACL messages
  */
  public static final String PERSISTENT_DELIVERY_FILTER = "persistent-delivery-filter";

  /**
     This constant is the name of the property whose value contains an
     integer representing how often (in milliseconds) the 
     PersistentDeliveryService will try to
     send again previously undelivered ACL messages which have been
     buffered.
  */
  public static final String PERSISTENT_DELIVERY_SENDFAILUREPERIOD = "persistent-delivery-sendfailureperiod";

  /**
     This constant is the name of the property whose value contains
     the storage method used to persist undelivered ACL messages by
     the PersistentDeliveryService on the local container.
     The supported values for this parameter are:
     <ul>
     <li><b>file</b> - A directory tree on the local filesystem is used.</li>
     </ul>
     If this property is not specified undelivered ACL messages are
     kept in memory and not persisted at all.
  */
  public static final String PERSISTENT_DELIVERY_STORAGEMETHOD = "persistent-delivery-storagemethod";

  /**
     This constant is the name of the property whose value contains
     the root of the directory tree that is used to persist
     undelivered ACL messages when the <i>file</i> storage
     method is selected.
  */
  public static final String PERSISTENT_DELIVERY_BASEDIR = "persistent-delivery-basedir";


	static final String ACL_USERDEF_DUE_DATE = "JADE-persistentdelivery-duedate";

    private static final String[] OWNED_COMMANDS = new String[] {
	/*PersistentDeliverySlice.ACTIVATE_MESSAGE_STORE,
	PersistentDeliverySlice.DEACTIVATE_MESSAGE_STORE,
	PersistentDeliverySlice.REGISTER_MESSAGE_TEMPLATE,
	PersistentDeliverySlice.DEREGISTER_MESSAGE_TEMPLATE*/
    };



    public void init(AgentContainer ac, Profile p) throws ProfileException {
		super.init(ac, p);
		myContainer = ac;
		myServiceFinder = myContainer.getServiceFinder();
    }
    
    public String getName() {
	return PersistentDeliverySlice.NAME;
    }

    public Class getHorizontalInterface() {
	try {
	    return Class.forName(PersistentDeliverySlice.NAME + "Slice");
	}
	catch(ClassNotFoundException cnfe) {
	    return null;
	}
    }

    public Service.Slice getLocalSlice() {
	return localSlice;
    }

    public Filter getCommandFilter(boolean direction) {
	if(direction == Filter.INCOMING) {
	    return inFilter;
	}
	else {
	    return outFilter;
	}
    }

    public Sink getCommandSink(boolean side) {
	/*if(side == Sink.COMMAND_SOURCE) {
	    return senderSink;
	}
	else {
	    return receiverSink;
	}*/
	return null;
    }

    public String[] getOwnedCommands() {
	return OWNED_COMMANDS;
    }

/*
    // This inner class handles the messaging commands on the command
    // issuer side, turning them into horizontal commands and
    // forwarding them to remote slices when necessary.
    private class CommandSourceSink implements Sink {

	// Implementation of the Sink interface

	public void consume(VerticalCommand cmd) {

	    try {
		String name = cmd.getName();
		if(name.equals(PersistentDeliverySlice.ACTIVATE_MESSAGE_STORE)) {
		    handleActivateMessageStore(cmd);
		}
		if(name.equals(PersistentDeliverySlice.DEACTIVATE_MESSAGE_STORE)) {
		    handleDeactivateMessageStore(cmd);
		}
		else if(name.equals(PersistentDeliverySlice.REGISTER_MESSAGE_TEMPLATE)) {
		    handleRegisterMessageTemplate(cmd);
		}
		else if(name.equals(PersistentDeliverySlice.DEREGISTER_MESSAGE_TEMPLATE)) {
		    handleDeregisterMessageTemplate(cmd);
		}
	    }
	    catch(IMTPException imtpe) {
		cmd.setReturnValue(imtpe);
	    }
	    catch(NotFoundException nfe) {
		cmd.setReturnValue(nfe);
	    }
	    catch(NameClashException nce) {
		cmd.setReturnValue(nce);
	    }
	    catch(ServiceException se) {
		cmd.setReturnValue(new IMTPException("A Service Exception occurred", se));
	    }
	}


	// Vertical command handler methods

	private void handleActivateMessageStore(VerticalCommand cmd) throws IMTPException, ServiceException, NameClashException {
	    Object[] params = cmd.getParams();
	    String sliceName = (String)params[0];
	    String storeName = (String)params[1];

	    PersistentDeliverySlice targetSlice = (PersistentDeliverySlice)getSlice(sliceName);
	    try {
		targetSlice.activateMsgStore(storeName);
	    }
	    catch(IMTPException imtpe) {
		targetSlice = (PersistentDeliverySlice)getFreshSlice(sliceName);
		targetSlice.activateMsgStore(storeName);
	    }

	}

	private void handleDeactivateMessageStore(VerticalCommand cmd) throws IMTPException, ServiceException, NotFoundException {
	    Object[] params = cmd.getParams();
	    String sliceName = (String)params[0];
	    String storeName = (String)params[1];

	    PersistentDeliverySlice targetSlice = (PersistentDeliverySlice)getSlice(sliceName);
	    try {
		targetSlice.deactivateMsgStore(storeName);
	    }
	    catch(IMTPException imtpe) {
		targetSlice = (PersistentDeliverySlice)getFreshSlice(sliceName);
		targetSlice.deactivateMsgStore(storeName);
	    }

	}

	private void handleRegisterMessageTemplate(VerticalCommand cmd) throws IMTPException, ServiceException, NotFoundException, NameClashException {
	    Object[] params = cmd.getParams();
	    String sliceName = (String)params[0];
	    String storeName = (String)params[1];
	    MessageTemplate mt = (MessageTemplate)params[2];

	    PersistentDeliverySlice targetSlice = (PersistentDeliverySlice)getSlice(sliceName);
	    try {
		targetSlice.registerTemplate(storeName, mt);
	    }
	    catch(IMTPException imtpe) {
		targetSlice = (PersistentDeliverySlice)getFreshSlice(sliceName);
		targetSlice.registerTemplate(storeName, mt);
	    }

	}

	private void handleDeregisterMessageTemplate(VerticalCommand cmd) throws IMTPException, ServiceException, NotFoundException {
	    Object[] params = cmd.getParams();
	    String sliceName = (String)params[0];
	    String storeName = (String)params[1];
	    MessageTemplate mt = (MessageTemplate)params[2];

	    PersistentDeliverySlice targetSlice = (PersistentDeliverySlice)getSlice(sliceName);
	    try {
		targetSlice.deregisterTemplate(storeName, mt);
	    }
	    catch(IMTPException imtpe) {
		targetSlice = (PersistentDeliverySlice)getFreshSlice(sliceName);
		targetSlice.deregisterTemplate(storeName, mt);
	    }
	}

    } // End of CommandSourceSink class


    private class CommandTargetSink implements Sink {

	// Implementation of the Sink interface

	public void consume(VerticalCommand cmd) {

		String name = cmd.getName();
		if(name.equals(PersistentDeliverySlice.ACTIVATE_MESSAGE_STORE)) {
		    handleActivateMessageStore(cmd);
		}
		if(name.equals(PersistentDeliverySlice.DEACTIVATE_MESSAGE_STORE)) {
		    handleDeactivateMessageStore(cmd);
		}
		else if(name.equals(PersistentDeliverySlice.REGISTER_MESSAGE_TEMPLATE)) {
		    handleRegisterMessageTemplate(cmd);
		}
		else if(name.equals(PersistentDeliverySlice.DEREGISTER_MESSAGE_TEMPLATE)) {
		    handleDeregisterMessageTemplate(cmd);
		}
	}


	// Vertical command handler methods

	private void handleActivateMessageStore(VerticalCommand cmd) {
	    Object[] params = cmd.getParams();
	    String sliceName = (String)params[0];
	    String storeName = (String)params[1];

	    System.out.println("--- ACTIVATE_MESSAGE_STORE: Not Implemented ---");

	}

	private void handleDeactivateMessageStore(VerticalCommand cmd) {
	    Object[] params = cmd.getParams();
	    String sliceName = (String)params[0];
	    String storeName = (String)params[1];

	    System.out.println("--- DEACTIVATE_MESSAGE_STORE: Not Implemented ---");

	}

	private void handleRegisterMessageTemplate(VerticalCommand cmd) {
	    Object[] params = cmd.getParams();
	    String sliceName = (String)params[0];
	    String storeName = (String)params[1];
	    MessageTemplate mt = (MessageTemplate)params[2];

	    System.out.println("--- REGISTER_MESSAGE_TEMPLATE: Not Implemented ---");

	}

	private void handleDeregisterMessageTemplate(VerticalCommand cmd) {
	    Object[] params = cmd.getParams();
	    String sliceName = (String)params[0];
	    String storeName = (String)params[1];
	    MessageTemplate mt = (MessageTemplate)params[2];

	    System.out.println("--- DEREGISTER_MESSAGE_TEMPLATE: Not Implemented ---");

	}


    } // End of CommandTargetSink class
*/

    /**
       Outgoing command FILTER.
       Processes the NOTIFY_FAILURE command
     */
    private class CommandOutgoingFilter extends Filter {

⌨️ 快捷键说明

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