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

📄 fipa_2000_http_connection.java

📁 人工智能中Agent开发包。多 Agent 系统是处理自治 Agent 之间知识层的协作问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        // works for jade
        if (tmp.startsWith("content-type: multipart/mixed; boundary=\"")) {
            //     debug (ln.substring (41,ln.length()-1));
            debug(">> returning boundary!");
            return (ln.substring(41,ln.length()-1));
        }
        if (tmp.startsWith("content-type: multipart/mixed;  boundary=\"")) {
            //     debug (ln.substring (41,ln.length()-1));
            debug(">> returning boundary!");
            return (ln.substring(42,ln.length()-1));
        }
        if (tmp.startsWith("content-type: multipart/mixed ; boundary=\""))    {
            return (ln.substring(42,ln.length()-1));
        }
        else if (tmp.startsWith("\tboundary=\"")) {
            //         debug (ln.substring (11,ln.length()-1));
            debug(">> returning boundary!");
            return (ln.substring(11, ln.length()-1));
        }
        else {
            debug(">> returning null!");
            return null;
        }
    }
    
    
    /**
     * pull the content length from the header
     */
    private int testAndSetContentLength(String ln) {
        if (ln.startsWith("Content-length") || ln.startsWith("Content-Length")) {
            String lengthContent = ln.substring(15,ln.length()).trim();
            int contentLength = Integer.parseInt(lengthContent);
            //       debug (String.valueOf(contentLength));
            return contentLength;
        }
        else {
            return 0;
        }
    }
    
    
    
    /**
     * send the mandated response to a successful message reception
     * episode (see XC00084C)
     */
    public void respondOK(PrintWriter out) {
        out.println(response_ok);
        out.flush();
        out.close();
    }
    
    
    /**
     * send a not OK response (hopefully we won't have to do this!)<p>
     * This is not something that I could see mandated in the FIPA-spec,
     * but I think that it is a good idea - otherwise the connection remains open
     * until a timeout. <p>
     * Perhaps sufficient open connections could be used as some sort of attack
     * on the agent.
     */
    public void respondNotOK(PrintWriter out) {
        out.println(response_not_ok);
        out.flush();
        out.close();
    }
    
    
    /**
     * message is used to handle the completely read and parsed message when
     * it has come off the message queue
     */
    public void message(FIPAPerformative aFipaMessage) {
        //  FIPAPerformative perf = new FIPAPerformative (aFIPAMessage);
        queue.enqueue(aFipaMessage);
    }
    
    
    public FIPA_2000_HTTP_Connection(String host, String port, String name) {
        this.host = host;
        this.name = name;
        this.port = port;
        try {
            
            java.io.File file = new java.io.File("http.out");
            java.io.FileOutputStream outStream = new java.io.FileOutputStream(file);
            java.io.PrintStream err = new java.io.PrintStream(outStream);
            // System.setErr(err);
            
            serverSocket = new ServerSocket(Integer.parseInt(port),MAX_HTTP_QUEUE); }
        catch (IOException e) {
            e.printStackTrace();
            System.out.println("cannot get a socket to listen for http connections on: recovering (without HTTP connection)");
        }
        
        
    }
    
    
    /**
     * used to test the code here!
     */
    protected boolean test(String myPort, String testHost, String testPort) {
        try {
            TransportFactory tf = new IIOP_Z_HTTP_TransportFactory();
            InetAddress ip = InetAddress.getLocalHost();
            String localHost = ip.getHostAddress();
            String targetAddress = new String("http://"+testHost+":"+testPort+"/test");
            String sourceAddress = new String("http://"+localHost+":"+myPort+"/test");
            OutTray transport = tf.getTransport(targetAddress);
            FIPAPerformative fperf = new FIPAPerformative("inform");
            javax.agent.Envelope env = fperf.jasEnvelope(new FIPA_AID_Address(sourceAddress),targetAddress);
            transport.send(env);
            return true; }
        catch (Exception e ) {
            e.printStackTrace();
            return false;
        }
    }
    
    
    /**
     * main method for testing this module<p>
     * <p> parameters are <port number for this> <host to test connection to> <port on test host>
     */
    public static void main(String argv[]) {
        String myPort = argv[0];
        String testHost = argv[1];
        String testPort = argv[2];
        FIPA_2000_HTTP_Connection test = new FIPA_2000_HTTP_Connection("tb-toledo.futures.bt.co.uk",myPort,"test");
        boolean done = false;
        while (!done) {
            try {
                Thread.sleep(1000);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            //  done = test.test(myPort,testHost,testPort);
        }
        
    }
    
    
    private FIPAPerformative process(String inMess,String bound) {
        debug("inMess == \n" + inMess + "\n end inMess");
        String message = stripEnvelope(inMess, bound);
        debug ("......\n " + message + " -------"); 
        FIPAPerformative fperf = ZeusParser.fipaPerformative(message);
        String content = fperf.getContent();
        try {
            fperf.setContent (addEscape(content));}
        catch (Exception e) { 
            System.out.println("EXCEPTION"); 
            e.printStackTrace(); 
        }
        debug("outMess == \n" + fperf.getContent() + "\n end inMess");
        return (fperf);
        
    }
    
    private String addEscape (String in) { 
        try { // content can be null 
     int counter = 0; 
     int pointer = 0; 
     StringBuffer buf = new StringBuffer (in);
     StringBuffer newOne = new StringBuffer(); 
     while (counter<in.length()-1) {
         char thisOne = buf.charAt(counter); 
         if (thisOne != '"') {
             newOne.append(thisOne); 
         }
         else {
             System.out.println("replacing"); 
             newOne.append('\\'); 
             newOne.append('\\'); 
             newOne.append('\\'); 
             newOne.append(thisOne); 
         }
         counter++; 
     }
     String ret = newOne.toString(); 
     return ret; }
        catch (Exception e) { 
            return (new String()); 
        } 
    }
    
    /**
     * crude for the moment - this needs redoing so it is less fragile.
     */
    private String stripEnvelope(String HTTPmessage,String bound) {
        //         debug ("boundary == " + bound);
        String remLastBoundary = HTTPmessage.substring(0, HTTPmessage.lastIndexOf(bound)-2);
        //  debug (remLastBoundary);
        //add
        String message = remLastBoundary.substring(HTTPmessage.indexOf(bound)+bound.length(),remLastBoundary.length());
        message =  remLastBoundary.substring(remLastBoundary.indexOf('('),remLastBoundary.length());
        //endadd
        //           String message = remLastBoundary.substring (HTTPmessage.indexOf(bound)+bound.length()+30, remLastBoundary.length());
        //      debug ("");
        //    debug ("");
        //    debug ("stripped content = "+message);
        return message.trim();
    }
    
    
    private void debug(String val) {
        System.out.println("\nFIPA_2000_HTTP_Connection>>"+val+"\n");
    }
}

⌨️ 快捷键说明

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