-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.cpp
175 lines (144 loc) · 5.18 KB
/
handler.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
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
#include <iostream>
#include <string>
#include <memory>
#include "http_server/reply.hpp" // for stock reply HTTP 202 'accepted'
#include "http_server/request.hpp"
#include "handler.h"
#include "table.h"
#include "light/fade.h"
#include "light/noise.h"
#include "light/gradientfade.h"
#include "light/gradients.h"
using namespace std;
using namespace http::server; // boost asio HTTP server
// taken from http://stackoverflow.com/questions/16388510/evaluate-a-string-with-a-switch-in-c
constexpr unsigned int str2int(const char* str, int h = 0)
{
return !str[h] ? 5381 : (str2int(str, h+1)*33) ^ str[h];
}
void handler::handle_request(const request& request, reply& response)
{
// Decode url to path.
std::string request_path;
if (!url_decode(request.uri, request_path))
{
cerr << "Unable to decode URI: " << request_path << endl;
response = reply::stock_reply(reply::bad_request);
return;
}
// Request path must be absolute and not contain "..".
if (request_path.empty() || request_path[0] != '/'
|| request_path.find("..") != std::string::npos)
{
cerr << "Invalid URI: " << request_path << endl;
response = reply::stock_reply(reply::bad_request);
return;
}
if (request_path.find("content=") != std::string::npos)
{
Json::Value root; // will contains the root value after parsing.
string query = request_path.substr(request_path.find("content=") + 8);
bool parsingSuccessful = reader.parse( query, root );
if (!parsingSuccessful) {
cerr << "Invalid command: " << query << endl;
response = reply::stock_reply(reply::bad_request);
return;
}
response = process_command(root);
}
else if (request_path.find("state") != std::string::npos)
{
response = process_get_state();
}
else
{
cerr << "URI must contains a 'content': " << request_path << endl;
response = reply::stock_reply(reply::bad_request);
return;
}
}
reply handler::process_command(const Json::Value& msg)
{
//cout << "Command successfully parsed:\n";
//cout << msg;
// activate the table first, in case it was off (mode 'STOP' may disable it below)
//table->active = true;
if (!table->active) {
cerr << "Table in sleep mode!" << endl;
return reply::stock_reply(reply::bad_request);
}
// Handle the table's mode
auto mode = msg.get("mode", "PLAIN").asString();
// ------------ LIGHT EFFECTS ---------------
if (mode == "PLAIN") {
auto color = Color(msg["src"]["value"][0u].asInt(),
msg["src"]["value"][1u].asInt(),
msg["src"]["value"][2u].asInt());
table->set_next_effect(make_shared<Plain>(color));
cout << "PLAIN mode, color: " << color << endl;
return reply::stock_reply(reply::accepted);
}
else if (mode == "COLOR_MIX") {
cout << "Switching to COLOR_MIX mode" << endl;
cout << "NOT IMPLEMENTED!" << endl;
}
else if (mode == "PULSE") {
cout << "Switching to PULSE mode" << endl;
cout << "NOT IMPLEMENTED!" << endl;
}
else if (mode == "NOISE") {
auto type = msg["src"]["type"].asString();
table->set_next_effect(make_shared<Noise>(GRADIENTS.at(type)));
cout << "NOISE mode, type: " << type << endl;
return reply::stock_reply(reply::accepted);
}
else if (mode == "GRADIENTFADE") {
auto type = msg["src"]["type"].asString();
table->set_next_effect(make_shared<GradientFade>(GRADIENTS.at(type)));
cout << "GRADIENT-FADE mode, type: " << type << endl;
return reply::stock_reply(reply::accepted);
}
#ifdef WITH_SOUNDS
// ------------ SOUND EFFECTS ---------------
else if (mode == "SOUND") {
auto sound = msg["src"]["value"].asString();
table->sounds.play(sound);
cout << "Playing sound: " << sound << endl;
return reply::stock_reply(reply::accepted);
}
else if (mode == "BACKGROUND_SOUND") {
auto sound = msg["src"]["value"].asString();
table->sounds.background(sound);
cout << "Playing background sound in loop: " << sound << endl;
return reply::stock_reply(reply::accepted);
}
#endif
else if (mode == "STOP") {
cout << "STOP mode" << endl;
table->active = false;
return reply::stock_reply(reply::accepted);
}
else {
cerr << "Invalid command: Unknown mode " << mode << endl;
return reply::stock_reply(reply::bad_request);
}
auto src = msg["src"];
int id = src["id"].asInt();
auto source = table->get_source(id);
if (source) {
source->position(src["x"].asInt(), src["y"].asInt(), 0);
source->color = Color(src["value"][0u].asInt(),
src["value"][1u].asInt(),
src["value"][2u].asInt());
return reply::stock_reply(reply::accepted);
}
else
{
cerr << "Invalid command: Unknown source " << id << endl;
return reply::stock_reply(reply::bad_request);
}
}
reply handler::process_get_state()
{
return reply::json_reply(table->sources_to_JSON());
}