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"
|
2018-07-01 23:42:49 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <locale>
|
2017-03-30 16:06:59 +02:00
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
using namespace tgvoip;
|
2017-03-30 16:06:59 +02:00
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
ServerConfig* ServerConfig::sharedInstance=NULL;
|
|
|
|
|
|
|
|
ServerConfig::ServerConfig(){
|
2017-03-30 16:06:59 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
ServerConfig::~ServerConfig(){
|
2017-03-30 16:06:59 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
ServerConfig *ServerConfig::GetSharedInstance(){
|
2017-03-30 16:06:59 +02:00
|
|
|
if(!sharedInstance)
|
2017-04-17 20:57:07 +02:00
|
|
|
sharedInstance=new ServerConfig();
|
2017-03-30 16:06:59 +02:00
|
|
|
return sharedInstance;
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
bool ServerConfig::GetBoolean(std::string name, bool fallback){
|
2017-04-28 13:17:56 +02:00
|
|
|
MutexGuard sync(mutex);
|
2017-03-30 16:06:59 +02:00
|
|
|
if(ContainsKey(name)){
|
|
|
|
std::string val=config[name];
|
|
|
|
if(val=="true")
|
|
|
|
return true;
|
|
|
|
if(val=="false")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
double ServerConfig::GetDouble(std::string name, double fallback){
|
2017-04-28 13:17:56 +02:00
|
|
|
MutexGuard sync(mutex);
|
2017-03-30 16:06:59 +02:00
|
|
|
if(ContainsKey(name)){
|
|
|
|
std::string val=config[name];
|
2018-07-01 23:42:49 +02:00
|
|
|
std::istringstream stm(val);
|
|
|
|
double rval=fallback;
|
|
|
|
stm.imbue(std::locale("C"));
|
|
|
|
stm >> rval;
|
|
|
|
if(!stm.fail())
|
|
|
|
return rval;
|
2017-03-30 16:06:59 +02:00
|
|
|
}
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
int32_t ServerConfig::GetInt(std::string name, int32_t fallback){
|
2017-04-28 13:17:56 +02:00
|
|
|
MutexGuard sync(mutex);
|
2017-03-30 16:06:59 +02:00
|
|
|
if(ContainsKey(name)){
|
|
|
|
std::string val=config[name];
|
|
|
|
char* end;
|
|
|
|
const char* start=val.c_str();
|
|
|
|
int32_t d=strtol(start, &end, 0);
|
|
|
|
if(end!=start){
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
std::string ServerConfig::GetString(std::string name, std::string fallback){
|
2017-04-28 13:17:56 +02:00
|
|
|
MutexGuard sync(mutex);
|
2017-03-30 16:06:59 +02:00
|
|
|
if(ContainsKey(name))
|
|
|
|
return config[name];
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
void ServerConfig::Update(std::map<std::string, std::string> newValues){
|
2017-04-28 13:17:56 +02:00
|
|
|
MutexGuard sync(mutex);
|
2017-03-30 16:06:59 +02:00
|
|
|
LOGD("=== Updating voip config ===");
|
|
|
|
config.clear();
|
|
|
|
for(std::map<std::string, std::string>::iterator itr=newValues.begin();itr!=newValues.end();++itr){
|
|
|
|
std::string key=itr->first;
|
|
|
|
std::string val=itr->second;
|
|
|
|
LOGV("%s -> %s", key.c_str(), val.c_str());
|
|
|
|
config[key]=val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
void ServerConfig::Update(const char **values, int count) {
|
2017-03-30 16:06:59 +02:00
|
|
|
std::map<std::string, std::string> result;
|
|
|
|
for (int i = 0; i < count / 2; i++) {
|
|
|
|
result[values[i * 2 + 0]] = std::string(values[i * 2 + 1]);
|
|
|
|
}
|
|
|
|
Update(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
bool ServerConfig::ContainsKey(std::string key){
|
2017-03-30 16:06:59 +02:00
|
|
|
return config.find(key)!=config.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|