-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestHandler.cpp
104 lines (78 loc) · 2.9 KB
/
RestHandler.cpp
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
#include <future>
#include <iostream>
#include <type_traits>
#include <vector>
#include <codecvt>
#include <stdlib.h>
#include "RestHandler.h"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
#include <iconv.h>
std::string convert(const char * input) {
char dest_str[255];
char *out = dest_str;
size_t inbytes = strlen(input);
size_t outbytes = sizeof dest_str;
iconv_t conv = iconv_open("CP850//TRANSLIT", "UTF-8");
if (conv == (iconv_t)-1) {
perror("iconv_open");
return "";
}
char * i = const_cast<char *>(input);
if (iconv(conv, &i, &inbytes, &out, &outbytes) == (size_t)-1) {
perror("iconv");
return "";
}
dest_str[sizeof dest_str - outbytes] = 0;
return dest_str;
}
void RestHandler::setup() {
mux.handle("/start").post([&](served::response &res, const served::request &req) {
if (scroller.isRunning()) {
scroller.setRun(false);
}
res << "Scroll started";
std::cout << "Received message:" << req.body() << std::endl;
auto receivedMessage = req.body();
std::thread result([&](std::string message) {
json parsedMessage = nullptr;
try {
parsedMessage = json::parse(message);
} catch (json::parse_error &e) {
std::cout << "message: " << e.what() << '\n'
<< "exception id: " << e.id << '\n'
<< "byte position of error: " << e.byte << std::endl;
}
if (parsedMessage != nullptr) {
try {
std::string str = parsedMessage.at("message").get<std::string>();
auto convertedText = convert(str.c_str());
std::vector<int> text;
for (auto character : convertedText) {
unsigned char ch = static_cast<unsigned char>(character);
text.push_back(ch);
}
auto times = 3;
if (parsedMessage.find("times") != parsedMessage.end()) {
times = parsedMessage.at("times").get<std::int32_t>();
}
scroller.setupText(times, text);
} catch (json::out_of_range& e) {
std::cout << "message: " << e.what() << '\n'
<< "exception id: " << e.id << std::endl;
}
}
}, receivedMessage);
result.detach();
});
mux.handle("/stop").post([&](served::response &res, const served::request &req) {
res << "Scroll stopped";
scroller.setRun(false);
});
}
void RestHandler::start() {
served::net::server server("0.0.0.0", "8080", mux);
server.run(1);
std::cout << "Server running on port 8080" << std::endl;
}
RestHandler::RestHandler(Scroller &scroller) : scroller(scroller) {}