Skip to content

Commit 66d04e7

Browse files
committed
Singleton Config
1 parent cef6b21 commit 66d04e7

File tree

6 files changed

+43
-25
lines changed

6 files changed

+43
-25
lines changed

src/Config.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
std::map<string, string> configMap;
1616

1717
Logger *logger;
18+
19+
Config* Config::getInstance() {
20+
static Config configSingleton;
21+
return &configSingleton;
22+
}
23+
1824
Config::Config() {
1925
queue_size = 10;
2026
loadConfigFileToMap();
21-
logger = new Logger("Config");
22-
logger->debug("Ready");
27+
2328
}
2429

2530

31+
2632
void Config::loadConfigFileToMap(){
2733
configMap.clear();
2834
std::string configString;
@@ -83,6 +89,7 @@ Config::~Config() {
8389

8490

8591
int Config::get_int_setting(std::string setting_name) {
92+
confMutex.lock();
8693
if(isSigusr1Received){
8794
Config::loadConfigFileToMap();
8895
Config::printMapContents();
@@ -96,11 +103,13 @@ int Config::get_int_setting(std::string setting_name) {
96103
} catch (invalid_argument){
97104
throw ConfigException("Error parsing config to int, check config file syntax at " + setting_name);
98105
}
106+
confMutex.unlock();
99107
return returnInt;
100108
}
101109

102110

103111
std::string Config::get_str_setting(std::string setting_name) {
112+
confMutex.lock();
104113
if(isSigusr1Received){
105114
Config::loadConfigFileToMap();
106115
Config::printMapContents();
@@ -113,5 +122,6 @@ std::string Config::get_str_setting(std::string setting_name) {
113122
} catch (out_of_range) {
114123
throw ConfigException("Unknown string option passed: " + setting_name);
115124
}
125+
confMutex.unlock();
116126
return returnString;
117127
}

src/Config.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
#ifndef SRC_CONFIG_H_
22
#define SRC_CONFIG_H_
33

4+
#include <mutex>
45
#include "Common.h"
56
#include "globals.h"
7+
#include "Logger.h"
68

79
class Config {
810
private:
911
int queue_size;
10-
static std::string configString;
11-
static std::string delimiter;
12-
static std::string configRelevantString;
13-
static std::string configLine;
14-
static std::string key;
15-
static size_t position;
16-
static void loadConfigFileToMap();
17-
static void printMapContents();
12+
std::string configString;
13+
std::string delimiter;
14+
std::string configRelevantString;
15+
std::string configLine;
16+
std::string key;
17+
size_t position;
18+
void loadConfigFileToMap();
19+
void printMapContents();
20+
Config();
21+
static Config* configSingleton;
22+
static bool isConfigLoggerCreated;
23+
std::mutex confMutex;
24+
1825

1926
public:
20-
Config();
27+
static Config* getInstance();
2128
virtual ~Config();
22-
static int get_int_setting(std::string);
23-
static std::string get_str_setting(std::string);
29+
int get_int_setting(std::string);
30+
std::string get_str_setting(std::string);
2431
};
2532

2633
class ConfigException: public BaseException {

src/Logger.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
// global
1717
std::time_t epochTimestamp;
18+
Config* config;
1819

19-
Logger::Logger(string path, string name) {
20+
Logger::Logger(string name) {
21+
config = Config::getInstance();
2022
isLoggingToFileEnabled = true;
2123
std::map<string, int > map;
2224
map.insert(pair<string, int>("QUIET", 0));
@@ -25,9 +27,9 @@ Logger::Logger(string path, string name) {
2527
map.insert(pair<string, int>("DEBUG", 3));
2628
std::string loggerStatus;
2729
const char* cLoggerStatus;
28-
this->current_log_level = map.at(Config::get_str_setting("current_log_level"));
30+
this->current_log_level = map.at(config->get_str_setting("current_log_level"));
2931
this->class_name = name;
30-
log_path = path;
32+
log_path = config->get_str_setting("log_path");
3133
logFileDescriptor = open(log_path.c_str(), O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
3234

3335
if(logFileDescriptor < 0){

src/Logger.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ class Logger {
2222

2323
public:
2424
Logger() {}
25-
Logger(string path, string name);
26-
// delegation is fun
27-
Logger(string name) : Logger(Config::get_str_setting("log_path"), name) {};
25+
Logger(string name);
2826
virtual ~Logger();
2927
void set_postfix(string postfix);
3028

src/Manager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
pthread_mutex_t * mutex_pool; // separate locks for each thread
1111

1212
Manager::Manager() {
13+
Config* config = Config::getInstance();
1314
this->logger = new Logger("Manager");
14-
this->worker_count = Config::get_int_setting("worker_count");
15+
this->worker_count = config->get_int_setting("worker_count");
1516
this->pool = (pthread_t*) calloc(worker_count, sizeof(pthread_t));
1617
mutex_pool = (pthread_mutex_t*) calloc(worker_count, sizeof(pthread_mutex_t));
1718
for (int i = 0; i < this->worker_count; i++) {

src/server.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616

1717
int main() {
1818
// Init basic classes
19-
CmdLine cmdline;
20-
Config config;
19+
CmdLine cmdline;
20+
Config* config = Config::getInstance();
2121
Logger logger("Server");
2222
SignalHandler signalHandler;
2323

2424
logger.info("Process id: " + std::to_string(getpid()));
25-
if(config.get_str_setting("start_full_server") == "true") {
25+
if(config->get_str_setting("start_full_server") == "true") {
2626
// starting the full server
27-
std::string port = config.get_str_setting("port");
28-
int queue_size = config.get_int_setting("queue_size");
27+
std::string port = config->get_str_setting("port");
28+
int queue_size = config->get_int_setting("queue_size");
2929
logger.info("Starting HackTTP at port: " + port);
3030

3131
try {

0 commit comments

Comments
 (0)