From 769c0762f3fa7abe0c45447c6dba129dc4de494d Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 4 Apr 2021 21:04:05 -0700 Subject: [PATCH] got web framework working --- CMakeLists.txt | 1 + include/bank.hpp | 8 +++++++- main.cpp | 26 +++++++++++++++++--------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f84d4d6..56b4131 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(${PROJECT_NAME} main.cpp) target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PUBLIC third_party) +target_include_directories(${PROJECT_NAME} PUBLIC third_party/drogon/lib/inc) add_subdirectory(third_party/drogon) target_link_libraries(${PROJECT_NAME} PRIVATE drogon) diff --git a/include/bank.hpp b/include/bank.hpp index ab4c66d..9ebba55 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -16,6 +16,12 @@ private: std::mutex> users; + /** + * @brief any function that WRITES to users in a way that CHANGES its SIZE OR takes MORE THEN ONE OPERATION must share lock this lock + * as to be stopped when saving, as saving data must not be corrupted. one operation writes that done modify size aswell as reads are + * permitted while saving so they do not have to share this lock + * + */ std::shared_mutex save_lock; public: @@ -54,7 +60,7 @@ public: { //if A exists, A can afford it, and A's password matches bool state = false; - std::shared_lock lock{save_lock}; //because SendFunds is non-atomic, it requires 3 locking operations + std::shared_lock lock{save_lock}; //because SendFunds requires 3 locking operations users.modify_if(a_name, [&state, amount, &attempt](User &a) { if (state = (a.balance >= amount) && (a.password == attempt), state) { diff --git a/main.cpp b/main.cpp index 17575aa..5ad8cb1 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,9 @@ #include #include #include -#include "bank.hpp" +#include "bank_f.hpp" + +using namespace drogon; int main(int argc, char **argv) { @@ -25,14 +27,20 @@ int main(int argc, char **argv) Bank.admin_pass = argv[1]; //Auto Saving - volatile bool saving_flag = true; - std::thread([&argv, &saving_flag]() { - while (saving_flag) - { - std::this_thread::sleep_for(std::chrono::minutes(std::stoi(argv[2]))); - Bank.Save(); - } - }).detach(); + const unsigned long saving_freq = std::stoul(argv[2]); + if (saving_freq) //if saving frequency is 0 then auto saving is turned off + { + std::thread([&argv, saving_freq]() { + while (1) + { + std::this_thread::sleep_for(std::chrono::minutes(saving_freq)); + Bank.Save(); + } + }).detach(); + } + + auto API = std::make_shared(); + app().addListener("0.0.0.0", 80).registerController(API).run(); return 0; } \ No newline at end of file