-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
317 lines (309 loc) · 8.19 KB
/
main.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include<iostream>
#include<sstream>
#include<thread>
#include<memory>
#include<cstring>
#include<vector>
#include<functional>
#include<curl/curl.h>
#include<cstdlib>
#include<assert.h>
#ifndef HOST
#define HOST ("127.0.0.1:5000")
#endif
typedef std::function<void(const std::vector<int>&)> Callback;
class Widget
{
public:
std::string name;
Widget(const std::string&);
virtual std::string serial() = 0;
//virtual int parse(const std::string&);
};
class NumberWidget: public Widget
{
int upper, lower;
public:
NumberWidget(const std::string&, int, int=0);
std::string serial();
};
class CheckWidget: public Widget
{
public:
CheckWidget(const std::string&);
std::string serial();
};
class OptionWidget: public Widget
{
std::vector<std::string> options;
public:
OptionWidget(const std::string&, const std::vector<std::string>&);
std::string serial();
};
typedef std::shared_ptr<Widget> pW;
typedef std::shared_ptr<NumberWidget> pNW;
typedef std::shared_ptr<CheckWidget> pCW;
typedef std::shared_ptr<OptionWidget> pOW;
class Tuner
{
std::string name;
Callback callback;
std::vector<pW> widgets;
std::vector<int> data;
bool started, lazy;
bool start(int);
void loop();
std::vector<int> parse(const std::string&);
public:
Tuner(const std::string&, Callback, bool=true);
void addWidgets(pW);
template<typename ...Args>
void addWidgets(pW, Args...);
bool start();
std::string serial();
};
bool validName(const std::string&);
bool validNames(const std::vector<std::string>&);
std::string curlGET(const std::string&);
std::string curlPOST(const std::string&, const std::string&);
pNW NW(const std::string&, int, int=0);
pCW CW(const std::string&);
template<typename ...Args>
pOW OW(const std::string&, Args...);
static size_t WriteCallback(void*, size_t, size_t, void*);
//TEST
void cb(const std::vector<int> &dat)
{
for(auto v: dat) {
std::cout<<v<<' ';
}
std::cout<<std::endl;
return;
}
int main()
{
Tuner tuner("test", cb);
tuner.addWidgets(CW("tttt0"), NW("ttt1", 255), OW("ttt2", "123", "fff"));
tuner.start();
std::this_thread::sleep_for(std::chrono::seconds(100000));
return 0;
}
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
template<typename ...Args>
pOW OW(const std::string &name, Args ...args)
{
std::vector<std::string> options;
(options.push_back(args), ...);
pOW res(new OptionWidget(name, options));
return res;
}
pCW CW(const std::string &name)
{
pCW res(new CheckWidget(name));
return res;
}
pNW NW(const std::string &name, int upper, int lower)
{
pNW res(new NumberWidget(name, upper, lower));
return res;
}
std::string Tuner::serial()
{
std::string res;
std::stringstream sio;
sio << "[" << widgets.size();
for(pW widget: widgets) {
sio << "," << widget->serial();
}
sio << "]";
sio >> res;
return res;
}
std::vector<int> Tuner::parse(const std::string &res)
{
int n=0, dat=0;
std::vector<int> data;
std::stringstream sio;
sio << res;
sio >> n >> n; //omitting the status "1"
for(int i=0; i<n; ++i) {
sio >> dat;
data.push_back(dat);
}
return data;
}
void Tuner::loop()
{
while(true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
const std::string &res = curlGET("/get/" + name);
if(res[0] != '1') {
std::cerr << "Updating Fail..." << std::endl;
continue;
}
const std::vector<int> &dat = parse(res);
if(dat.size() != widgets.size()) {
std::cerr << "Data Length Error..." << std::endl;
continue;
}
if(lazy && dat == data) {
continue;
}
data = dat;
callback(data);
}
return;
}
bool Tuner::start()
{
if(started) {
return true;
}
return started = start(1);
}
bool Tuner::start(int cnt)
{
const std::string &res = curlPOST("/register/" + name, serial());
if(res[0] != '1') {
if(cnt > 3) {
std::cerr << "Registration Fail." << std::endl;
return false;
}
//std::cerr << std::format("Registration Fail, retrying... ({})", cnt) << std::endl;
std::cerr << "Registration Fail, retrying... (" << cnt << ")" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
return start(cnt+1);
}
std::thread looper(&Tuner::loop, this);
looper.detach();
return true;
}
Tuner::Tuner(const std::string &name_, Callback callback_, bool lazy_):name(name_), callback(callback_), lazy(lazy_), started(false){}
template<typename ...Args>
void Tuner::addWidgets(pW widget, Args... args)
{
widgets.push_back(widget);
addWidgets(args...);
return;
}
void Tuner::addWidgets(pW widget)
{
widgets.push_back(widget);
return;
}
std::string OptionWidget::serial()
{
//std::format
std::string res;
std::stringstream sio;
sio << "[3,\"" << name << "\"";
for(const std::string &option: options) {
sio << ",\"" << option << "\"";
}
sio << "]";
sio >> res;
return res;
}
std::string CheckWidget::serial()
{
//std::format
std::string res;
std::stringstream sio;
sio << "[2,\"" << name <<"\"]";
sio >> res;
return res;
}
std::string NumberWidget::serial()
{
//return std::format("['{}',{},{}]", name, lower, upper);
std::string res;
std::stringstream sio;
sio << "[1,\"" << name << "\"," << upper << ',' << lower << ']';
sio >> res;
return res;
}
OptionWidget::OptionWidget(const std::string &name_, const std::vector<std::string> &options_): Widget(name_)
{
assert(validNames(options_));
options = options_;
return;
}
CheckWidget::CheckWidget(const std::string &name_): Widget(name_){}
NumberWidget::NumberWidget(const std::string &name_, int upper_, int lower_): Widget(name_), lower(lower_), upper(upper_)
{
assert(lower_ <= upper_);
}
Widget::Widget(const std::string &name_)
{
assert(validName(name_));
name = name_;
}
bool validNames(const std::vector<std::string> &names)
{
for(const std::string &name: names) {
if(!validName(name)) {
return false;
}
}
return true;
}
bool validName(const std::string &name)
{
for(char c: name) {
if(!isalnum(c)) {
std::cerr << "Invalid Name: '" << name << "'" << std::endl;
return false;
}
}
return true;
}
std::string curlPOST(const std::string &rurl, const std::string &data) // rurl: url relative to HOST
{
CURL *curl = curl_easy_init();
std::string url = HOST + rurl;
std::string readBuffer;
CURLcode res;
//std::cout << std::format("[POST] send to {}: {}", rurl, data) << std::endl;
std::cout << "[POST] send to " << url << ": " << data << std::endl;
assert(curl != NULL);
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res == CURLE_OK) {
std::cout << "[POST] receive: " << readBuffer << std::endl;
return readBuffer;
}
}
std::cerr << "[POST] CURL Error" << std::endl;
return "fail";
}
std::string curlGET(const std::string &rurl) // rurl: url relative to HOST
{
CURL *curl = curl_easy_init();
std::string url = HOST + rurl;
std::string readBuffer;
CURLcode res;
std::cout << "[GET] send: " << url << std::endl;
assert(curl != NULL);
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res == CURLE_OK) {
std::cout << "[GET] receive:" << readBuffer << std::endl;
return readBuffer;
}
}
std::cerr << "[GET] CURL Error" << std::endl;
return "fail";
}