From 35bf76ea71e153fab140af166ec07b3a961af3f0 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 4 Nov 2024 13:02:28 -0500 Subject: Requests are no longer saved --- .gitmodules | 3 -- CMakeLists.txt | 3 +- database.cpp | 91 ------------------------------------------------------- database.h | 40 ------------------------ server_main.cpp | 94 +++++++++------------------------------------------------ vendor/stduuid | 1 - 6 files changed, 16 insertions(+), 216 deletions(-) delete mode 100644 database.cpp delete mode 100644 database.h delete mode 160000 vendor/stduuid diff --git a/.gitmodules b/.gitmodules index de49c9f..2f929e3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,6 +10,3 @@ [submodule "vendor/base64"] path = vendor/base64 url = https://github.com/tobiaslocker/base64 -[submodule "vendor/stduuid"] - path = vendor/stduuid - url = https://github.com/mariusbancila/stduuid diff --git a/CMakeLists.txt b/CMakeLists.txt index b1a7b38..82e9d64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,6 @@ include_directories( vendor/hkutil vendor/asio/asio/include vendor/base64/include - vendor/stduuid ${tesseract_INCLUDE_DIRS} ${lept_INCLUDE_DIRS} ${GraphicsMagick_INCLUDE_DIRS} @@ -52,7 +51,7 @@ target_link_libraries( wizard ) -add_executable(wizard_server database.cpp server_main.cpp) +add_executable(wizard_server server_main.cpp) set_property(TARGET wizard_server PROPERTY CXX_STANDARD 17) set_property(TARGET wizard_server PROPERTY CXX_STANDARD_REQUIRED ON) target_link_libraries( diff --git a/database.cpp b/database.cpp deleted file mode 100644 index 037579b..0000000 --- a/database.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "database.h" - -#include - -#include - -std::string database::create(std::mt19937& rng) { - std::lock_guard state_guard(mutex_); - - std::string token = uuids::to_string(uuids::uuid_random_generator{rng}()); - requests_[token] = request{}; - - return token; -} - -void database::subscribe(const std::string& token, - subscribe_callback_type callback) { - std::lock_guard state_guard(mutex_); - - if (!requests_.count(token)) { - throw std::invalid_argument("Could not find request."); - } - - request& req = requests_[token]; - size_t nextId = req.subscribers.size(); - req.subscribers.push_back(callback); -} - -void database::post(const std::string& token, const std::string& msg) { - std::lock_guard state_guard(mutex_); - - if (!requests_.count(token)) { - throw std::invalid_argument("Could not find request."); - } - - request& req = requests_.at(token); - - for (std::optional& callback : req.subscribers) { - if (callback) { - try { - (*callback)(msg); - } catch (const std::exception& ex) { - callback = std::nullopt; - } - } - } -} - -void database::setResult(const std::string& token, const std::string& result) { - std::lock_guard state_guard(mutex_); - - if (!requests_.count(token)) { - throw std::invalid_argument("Could not find request."); - } - - request& req = requests_[token]; - req.result = result; -} - -const std::string& database::getResult(const std::string& token) { - std::lock_guard state_guard(mutex_); - - if (!requests_.count(token)) { - throw std::invalid_argument("Could not find request."); - } - - const request& req = requests_.at(token); - return req.result; -} - -void database::mark_done(const std::string& token) { - std::lock_guard state_guard(mutex_); - - if (!requests_.count(token)) { - throw std::invalid_argument("Could not find request."); - } - - request& req = requests_[token]; - req.done = true; -} - -bool database::is_done(const std::string& token) { - std::lock_guard state_guard(mutex_); - - if (!requests_.count(token)) { - throw std::invalid_argument("Could not find request."); - } - - const request& req = requests_.at(token); - return req.done; -} diff --git a/database.h b/database.h deleted file mode 100644 index d8d9b67..0000000 --- a/database.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef DATABASE_H_0A27B356 -#define DATABASE_H_0A27B356 - -#include -#include -#include -#include -#include -#include -#include - -class database { - public: - // Feel free to throw an exception if the subscriber should be killed. - using subscribe_callback_type = std::function; - - std::string create(std::mt19937& rng); - - void subscribe(const std::string& token, subscribe_callback_type callback); - void post(const std::string& token, const std::string& msg); - - void setResult(const std::string& token, const std::string& result); - const std::string& getResult(const std::string& token); - - void mark_done(const std::string& token); - bool is_done(const std::string& token); - - private: - struct request { - std::vector> subscribers; - std::string result; - bool done = false; - }; - - std::mutex mutex_; - - std::map requests_; -}; - -#endif /* end of include guard: DATABASE_H_0A27B356 */ diff --git a/server_main.cpp b/server_main.cpp index 145f8a2..00a4e7a 100644 --- a/server_main.cpp +++ b/server_main.cpp @@ -12,7 +12,6 @@ #include #include "cardset.h" -#include "database.h" #include "imagestore.h" #include "wizard.h" @@ -67,8 +66,6 @@ class server { std::string cmd = msgJson["cmd"]; if (cmd == "generate") { cmd_generate(connection, msgJson["text"]); - } else if (cmd == "check") { - cmd_check(connection, msgJson["token"]); } else { std::string response = R"( { @@ -84,71 +81,11 @@ class server { } void cmd_generate(websocketpp::connection_hdl connection, std::string text) { - std::string token; - - { - std::lock_guard rng_guard(rng_mutex_); - token = database_.create(rng_); - } - - nlohmann::json tokenMsg; - tokenMsg["type"] = "token"; - tokenMsg["token"] = token; - socket_.send(connection, tokenMsg.dump(), - websocketpp::frame::opcode::value::TEXT); - - database_.subscribe(token, [this, connection](const std::string& msg) { - socket_.send(connection, msg, websocketpp::frame::opcode::value::TEXT); - }); - - asio::post(std::bind(&server::generate_thread, this, token, text)); + asio::post(std::bind(&server::generate_thread, this, connection, text)); } - void cmd_check(websocketpp::connection_hdl connection, std::string token) { - bool failed = false; - - try { - database_.subscribe(token, [this, connection](const std::string& msg) { - socket_.send(connection, msg, websocketpp::frame::opcode::value::TEXT); - }); - - if (!database_.is_done(token)) { - return; - } - - std::string result = database_.getResult(token); - nlohmann::json resultMsg; - if (result.empty()) { - resultMsg["type"] = "error"; - resultMsg["msg"] = "Unknown error occurred."; - } else { - resultMsg["type"] = "result"; - resultMsg["image"] = database_.getResult(token); - resultMsg["msg"] = "Success!"; - } - - socket_.send(connection, resultMsg.dump(), - websocketpp::frame::opcode::value::TEXT); - } catch (const std::exception& ex) { - failed = true; - } - - if (failed) { - try { - socket_.send(connection, R"( - { - "type": "error", - "msg": "Error retrieving request." - })", - websocketpp::frame::opcode::value::TEXT); - } catch (const std::exception& ex) { - // Well, okay - std::cout << ex.what() << std::endl; - } - } - } - - void generate_thread(std::string token, std::string text) { + void generate_thread(websocketpp::connection_hdl connection, + std::string text) { std::unique_ptr generator; { @@ -157,13 +94,15 @@ class server { } try { - generator->set_status_callback([this, token](const std::string& status) { - nlohmann::json msg; - msg["type"] = "status"; - msg["msg"] = status; + generator->set_status_callback( + [this, connection](const std::string& status) { + nlohmann::json msg; + msg["type"] = "status"; + msg["msg"] = status; - database_.post(token, msg.dump()); - }); + socket_.send(connection, msg.dump(), + websocketpp::frame::opcode::value::TEXT); + }); Magick::Image resultImage = generator->run(); Magick::Blob resultBlob; @@ -173,24 +112,22 @@ class server { resultBlob.length()); std::string resultEncoded = base64::to_base64(resultBytes); - database_.setResult(token, resultEncoded); - nlohmann::json resultMsg; resultMsg["type"] = "result"; resultMsg["image"] = resultEncoded; resultMsg["msg"] = "Success!"; - database_.post(token, resultMsg.dump()); + socket_.send(connection, resultMsg.dump(), + websocketpp::frame::opcode::value::TEXT); } catch (const std::exception& ex) { nlohmann::json response; response["type"] = "error"; response["msg"] = std::string("Error generating card (") + ex.what() + ")"; - database_.post(token, response.dump()); + socket_.send(connection, response.dump(), + websocketpp::frame::opcode::value::TEXT); } - - database_.mark_done(token); } void cleanup_thread() { @@ -207,7 +144,6 @@ class server { std::mt19937& rng_; socket_type socket_; - database database_; }; } // namespace diff --git a/vendor/stduuid b/vendor/stduuid deleted file mode 160000 index 3afe719..0000000 --- a/vendor/stduuid +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3afe7193facd5d674de709fccc44d5055e144d7a -- cgit 1.4.1