-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathEngine.cc
134 lines (110 loc) · 3.28 KB
/
Engine.cc
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
#include <iostream>
#include <sstream>
#include "Engine.h"
#include "Detail/Engine_impl.h"
#include "Network/Connection.h"
#include <libjson/libjson.h>
namespace engine {
using namespace detail;
using namespace network;
using namespace sql;
Engine::Engine()
: impl_(new Engine_impl)
{
}
Engine::~Engine()
{
}
void Engine::OnAcceptCallback(const Connection& connection)
{
connection.Send("Hallo");
}
void Engine::OnReceiveCallback(const Connection& connection)
{
// example of how you could fetch from mysql and post it back to the user
// as a json file
std::string stream = connection.Stream();
if (stream.length() > 0) {
std::ostringstream oss;
if (std::isdigit(stream[0])) {
oss << "SELECT * FROM cards WHERE id = ANY (SELECT card_id FROM rel_user_cards WHERE user_id = '" << stream[0] << "');";
} else if (stream == "all") {
oss << "SELECT * FROM cards" << std::endl;
} else {
connection.Send("Unknown command");
return;
}
Collection collection = impl_->database_.Query(oss.str());
JSONNODE *node = json_new(JSON_NODE);
JSONNODE *cards = json_new(JSON_ARRAY);
json_set_name(cards, "all_cards");
std::cout << " Query finished" << std::endl;
auto it = collection.begin();
for (; it != collection.end(); ++it) {
sql::Dictionary dict = *it;
auto dict_it = dict.begin();
JSONNODE *card_info = json_new(JSON_NODE);
for (; dict_it != dict.end(); ++dict_it) {
json_push_back(card_info, json_new_a(
JSON_TEXT(dict_it->first.c_str()),
JSON_TEXT(dict_it->second.c_str())));
}
json_push_back(cards, card_info);
}
json_push_back(node, cards);
json_char* json_string = json_write_formatted(node);
connection.Send(json_string);
json_free(json_string);
json_delete(node);
} else {
connection.Send("Unknown request");
}
}
void Engine::OnAbortCallback(const Connection& connection)
{
}
bool Engine::Run(const std::string& init_file)
{
int port = <port>; // get from init_file
std::string username = <username>;
std::string password = <password>;
std::string database = <database>;
std::string host = <server>;
if (!impl_->database_.Startup(username, password, database, host)) {
return false;
}
std::function<void (const Connection&)> on_accept = std::bind(&Engine::OnAcceptCallback,
this,
std::placeholders::_1);
std::function<void (const Connection&)> on_receive = std::bind(&Engine::OnReceiveCallback,
this,
std::placeholders::_1);
std::function<void (const Connection&)> on_abort = std::bind(&Engine::OnAbortCallback,
this,
std::placeholders::_1);
std::thread reactor_thread(std::bind(&Reactor::Run,
std::ref(impl_->reactor_),
port,
on_accept,
on_receive,
on_abort));
impl_->reactor_thread_ = std::move(reactor_thread);
//impl_->reactor_.Run(port, on_accept, on_receive, on_abort);
return true;
}
void Engine::Stop()
{
std::cout << "Engine::Stop()" << std::endl;
impl_->reactor_.Stop();
if (impl_->reactor_thread_.joinable()) {
std::cout << "Engine::Stop() :: join reactor thread" << std::endl;
impl_->reactor_thread_.join();
}
impl_->runloop_queue_.Stop();
}
Engine& Engine::Singleton()
{
static Engine singleton;
return singleton;
}
}