Commit daa339b0c2f2193b64fa15c6b1cb4b0e8024937a
1 parent
54f45b27
random stuff
Showing
5 changed files
with
40 additions
and
5 deletions
main.cpp
| ... | ... | @@ -5,7 +5,7 @@ |
| 5 | 5 | |
| 6 | 6 | #include "mainapp.h" |
| 7 | 7 | |
| 8 | -static MainApp mainApp; | |
| 8 | +MainApp *mainApp = MainApp::getMainApp(); | |
| 9 | 9 | |
| 10 | 10 | static void signal_handler(int signal) |
| 11 | 11 | { |
| ... | ... | @@ -19,7 +19,7 @@ static void signal_handler(int signal) |
| 19 | 19 | } |
| 20 | 20 | else if (signal == SIGTERM || signal == SIGINT) |
| 21 | 21 | { |
| 22 | - mainApp.quit(); | |
| 22 | + mainApp->quit(); | |
| 23 | 23 | } |
| 24 | 24 | else |
| 25 | 25 | { |
| ... | ... | @@ -59,7 +59,7 @@ int main() |
| 59 | 59 | try |
| 60 | 60 | { |
| 61 | 61 | check<std::runtime_error>(register_signal_handers()); |
| 62 | - mainApp.start(); | |
| 62 | + mainApp->start(); | |
| 63 | 63 | } |
| 64 | 64 | catch (std::exception &ex) |
| 65 | 65 | { | ... | ... |
mainapp.cpp
| ... | ... | @@ -4,6 +4,8 @@ |
| 4 | 4 | #define MAX_EVENTS 1024 |
| 5 | 5 | #define NR_OF_THREADS 4 |
| 6 | 6 | |
| 7 | +MainApp *MainApp::instance = nullptr; | |
| 8 | + | |
| 7 | 9 | void do_thread_work(ThreadData *threadData) |
| 8 | 10 | { |
| 9 | 11 | int epoll_fd = threadData->epollfd; |
| ... | ... | @@ -86,6 +88,14 @@ MainApp::MainApp() : |
| 86 | 88 | |
| 87 | 89 | } |
| 88 | 90 | |
| 91 | +MainApp *MainApp::getMainApp() | |
| 92 | +{ | |
| 93 | + if (instance == nullptr) | |
| 94 | + instance = new MainApp(); | |
| 95 | + | |
| 96 | + return instance; | |
| 97 | +} | |
| 98 | + | |
| 89 | 99 | void MainApp::start() |
| 90 | 100 | { |
| 91 | 101 | int listen_fd = check<std::runtime_error>(socket(AF_INET, SOCK_STREAM, 0)); | ... | ... |
mainapp.h
| ... | ... | @@ -17,15 +17,19 @@ |
| 17 | 17 | #include "mqttpacket.h" |
| 18 | 18 | #include "subscriptionstore.h" |
| 19 | 19 | |
| 20 | - | |
| 21 | 20 | class MainApp |
| 22 | 21 | { |
| 22 | + static MainApp *instance; | |
| 23 | + | |
| 23 | 24 | bool running = true; |
| 24 | 25 | std::vector<std::shared_ptr<ThreadData>> threads; |
| 25 | 26 | std::shared_ptr<SubscriptionStore> subscriptionStore; |
| 26 | 27 | |
| 27 | -public: | |
| 28 | 28 | MainApp(); |
| 29 | +public: | |
| 30 | + MainApp(const MainApp &rhs) = delete; | |
| 31 | + MainApp(MainApp &&rhs) = delete; | |
| 32 | + static MainApp *getMainApp(); | |
| 29 | 33 | void start(); |
| 30 | 34 | void quit(); |
| 31 | 35 | }; | ... | ... |
utils.cpp
| 1 | 1 | #include "utils.h" |
| 2 | 2 | |
| 3 | 3 | |
| 4 | + | |
| 5 | +std::list<std::__cxx11::string> split(const std::string &input, const char sep, size_t max, bool keep_empty_parts) | |
| 6 | +{ | |
| 7 | + std::list<std::string> list; | |
| 8 | + size_t start = 0; | |
| 9 | + size_t end; | |
| 10 | + | |
| 11 | + while (list.size() < max && (end = input.find(sep, start)) != std::string::npos) | |
| 12 | + { | |
| 13 | + if (start != end || keep_empty_parts) | |
| 14 | + list.push_back(input.substr(start, end - start)); | |
| 15 | + start = end + 1; // increase by length of seperator. | |
| 16 | + } | |
| 17 | + if (start != input.size() || keep_empty_parts) | |
| 18 | + list.push_back(input.substr(start, std::string::npos)); | |
| 19 | + return list; | |
| 20 | +} | ... | ... |
utils.h
| ... | ... | @@ -4,6 +4,8 @@ |
| 4 | 4 | #include <string.h> |
| 5 | 5 | #include <errno.h> |
| 6 | 6 | #include <string> |
| 7 | +#include <list> | |
| 8 | +#include <limits> | |
| 7 | 9 | |
| 8 | 10 | template<typename T> int check(int rc) |
| 9 | 11 | { |
| ... | ... | @@ -17,4 +19,6 @@ template<typename T> int check(int rc) |
| 17 | 19 | return rc; |
| 18 | 20 | } |
| 19 | 21 | |
| 22 | +std::list<std::string> split(const std::string &input, const char sep, size_t max = std::numeric_limits<int>::max(), bool keep_empty_parts = true); | |
| 23 | + | |
| 20 | 24 | #endif // UTILS_H | ... | ... |