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 --- server_main.cpp | 94 +++++++++------------------------------------------------ 1 file changed, 15 insertions(+), 79 deletions(-) (limited to 'server_main.cpp') 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 -- cgit 1.4.1