1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-27 04:34:42 +01:00
libtgvoip/VoIPServerConfig.cpp

71 lines
1.8 KiB
C++
Raw Normal View History

2017-03-30 16:06:59 +02:00
//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#include "VoIPServerConfig.h"
#include <stdlib.h>
#include "logging.h"
#include <sstream>
#include <locale>
2017-03-30 16:06:59 +02:00
using namespace tgvoip;
2017-03-30 16:06:59 +02:00
ServerConfig* ServerConfig::sharedInstance=NULL;
ServerConfig::ServerConfig(){
2017-03-30 16:06:59 +02:00
}
ServerConfig::~ServerConfig(){
2017-03-30 16:06:59 +02:00
}
ServerConfig *ServerConfig::GetSharedInstance(){
2017-03-30 16:06:59 +02:00
if(!sharedInstance)
sharedInstance=new ServerConfig();
2017-03-30 16:06:59 +02:00
return sharedInstance;
}
bool ServerConfig::GetBoolean(std::string name, bool fallback){
MutexGuard sync(mutex);
if(ContainsKey(name) && config[name].is_bool())
return config[name].bool_value();
2017-03-30 16:06:59 +02:00
return fallback;
}
double ServerConfig::GetDouble(std::string name, double fallback){
MutexGuard sync(mutex);
if(ContainsKey(name) && config[name].is_number())
return config[name].number_value();
2017-03-30 16:06:59 +02:00
return fallback;
}
int32_t ServerConfig::GetInt(std::string name, int32_t fallback){
MutexGuard sync(mutex);
if(ContainsKey(name) && config[name].is_number())
return config[name].int_value();
2017-03-30 16:06:59 +02:00
return fallback;
}
std::string ServerConfig::GetString(std::string name, std::string fallback){
MutexGuard sync(mutex);
if(ContainsKey(name) && config[name].is_string())
return config[name].string_value();
2017-03-30 16:06:59 +02:00
return fallback;
}
void ServerConfig::Update(std::string jsonString){
MutexGuard sync(mutex);
2017-03-30 16:06:59 +02:00
LOGD("=== Updating voip config ===");
LOGD("%s", jsonString.c_str());
std::string jsonError;
config=json11::Json::parse(jsonString, jsonError);
if(!jsonError.empty())
LOGE("Error parsing server config: %s", jsonError.c_str());
2017-03-30 16:06:59 +02:00
}
bool ServerConfig::ContainsKey(std::string key){
return config.object_items().find(key)!=config.object_items().end();
2017-03-30 16:06:59 +02:00
}