-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.hpp
228 lines (188 loc) · 5.78 KB
/
websocket.hpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "stdafx.h"
#include <iostream>
#include <set>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include "websocketpp/config/asio_no_tls.hpp"
#include "websocketpp/server.hpp"
#include "state_controller.hpp"
//#include "nlohmann/json.hpp"
//#include "websocketpp/common/thread.hpp"
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::thread;
using websocketpp::lib::mutex;
using websocketpp::lib::lock_guard;
using websocketpp::lib::unique_lock;
using websocketpp::lib::condition_variable;
//using json = nlohmann::json;
/* on_open insert connection_hdl into channel
* on_close remove connection_hdl from channel
* on_message queue send to all channels
*/
enum action_type {
SUBSCRIBE,
UNSUBSCRIBE,
MESSAGE
};
struct action {
action(action_type t, connection_hdl h) : type(t), hdl(h) {}
action(action_type t, connection_hdl h, server::message_ptr m)
: type(t), hdl(h), msg(m) {}
action_type type;
websocketpp::connection_hdl hdl;
server::message_ptr msg;
};
class broadcast_server {
public:
broadcast_server() {
// Initialize Asio Transport
m_server.init_asio();
// Register handler callbacks
m_server.set_open_handler(bind(&broadcast_server::on_open, this, ::_1));
m_server.set_close_handler(bind(&broadcast_server::on_close, this, ::_1));
m_server.set_message_handler(bind(&broadcast_server::on_message, this, ::_1, ::_2));
m_server.set_access_channels(websocketpp::log::alevel::none);
}
void run(uint16_t port) {
// listen on specified port
m_server.listen(port);
// Start the server accept loop
m_server.start_accept();
// Start the ASIO io_service run loop
try {
m_server.run();
}
catch (const std::exception & e) {
std::cout << e.what() << std::endl;
}
}
void on_open(connection_hdl hdl) {
{
lock_guard<mutex> guard(m_action_lock);
//std::cout << "on_open" << std::endl;
m_actions.push(action(SUBSCRIBE, hdl));
}
m_action_cond.notify_one();
}
void on_close(connection_hdl hdl) {
{
lock_guard<mutex> guard(m_action_lock);
std::cout << "\nconnection closed" << std::endl;
m_actions.push(action(UNSUBSCRIBE, hdl));
}
m_action_cond.notify_one();
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
// queue message up for sending by processing thread
std::string ev = msg->get_payload();
std::cout << "\n" + ev << std::endl;
auto j = json::parse(ev);
if (j["type"] == "START_TRAINING") {
set_state(1);
sendall("{\"type\":\"TRAINING_STARTED\"}");
std::cout << "\nstart training" << std::endl;
}
if (j["type"] == "GET_PRO") {
sendall("{\"type\":\"pro_driver\",\"data\":" + std::to_string(pro_driver) + "}");
}
else if (j["type"] == "MANUAL_CLASSIFIER") {
set_state(3);
std::cout << "\nuntrained classifier" << std::endl;
}
else if (j["type"] == "TRAINED_CLASSIFIER") {
set_state(0);
std::cout << "\ntrained classifier" << std::endl;
}
else if (j["type"] == "INTERRUPT_TRAINING") {
set_state(0);
std::cout << "\ntrained classifier" << std::endl;
}
else if (j["type"] == "TRAINING_VIDEO_END") {
if(get_state() == 1) training_video_end = true;
std::cout << "\ntraining video end" << std::endl;
}
else if (j["type"] == "FIXATION_TIME") {
untrained_fixation_time = j["data"].get<int>();
std::cout << "\nnew fixation time: " << untrained_fixation_time << std::endl;
}
else if (j["type"] == "FIXATION_RADIUS") {
untrained_fixation_radius = j["data"].get<double>();
std::cout << "\nnew fixation radius: " << untrained_fixation_radius << std::endl;
}
else if (j["type"] == "RESET_TIMER") {
set_reset_timer(true);
std::cout << "\nreset timer" << std::endl;
}
else if (j["type"] == "SET_TRAINED_PARAMS") {
if (!j["data"]["transition_matrix"].is_null()) {
set_transitions_model(j["data"]["transition_matrix"].get<std::string>());
custom_transition = true;
}
if (!j["data"]["trained_fixation_time"].is_null()) {
set_trained_fixation_time(j["data"]["trained_fixation_time"].get<double>());
}
set_state(0);
std::cout << "new trained params: " << j["data"];
}
else if (j["type"] == "SET_UNTRAINED_PARAMS") {
set_untrained_fixation_radius(j["data"]["fixation_radius"].get<double>());
set_untrained_fixation_time(j["data"]["fixation_time"].get<double>());
set_state(3);
std::cout << "new untrained params: " << j["data"];
}
}
void sendall(std::string msg) {
con_list::iterator it;
try
{
for (it = m_connections.begin(); it != m_connections.end(); ++it) {
m_server.send(*it, msg, websocketpp::frame::opcode::text);
}
}
catch (std::exception& e) {
std::cout << "exception: " << e.what() << std::endl;
}
}
void process_messages() {
while (1) {
unique_lock<mutex> lock(m_action_lock);
while (m_actions.empty()) {
m_action_cond.wait(lock);
}
action a = m_actions.front();
m_actions.pop();
lock.unlock();
if (a.type == SUBSCRIBE) {
lock_guard<mutex> guard(m_connection_lock);
m_connections.insert(a.hdl);
}
else if (a.type == UNSUBSCRIBE) {
lock_guard<mutex> guard(m_connection_lock);
m_connections.erase(a.hdl);
}
else if (a.type == MESSAGE) {
lock_guard<mutex> guard(m_connection_lock);
con_list::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ++it) {
m_server.send(*it, a.msg);
}
}
else {
// undefined.
}
}
}
private:
typedef std::set<connection_hdl, std::owner_less<connection_hdl> > con_list;
server m_server;
con_list m_connections;
std::queue<action> m_actions;
mutex m_action_lock;
mutex m_connection_lock;
condition_variable m_action_cond;
};