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

Author: Václav Kubart <[email protected]>
Committer: Václav Kubart <[email protected]>
Date:   Fri Feb  3 16:20:34 2012 +0100

added json support

Internal data structure holding parameter values changed from std::map to AmArg
(json support was already existing for it).

---

 apps/sbc/call_control/rest/RestModule.cpp |   67 ++++++++++++++++++----------
 apps/sbc/call_control/rest/RestParams.cpp |   33 +++++++++-----
 apps/sbc/call_control/rest/RestParams.h   |    2 +-
 3 files changed, 66 insertions(+), 36 deletions(-)

diff --git a/apps/sbc/call_control/rest/RestModule.cpp 
b/apps/sbc/call_control/rest/RestModule.cpp
index c0fe51f..6de853f 100644
--- a/apps/sbc/call_control/rest/RestModule.cpp
+++ b/apps/sbc/call_control/rest/RestModule.cpp
@@ -175,6 +175,25 @@ void RestModule::invoke(const string& method, const AmArg& 
args, AmArg& ret)
     throw AmDynInvoke::NotImplemented(method);
 }
 
+static RestParams::Format getFormat(const AmArg &values, RestParams::Format 
_default)
+{
+  if (!values.hasMember("format")) return _default;
+
+  const AmArg &a = values["format"];
+  if (!isArgCStr(a)) throw string("configuration error: wrong arg type\n");
+
+  const char *str = a.asCStr();
+  if (str) {
+    if (strcmp(str, "text") == 0) return RestParams::TEXT;
+    if (strcmp(str, "json") == 0) return RestParams::JSON;
+    if (strcmp(str, "xml") == 0) return RestParams::XML;
+  }
+
+  throw string("invalid format parameter value\n");
+  
+  return _default;
+}
+
 void RestModule::start(const string& cc_name, const string& ltag,
                       SBCCallProfile* call_profile,
                       int start_ts_sec, int start_ts_usec,
@@ -195,45 +214,45 @@ void RestModule::start(const string& cc_name, const 
string& ltag,
     url = values["url"].asCStr();
 
     RestParams params;
-    params.retrieve(url); // handle errors here if needed
+    if (params.retrieve(url, getFormat(values, RestParams::TEXT))) {
+      // parameters successfully read from server
 
-    params.getIfSet("ruri", call_profile->ruri);
-    params.getIfSet("from", call_profile->from);
-    params.getIfSet("to", call_profile->to);
-    params.getIfSet("contact", call_profile->contact);
-    params.getIfSet("call-id", call_profile->callid);
-    params.getIfSet("outbound_proxy", call_profile->outbound_proxy);
-    params.getIfSet("force_outbound_proxy", 
call_profile->force_outbound_proxy);
-    params.getIfSet("next_hop_ip", call_profile->next_hop_ip);
-    params.getIfSet("next_hop_port", call_profile->next_hop_port);
-    params.getIfSet("next_hop_for_replies", 
call_profile->next_hop_for_replies);
+      params.getIfSet("ruri", call_profile->ruri);
+      params.getIfSet("from", call_profile->from);
+      params.getIfSet("to", call_profile->to);
+      params.getIfSet("contact", call_profile->contact);
+      params.getIfSet("call-id", call_profile->callid);
+      params.getIfSet("outbound_proxy", call_profile->outbound_proxy);
+      params.getIfSet("force_outbound_proxy", 
call_profile->force_outbound_proxy);
+      params.getIfSet("next_hop_ip", call_profile->next_hop_ip);
+      params.getIfSet("next_hop_port", call_profile->next_hop_port);
+      params.getIfSet("next_hop_for_replies", 
call_profile->next_hop_for_replies);
 
-    // TODO: headerfilter, messagefilter
-    // sdpfilter, anonymize_sdp
+      // TODO: headerfilter, messagefilter
+      // sdpfilter, anonymize_sdp
 
-    params.getIfSet("sst_enabled", call_profile->sst_enabled);
-    //doesn't work:params.getIfSet("sst_aleg_enabled", 
call_profile->sst_aleg_enabled);
+      params.getIfSet("sst_enabled", call_profile->sst_enabled);
+      //doesn't work:params.getIfSet("sst_aleg_enabled", 
call_profile->sst_aleg_enabled);
 
-    // TODO: autho, auth_aleg, reply translations
+      // TODO: autho, auth_aleg, reply translations
 
-    params.getIfSet("append_headers", call_profile->append_headers); // CRLF 
is handled in SBC
+      params.getIfSet("append_headers", call_profile->append_headers); // CRLF 
is handled in SBC
 
-    //doesn't work: params.getIfSet("refuse_with", call_profile->refuse_with);
+      //doesn't work: params.getIfSet("refuse_with", 
call_profile->refuse_with);
 
-    // TODO: rtprelay, symmetric_rtp, ...
-    params.getIfSet("rtprelay_interface", call_profile->rtprelay_interface);
-    params.getIfSet("aleg_rtprelay_interface", 
call_profile->aleg_rtprelay_interface);
+      // TODO: rtprelay, symmetric_rtp, ...
+      params.getIfSet("rtprelay_interface", call_profile->rtprelay_interface);
+      params.getIfSet("aleg_rtprelay_interface", 
call_profile->aleg_rtprelay_interface);
 
-    params.getIfSet("outbound_interface", call_profile->outbound_interface);
+      params.getIfSet("outbound_interface", call_profile->outbound_interface);
+    }
   }
   catch (string &err) {
     ERROR(err.c_str());
     res_cmd[SBC_CC_ACTION] = SBC_CC_REFUSE_ACTION;
     res_cmd[SBC_CC_REFUSE_CODE] = 500;
     res_cmd[SBC_CC_REFUSE_REASON] = "REST configuration error";
-    return;
   }
-
 }
 
 void RestModule::connect(const string& cc_name, const string& ltag,
diff --git a/apps/sbc/call_control/rest/RestParams.cpp 
b/apps/sbc/call_control/rest/RestParams.cpp
index b36fa46..a600687 100644
--- a/apps/sbc/call_control/rest/RestParams.cpp
+++ b/apps/sbc/call_control/rest/RestParams.cpp
@@ -2,6 +2,7 @@
 #include "log.h"
 #include <curl/curl.h>
 #include <map>
+#include "jsonArg.h"
 
 using namespace std;
 
@@ -16,25 +17,33 @@ static void trim_spaces(string &s)
   else s.erase(s.begin(), s.end());
 }
 
-static bool str2bool(const string &s)
+static bool str2bool(const char *s)
 {
-  if (s.empty()) return true; // understand as just bool option which should 
be true
-  if ((s == "yes") || (s == "1") || (s == "true")) return true;
-  else return false;
+  if ((!s) || (!*s)) return true; // understand as just bool option which 
should be true
+  if (strcasecmp(s, "yes") == 0) return true;
+  if (strcasecmp(s, "true") == 0) return true;
+  if (strcmp(s, "1") == 0) return true;
+  
+  return false;
 }
 
 
////////////////////////////////////////////////////////////////////////////////
 
 void RestParams::getIfSet(const char *param_name, string &dst)
 {
-  map<string, string>::iterator i = params.find(param_name);
-  if (i != params.end()) dst = i->second;
+  if (params.hasMember(param_name)) {
+    const AmArg &a = params[param_name];
+    if (isArgCStr(a)) dst = a.asCStr();
+  }
 }
 
 void RestParams::getIfSet(const char *param_name, bool &dst)
 {
-  map<string, string>::iterator i = params.find(param_name);
-  if (i != params.end()) dst = str2bool(i->second);
+  if (params.hasMember(param_name)) {
+    const AmArg &a = params[param_name];
+    if (isArgCStr(a)) dst = str2bool(a.asCStr());
+    if (isArgBool(a)) dst = a.asBool();
+  }
 }
 
 void RestParams::handleParamLine(const string &line, size_t begin, size_t end)
@@ -53,16 +62,16 @@ void RestParams::handleParamLine(const string &line, size_t 
begin, size_t end)
 
   if (name.size() > 0) {
     DBG("REST: param %s='%s'\n", name.c_str(), value.c_str());
-    params[name] = value;
+    params.push(name, AmArg(value));
   }
 }
     
 bool RestParams::readFromText(const string &data)
 {
-  // TODO: read as XML instead of this internal format
   size_t first = 0;
   size_t last;
 
+  params.assertStruct();
   while (true) { 
     last = data.find('\n', first);
     if (last == string::npos) {
@@ -72,12 +81,14 @@ bool RestParams::readFromText(const string &data)
     else handleParamLine(data, first, last);
     first = last + 1;
   }
+
+  string dbg = arg2json(params);
   return true;
 }
 
 bool RestParams::readFromJson(const string &data)
 {
-  return false; // not implemented yet
+  return json2arg(data, params);
 }
 
 bool RestParams::readFromXML(const string &data)
diff --git a/apps/sbc/call_control/rest/RestParams.h 
b/apps/sbc/call_control/rest/RestParams.h
index f8abc47..b68f45b 100644
--- a/apps/sbc/call_control/rest/RestParams.h
+++ b/apps/sbc/call_control/rest/RestParams.h
@@ -9,7 +9,7 @@ class RestParams {
     enum Format { JSON, XML, TEXT };
 
   protected:
-    map<string, string> params;
+    AmArg params;
 
     // parsing data
     void handleParamLine(const string &line, size_t begin, size_t end);

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

Reply via email to