[Webfunds-commits] java/webfunds/comms BasicCommsManager.java HttpSocketAgent.java

Ian Grigg iang@cypherpunks.ai
Sun, 8 Apr 2001 15:15:08 -0400 (AST)


iang        01/04/08 15:15:08

  Modified:    webfunds/comms BasicCommsManager.java HttpSocketAgent.java
  Log:
  Made the default SingleRequestor an inner class; added Diags

Revision  Changes    Path
1.2       +86 -6     java/webfunds/comms/BasicCommsManager.java

Index: BasicCommsManager.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/BasicCommsManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BasicCommsManager.java	2001/04/04 14:19:53	1.1
+++ BasicCommsManager.java	2001/04/08 19:15:07	1.2
@@ -7,6 +7,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.IOException;
+import java.io.PrintWriter;
 
 import java.net.ConnectException;
 import java.net.NoRouteToHostException;
@@ -15,21 +16,35 @@
 import java.net.Socket;
 import java.net.SocketException;
 
+import webfunds.utils.Diagnostics;
+
 /**
- * A CommsManager that can return BasicRequestors.
+ * A CommsManager that can return SingleRequestors.
  *
- * @version $Id: BasicCommsManager.java,v 1.1 2001/04/04 14:19:53 iang Exp $
+ * @version $Id: BasicCommsManager.java,v 1.2 2001/04/08 19:15:07 iang Exp $
  */
 public class BasicCommsManager
-    implements CommsManager
+    implements CommsManager, Diagnostics
 {
+    // mixing these classes is a bit non-OO.  But, it seems many need both.
+    // another option would be to extend from Debug.
+    protected PrintWriter bug = null;
+    protected String      fix = "        Bcw- ";
+    public void logmsg(String s)  { if (bug != null) bug.println(fix + s); }
+    public PrintWriter getDebug() { return bug ; }
+    // hmm, no autoflush.
+    public PrintWriter err()
+    { return (bug == null) ? new PrintWriter(System.err, true) : bug ; }
+            
+
 
     /**
      * Create a new BasicCommsManager object.
      *
      */
-    public BasicCommsManager()
+    public BasicCommsManager(PrintWriter bug)
     {
+        this.bug = bug;
     }
 
     /**
@@ -43,7 +58,11 @@
     {
         if (url == null)
             throw new IllegalArgumentException("url null");
-        return getSingleRequestor(url.getHost(), url.getPort());
+        int port = url.getPort();
+        if (port == -1)
+            port = 80;
+        SingleRequestor req = getSingleRequestor(url.getHost(), port);
+        return req;
     }
 
     /**
@@ -57,7 +76,68 @@
     {
         SocketSupport.checkParams(host, port);
 
-        BasicRequestor req = new BasicRequestor(host, port);
+        Requestor req = new Requestor(host, port);
         return req;
     }
+
+    /**
+     * Provides SingleRequestor that can pass a single request to a server.
+     *
+     */
+    class Requestor
+        implements SingleRequestor
+    {
+        /**
+         * The location of the remote.
+         */
+        protected String host;
+        protected int    port;
+         
+        /**
+         * Create a new Requestor object.
+         *
+         * @param host is the domain name or IP number for the remote node
+         * @param port is the port to connect to
+         */
+        public Requestor(String host, int port)
+        {
+            this.host = host;
+            this.port = port;
+        }
+          
+        public int max  = SocketSupport.MAX;
+        /** Set the maximum number of characters to be returned.  */
+        public void setMax(int max)               { this.max = max; }
+        public int  getMax()                      { return max; }
+            
+        public String   getHost()                 { return host; }
+        public int      getPort()                 { return port; }
+         
+        private boolean used = false;
+        public boolean  isUsed()                  { return used; }
+    
+          
+         
+        /**
+         *  Send the packet to host/port, and
+         *  return the single packet that returns.
+         *  Only getMax() bytes or less will be returned.
+         *
+         *  @param packet a (non-null) data set to send to the socket
+         */
+        public byte[] get(byte[] packet)
+            throws RawException
+        {
+            if (used)
+                throw new RawUsedException("already used");
+            if (packet == null)
+                throw new IllegalArgumentException("null packet?");
+            used = true;
+    
+            logmsg(host + ":" + port + " (" + packet.length + "," + max +")");
+            byte[] buf = SocketSupport.get(host, port, packet, max);
+            return buf;
+        }
+    }
+
 }



1.3       +2 -2      java/webfunds/comms/HttpSocketAgent.java

Index: HttpSocketAgent.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/comms/HttpSocketAgent.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HttpSocketAgent.java	2001/04/02 18:42:03	1.2
+++ HttpSocketAgent.java	2001/04/08 19:15:07	1.3
@@ -1,5 +1,5 @@
 /*
- * $Id: HttpSocketAgent.java,v 1.2 2001/04/02 18:42:03 iang Exp $
+ * $Id: HttpSocketAgent.java,v 1.3 2001/04/08 19:15:07 iang Exp $
  *
  * Copyright (c) 1995-2001 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -39,7 +39,7 @@
     {
         debug(bug, "             a= ");
         this.url = url;
-        comms = new BasicCommsManager();
+        comms = new BasicCommsManager(bug);
     }
 
     /**