From 1fe40d7d0e73ac1a28a5707b5f4ed07dfcef9dd3 Mon Sep 17 00:00:00 2001 From: Omar Ramadan Date: Mon, 5 Jan 2015 13:26:02 +0300 Subject: [PATCH 1/2] exposing SR tables through NodeManager --- JSONDB/JSONDB.cpp | 31 +++++++++++++++++++++++++++++-- JSONDB/JSONDB.h | 1 + 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/JSONDB/JSONDB.cpp b/JSONDB/JSONDB.cpp index 095b741..6eff1a2 100644 --- a/JSONDB/JSONDB.cpp +++ b/JSONDB/JSONDB.cpp @@ -72,6 +72,25 @@ std::string JSONDB::generateWhereClause(JsonBox::Object request) return ss.str(); } +std::string JSONDB::generateSelectFields(JsonBox::Object request) +{ + std::stringstream ss; + std::vector fields_str; + + JsonBox::Array fields = request["fields"].getArray(); + + if (fields.size() > 0) { + JsonBox::Array::iterator jit; + for (jit = fields.begin(); jit != fields.end(); jit++) { + fields_str.push_back(jit->getString()); + } + ss << implode(", ", fields_str); + } else { + ss << "*"; + } + return ss.str(); +} + std::string JSONDB::implode(std::string delimiter, std::vector pieces) { std::stringstream ss; @@ -289,6 +308,10 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries) } else if (action.compare("read") == 0) { q << "select sip_buddies.username as \"imsi\", sip_buddies.name as \"name\", dialdata_table.exten as \"msisdn\" from sip_buddies left outer join dialdata_table on sip_buddies.username = dialdata_table.dial"; + + // add match clause + q << generateWhereClause(request); + return read(q.str()); } else if (action.compare("update") == 0) { @@ -403,11 +426,15 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries) // READ } else if (action.compare("read") == 0) { // start query - q << "select * from " << table; + q << "select "; + + // add select fields + q << generateSelectFields(request); + + q << " from " << table; // add match clause q << generateWhereClause(request); - return read(q.str()); // UPDATE diff --git a/JSONDB/JSONDB.h b/JSONDB/JSONDB.h index 319da4a..1b6e192 100644 --- a/JSONDB/JSONDB.h +++ b/JSONDB/JSONDB.h @@ -37,6 +37,7 @@ class JSONDB { sqlite3* mDB; std::string generateWhereClause(JsonBox::Object request); + std::string generateSelectFields(JsonBox::Object request); std::string implode(std::string delimiter, std::vector pieces); JsonBox::Object read(std::string query, unsigned retries = 5); JsonBox::Object execOnly(std::string query, unsigned retries = 5); From 56861b1a052bb5fae413d507b034346d48b2b615 Mon Sep 17 00:00:00 2001 From: Omar Ramadan Date: Wed, 14 Jan 2015 22:11:47 -0800 Subject: [PATCH 2/2] adding ipaddr/port to subscriber entries --- JSONDB/JSONDB.cpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/JSONDB/JSONDB.cpp b/JSONDB/JSONDB.cpp index 6eff1a2..fc78311 100644 --- a/JSONDB/JSONDB.cpp +++ b/JSONDB/JSONDB.cpp @@ -277,6 +277,22 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries) } std::string msisdn = jit->second.getString(); + jit = fields.find("ipaddr"); + if (jit == fields.end()) { + response["code"] = JsonBox::Value(406); + response["data"] = JsonBox::Value("missing ipaddr"); + return response; + } + std::string ipaddr = jit->second.getString(); + + jit = fields.find("port"); + if (jit == fields.end()) { + response["code"] = JsonBox::Value(406); + response["data"] = JsonBox::Value("missing port"); + return response; + } + std::string port = jit->second.getString(); + jit = fields.find("ki"); if (jit == fields.end()) { response["code"] = JsonBox::Value(406); @@ -289,8 +305,8 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries) JsonBox::Object responseSIP; JsonBox::Object responseDIAL; - queryTMP << "insert into sip_buddies (username, name, callerid, ki, host, allow, ipaddr)"; - queryTMP << " values (\"" << imsi << "\",\"" << name << "\",\"" << msisdn << "\",\"" << ki << "\",\"dynamic\",\"gsm\",\"127.0.0.1\")"; + queryTMP << "insert into sip_buddies (username, name, callerid, ki, host, allow, ipaddr, port)"; + queryTMP << " values (\"" << imsi << "\",\"" << name << "\",\"" << msisdn << "\",\"" << ki << "\",\"dynamic\",\"gsm\",\"" << ipaddr << "\",\"" << port << "\" )"; responseSIP = execOnly(queryTMP.str()); queryTMP.str(""); @@ -343,11 +359,26 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries) } std::string msisdn = jit->second.getString(); + jit = fields.find("ipaddr"); + if (jit == fields.end()) { + response["code"] = JsonBox::Value(406); + response["data"] = JsonBox::Value("missing ipaddr"); + return response; + } + std::string ipaddr = jit->second.getString(); + + jit = fields.find("port"); + if (jit == fields.end()) { + response["code"] = JsonBox::Value(406); + response["data"] = JsonBox::Value("missing port"); + return response; + } + std::string port = jit->second.getString(); std::stringstream queryTMP; JsonBox::Object responseSIP; JsonBox::Object responseDIAL; - queryTMP << "update sip_buddies set name=\"" << name << "\", callerid=\"" << msisdn << "\" where username=\"" << imsi << "\""; + queryTMP << "update sip_buddies set name=\"" << name << "\", callerid=\"" << msisdn << "\", ipaddr=\"" << ipaddr << "\", port=\"" << port << "\" where username=\"" << imsi << "\""; responseSIP = execOnly(queryTMP.str()); queryTMP.str("");