[Webfunds-commits] java/webfunds/comms AgentConnectException.java AgentException.java AgentLaterException.java AgentReplyException.java AgentURLException.java CommsAgent.java HttpAgent.java RawConnectException.java RawException.java RawHttp.java RawReplyException.java RawURLException.java

Ian Grigg iang@cypherpunks.ai
Tue, 20 Mar 2001 19:10:14 -0400 (AST)


iang        01/03/20 19:10:13

  Modified:    webfunds/comms CommsAgent.java HttpAgent.java
                        RawConnectException.java RawException.java
                        RawHttp.java RawReplyException.java
                        RawURLException.java
  Added:       webfunds/comms AgentConnectException.java
                        AgentException.java AgentLaterException.java
                        AgentReplyException.java AgentURLException.java
  Log:
  got all COMMS working in this new webfunds.comms

Revision  Changes    Path
1.5       +18 -13    java/webfunds/comms/CommsAgent.java

Index: CommsAgent.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/CommsAgent.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- CommsAgent.java	2000/04/15 19:05:44	1.4
+++ CommsAgent.java	2001/03/20 23:10:09	1.5
@@ -1,19 +1,23 @@
 /*
- * $Id: CommsAgent.java,v 1.4 2000/04/15 19:05:44 iang Exp $
+ * $Id: CommsAgent.java,v 1.5 2001/03/20 23:10:09 iang Exp $
  *
- * Copyright (c) Systemics Ltd 1995-1999 on behalf of
+ * Copyright (c) 1995-2001 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
-package webfunds.sox;
+package webfunds.comms;
 
-//import java.io.*;
-
 import webfunds.utils.Debug;
 
 
 /**
- * Objects of classes derived from this are used to
- * perform requests at the transport (comms) layer.
+ *  This abstract interface embodies the SOX request model.
+ *  All actions are request/response pairs, with one request
+ *  packet of data going from client to server and one reply
+ *  packet coming back.
+ *
+ *  Objects of classes derived from this are used to
+ *  perform requests at the transport (comms) layer
+ *  within SOX.
  */
 public abstract class CommsAgent
     extends Debug
@@ -23,12 +27,13 @@
     /**
      * Issue a request
      * 
-     * @except SOXAgentException parent exception to all below
-     * @except SOXAgentReplyException could not interpret reply
-     * @except SOXAgentURLException connect details bad, check them
-     * @except SOXAgentConnectException connection refused (try again later)
+     * @except AgentException parent exception to all below
+     * @except AgentReplyException could not interpret reply
+     * @except AgentURLException connect details bad, check them
+     * @except AgentConnectException connection refused (try again later)
+     * @except AgentLaterException server down (try again later)
      */
     public abstract byte[] request(byte[] request)
-      throws SOXAgentReplyException, SOXAgentURLException,
-             SOXAgentConnectException ;
+      throws AgentReplyException, AgentURLException,
+             AgentConnectException, AgentLaterException ;
 }



1.22      +18 -301   java/webfunds/comms/HttpAgent.java

Index: HttpAgent.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/HttpAgent.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- HttpAgent.java	2001/02/27 01:25:44	1.21
+++ HttpAgent.java	2001/03/20 23:10:09	1.22
@@ -1,17 +1,15 @@
 /*
- * $Id: HttpAgent.java,v 1.21 2001/02/27 01:25:44 iang Exp $
+ * $Id: HttpAgent.java,v 1.22 2001/03/20 23:10:09 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
-package webfunds.sox;
+package webfunds.comms;
 
 import java.io.*;
 import java.net.*;
 
-import webfunds.utils.RawHttp;
-import webfunds.utils.RawURLException;
-import webfunds.utils.RawConnectException;
+import webfunds.util.Panic;
 
 /**
  * Instances of this class are used to perform Http requests.
@@ -26,8 +24,6 @@
 
     transient protected PrintWriter pw;
 
-    protected boolean directSocket = false;
-
     /**
      * Create a new HttpAgent object.
      *
@@ -37,33 +33,22 @@
     {
         debug(bug, "       a= ");
         this.url = url;
-        directSocket = false;
-    }
-
-    public HttpAgent(URL url, boolean sock, PrintWriter bug)
-    {
-        debug(bug, "       a+ ");
-        this.url = url;
-        directSocket = sock;
     }
 
     /**
      * Send a Http request and await the reply.
      * @return the reply
-     * @except SOXAgentReplyException could not interpret reply
-     * @except SOXAgentURLException connect details bad, check them
-     * @except SOXAgentConnectException connection refused (try again later)
+     * @except AgentReplyException could not interpret reply
+     * @except AgentURLException connect details bad, check them
+     * @except AgentConnectException connection refused (try again later)
      */
     public byte[] request(byte[] request)
-        throws SOXAgentReplyException, SOXAgentURLException,
-               SOXAgentConnectException
+        throws AgentReplyException, AgentURLException,
+               AgentConnectException
     {
         try
         {
-            if (directSocket)
-                return requestViaSocket(request);
-            else
-                return requestViaURL(request);
+            return requestViaURL(request);
         }
         catch (IOException ex)
         {
@@ -72,11 +57,10 @@
             // Figure out what and why, catch it in the code,
             // and convert it to a meaningful exception.
             //
-            String typ = (directSocket ? "Sock" : "URL");
-            String s = "caught bad IOEx (" + typ + ") : " + ex;
+            String s = "caught bad IOEx : " + ex;
             logmsg(s);
             ex.printStackTrace(err());
-            throw new RuntimeException("Fatal: " + ex);
+            throw new Panic("Fatal: " + ex);
         }
     }
 
@@ -87,13 +71,13 @@
      * to set timouts.
      *
      * @return the reply
-     * @except SOXAgentReplyException could not interpret reply
-     * @except SOXAgentURLException connect details bad, check them
-     * @except SOXAgentConnectException connection refused (try again later)
+     * @except AgentReplyException could not interpret reply
+     * @except AgentURLException connect details bad, check them
+     * @except AgentConnectException connection refused (try again later)
      * @except IOException code error, catch and die
      */
-    public byte[] requestViaURL(byte[] request)
-        throws SOXAgentReplyException, SOXAgentURLException,
+    private byte[] requestViaURL(byte[] request)
+        throws AgentReplyException, AgentURLException,
                IOException  // failure!  catch and convert
     {
         logmsg("Opening URL to: " + url);
@@ -116,7 +100,7 @@
             oc.connect();
             status = oc.getResponseCode();
             if (status != 200)
-                throw new SOXAgentReplyException("Http request failed: " +
+                throw new AgentReplyException("Http request failed: " +
                                    status + ", " +
                                    oc.getResponseMessage());
 
@@ -125,7 +109,7 @@
         //
         int len = oc.getContentLength();
         if (len == -1)
-            throw new SOXAgentReplyException("Http request failed - " +
+            throw new AgentReplyException("Http request failed - " +
                                   "reply has no content-length");
         byte[] reply = new byte[len];
 
@@ -137,273 +121,6 @@
             dis.close();
 
         return reply;
-    }
-
-
-
-
-    /**
-     * Send a Http request and await the reply.
-     * This method uses the low level socket code to get more control.
-     *
-     * @return the reply
-     * @except SOXAgentReplyException could not interpret reply
-     * @except SOXAgentURLException connect details bad, check them
-     * @except SOXAgentConnectException connection refused (try again later)
-     * @except IOException code error, catch and die
-     */
-    public byte[] requestViaSocket(byte[] request)
-        throws SOXAgentReplyException, SOXAgentURLException,
-               SOXAgentConnectException,
-               IOException  // failure!  catch and convert
-    {
-
-        String host = url.getHost();
-        int    port = url.getPort();
-        byte[] post = getPostData(request, host + ":" + port);
-
-        logmsg("Opening Socket to: " + host + " : " + port + " (" + 0 + ")");
-        byte[] out;
-        try {
-            out = RawHttp.getSocket(host, port, post, 0);
-        } catch (RawURLException ex) {
-            String s = "RawURLEx: " + ex;
-            logmsg(s);
-            throw new SOXAgentURLException(s);
-        } catch (RawConnectException ex) {
-            String s = "RawConnectEx: " + ex;
-            logmsg(s);
-            throw new SOXAgentConnectException(s);
-        }
-
-        byte[] reply = getPostReply(out);        // throws ReplyEx
-        return reply ;
-
-    }
-
-
-
-    /**
-     * @Return the next line
-     * @except IOException some unknown failure (others inherit from IOEx)
-     */
-    protected String getLine(InputStream is)
-        throws IOException
-    {
-        byte[] buf   = new byte[128];
-        int    i     = 0;
-
-        do
-        {
-             int ch = is.read();
-             if (ch < 0)
-             {
-                 if (i == 0)                                   // EOF
-                     return null;
-                 throw new IOException("EOF after " + i + ":" +
-                                       " <<" + new String(buf, 0, i) + ">>");
-             }
-             buf[i] = (byte)ch;
-             if (i > 0 && buf[i] == '\n' && buf[i-1] == '\r')  // CRNL
-                 return new String(buf, 0, i - 1);             // no term
-             else if (buf[i] == '\n')                          // NL only?
-                 return new String(buf, 0, i);                 // no term
-
-        } while (++i < buf.length) ;
-
-        throw new IOException("too long: <<" + new String(buf) + ">>");
-    }
-
-
-
-    protected static final String serverVersion = "HTTP/";
-    protected static final String contentLength = "Content-length: ";
-
-    /**
-     * Return the message from a POST reply
-     * @param buf the byte array with the reply in it
-     *
-     * @return the reply body
-     * @except IOException some unknown failure (others inherit from IOEx)
-     */
-    protected byte[] getPostReply(byte[] buf)
-        throws SOXAgentReplyException
-    {
-        ByteArrayInputStream bais = new ByteArrayInputStream(buf);
-        return getPostReply(bais);
-    }
-
-    /**
-     * Return the message from a POST reply
-     * @return the reply body
-     * @except IOException some unknown failure (others inherit from IOEx)
-     */
-    protected byte[] getPostReply(InputStream is)
-        throws SOXAgentReplyException
-    {
-        boolean ok = true;
-        int length = -1;
-        String errors = "";
-        int errno = SOXException.UNKNOWN;
-
-        //
-        // On the reads, the timeout will cause a
-        // java.io.InterruptedIOException.
-        //
-        while (true)
-        {
-            String s;
-            try {
-                s = getLine(is);
-            } catch (IOException ex) {
-                logmsg(ex.getMessage());
-                throw new SOXAgentReplyException(errno, "headers: " + ex);
-            }
-            if (s == null)           // sometimes returns closed as EOF
-            {
-                ok = false;
-                errno = SOXException.LATER_DOWN;
-                errors += "  later, down: <<EOF>>";
-                break;
-            }
-
-            String line = s.trim();
-
-            if ("".equals(line))     // this is empty line, waiting for
-                break ;              // next is message body
-
-            if (line.startsWith(serverVersion))
-            {
-                if (line.indexOf(" 500 Internal Error") >= 0)
-                {
-                    ok = false;
-                    errno = SOXException.LATER_DOWN;
-                    errors += "  later, down: <<" + line + ">>";
-                }
-                else if (line.indexOf(" 200 ") < 0)
-                {
-                    ok = false;
-                    errors += "  failed: <<" + line + ">>";
-                }
-            }
-            else if (line.startsWith(contentLength))
-            {
-                int len = line.indexOf(": ");
-                if (len < 0 || (len + 2) >= line.length())
-                    ok = false;
-                len += 2;
-                String lenString = line.substring(len);
-                try {
-                    length = new Integer(lenString).intValue();
-                } catch (NumberFormatException ex) {
-                    ok = false;
-                    errors += "  bad len: <<" + line + ">>";
-                    logmsg(errors);
-                }
-            }
-        }
-
-        int avail;
-        try {
-            avail = is.available();
-        } catch (IOException ex) {
-            throw new RuntimeException("is.available: " + ex);
-        }
-
-        if (errors.length() > 0)
-        {
-            String s = "HTTP failed (" + avail + " bytes) : " + errors + "\n";
-            byte[] errorHtml = new byte[avail];
-            int i;
-            try {
-                i = is.read(errorHtml);
-            } catch (IOException ex) {
-                throw new SOXAgentReplyException("is.read: " + ex);
-            }
-            if (i > 0)
-                s += "\n" + new String(errorHtml, 0, i) + "\n";
-            logmsg(s);
-            throw new SOXAgentReplyException(errno, s);
-        }
-
-        if (length <= 0)
-        {
-            String s = "no Content-length: " + errors + "\n";
-            logmsg(s);
-            throw new SOXAgentReplyException("HTTP failed: " + s + "\n");
-        }
-
-        //
-        //  All looks good.  Read in the data.
-        //  Hopefully only one read required, but you never know.
-        //
-        byte[] buf;
-        buf = new byte[length];
-        int rr = 0;
-        while (rr < length)
-        {
-            int i;
-            try {
-                i = is.read(buf, rr, length - rr);
-            } catch (IOException ex) {
-                throw new SOXAgentReplyException("is.read ("+rr+"): " + ex);
-            }
-            if (i < 0)
-                throw new SOXAgentReplyException("premature EOF at " +
-                                                 rr + " of " + length);
-            rr += i;
-        }
-
-        return buf;
-    }
-
-    //
-    //  Doesn't handle
-    //           * proxy authentication
-    //           * chunking
-    //  Need to add these.
-    //
-    protected static final String end = "\r\n";
-                                    // also. * is possible in place of empty /
-    protected static final String cmd = "POST / HTTP/1.1" + end;
-    protected static final String len = "Content-Length: ";
-    protected static final String host = "Host: ";
-    protected static final String con  = "Connection: close" + end;
-    protected static final String type = "x-application/sox-2";
-    protected static final String Type = "Content-Type: " + type + end;
-
-    protected byte[] getPostData(byte[] request, String whereTo)
-    {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
-        String Length = len + request.length + end;
-        String Host = host + whereTo + end;
-
-
-        try
-        {
-            baos.write(cmd.getBytes());
-            baos.write(Host.getBytes());
-            baos.write(Length.getBytes());
-            baos.write(Type.getBytes());
-            // baos.write(con.getBytes());
-            baos.write(end.getBytes());       // extra line to separate body
-            baos.write(request);
-        }
-        catch (IOException ex)
-        {
-            ex.printStackTrace();
-            throw new RuntimeException("getPostData: baos.write() " + ex);
-        }
-
-        byte[] buf = baos.toByteArray();
-        try {
-            baos.close();
-        } catch (IOException ex) {
-            ex.printStackTrace();
-            logmsg("getPostData: baos.close() " + ex);
-        }
-        return buf ;
     }
 
     public String toString()



1.2       +1 -1      java/webfunds/comms/RawConnectException.java

Index: RawConnectException.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/RawConnectException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RawConnectException.java	2000/05/16 11:12:32	1.1
+++ RawConnectException.java	2001/03/20 23:10:10	1.2
@@ -1,10 +1,10 @@
 /*
- * $Id: RawConnectException.java,v 1.1 2000/05/16 11:12:32 iang Exp $
+ * $Id: RawConnectException.java,v 1.2 2001/03/20 23:10:10 iang Exp $
  *
  * Copyright (c) 2000 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
-package webfunds.utils;
+package webfunds.comms;
 
 
 /**



1.2       +1 -1      java/webfunds/comms/RawException.java

Index: RawException.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/RawException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RawException.java	2000/05/16 11:12:32	1.1
+++ RawException.java	2001/03/20 23:10:10	1.2
@@ -1,10 +1,10 @@
 /*
- * $Id: RawException.java,v 1.1 2000/05/16 11:12:32 iang Exp $
+ * $Id: RawException.java,v 1.2 2001/03/20 23:10:10 iang Exp $
  *
  * Copyright (c) 2000 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
-package webfunds.utils;
+package webfunds.comms;
 
 
 /**



1.7       +2 -2      java/webfunds/comms/RawHttp.java

Index: RawHttp.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/RawHttp.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- RawHttp.java	2000/10/07 01:12:03	1.6
+++ RawHttp.java	2001/03/20 23:10:10	1.7
@@ -1,10 +1,10 @@
 /*
- * $Id: RawHttp.java,v 1.6 2000/10/07 01:12:03 iang Exp $
+ * $Id: RawHttp.java,v 1.7 2001/03/20 23:10:10 iang Exp $
  *
  * Copyright (c) 2000 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
-package webfunds.utils;
+package webfunds.comms;
 
 import java.io.*;
 import java.net.*;



1.2       +1 -1      java/webfunds/comms/RawReplyException.java

Index: RawReplyException.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/RawReplyException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RawReplyException.java	2000/05/16 11:12:33	1.1
+++ RawReplyException.java	2001/03/20 23:10:10	1.2
@@ -1,10 +1,10 @@
 /*
- * $Id: RawReplyException.java,v 1.1 2000/05/16 11:12:33 iang Exp $
+ * $Id: RawReplyException.java,v 1.2 2001/03/20 23:10:10 iang Exp $
  *
  * Copyright (c) 2000 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
-package webfunds.utils;
+package webfunds.comms;
 
 
 /**



1.2       +1 -1      java/webfunds/comms/RawURLException.java

Index: RawURLException.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/RawURLException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RawURLException.java	2000/05/16 11:12:33	1.1
+++ RawURLException.java	2001/03/20 23:10:11	1.2
@@ -1,10 +1,10 @@
 /*
- * $Id: RawURLException.java,v 1.1 2000/05/16 11:12:33 iang Exp $
+ * $Id: RawURLException.java,v 1.2 2001/03/20 23:10:11 iang Exp $
  *
  * Copyright (c) 2000 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
-package webfunds.utils;
+package webfunds.comms;
 
 
 /**



1.1                  java/webfunds/comms/AgentConnectException.java

Index: AgentConnectException.java
===================================================================
/*
 * $Id: AgentConnectException.java,v 1.1 2001/03/20 23:10:04 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.comms;


/**
 * This exception class is thrown when the client sees a refused
 * connection or other indicator that information is correct, but time is bad.
 * Turn it into a LaterException further up the request layer(s).
 */
public class AgentConnectException
    extends AgentException
{

    public AgentConnectException(String m)            { super(UNKNOWN, m); }
    public AgentConnectException(int errno, String m) { super(errno, m); }
    public AgentConnectException(int errno)           { super(errno, ""); }
}



1.1                  java/webfunds/comms/AgentException.java

Index: AgentException.java
===================================================================
/*
 * $Id: AgentException.java,v 1.1 2001/03/20 23:10:05 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.comms;

import webfunds.util.ExceptionModel;

/**
 * This exception class is thrown by a CommsAgent.
 * Inherit precise delitos from here.
 */
public abstract class AgentException
    extends ExceptionModel
{

    public AgentException(String msg)            { super(UNKNOWN, msg); }
    public AgentException(int errno, String msg) { super(errno, msg); }
    public AgentException(int errno)             { super(errno, ""); }
}



1.1                  java/webfunds/comms/AgentLaterException.java

Index: AgentLaterException.java
===================================================================
/*
 * $Id: AgentLaterException.java,v 1.1 2001/03/20 23:10:06 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.comms;


/**
 * This exception class is thrown when the server indicates it
 * is temporarily unavailable.  Try again later.
 */
public class AgentLaterException
    extends AgentException
{

    public AgentLaterException(String msg)            { super(UNKNOWN, msg); }
    public AgentLaterException(int errno, String msg) { super(errno, msg); }
    public AgentLaterException(int errno)             { super(errno, ""); }
}



1.1                  java/webfunds/comms/AgentReplyException.java

Index: AgentReplyException.java
===================================================================
/*
 * $Id: AgentReplyException.java,v 1.1 2001/03/20 23:10:06 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.comms;


/**
 * This exception class is thrown when the server returns an unreadable
 * packet.  Nothing to be done about it, just log it and return.
 */
public class AgentReplyException
    extends AgentException
{

    public AgentReplyException(String m)            { super(UNKNOWN, m); }
    public AgentReplyException(int errno, String m) { super(errno, m); }
    public AgentReplyException(int errno)           { super(errno, ""); }
}



1.1                  java/webfunds/comms/AgentURLException.java

Index: AgentURLException.java
===================================================================
/*
 * $Id: AgentURLException.java,v 1.1 2001/03/20 23:10:08 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.comms;


/**
 * This exception class is thrown when the client detects that
 * the host : port is bad, and should be retried or changed.
 */
public class AgentURLException
    extends AgentException
{

    public AgentURLException(String msg)            { super(UNKNOWN, msg); }
    public AgentURLException(int errno, String msg) { super(errno, msg); }
    public AgentURLException(int errno)             { super(errno, ""); }
}