VoIPServerConfig.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include "VoIPServerConfig.h"
  7. #include <stdlib.h>
  8. #include "logging.h"
  9. #include <sstream>
  10. #include <locale>
  11. using namespace tgvoip;
  12. ServerConfig* ServerConfig::sharedInstance=NULL;
  13. ServerConfig::ServerConfig(){
  14. }
  15. ServerConfig::~ServerConfig(){
  16. }
  17. ServerConfig *ServerConfig::GetSharedInstance(){
  18. if(!sharedInstance)
  19. sharedInstance=new ServerConfig();
  20. return sharedInstance;
  21. }
  22. bool ServerConfig::GetBoolean(std::string name, bool fallback){
  23. MutexGuard sync(mutex);
  24. if(ContainsKey(name) && config[name].is_bool())
  25. return config[name].bool_value();
  26. return fallback;
  27. }
  28. double ServerConfig::GetDouble(std::string name, double fallback){
  29. MutexGuard sync(mutex);
  30. if(ContainsKey(name) && config[name].is_number())
  31. return config[name].number_value();
  32. return fallback;
  33. }
  34. int32_t ServerConfig::GetInt(std::string name, int32_t fallback){
  35. MutexGuard sync(mutex);
  36. if(ContainsKey(name) && config[name].is_number())
  37. return config[name].int_value();
  38. return fallback;
  39. }
  40. std::string ServerConfig::GetString(std::string name, std::string fallback){
  41. MutexGuard sync(mutex);
  42. if(ContainsKey(name) && config[name].is_string())
  43. return config[name].string_value();
  44. return fallback;
  45. }
  46. void ServerConfig::Update(std::string jsonString){
  47. MutexGuard sync(mutex);
  48. LOGD("=== Updating voip config ===");
  49. LOGD("%s", jsonString.c_str());
  50. std::string jsonError;
  51. config=json11::Json::parse(jsonString, jsonError);
  52. if(!jsonError.empty())
  53. LOGE("Error parsing server config: %s", jsonError.c_str());
  54. }
  55. bool ServerConfig::ContainsKey(std::string key){
  56. return config.object_items().find(key)!=config.object_items().end();
  57. }