diff --git a/main.cpp b/main.cpp index 6c122aa..0c80ad9 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,7 @@ #include "mainapp.h" -static MainApp mainApp; +MainApp *mainApp = MainApp::getMainApp(); static void signal_handler(int signal) { @@ -19,7 +19,7 @@ static void signal_handler(int signal) } else if (signal == SIGTERM || signal == SIGINT) { - mainApp.quit(); + mainApp->quit(); } else { @@ -59,7 +59,7 @@ int main() try { check(register_signal_handers()); - mainApp.start(); + mainApp->start(); } catch (std::exception &ex) { diff --git a/mainapp.cpp b/mainapp.cpp index 2e3fac8..9c59358 100644 --- a/mainapp.cpp +++ b/mainapp.cpp @@ -4,6 +4,8 @@ #define MAX_EVENTS 1024 #define NR_OF_THREADS 4 +MainApp *MainApp::instance = nullptr; + void do_thread_work(ThreadData *threadData) { int epoll_fd = threadData->epollfd; @@ -86,6 +88,14 @@ MainApp::MainApp() : } +MainApp *MainApp::getMainApp() +{ + if (instance == nullptr) + instance = new MainApp(); + + return instance; +} + void MainApp::start() { int listen_fd = check(socket(AF_INET, SOCK_STREAM, 0)); diff --git a/mainapp.h b/mainapp.h index 556e5f1..c3a83c4 100644 --- a/mainapp.h +++ b/mainapp.h @@ -17,15 +17,19 @@ #include "mqttpacket.h" #include "subscriptionstore.h" - class MainApp { + static MainApp *instance; + bool running = true; std::vector> threads; std::shared_ptr subscriptionStore; -public: MainApp(); +public: + MainApp(const MainApp &rhs) = delete; + MainApp(MainApp &&rhs) = delete; + static MainApp *getMainApp(); void start(); void quit(); }; diff --git a/utils.cpp b/utils.cpp index 67cfb5a..fe36c47 100644 --- a/utils.cpp +++ b/utils.cpp @@ -1,3 +1,20 @@ #include "utils.h" + +std::list split(const std::string &input, const char sep, size_t max, bool keep_empty_parts) +{ + std::list list; + size_t start = 0; + size_t end; + + while (list.size() < max && (end = input.find(sep, start)) != std::string::npos) + { + if (start != end || keep_empty_parts) + list.push_back(input.substr(start, end - start)); + start = end + 1; // increase by length of seperator. + } + if (start != input.size() || keep_empty_parts) + list.push_back(input.substr(start, std::string::npos)); + return list; +} diff --git a/utils.h b/utils.h index c6018dc..aebc401 100644 --- a/utils.h +++ b/utils.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include template int check(int rc) { @@ -17,4 +19,6 @@ template int check(int rc) return rc; } +std::list split(const std::string &input, const char sep, size_t max = std::numeric_limits::max(), bool keep_empty_parts = true); + #endif // UTILS_H