1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#define ASIO_STANDALONE
#include <base64.hpp>
#include <chrono>
#include <fstream>
#include <functional>
#include <json.hpp>
#include <mutex>
#include <random>
#include <thread>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include "cardset.h"
#include "imagestore.h"
#include "wizard.h"
namespace {
using socket_type = websocketpp::server<websocketpp::config::asio>;
class server {
public:
server(const cardset& cards, const imagestore& images, int port,
std::mt19937& rng)
: cards_(cards), images_(images), port_(port), rng_(rng) {
socket_.init_asio();
socket_.set_message_handler([this](websocketpp::connection_hdl connection,
socket_type::message_ptr message) {
on_message(connection, message);
});
}
void run() {
socket_.listen(port_);
socket_.start_accept();
std::thread cleanup(std::bind(&server::cleanup_thread, this));
cleanup.detach();
std::cout << "Listening on port " << port_ << "..." << std::endl;
socket_.run();
}
private:
void on_message(websocketpp::connection_hdl connection,
socket_type::message_ptr message) {
nlohmann::json msgJson;
try {
msgJson = nlohmann::json::parse(message->get_payload());
} catch (const std::exception& ex) {
std::string response = R"(
{
"type": "error",
"msg": "Could not parse message."
}
)";
socket_.send(connection, response,
websocketpp::frame::opcode::value::TEXT);
return;
}
std::string cmd = msgJson["cmd"];
if (cmd == "generate") {
cmd_generate(connection, msgJson["text"]);
} else {
std::string response = R"(
{
"type": "error",
"msg": "No command in message."
}
)";
socket_.send(connection, response,
websocketpp::frame::opcode::value::TEXT);
return;
}
}
void cmd_generate(websocketpp::connection_hdl connection, std::string text) {
if (text.size() > 150) {
std::string response = R"(
{
"type": "error",
"msg": "Input is too long (>150 characters)."
}
)";
socket_.send(connection, response,
websocketpp::frame::opcode::value::TEXT);
return;
}
asio::post(std::bind(&server::generate_thread, this, connection, text));
}
void generate_thread(websocketpp::connection_hdl connection,
std::string text) {
std::unique_ptr<wizard> generator;
{
std::lock_guard rng_guard(rng_mutex_);
generator = std::make_unique<wizard>(cards_, images_, text, rng_);
}
try {
generator->set_status_callback(
[this, connection](const std::string& status) {
nlohmann::json msg;
msg["type"] = "status";
msg["msg"] = status;
socket_.send(connection, msg.dump(),
websocketpp::frame::opcode::value::TEXT);
});
Magick::Image resultImage = generator->run();
Magick::Blob resultBlob;
resultImage.write(&resultBlob);
std::string resultBytes((const char*)resultBlob.data(),
resultBlob.length());
std::string resultEncoded = base64::to_base64(resultBytes);
nlohmann::json resultMsg;
resultMsg["type"] = "result";
resultMsg["image"] = resultEncoded;
resultMsg["msg"] = "Success!";
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() + ")";
socket_.send(connection, response.dump(),
websocketpp::frame::opcode::value::TEXT);
}
}
void cleanup_thread() {
for (;;) {
std::this_thread::sleep_for(std::chrono::hours(24));
images_.cleanup();
}
}
const cardset& cards_;
const imagestore& images_;
const int port_;
std::mutex rng_mutex_;
std::mt19937& rng_;
socket_type socket_;
};
} // namespace
int main(int argc, char** argv) {
Magick::InitializeMagick(nullptr);
std::random_device randomDevice;
std::mt19937 rng(5); // randomDevice());
if (argc != 2) {
std::cout << "usage: wizard_server [configfile]" << std::endl;
return -1;
}
std::ifstream config_file(argv[1]);
nlohmann::json config_data = nlohmann::json::parse(config_file);
cardset cards(config_data["cards_path"]);
imagestore images(config_data["cache_path"]);
int port = config_data["port"];
server app(cards, images, port, rng);
app.run();
}
|