Module: sems
Branch: kubartv/cc_rest
Commit: 21bfe4ca51168654aac296f46eaa6dae810d3c89
URL:    
http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=21bfe4ca51168654aac296f46eaa6dae810d3c89

Author: Václav Kubart <[email protected]>
Committer: Václav Kubart <[email protected]>
Date:   Fri Feb  3 14:39:26 2012 +0100

code cleanup

---

 apps/sbc/call_control/rest/RestModule.cpp |   16 ++-------
 apps/sbc/call_control/rest/RestParams.cpp |   57 ++++++++++++++++++++---------
 apps/sbc/call_control/rest/RestParams.h   |   20 +++++++----
 3 files changed, 55 insertions(+), 38 deletions(-)

diff --git a/apps/sbc/call_control/rest/RestModule.cpp 
b/apps/sbc/call_control/rest/RestModule.cpp
index 56d561c..c0fe51f 100644
--- a/apps/sbc/call_control/rest/RestModule.cpp
+++ b/apps/sbc/call_control/rest/RestModule.cpp
@@ -184,7 +184,6 @@ void RestModule::start(const string& cc_name, const string& 
ltag,
 
   try {
     string url;
-    bool ignore_errors = true;
 
     if (!values.hasMember("url")) 
       throw string("configuration error: url must be configured for REST 
queries\n");
@@ -193,20 +192,11 @@ void RestModule::start(const string& cc_name, const 
string& ltag,
       throw string("configuration error: invalid value of url\n");
     }
 
-    /*FIXME
-      if (values.hasMember("ignore_errors")) {
-
-      if (!isArgBool(values["ignore_errors"])) {
-       throw string("configuration error: invalid value of parameter 
ignore_errors\n");
-      }
-
-      ignore_errors = values["ignore_errors"].asBool();
-    }*/
-
     url = values["url"].asCStr();
-    DBG("REST: url = %s\n", url.c_str());
 
-    RestParams params(url, ignore_errors);
+    RestParams params;
+    params.retrieve(url); // handle errors here if needed
+
     params.getIfSet("ruri", call_profile->ruri);
     params.getIfSet("from", call_profile->from);
     params.getIfSet("to", call_profile->to);
diff --git a/apps/sbc/call_control/rest/RestParams.cpp 
b/apps/sbc/call_control/rest/RestParams.cpp
index d2e827a..b36fa46 100644
--- a/apps/sbc/call_control/rest/RestParams.cpp
+++ b/apps/sbc/call_control/rest/RestParams.cpp
@@ -57,7 +57,7 @@ void RestParams::handleParamLine(const string &line, size_t 
begin, size_t end)
   }
 }
     
-void RestParams::processData(const string &data)
+bool RestParams::readFromText(const string &data)
 {
   // TODO: read as XML instead of this internal format
   size_t first = 0;
@@ -72,15 +72,36 @@ void RestParams::processData(const string &data)
     else handleParamLine(data, first, last);
     first = last + 1;
   }
+  return true;
 }
 
-RestParams::RestParams(const string &url, bool _ignore_errors): 
-  ignore_errors(_ignore_errors)
+bool RestParams::readFromJson(const string &data)
+{
+  return false; // not implemented yet
+}
+
+bool RestParams::readFromXML(const string &data)
+{
+  // TODO
+  ERROR("REST: trying to decode XML data - not implemented yet!\n");
+  return false;
+}
+
+bool RestParams::retrieve(const string &url, Format fmt)
 {
   string data;
+    
+  DBG("REST: reading from url %s\n", url.c_str());
+
+  if (!get(url, data)) return false;
 
-  retrieve(url, data);
-  processData(data);
+  switch (fmt) {
+    case TEXT: return readFromText(data);
+    case JSON: return readFromJson(data);
+    case XML: return readFromXML(data);
+  }
+
+  return false;
 }
 
 static size_t store_data_cb(void *contents, size_t size, size_t nmemb, void 
*userp)
@@ -98,7 +119,7 @@ static size_t store_data_cb(void *contents, size_t size, 
size_t nmemb, void *use
   return realsize;
 }
 
-void RestParams::retrieve(const string &url, string &data)
+bool RestParams::get(const string &url, string &data)
 {
   // based on http://curl.haxx.se/libcurl/c/getinmemory.html
   CURL *curl_handle = curl_easy_init();
@@ -118,18 +139,18 @@ void RestParams::retrieve(const string &url, string &data)
   
   curl_easy_cleanup(curl_handle);
 
-  long code = 0;
-  if (res == 0) curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &code);
-  if ((res != 0) || (code < 200) || (code > 299)) {
-    DBG("libcurl returned %d, response code: %ld\n", res, code);
-
-    if (!ignore_errors) {
-      string s("error returned when reading data from ");
-      s += url;
-      s += "\n";
-      throw s;
-    }
+  if (res != 0) {
+    // really ignore these errors?
+    DBG("libcurl returned error %d\n", res);
+    return false;
+  }
 
-    data.clear(); // no data available
+  long code = 0;
+  curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &code);
+  if ((code < 200) || (code > 299)) {
+    DBG("non-ok response code when downloading data: %ld\n", code);
+    return false;
   }
+
+  return true;
 }
diff --git a/apps/sbc/call_control/rest/RestParams.h 
b/apps/sbc/call_control/rest/RestParams.h
index 8b89860..f8abc47 100644
--- a/apps/sbc/call_control/rest/RestParams.h
+++ b/apps/sbc/call_control/rest/RestParams.h
@@ -5,21 +5,27 @@
 #include "SBCCallProfile.h"
 
 class RestParams {
+  public:
+    enum Format { JSON, XML, TEXT };
+
   protected:
     map<string, string> params;
-    bool ignore_errors;
 
     // parsing data
     void handleParamLine(const string &line, size_t begin, size_t end);
-    void processData(const string &data); // handle retrieved data
+    bool readFromText(const string &data); // read content in text format
+    bool readFromXML(const string &data); // read content in XML format
+    bool readFromJson(const string &data); // read content in json format
 
-    void retrieve(const std::string &url, std::string &dst); // retrieve data 
from given URL into dst
+    /* retrieve data from given URL into dst
+     * can throw an exception if error occurs (for example libcurl can not be
+     * initialized; non-ok reply/connect errors are not considered 
exceptional) */
+    bool get(const std::string &url, std::string &dst);
 
   public:
-    /* retrieve data stored at given URL and process them internally
-     * throws an exception if data can not be loaded
-     * retrieve errors are ignored if _ignore_errors is set */
-    RestParams(const std::string &url, bool _ignore_errors = true);
+    /* retrieve data from given URL and decode them using given format
+     * can throw an exception if something strange happens (see get) */
+    bool retrieve(const std::string &url, Format fmt = TEXT);
     
     // sets dst to value of given parameter if the parameter is set
     void getIfSet(const char *param_name, string &dst);

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

Reply via email to