diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2024-11-04 13:02:28 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2024-11-04 13:02:28 -0500 |
commit | 35bf76ea71e153fab140af166ec07b3a961af3f0 (patch) | |
tree | d1765dbc2bdfec81b715c88e6131eb77e73a104a /server_main.cpp | |
parent | 992d6afde08ec641c555723e139e589c5bc61bbb (diff) | |
download | wizard-35bf76ea71e153fab140af166ec07b3a961af3f0.tar.gz wizard-35bf76ea71e153fab140af166ec07b3a961af3f0.tar.bz2 wizard-35bf76ea71e153fab140af166ec07b3a961af3f0.zip |
Requests are no longer saved
Diffstat (limited to 'server_main.cpp')
-rw-r--r-- | server_main.cpp | 94 |
1 files changed, 15 insertions, 79 deletions
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 @@ | |||
12 | #include <websocketpp/server.hpp> | 12 | #include <websocketpp/server.hpp> |
13 | 13 | ||
14 | #include "cardset.h" | 14 | #include "cardset.h" |
15 | #include "database.h" | ||
16 | #include "imagestore.h" | 15 | #include "imagestore.h" |
17 | #include "wizard.h" | 16 | #include "wizard.h" |
18 | 17 | ||
@@ -67,8 +66,6 @@ class server { | |||
67 | std::string cmd = msgJson["cmd"]; | 66 | std::string cmd = msgJson["cmd"]; |
68 | if (cmd == "generate") { | 67 | if (cmd == "generate") { |
69 | cmd_generate(connection, msgJson["text"]); | 68 | cmd_generate(connection, msgJson["text"]); |
70 | } else if (cmd == "check") { | ||
71 | cmd_check(connection, msgJson["token"]); | ||
72 | } else { | 69 | } else { |
73 | std::string response = R"( | 70 | std::string response = R"( |
74 | { | 71 | { |
@@ -84,71 +81,11 @@ class server { | |||
84 | } | 81 | } |
85 | 82 | ||
86 | void cmd_generate(websocketpp::connection_hdl connection, std::string text) { | 83 | void cmd_generate(websocketpp::connection_hdl connection, std::string text) { |
87 | std::string token; | 84 | asio::post(std::bind(&server::generate_thread, this, connection, text)); |
88 | |||
89 | { | ||
90 | std::lock_guard rng_guard(rng_mutex_); | ||
91 | token = database_.create(rng_); | ||
92 | } | ||
93 | |||
94 | nlohmann::json tokenMsg; | ||
95 | tokenMsg["type"] = "token"; | ||
96 | tokenMsg["token"] = token; | ||
97 | socket_.send(connection, tokenMsg.dump(), | ||
98 | websocketpp::frame::opcode::value::TEXT); | ||
99 | |||
100 | database_.subscribe(token, [this, connection](const std::string& msg) { | ||
101 | socket_.send(connection, msg, websocketpp::frame::opcode::value::TEXT); | ||
102 | }); | ||
103 | |||
104 | asio::post(std::bind(&server::generate_thread, this, token, text)); | ||
105 | } | 85 | } |
106 | 86 | ||
107 | void cmd_check(websocketpp::connection_hdl connection, std::string token) { | 87 | void generate_thread(websocketpp::connection_hdl connection, |
108 | bool failed = false; | 88 | std::string text) { |
109 | |||
110 | try { | ||
111 | database_.subscribe(token, [this, connection](const std::string& msg) { | ||
112 | socket_.send(connection, msg, websocketpp::frame::opcode::value::TEXT); | ||
113 | }); | ||
114 | |||
115 | if (!database_.is_done(token)) { | ||
116 | return; | ||
117 | } | ||
118 | |||
119 | std::string result = database_.getResult(token); | ||
120 | nlohmann::json resultMsg; | ||
121 | if (result.empty()) { | ||
122 | resultMsg["type"] = "error"; | ||
123 | resultMsg["msg"] = "Unknown error occurred."; | ||
124 | } else { | ||
125 | resultMsg["type"] = "result"; | ||
126 | resultMsg["image"] = database_.getResult(token); | ||
127 | resultMsg["msg"] = "Success!"; | ||
128 | } | ||
129 | |||
130 | socket_.send(connection, resultMsg.dump(), | ||
131 | websocketpp::frame::opcode::value::TEXT); | ||
132 | } catch (const std::exception& ex) { | ||
133 | failed = true; | ||
134 | } | ||
135 | |||
136 | if (failed) { | ||
137 | try { | ||
138 | socket_.send(connection, R"( | ||
139 | { | ||
140 | "type": "error", | ||
141 | "msg": "Error retrieving request." | ||
142 | })", | ||
143 | websocketpp::frame::opcode::value::TEXT); | ||
144 | } catch (const std::exception& ex) { | ||
145 | // Well, okay | ||
146 | std::cout << ex.what() << std::endl; | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | |||
151 | void generate_thread(std::string token, std::string text) { | ||
152 | std::unique_ptr<wizard> generator; | 89 | std::unique_ptr<wizard> generator; |
153 | 90 | ||
154 | { | 91 | { |
@@ -157,13 +94,15 @@ class server { | |||
157 | } | 94 | } |
158 | 95 | ||
159 | try { | 96 | try { |
160 | generator->set_status_callback([this, token](const std::string& status) { | 97 | generator->set_status_callback( |
161 | nlohmann::json msg; | 98 | [this, connection](const std::string& status) { |
162 | msg["type"] = "status"; | 99 | nlohmann::json msg; |
163 | msg["msg"] = status; | 100 | msg["type"] = "status"; |
101 | msg["msg"] = status; | ||
164 | 102 | ||
165 | database_.post(token, msg.dump()); | 103 | socket_.send(connection, msg.dump(), |
166 | }); | 104 | websocketpp::frame::opcode::value::TEXT); |
105 | }); | ||
167 | 106 | ||
168 | Magick::Image resultImage = generator->run(); | 107 | Magick::Image resultImage = generator->run(); |
169 | Magick::Blob resultBlob; | 108 | Magick::Blob resultBlob; |
@@ -173,24 +112,22 @@ class server { | |||
173 | resultBlob.length()); | 112 | resultBlob.length()); |
174 | std::string resultEncoded = base64::to_base64(resultBytes); | 113 | std::string resultEncoded = base64::to_base64(resultBytes); |
175 | 114 | ||
176 | database_.setResult(token, resultEncoded); | ||
177 | |||
178 | nlohmann::json resultMsg; | 115 | nlohmann::json resultMsg; |
179 | resultMsg["type"] = "result"; | 116 | resultMsg["type"] = "result"; |
180 | resultMsg["image"] = resultEncoded; | 117 | resultMsg["image"] = resultEncoded; |
181 | resultMsg["msg"] = "Success!"; | 118 | resultMsg["msg"] = "Success!"; |
182 | 119 | ||
183 | database_.post(token, resultMsg.dump()); | 120 | socket_.send(connection, resultMsg.dump(), |
121 | websocketpp::frame::opcode::value::TEXT); | ||
184 | } catch (const std::exception& ex) { | 122 | } catch (const std::exception& ex) { |
185 | nlohmann::json response; | 123 | nlohmann::json response; |
186 | response["type"] = "error"; | 124 | response["type"] = "error"; |
187 | response["msg"] = | 125 | response["msg"] = |
188 | std::string("Error generating card (") + ex.what() + ")"; | 126 | std::string("Error generating card (") + ex.what() + ")"; |
189 | 127 | ||
190 | database_.post(token, response.dump()); | 128 | socket_.send(connection, response.dump(), |
129 | websocketpp::frame::opcode::value::TEXT); | ||
191 | } | 130 | } |
192 | |||
193 | database_.mark_done(token); | ||
194 | } | 131 | } |
195 | 132 | ||
196 | void cleanup_thread() { | 133 | void cleanup_thread() { |
@@ -207,7 +144,6 @@ class server { | |||
207 | std::mt19937& rng_; | 144 | std::mt19937& rng_; |
208 | 145 | ||
209 | socket_type socket_; | 146 | socket_type socket_; |
210 | database database_; | ||
211 | }; | 147 | }; |
212 | 148 | ||
213 | } // namespace | 149 | } // namespace |