Revision: 4864
          http://sourceforge.net/p/vexi/code/4864
Author:   clrg
Date:     2016-06-20 15:57:09 +0000 (Mon, 20 Jun 2016)
Log Message:
-----------
Add timeout to RPC calls
- previously was -1 (infinite) but now defaults to 30s
- can be set (or get) via vexi.net.setDefaultTimeout (and .get)

Modified Paths:
--------------
    branches/vexi3/org.vexi-core.main/src/main/jpp/org/vexi/core/Vexi.jpp
    branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/HTTP.java
    branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/JreHTTP.java
    branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/OrigHTTP.java
    
branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/OrigHTTP_reconnect.java
    branches/vexi3/org.vexi-library.net/src/main/jpp/org/ibex/net/ApacheHTTP.jpp

Modified: branches/vexi3/org.vexi-core.main/src/main/jpp/org/vexi/core/Vexi.jpp
===================================================================
--- branches/vexi3/org.vexi-core.main/src/main/jpp/org/vexi/core/Vexi.jpp       
2016-06-19 15:52:18 UTC (rev 4863)
+++ branches/vexi3/org.vexi-core.main/src/main/jpp/org/vexi/core/Vexi.jpp       
2016-06-20 15:57:09 UTC (rev 4864)
@@ -10,6 +10,7 @@
 import org.ibex.js.Thread;
 import org.ibex.js.JSU.*;
 import org.vexi.js.*;
+import org.ibex.net.HTTP;
 import org.ibex.util.Cache;
 import org.ibex.util.Callable;
 import org.ibex.util.Encode;
@@ -404,6 +405,14 @@
          * @return(xmlrpc) */
         case "net.rpc.xml": return METHOD;
         
+        /* Set how long in milliseconds Vexi will attempt an RPC call before 
it times out
+         * @param(ms) */
+        case "net.getDefaultTimeout": return METHOD;
+        
+        /* Get how long in milliseconds Vexi will attempt an RPC call before 
it times out
+         * @return(number) */
+        case "net.getDefaultTimeout": return METHOD;
+        
         /* <p>Paramaters passed into vexi when launched can read as properties 
on this object</p> 
          * 
          * <pre> var host = params["host"];</pre>
@@ -779,6 +788,8 @@
                 case "crypto.rc4": /* FEATURE */ return null;
                 case "file.remove": ((Fountain.File)args[0]).remove(); return 
null;
                 case "js.stringify": return JSON.marshal(args[0]);
+                case "net.getDefaultTimeout": return 
JSU.N(HTTP.getDefaultSettings().getConnectTimeout());
+                case "net.setDefaultTimeout": 
HTTP.getDefaultSettings().setConnectTimeout(JSU.toInt(args[0])); return null;
                 case "net.rpc.xml": return new XMLRPC(Log.rpc, 
JSU.toString(args[0]), "");
                 case "net.rpc.soap": throw new JSExn("Not yet implemented");
                 case "regexp": return new JSRegexp(args[0], null);

Modified: 
branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/HTTP.java
===================================================================
--- branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/HTTP.java    
2016-06-19 15:52:18 UTC (rev 4863)
+++ branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/HTTP.java    
2016-06-20 15:57:09 UTC (rev 4864)
@@ -2,8 +2,11 @@
 
 import java.io.*;
 
-public interface HTTP {
-    static public class HTTPEntityInfo{
+import org.ibex.net.HTTP.HTTPSettings;
+
+public abstract class HTTP {
+       
+    static public class HTTPEntityInfo {
        final public int contentLength;       ///< the length of the entire 
content body; -1 if chunked
        final public String contentType;
         final public String lastModified;
@@ -28,15 +31,45 @@
        } 
     }
     
-    static public class HTTPResponse{
+    static public class HTTPResponse {
        final public HTTPEntityInfo info;
        final public InputStream body;
-       public HTTPResponse(HTTPEntityInfo info, InputStream body){
+       public HTTPResponse(HTTPEntityInfo info, InputStream body) {
                this.info = info;
                this.body = body;
        }
     }
 
-    public HTTPResponse GET() throws IOException;
-       public HTTPResponse POST(String mime, byte[] bytes) throws IOException;
+    abstract public HTTPResponse GET() throws IOException;
+    abstract public HTTPResponse POST(String mime, byte[] bytes) throws 
IOException;
+       
+
+       static public class HTTPSettings {
+               int connectTimeout; // in milliseconds
+               public void setConnectTimeout(int ms) { connectTimeout = ms; }
+               public int getConnectTimeout() { return connectTimeout; }
+               
+               public HTTPSettings(int ct) { connectTimeout = ct; }
+       }
+       
+       static private HTTPSettings defaultSettings = new HTTPSettings(30000);
+       private HTTPSettings settings;
+
+       static public HTTPSettings getDefaultSettings() {
+               return defaultSettings;
+       }
+       
+       public int getConnectTimeout() {
+               if (settings==null)
+                       return defaultSettings.getConnectTimeout();
+               return settings.getConnectTimeout();
+       }
+       
+       public void setConnectTimeout(int ms) {
+               if (settings==null)
+                       settings = new HTTPSettings(ms);
+               else if (ms>=0)
+                       settings.setConnectTimeout(ms);
+               else settings = null;
+       }
 }

Modified: 
branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/JreHTTP.java
===================================================================
--- branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/JreHTTP.java 
2016-06-19 15:52:18 UTC (rev 4863)
+++ branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/JreHTTP.java 
2016-06-20 15:57:09 UTC (rev 4864)
@@ -10,9 +10,9 @@
 import org.ibex.util.IOUtil;
 import org.ibex.util.Logger;
 
-public class JreHTTP implements HTTP {
+public class JreHTTP extends HTTP {
 
-       static synchronized JreHTTP create(Logger logger, String url) throws 
IOException{
+       static synchronized JreHTTP create(Logger logger, String url) throws 
IOException {
                if (url.indexOf("://") == -1)
                        throw new IOException("URLs must contain a ://");
                return new JreHTTP(logger, url);
@@ -29,7 +29,6 @@
                if (url.startsWith("https:")) {
                        ssl = true;
                } else if (!url.startsWith("http:")) {
-
                        throw new IOException("HTTP only supports http/https 
urls");
                }
        }
@@ -39,8 +38,9 @@
                try {
                        //Create connection
                        connection = newConnection();
+                       connection.setConnectTimeout(getConnectTimeout());
                        connection.setRequestMethod("GET");
-                       connection.setUseCaches (false);
+                       connection.setUseCaches(false);
                        connection.setDoInput(true);
                        connection.setDoOutput(false);
 
@@ -49,7 +49,7 @@
                } catch (IOException e) {
                        throw e;
                } finally{
-//                     if(connection!=null){
+//                     if (connection!=null) {
 //                             connection.disconnect();
 //                     }
                }
@@ -60,6 +60,7 @@
                try {
                        //Create connection
                        connection = newConnection();
+                       connection.setConnectTimeout(getConnectTimeout());
                        connection.setRequestMethod("POST");
                        connection.setRequestProperty("Content-Type", 
                                        contentType);
@@ -67,22 +68,22 @@
                        connection.setRequestProperty("Content-Length", "" + 
Integer.toString(content.length));
                        connection.setRequestProperty("Content-Language", 
"en-US");  
 
-                       connection.setUseCaches (false);
+                       connection.setUseCaches(false);
                        connection.setDoInput(true);
                        connection.setDoOutput(true);
 
                        //Send request
                        OutputStream wr = connection.getOutputStream();
                        wr.write(content);
-                       wr.flush ();
-                       wr.close ();
+                       wr.flush();
+                       wr.close();
 
                        //Get Response
                        return readResponse(connection);
                } catch (IOException e) {
                        throw e;
                } finally{
-//                     if(connection!=null){
+//                     if (connection!=null) {
 //                             connection.disconnect();
 //                     }
                }
@@ -93,22 +94,22 @@
                return (HttpURLConnection)url.openConnection();
        }
        
-       public HTTPResponse readResponse(HttpURLConnection connection) throws 
IOException{
+       public HTTPResponse readResponse(HttpURLConnection connection) throws 
IOException {
                int statusCode = connection.getResponseCode();
-               if(statusCode>=400){
+               if (statusCode>=400) {
                        InputStream is = connection.getErrorStream();
                        byte[] bytes = IOUtil.toByteArray(is); // HACK doesn't 
seem to work unless we read this here
                        HTTPEntityInfo info = new 
HTTPEntityInfo((int)bytes.length,"",connection.getContentType());
                        throw new 
HTTPErrorResponse(connection.getResponseMessage(), statusCode+"", bytes, info);
-               }else{
+               } else {
                        InputStream is = connection.getInputStream();
                        String lastmod = 
connection.getHeaderField("Last-Modified");
                        String lengthStr = 
connection.getHeaderField("Content-Length");
                        String contentTypeR = 
connection.getHeaderField("Content-Type");
                        int length;
-                       if(lengthStr!=null){
+                       if( lengthStr!=null) {
                                length = Integer.parseInt(lengthStr);
-                       }else{
+                       } else {
                                length = -1;
                        }
                        HTTPEntityInfo info = new HTTPEntityInfo(length, 
lastmod, contentTypeR);

Modified: 
branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/OrigHTTP.java
===================================================================
--- 
branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/OrigHTTP.java    
    2016-06-19 15:52:18 UTC (rev 4863)
+++ 
branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/OrigHTTP.java    
    2016-06-20 15:57:09 UTC (rev 4864)
@@ -14,7 +14,7 @@
  *  This object encapsulates a *single* HTTP connection. Multiple requests may 
be pipelined over a connection (thread-safe),
  *  although any IOException encountered in a request will invalidate all 
later requests.
  */
-public class OrigHTTP implements HTTP {
+public class OrigHTTP extends HTTP {
 
     public static InetAddress originAddr = null;
     public static String      originHost = null;

Modified: 
branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/OrigHTTP_reconnect.java
===================================================================
--- 
branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/OrigHTTP_reconnect.java
      2016-06-19 15:52:18 UTC (rev 4863)
+++ 
branches/vexi3/org.vexi-library.net/src/main/java/org/ibex/net/OrigHTTP_reconnect.java
      2016-06-20 15:57:09 UTC (rev 4864)
@@ -2,11 +2,13 @@
 
 import java.io.*;
 
+import org.ibex.net.HTTP.HTTPSettings;
 import org.ibex.util.Logger;
 
 // HACK - ideally we should fix the http object and allow it to be reused (
 // as this should enable piping)
-public class OrigHTTP_reconnect implements HTTP {
+public class OrigHTTP_reconnect extends HTTP {
+       
        final Logger logger;
        final String url;
        public OrigHTTP_reconnect(Logger logger, String url) {

Modified: 
branches/vexi3/org.vexi-library.net/src/main/jpp/org/ibex/net/ApacheHTTP.jpp
===================================================================
--- 
branches/vexi3/org.vexi-library.net/src/main/jpp/org/ibex/net/ApacheHTTP.jpp    
    2016-06-19 15:52:18 UTC (rev 4863)
+++ 
branches/vexi3/org.vexi-library.net/src/main/jpp/org/ibex/net/ApacheHTTP.jpp    
    2016-06-20 15:57:09 UTC (rev 4864)
@@ -16,7 +16,7 @@
 import org.apache.http.protocol.*;
 import org.ibex.util.*;
 
-public class ApacheHTTP implements HTTP {
+public class ApacheHTTP extends HTTP.Common {
        
        static private HttpClientBuilder builder;
        static synchronized HttpClientBuilder get(){

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports. http://sdm.link/zohomanageengine
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to