| 1 | // |
| 2 | // libtgvoip is free and unencumbered public domain software. |
| 3 | // For more information, see http://unlicense.org or the UNLICENSE file |
| 4 | // you should have received with this source code distribution. |
| 5 | // |
| 6 | |
| 7 | #include "VoIPServerConfig.h" |
| 8 | #include <stdlib.h> |
| 9 | #include "logging.h" |
| 10 | #include <sstream> |
| 11 | #include <locale> |
| 12 | |
| 13 | using namespace tgvoip; |
| 14 | |
| 15 | ServerConfig* ServerConfig::sharedInstance=NULL; |
| 16 | |
| 17 | ServerConfig::ServerConfig(){ |
| 18 | } |
| 19 | |
| 20 | ServerConfig::~ServerConfig(){ |
| 21 | } |
| 22 | |
| 23 | ServerConfig *ServerConfig::GetSharedInstance(){ |
| 24 | if(!sharedInstance) |
| 25 | sharedInstance=new ServerConfig(); |
| 26 | return sharedInstance; |
| 27 | } |
| 28 | |
| 29 | bool ServerConfig::GetBoolean(std::string name, bool fallback){ |
| 30 | MutexGuard sync(mutex); |
| 31 | if(ContainsKey(name) && config[name].is_bool()) |
| 32 | return config[name].bool_value(); |
| 33 | return fallback; |
| 34 | } |
| 35 | |
| 36 | double ServerConfig::GetDouble(std::string name, double fallback){ |
| 37 | MutexGuard sync(mutex); |
| 38 | if(ContainsKey(name) && config[name].is_number()) |
| 39 | return config[name].number_value(); |
| 40 | return fallback; |
| 41 | } |
| 42 | |
| 43 | int32_t ServerConfig::GetInt(std::string name, int32_t fallback){ |
| 44 | MutexGuard sync(mutex); |
| 45 | if(ContainsKey(name) && config[name].is_number()) |
| 46 | return config[name].int_value(); |
| 47 | return fallback; |
| 48 | } |
| 49 | |
| 50 | std::string ServerConfig::GetString(std::string name, std::string fallback){ |
| 51 | MutexGuard sync(mutex); |
| 52 | if(ContainsKey(name) && config[name].is_string()) |
| 53 | return config[name].string_value(); |
| 54 | return fallback; |
| 55 | } |
| 56 | |
| 57 | void ServerConfig::Update(std::string jsonString){ |
| 58 | MutexGuard sync(mutex); |
| 59 | LOGD("=== Updating voip config ==="); |
| 60 | LOGD("%s", jsonString.c_str()); |
| 61 | std::string jsonError; |
| 62 | config=json11::Json::parse(jsonString, jsonError); |
| 63 | if(!jsonError.empty()) |
| 64 | LOGE("Error parsing server config: %s", jsonError.c_str()); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | bool ServerConfig::ContainsKey(std::string key){ |
| 69 | return config.object_items().find(key)!=config.object_items().end(); |
| 70 | } |
| 71 | |