Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer to ServerConfigImpl #69

Open
wants to merge 1 commit into
base: public
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions VoIPServerConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,61 @@
#include "logging.h"
#include <sstream>
#include <locale>
#include <utility>
#include "threading.h"
#include "json11.hpp"

using namespace tgvoip;

ServerConfig* ServerConfig::sharedInstance=NULL;
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);

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

ServerConfig::~ServerConfig(){
}

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

bool ServerConfig::GetBoolean(std::string name, bool fallback){
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 ServerConfig::GetDouble(std::string name, double 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 ServerConfig::GetInt(std::string name, int32_t 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 ServerConfig::GetString(std::string name, std::string 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 ServerConfig::Update(std::string jsonString){
void ServerConfigImpl::Update(std::string jsonString){
MutexGuard sync(mutex);
LOGD("=== Updating voip config ===");
LOGD("%s", jsonString.c_str());
Expand All @@ -64,7 +73,39 @@ void ServerConfig::Update(std::string jsonString){
LOGE("Error parsing server config: %s", jsonError.c_str());
}


bool ServerConfig::ContainsKey(std::string key){
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));
}
11 changes: 4 additions & 7 deletions VoIPServerConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
#ifndef TGVOIP_VOIPSERVERCONFIG_H
#define TGVOIP_VOIPSERVERCONFIG_H

#include <map>
#include <memory>
#include <string>
#include <stdint.h>
#include "threading.h"
#include "json11.hpp"

namespace tgvoip{

class ServerConfigImpl;

class ServerConfig{
public:
ServerConfig();
Expand All @@ -27,10 +27,7 @@ class ServerConfig{
void Update(std::string jsonString);

private:
static ServerConfig* sharedInstance;
bool ContainsKey(std::string key);
json11::Json config;
Mutex mutex;
std::shared_ptr<ServerConfigImpl> p_impl;
};
}

Expand Down