File: VoIPServerConfig.cpp

package info (click to toggle)
libtgvoip 2.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 9,004 kB
  • sloc: cpp: 61,331; ansic: 24,725; sh: 4,150; makefile: 749; asm: 242
file content (111 lines) | stat: -rw-r--r-- 2,960 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// 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>
#include <utility>
#include "threading.h"
#include "json11.hpp"

using namespace tgvoip;

namespace tgvoip {
class ServerConfigImpl {
public:
	int32_t GetInt(std::string name, int32_t fallback);
	double GetDouble(std::string name, double fallback);
	std::string GetString(std::string name, std::string fallback);
	bool GetBoolean(std::string name, bool fallback);
	void Update(std::string jsonString);

private:
	bool ContainsKey(std::string key);
	json11::Json config;
	Mutex mutex;
};
}

namespace{
ServerConfig* sharedInstance=NULL;
}

bool ServerConfigImpl::GetBoolean(std::string name, bool fallback){
	MutexGuard sync(mutex);
	if(ContainsKey(name) && config[name].is_bool())
		return config[name].bool_value();
	return fallback;
}

double ServerConfigImpl::GetDouble(std::string name, double fallback){
	MutexGuard sync(mutex);
	if(ContainsKey(name) && config[name].is_number())
		return config[name].number_value();
	return fallback;
}

int32_t ServerConfigImpl::GetInt(std::string name, int32_t fallback){
	MutexGuard sync(mutex);
	if(ContainsKey(name) && config[name].is_number())
		return config[name].int_value();
	return fallback;
}

std::string ServerConfigImpl::GetString(std::string name, std::string fallback){
	MutexGuard sync(mutex);
	if(ContainsKey(name) && config[name].is_string())
		return config[name].string_value();
	return fallback;
}

void ServerConfigImpl::Update(std::string jsonString){
	MutexGuard sync(mutex);
	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());
}

bool ServerConfigImpl::ContainsKey(std::string key){
	return config.object_items().find(key)!=config.object_items().end();
}


ServerConfig::ServerConfig() : p_impl(std::make_shared<ServerConfigImpl>()){
}

ServerConfig::~ServerConfig(){
}

ServerConfig* ServerConfig::GetSharedInstance(){
	if(!sharedInstance)
		sharedInstance=new ServerConfig();
	return sharedInstance;
}

int32_t ServerConfig::GetInt(std::string name, int32_t fallback){
	return p_impl->GetInt(std::move(name), fallback);
}

double ServerConfig::GetDouble(std::string name, double fallback){
	return p_impl->GetDouble(std::move(name), fallback);
}

std::string ServerConfig::GetString(std::string name, std::string fallback){
	return p_impl->GetString(std::move(name), fallback);
}

bool ServerConfig::GetBoolean(std::string name, bool fallback){
	return p_impl->GetBoolean(std::move(name), fallback);
}

void ServerConfig::Update(std::string jsonString){
	p_impl->Update(std::move(jsonString));
}