Author: sayer
Date: 2008-12-19 20:19:59 +0100 (Fri, 19 Dec 2008)
New Revision: 1214

Added:
   trunk/apps/xmlrpc2di/TOXmlRpcClient.cpp
   trunk/apps/xmlrpc2di/TOXmlRpcClient.h
Modified:
   trunk/apps/xmlrpc2di/Makefile
   trunk/apps/xmlrpc2di/XMLRPC2DI.cpp
   trunk/apps/xmlrpc2di/XMLRPC2DI.h
   trunk/apps/xmlrpc2di/etc/xmlrpc2di.conf
Log:
support for server timeout 



Modified: trunk/apps/xmlrpc2di/Makefile
===================================================================
--- trunk/apps/xmlrpc2di/Makefile       2008-12-19 19:12:57 UTC (rev 1213)
+++ trunk/apps/xmlrpc2di/Makefile       2008-12-19 19:19:59 UTC (rev 1214)
@@ -13,7 +13,7 @@
 # -D XMLRPCPP_SUPPORT_STRUCT_ACCESS
 
 # use these for local installation: 
- module_cflags  = -I$(XMLRPCPP_DIR)/src -DHAVE_XMLRPCPP_SSL
+ module_cflags  = -I$(XMLRPCPP_DIR)/src -DHAVE_XMLRPCPP_SSL -D 
XMLRPCPP_SUPPORT_STRUCT_ACCESS
  module_extra_objs = $(XMLRPCPP_DIR)/libXmlRpc.a
  extra_clean = clean_libxmlrpc
 # and comment module_ldflags line above

Added: trunk/apps/xmlrpc2di/TOXmlRpcClient.cpp
===================================================================
--- trunk/apps/xmlrpc2di/TOXmlRpcClient.cpp     2008-12-19 19:12:57 UTC (rev 
1213)
+++ trunk/apps/xmlrpc2di/TOXmlRpcClient.cpp     2008-12-19 19:19:59 UTC (rev 
1214)
@@ -0,0 +1,42 @@
+#include "XmlRpc.h"
+#include "TOXmlRpcClient.h"
+// Clear the referenced flag even if exceptions or errors occur.
+struct ClearFlagOnExit {
+  ClearFlagOnExit(bool& flag) : _flag(flag) {}
+  ~ClearFlagOnExit() { _flag = false; }
+  bool& _flag;
+};
+
+bool TOXmlRpcClient::execute(const char* method, XmlRpcValue const& params, 
XmlRpcValue& result, double timeout) {
+
+  XmlRpcUtil::log(1, "XmlRpcClient::execute: method %s (_connectionState 
%d).", method, _connectionState);
+
+  // This is not a thread-safe operation, if you want to do multithreading, 
use separate
+  // clients for each thread. If you want to protect yourself from multiple 
threads
+  // accessing the same client, replace this code with a real mutex.
+  if (_executing)
+    return false;
+
+  _executing = true;
+  ClearFlagOnExit cf(_executing);
+
+  _sendAttempts = 0;
+  _isFault = false;
+
+  if ( ! setupConnection())
+    return false;
+
+  if ( ! generateRequest(method, params))
+    return false;
+
+  result.clear();
+  //  double msTime = -1.0;   // Process until exit is called
+  _disp.work(timeout);
+
+  if (_connectionState != IDLE || ! parseResponse(result))
+    return false;
+
+  XmlRpcUtil::log(1, "XmlRpcClient::execute: method %s completed.", method);
+  _response = "";
+  return true;
+}


Property changes on: trunk/apps/xmlrpc2di/TOXmlRpcClient.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/apps/xmlrpc2di/TOXmlRpcClient.h
===================================================================
--- trunk/apps/xmlrpc2di/TOXmlRpcClient.h       2008-12-19 19:12:57 UTC (rev 
1213)
+++ trunk/apps/xmlrpc2di/TOXmlRpcClient.h       2008-12-19 19:19:59 UTC (rev 
1214)
@@ -0,0 +1,24 @@
+#ifndef _TO_XMLRPCCLIENT_H
+#define _TO_XMLRPCCLIENT_H
+#include "XmlRpcClient.h"
+using namespace XmlRpc;
+
+/* xmlrpc client with timeout */
+class TOXmlRpcClient : public XmlRpc::XmlRpcClient {
+ public:
+  TOXmlRpcClient(const char* host, int port, const char* uri=0
+#ifdef HAVE_XMLRPCPP_SSL
+                , bool ssl=false
+#endif
+                )
+    : XmlRpcClient(host, port, uri
+#ifdef HAVE_XMLRPCPP_SSL
+                  , ssl
+#endif
+                  ) { }
+
+  bool execute(const char* method, XmlRpcValue const& params, 
+              XmlRpcValue& result, double timeout);
+};
+
+#endif


Property changes on: trunk/apps/xmlrpc2di/TOXmlRpcClient.h
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: trunk/apps/xmlrpc2di/XMLRPC2DI.cpp
===================================================================
--- trunk/apps/xmlrpc2di/XMLRPC2DI.cpp  2008-12-19 19:12:57 UTC (rev 1213)
+++ trunk/apps/xmlrpc2di/XMLRPC2DI.cpp  2008-12-19 19:19:59 UTC (rev 1214)
@@ -32,6 +32,7 @@
 #include "AmUtils.h"
 #include "AmArg.h"
 #include "AmSession.h"
+#include "TOXmlRpcClient.h"
 
 #define MOD_NAME "xmlrpc2di"
 
@@ -43,6 +44,8 @@
 // retry a failed server after 10 seconds
 unsigned int XMLRPC2DI::ServerRetryAfter = 10; 
 
+double XMLRPC2DI::ServerTimeout = -1;
+
 XMLRPC2DI* XMLRPC2DI::instance()
 {
   if(_instance == NULL){
@@ -94,6 +97,21 @@
   ServerRetryAfter = cfg.getParameterInt("server_retry_after", 10);
   DBG("retrying failed server after %u seconds\n", ServerRetryAfter);
 
+  
+  string server_timeout = cfg.getParameter("server_timeout");
+  if (!server_timeout.empty()) {
+    unsigned int server_timeout_i = 0;
+    if (str2i(server_timeout, server_timeout_i)) {
+      ERROR("could not understand server_timeout=%s\n", 
+           server_timeout.c_str());
+      return -1;
+    }
+    
+    if (server_timeout_i) {
+      ServerTimeout = (double)server_timeout_i/1000.0; // in millisec
+    }
+  }
+
   string run_server = cfg.getParameter("run_server","yes");
   if (run_server != "yes") {
     DBG("XMLRPC server will not be started.\n");
@@ -203,7 +221,7 @@
       ret.push("no active connections");
       return;
     }
-    XmlRpcClient c((const char*)srv->server.c_str(), (int)srv->port, 
+    TOXmlRpcClient c((const char*)srv->server.c_str(), (int)srv->port, 
                   (const char*)srv->uri.empty()?NULL:srv->uri.c_str()
 #ifdef HAVE_XMLRPCPP_SSL
                   , false
@@ -212,7 +230,8 @@
 
     XmlRpcValue x_args, x_result;
     XMLRPC2DIServer::amarg2xmlrpcval(params, x_args);
-    if (c.execute(method.c_str(), x_args, x_result) &&  !c.isFault()) {
+
+    if (c.execute(method.c_str(), x_args, x_result, XMLRPC2DI::ServerTimeout) 
&&  !c.isFault()) {
       DBG("successfully executed method %s on server %s:%d\n",
          method.c_str(), srv->server.c_str(), srv->port);
       ret.push(0);
@@ -239,7 +258,7 @@
       ret.push("no active connections");
       return;
     }
-    XmlRpcClient c((const char*)srv->server.c_str(), (int)srv->port, 
+    TOXmlRpcClient c((const char*)srv->server.c_str(), (int)srv->port, 
                   (const char*)srv->uri.empty()?NULL:srv->uri.c_str()
 #ifdef HAVE_XMLRPCPP_SSL
                   , false
@@ -253,7 +272,7 @@
       XMLRPC2DIServer::amarg2xmlrpcval(args.get(i), x_args[i-2]);
     }
 
-    if (c.execute(method.c_str(), x_args, x_result) &&  !c.isFault()) {
+    if (c.execute(method.c_str(), x_args, x_result, XMLRPC2DI::ServerTimeout) 
&&  !c.isFault()) {
       DBG("successfully executed method %s on server %s:%d\n",
          method.c_str(), srv->server.c_str(), srv->port);
       ret.push(0);
@@ -476,13 +495,13 @@
        xmlrpcval2amarg(v[i], a[a.size()-1], 0);
       } break;
 #ifdef XMLRPCPP_SUPPORT_STRUCT_ACCESS
-      case XmlRpcValue::TypeStruct: {  
-       for (XmlRpc::XmlRpcValue::ValueStruct::iterator it=
-                ((XmlRpcValue::ValueStruct)v).begin(); 
-            it != ((XmlRpcValue::ValueStruct)v).end(); it++) {     
-           a[it->first] = AmArg();
-           xmlrpcval2amarg(it->second, a[it->first], 0);
-         }
+      case XmlRpcValue::TypeStruct: {
+       for (XmlRpc::XmlRpcValue::ValueStruct::iterator it=
+                ((XmlRpcValue::ValueStruct)v).begin();
+            it != ((XmlRpcValue::ValueStruct)v).end(); it++) {
+           a[it->first] = AmArg();
+           xmlrpcval2amarg(it->second, a[it->first], 0);
+         }
       } break;
 #endif
 

Modified: trunk/apps/xmlrpc2di/XMLRPC2DI.h
===================================================================
--- trunk/apps/xmlrpc2di/XMLRPC2DI.h    2008-12-19 19:12:57 UTC (rev 1213)
+++ trunk/apps/xmlrpc2di/XMLRPC2DI.h    2008-12-19 19:19:59 UTC (rev 1214)
@@ -157,6 +157,8 @@
              const AmArg& args, AmArg& ret);
 
   static unsigned int ServerRetryAfter;
+  static double ServerTimeout;
+
 };
 
 #endif

Modified: trunk/apps/xmlrpc2di/etc/xmlrpc2di.conf
===================================================================
--- trunk/apps/xmlrpc2di/etc/xmlrpc2di.conf     2008-12-19 19:12:57 UTC (rev 
1213)
+++ trunk/apps/xmlrpc2di/etc/xmlrpc2di.conf     2008-12-19 19:19:59 UTC (rev 
1214)
@@ -25,3 +25,7 @@
 # run the XMLRPC server at all (default: yes)
 #
 # run_server=yes 
+
+# timeout for client requests, in milliseconds (0 to disable)
+#
+# server_timeout=500

_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to