[Webfunds-commits] java/webfunds/comms CommsManager.java RawUsedException.java SingleRequestor.java HttpSocketAgent.java RawHttp.java

Ian Grigg iang@cypherpunks.ai
Mon, 2 Apr 2001 14:42:04 -0400 (AST)


iang        01/04/02 14:42:04

  Modified:    webfunds/comms HttpSocketAgent.java RawHttp.java
  Added:       webfunds/comms CommsManager.java RawUsedException.java
                        SingleRequestor.java
  Log:
  new interface for raw comms model

Revision  Changes    Path
1.2       +22 -2     java/webfunds/comms/HttpSocketAgent.java

Index: HttpSocketAgent.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/HttpSocketAgent.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HttpSocketAgent.java	2001/03/20 23:10:48	1.1
+++ HttpSocketAgent.java	2001/04/02 18:42:03	1.2
@@ -1,5 +1,5 @@
 /*
- * $Id: HttpSocketAgent.java,v 1.1 2001/03/20 23:10:48 iang Exp $
+ * $Id: HttpSocketAgent.java,v 1.2 2001/04/02 18:42:03 iang Exp $
  *
  * Copyright (c) 1995-2001 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -26,6 +26,7 @@
      * The HTTP location
      */
     protected URL url;
+    CommsManager comms = null;
 
     transient protected PrintWriter pw;
 
@@ -38,9 +39,22 @@
     {
         debug(bug, "             a= ");
         this.url = url;
+        comms = new BasicCommsManager();
     }
 
     /**
+     * Create a new HttpSocketAgent object.
+     *
+     * @param url the http location where requests are sent
+     */
+    public HttpSocketAgent(URL url, CommsManager comms, PrintWriter bug)
+    {
+        debug(bug, "             a= ");
+        this.url = url;
+        this.comms = comms;
+    }
+
+    /**
      * Send a Http request and await the reply.
      * @return the reply
      * @except AgentReplyException could not interpret reply
@@ -91,8 +105,9 @@
 
         logmsg("Opening Socket to: " + host + " : " + port + " (" + 0 + ")");
         byte[] out;
+        SingleRequestor sr = comms.getSingleRequestor(host, port);
         try {
-            out = RawHttp.getSocket(host, port, post, 0);
+            out = sr.get(post);
         } catch (RawURLException ex) {
             String s = "RawURLEx: " + ex;
             logmsg(s);
@@ -101,6 +116,11 @@
             String s = "RawConnectEx: " + ex;
             logmsg(s);
             throw new AgentConnectException(s);
+        } catch (RawException ex) {
+            String s = "Unknown RawEx: " + ex;
+            logmsg(s);
+            ex.printStackTrace();
+            throw new Panic(s);
         }
 
         byte[] reply = getPostReply(out);        // throws ReplyEx



1.8       +40 -2     java/webfunds/comms/RawHttp.java

Index: RawHttp.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/RawHttp.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- RawHttp.java	2001/03/20 23:10:10	1.7
+++ RawHttp.java	2001/04/02 18:42:03	1.8
@@ -1,5 +1,5 @@
 /*
- * $Id: RawHttp.java,v 1.7 2001/03/20 23:10:10 iang Exp $
+ * $Id: RawHttp.java,v 1.8 2001/04/02 18:42:03 iang Exp $
  *
  * Copyright (c) 2000 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -48,6 +48,7 @@
      * @except RawURLException connect details bad, check them
      * @except RawConnectException connection refused (try again later)
      */
+// XXX: DEPRECATE THIS METHOD IN FAVOUR OF SocketSupport
     public static byte[] getSocket(String host, int port,
                                    byte[] request, int size)
         throws RawURLException, RawConnectException
@@ -209,7 +210,8 @@
             port = 80;
 
         byte[] request = getGetData(page, host + ":" + port);
-        byte[] buf = getSocket(host, port, request, 10240);
+        Socket sock = SocketSupport.getSocket(host, port);
+        byte[] buf = SocketSupport.getOnce(sock, request, 10240);
         byte[] output = getReply(buf);        // throws ReplyEx
 
         return output;
@@ -418,6 +420,7 @@
         return buf ;
     }
 
+    // XXX: deprecate
     public static byte[] getGetData(String page, String whereTo)
     {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -446,6 +449,41 @@
             throw new RuntimeException("getGetData: baos.close() " + ex);
         }
         return buf ;
+    }
+
+    public static byte[] getGetData(String page, String host, int port)
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+        String Host = HOSTC + host + ":" + port + end;
+        String Get = "GET " + page + " HTTP/1.1" + end;
+
+        try
+        {
+            baos.write(Get.getBytes());       // GET /page HTTP/1.1
+            baos.write(Host.getBytes());      // Host: foo.bar
+            baos.write(CON_CLOSE.getBytes()); // kills the Java socket?
+            baos.write(end.getBytes());       // extra line terminates...
+        }
+        catch (IOException ex)
+        {
+            ex.printStackTrace();
+            throw new RuntimeException("getGetData: baos.write() " + ex);
+        }
+
+        byte[] buf = baos.toByteArray();
+        try {
+            baos.close();
+        } catch (IOException ex) {
+            ex.printStackTrace();
+            throw new RuntimeException("getGetData: baos.close() " + ex);
+        }
+        return buf ;
+    }
+
+    public static byte[] getGetData(URL url)
+    {
+        return getGetData(url.getFile(), url.getHost(), url.getPort());
     }
 
 }



1.1                  java/webfunds/comms/CommsManager.java

Index: CommsManager.java
===================================================================
/*
 * Copyright (c) 2001 Systemics Inc on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.comms;

import java.net.URL;

/**
 * Provides SingleRequestor that can pass a single request to a server.
 *
 * @version $Id: CommsManager.java,v 1.1 2001/04/02 18:42:03 iang Exp $
 */
public interface CommsManager
{
    public SingleRequestor getSingleRequestor(URL url);
    public SingleRequestor getSingleRequestor(String remoteHost, int port);
}



1.1                  java/webfunds/comms/RawUsedException.java

Index: RawUsedException.java
===================================================================
/*
 * $Id: RawUsedException.java,v 1.1 2001/04/02 18:42:03 iang Exp $
 *
 * Copyright (c) 2000 Systemics Inc on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.comms;


/**
 *  This exception class is thrown when the Requestor has already
 *  been used, and cannot be used again.
 */
public class RawUsedException
    extends RawException
{

    public RawUsedException(int errno, String msg)
    {
        super(errno, msg);
    }

    public RawUsedException(String msg)
    {
        super(msg);
    }
}



1.1                  java/webfunds/comms/SingleRequestor.java

Index: SingleRequestor.java
===================================================================
/*
 * Copyright (c) 2001 Systemics Inc on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.comms;

/**
 * Provides SingleRequestor that can pass a single request to a server.
 *
 * @version $Id: SingleRequestor.java,v 1.1 2001/04/02 18:42:03 iang Exp $
 */
public interface SingleRequestor
{
    /**
     *  Send this packet to the preset host/port, and
     *  return the single packet that returns.
     *  Only getMax() bytes or less will be returned.
     */
    public byte[] get(byte[] packet)
        throws RawException;

    /** Set the maximum number of characters to be returned.  */
    public void     setMax(int max);
    public int      getMax();

    public String   getHost();
    public int      getPort();

    public boolean  isUsed();
}