-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.h
79 lines (69 loc) · 1.97 KB
/
console.h
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
#ifndef CONSOLE_H
#define CONSOLE_H
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <string>
#include <map>
#include "util/objects.h"
#include "json11/json11.hpp"
namespace felis {
class Console {
public:
enum ServerStatus {
Booting, Configuring, Listening, Connecting, Running, Exiting,
};
private:
std::mutex mutex;
std::condition_variable cond;
ServerStatus server_status = ServerStatus::Booting;
std::string node_name;
json11::Json conf;
template <typename T> friend T &util::Instance() noexcept;
public:
Console();
Console(const Console &rhs) = delete;
void set_server_node_name(std::string name) { node_name = name; }
std::string server_node_name() const { return node_name; }
void UpdateServerStatus(ServerStatus status) {
std::unique_lock<std::mutex> _(mutex);
server_status = status;
cond.notify_all();
}
void WaitForServerStatus(ServerStatus status) {
std::unique_lock<std::mutex> _(mutex);
while (server_status != status) {
cond.wait(_);
}
}
json11::Json FindConfigSection(const char *section_name) const {
auto &map = conf.object_items();
auto it = map.find(section_name);
if (it == map.end()) {
fprintf(stderr, "Configuration section %s cannot be found. "
"Check controller server's configuration\n", section_name);
std::abort();
}
return it->second;
}
std::string HandleAPI(const json11::Json &j) {
return HandleJsonAPI(j).dump();
}
json11::Json HandleJsonAPI(const json11::Json &j);
private:
json11::Json HandleStatusChange(const json11::Json &j);
json11::Json HandleGetStatus(const json11::Json &j);
json11::Json JsonResponse() {
return json11::Json::object({
{"type", "OK"},
{"host", node_name},
});
}
json11::Json JsonResponse(json11::Json::object props) {
auto hdr = JsonResponse().object_items();
props.insert(hdr.begin(), hdr.end());
return props;
}
};
}
#endif /* CONSOLE_H */