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

📄 zeus_acc_server.java

📁 人工智能中Agent开发包。多 Agent 系统是处理自治 Agent 之间知识层的协作问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * The contents of this file are subject to the BT "ZEUS" Open Source
 * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file
 * except in compliance with the Licence. You may obtain a copy of the Licence
 * from $ZEUS_INSTALL/licence.html or alternatively from
 * http://www.labs.bt.com/projects/agents/zeus/licence.htm
 *
 * Except as stated in Clause 7 of the Licence, software distributed under the
 * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the Licence for the specific language governing rights and
 * limitations under the Licence.
 *
 * The Original Code is within the package zeus.*.
 * The Initial Developer of the Original Code is British Telecommunications
 * public limited company, whose registered office is at 81 Newgate Street,
 * London, EC1A 7AJ, England. Portions created by British Telecommunications
 * public limited company are Copyright 1996-9. All Rights Reserved.
 *
 * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
 */

package zeus.actors.intrays;

import zeus.actors.outtrays.*;
import java.net.*;
import java.io.*;
import java.util.*;
import zeus.util.*;
import zeus.concepts.*;
import zeus.actors.*;
import zeus.agents.*;
import javax.naming.*;
import java.net.*;
import java.io.*;
import java.util.*;
import zeus.util.*;
import zeus.concepts.*;

import javax.rmi.*;
import java.rmi.*;


/**
 * Zeus_ACC_Server uses the FIPA_97_Server and FIPA_2000_Server classes to provide
 * a unified message reception and handleing service.On init this class will create
 * a FIPA_97_Server and a FIPA_2000_Server, using itself as a parameter.
 * When either of these intrays receives a message it will call back the appropiate
 * handle method in this class. The message will then be processed and placed on the
 * message handleing q in this instance. Finally this message will be handled and processed.
 * <p>
 * @author Simon Thompson
 * @since 1.1
 */
public class Zeus_ACC_Server extends Server implements InTray, FIPA_97_Handler, FIPA_2000_Handler {  // the ACC agent should include code to set this.
    
    public  String hostname = zeus.util.SystemProps.getProperty("HAP_Address");
    
    /**
    iiop2000port is used to store the number of the port that the FIPA_2000 IIOP
    listening service (the server) will use.
    This is package protected so that subclasses can do as they will!
    value is set to "2000" by default
     */
    protected String iiop2000Port = "2000";
    
    
    /**
     * iiop97port is used to store the number of the port that the FIPA_97 IIOP
     * listening service (the server) will use.
     * This is package protected so that subclasses can do as they will!
     * value is set to "1097" by default.
     */
    protected String iiop97Port = "1097";
    
    
    /**
    httpPort is the number of the port that the FIPA_2000 HTTP service
    will listenon.
    This is package protected so that you can fiddle with it in subclasses.
    value is set to "61000" by default
     */
    protected String httpPort = "61000";
    
    
    /**
    access to the context object is provided as protected as a convienience for
    API programmers and future extentions, it is passed to the init() of this class
     */
    protected AgentContext context = null;
    
    private Queue  msgQ = new Queue("Zeus_ACC_In");
    private ZeusParser parser = new ZeusParser();
    
    
    /**
    name is set to "ACC" by default
     */
    protected String name = "ACC";
    
    /**
    twoThousand_Server is the reference to the FIPA_2000_IIOP_Server that
    this class sets up and runs in it's init().
     */
    protected FIPA_2000_IIOP_Server twoThousand_Server = null ;
    
    
    /**
    ninetySeven_Server is the reference to the FIPA_97_Server that
    this class sets up and runs in it's init()
     */
    protected FIPA_97_Server ninetySeven_Server = null;
    
    
    /**
    httpServer is the reference to the FIPA_2000_HTTP_Server that
    this class sets up and runs in it's init()
     */
    protected FIPA_2000_HTTP_Server httpServer = null;
    
    
    /**
     host is protected to allow for interference by putative sub-classes
     */
    protected String host = null;
    
    
    /**
    class constructor that takes the AgentContext and registers this instance with it,
    and the MailBox and grabs the reference so that it can be used later. This also sets
    up the ServerThreads for the message transports that this Server is utilizing.
     */
    public Zeus_ACC_Server(AgentContext context, Zeus_ACC_MailBox mbox) {
        this.mbox = (MailBox) mbox;
        timeout = 10;
        Assert.notNull(context);
        this.context = context;
        TransportConfig twoIIOPConf = SystemProps.getTransport("FIPA_IIOP_2000");
        if (twoIIOPConf!=null) {
            debug("setting iiop 2000 port to : " + twoIIOPConf.getPort());
            iiop2000Port = twoIIOPConf.getPort(); }
        TransportConfig twoHTTPConf = SystemProps.getTransport("FIPA_HTTP_2000");
        if (twoHTTPConf != null) {
            debug("setting http 2000 port to : " + twoHTTPConf.getPort());
            httpPort = twoHTTPConf.getPort(); }
        TransportConfig nineIIOPConf = SystemProps.getTransport("FIPA_IIOP_1997");
        if (nineIIOPConf != null) {
            debug("setting iiop 1997 port to : " + nineIIOPConf.getPort());
            iiop97Port = nineIIOPConf.getPort(); }
        
        try {
            
            
            InetAddress ip = InetAddress.getLocalHost();
            host = ip.getHostAddress();
            if (hostname == null ) {
                hostname = host; }
            twoThousand_Server = new FIPA_2000_IIOP_Server(this,host, iiop2000Port,name,"2000Connection");
            ninetySeven_Server = new FIPA_97_Server(this,host, iiop97Port,name,"97Connection");
            httpServer = new FIPA_2000_HTTP_Server(this,host,httpPort,name, "HTTPConnection");
            // note to self - do http server here
            this.start();
            this.setName("Zeus_ACC_Server");}
        catch (Exception e ) {
            // will only be called if local host is not set
            // very unlikely...
            e.printStackTrace();
            System.out.println("ERROR - probably localhost cannot be looked up!\n WARNING: attempting to continue, but likely fatal");
        }
    }
    
    
    
    /**
    stopProcessing will cause the run loop of this class to stop
     */
    public void stopProcessing() {
        processing = false;
    }
    
    
    /**
    This is where the changes were needed. Instead of listening on a stupid socket ;-)
    we simply look in our message queue to see if any goodies have been put there by nice
    mr FIPA_97_Connection.
     */
    public void run() {
        processing = true;
        while (processing) {
            try {// robust!?
                Object obj = this.pop();
                debug ("popped message off queue"); 
                if (obj instanceof String) {
                    String text =(String) obj;
                    debug("message popped is: " + text);
                    //       try {
                    text = Misc.unescape(text);// swapped from underneath
                    //    text = text.replace('\n',' ');
                    //   text = text.replace('\r',' ');
                    //   text = text.replace('\t',' ');
                    
                    forwardFIPAMessage(text);
                    
                }
      
                yield();}
            catch (Exception e ) {
                e.printStackTrace();
                System.out.println("exception in ACC server - attempting to recover");
            }
            catch (Error er) {  
                er.printStackTrace(); 
                System.out.println("error - attempting to recover"); 
            }
            catch (Throwable tr) { 
                tr.printStackTrace();
                System.out.println("throwable - attempting to recover"); 
            }
        }
    }
    
    
    /**
     * for now the behaviour is :-
     * get message, <br>
     * act surprised, <br>
     * then parse it into a FIPAPerformative, <br>
     * map that to a Performative, <br>
     * register self as alias in nameserver,<br>
     * add alias to FIPAAddressBook, <br>
     * stamp message with said alias,<br>
     * assert a messageHandleing rule to invoke forwarding behaviour when
     * a response is received
     * send message to Zeus agent, <br
     * pray.<p>
     */
    public void forwardFIPAMessage(String text) {
        try {
            debug ("forwarding"); 
            FIPA_AddressBook addresses = ((FIPA_AddressBook)context.getAddressBook());
            FIPAPerformative fmsg = parser.fipaPerformative(text);
            FIPA_AID_Address fAddress = fmsg.getSender_As_FIPA_AID();
            fAddress = addresses.checkAddress(fAddress);
            if (fAddress.getAlias() == null) {
                registerAlias(fAddress); }
            Performative msg = fmsg.performative();
            String alias = fAddress.getAlias();
            msg.setSender(alias);
            debug ("setting send address to " + alias); 
            msg.setReplyTo(alias);
            addresses.add(fAddress);
            setHandleMessage(fAddress, alias);
     
            debug("Zeus content = " + msg.getContent());
            mbox.sendMsg(msg); }
        catch (Exception e) {
            try {
                java.io.File file = new java.io.File("debug.out");
                java.io.FileOutputStream fileout = new java.io.FileOutputStream(file);
                java.io.PrintWriter fw= new java.io.PrintWriter(fileout);
                e.printStackTrace(fw);
                fw.flush();

⌨️ 快捷键说明

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