-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_manager.cpp
More file actions
278 lines (241 loc) · 10.4 KB
/
Copy pathrequest_manager.cpp
File metadata and controls
278 lines (241 loc) · 10.4 KB
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
#include <iostream>
#include <fstream>
#include <random>
#include <string>
#include <unordered_map>
#include <thread>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/stacktrace.hpp>
#include <boost/asio.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/strand.hpp>
#include <boost/config.hpp>
#include <nlohmann/json.hpp>
#include "settings/request_manager.hpp"
#include "utils/Logger.hpp"
#include "types/app.hpp"
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = net::ip::tcp;
using json = nlohmann::json;
static std::unordered_map<std::string, std::string> app_id2ip{{"power", "127.0.0.1"}};
static std::unordered_map<std::string, std::string> app_id2port{{"power", "8000"}};
class HttpSession : public std::enable_shared_from_this<HttpSession>
{
public:
explicit HttpSession(tcp::socket socket, net::io_context& ioc) : _in_stream(std::move(socket)), _resolver(ioc), _out_stream(ioc), _strand(net::make_strand(ioc))
{
auto remote_endpoint = _in_stream.socket().remote_endpoint();
std::string remote_ip = remote_endpoint.address().to_string();
unsigned short remote_port = remote_endpoint.port();
SPDLOG_LOGGER_INFO(Logger::instance(), "Get Connection: IP: {}, port: {}", remote_ip, remote_port);
}
~HttpSession()
{
_in_stream.close();
_out_stream.close();
}
void run()
{
do_read();
}
private:
beast::tcp_stream _in_stream;
beast::flat_buffer _buffer;
http::request<http::string_body> _req;
tcp::resolver _resolver;
beast::tcp_stream _out_stream;
net::strand<net::io_context::executor_type> _strand;
bool has_connect = false;
void do_read()
{
_req = {}; // reset
_buffer.consume(_buffer.size());
auto self = shared_from_this();
http::async_read(_in_stream, _buffer, _req,
[self](beast::error_code ec, std::size_t)
{
if (ec == beast::http::error::end_of_stream || ec == net::error::eof)
{
auto remote_endpoint = self->_in_stream.socket().remote_endpoint();
std::string remote_ip = remote_endpoint.address().to_string();
unsigned short remote_port = remote_endpoint.port();
SPDLOG_LOGGER_WARN(Logger::instance(), "Client closed connection {}:{}", remote_ip, remote_port);
beast::error_code ec;
self->_in_stream.socket().shutdown(tcp::socket::shutdown_both, ec);
return;
}
if (ec)
{
SPDLOG_LOGGER_ERROR(Logger::instance(), "async_read failed: {}", ec.message());
return;
}
self->handle_request();
});
}
void handle_request()
{
SPDLOG_LOGGER_INFO(Logger::instance(), "Got request: {} {}", std::string(_req.method_string()), std::string(_req.target()));
SPDLOG_LOGGER_INFO(Logger::instance(), "body: {}", _req.body());
SPDLOG_LOGGER_INFO(Logger::instance(), "keep alive: {}", _req.keep_alive());
if (_req.method() == http::verb::post && _req.target() == request_manager_target_for_app)
{
try
{
auto res = std::make_shared<http::response<http::string_body>>(http::status::ok, _req.version());
res->set(http::field::content_type, "text/plain");
res->keep_alive(_req.keep_alive());
res->body() = "已收到 Request\n";
res->prepare_payload();
auto self = shared_from_this();
http::async_write(_in_stream, *res, [self, res](beast::error_code ec, std::size_t)
{
if (ec)
{
SPDLOG_LOGGER_ERROR(Logger::instance(), "async_write failed: {}", ec.message());
return;
}
SPDLOG_LOGGER_INFO(Logger::instance(), "Wait for next request...");
self->do_read(); // Go back to reading the next stroke
});
if (!has_connect)
{
boost::system::error_code ec;
const auto results = _resolver.resolve(sim_server_ip, sim_server_port);
_out_stream.connect(results, ec);
if (ec)
{
SPDLOG_LOGGER_ERROR(Logger::instance(), "Failed to connect to {}:{}: {}", sim_server_ip, sim_server_port, ec.message());
return;
}
has_connect = true;
}
forwarding(sim_server_ip, sim_server_target, _req.body());
}
catch (std::exception &e)
{
SPDLOG_LOGGER_ERROR(Logger::instance(), "handle request failed: {}", e.what());
do_read(); // Go back to reading the next stroke
}
}
else if (_req.method() == http::verb::post && _req.target() == request_manager_target_for_sim_server)
{
try
{
auto res = std::make_shared<http::response<http::string_body>>(http::status::ok, _req.version());
res->set(http::field::content_type, "text/plain");
res->keep_alive(_req.keep_alive());
res->body() = "Received Result\n";
res->prepare_payload();
auto self = shared_from_this();
http::async_write(_in_stream, *res, [self, res](beast::error_code ec, std::size_t)
{
if (ec)
{
SPDLOG_LOGGER_ERROR(Logger::instance(), "async_write failed: {}", ec.message());
return;
}
SPDLOG_LOGGER_INFO(Logger::instance(), "Wait for next request...");
self->do_read(); // Go back to reading the next stroke
});
json j = json::parse(_req.body());
SimulationResult sim_res = j.get<SimulationResult>();
if (!has_connect)
{
boost::system::error_code ec;
const auto results = _resolver.resolve(app_id2ip[sim_res.app_id], app_id2port[sim_res.app_id]);
_out_stream.connect(results, ec);
if (ec)
{
SPDLOG_LOGGER_ERROR(Logger::instance(), "Failed to connect to {}:{}: {}", app_id2ip[sim_res.app_id], app_id2port[sim_res.app_id], ec.message());
return;
}
has_connect = true;
}
forwarding(app_id2ip[sim_res.app_id], app_target, _req.body());
}
catch (std::exception &e)
{
SPDLOG_LOGGER_ERROR(Logger::instance(), "handle request failed: {}", e.what());
do_read(); // Go back to reading the next stroke
}
}
else
{
SPDLOG_LOGGER_ERROR(Logger::instance(),
"Unsupported method or path: {}, {}",
std::string(_req.method_string()), std::string(_req.target()));
}
}
// TODO: Instead, use a thread pool (1 or 2 threads are enough), and use blocking read/write operations within each thread.
void forwarding(const std::string &ip, const std::string &target, std::string &body)
{
auto req = std::make_shared<http::request<http::string_body>>(http::verb::post, target, _req.version());
req->set(http::field::host, ip);
req->keep_alive(_req.keep_alive());
req->set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req->set(http::field::content_type, "application/json");
req->body() = body;
req->prepare_payload();
auto target_ptr = std::make_shared<std::string>(target);
auto buffer = std::make_shared<beast::flat_buffer>();
auto res = std::make_shared<http::response<http::string_body>>();
auto self = shared_from_this();
// Post/dispatch to strand ensures no duplicate async operations.
net::post(_strand,
[self, req, target_ptr, buffer, res]()
{
SPDLOG_LOGGER_INFO(Logger::instance(), "start forwarding {} to {}", std::string(req->method_string()), *target_ptr);
beast::error_code ec;
http::write(self->_out_stream, *req, ec);
if (ec)
{
SPDLOG_LOGGER_ERROR(Logger::instance(), "forwarding write failed: {}", ec.message());
return;
}
SPDLOG_LOGGER_INFO(Logger::instance(), "forwarding {} to {}", std::string(req->method_string()), *target_ptr);
SPDLOG_LOGGER_INFO(Logger::instance(), "forwarding body = {}", req->body());
// Receive response
beast::flat_buffer buffer;
http::response<http::string_body> res;
http::read(self->_out_stream, buffer, res, ec);
if (ec)
{
SPDLOG_LOGGER_ERROR(Logger::instance(), "forwarding read failed: {}", ec.message());
return;
}
SPDLOG_LOGGER_INFO(Logger::instance(), "forwarding Response: code = {}", res.result_int());
SPDLOG_LOGGER_INFO(Logger::instance(), "forwarding body = {}", res.body());
});
}
};
int main(int argc, char *argv[])
{
auto cfg = Logger::parse_cli_args(argc, argv);
Logger::init(cfg);
SPDLOG_LOGGER_INFO(Logger::instance(), "Logger Loads Successfully!");
net::io_context ioc;
auto work = net::make_work_guard(ioc); // Prevent io_context from exiting prematurely
tcp::acceptor acceptor{ioc, {tcp::v4(), request_manager_port}};
// asynchronous accept loop
auto do_accept = [&](auto&& self) -> void {
acceptor.async_accept([&](beast::error_code ec, tcp::socket socket) {
if (!ec)
std::make_shared<HttpSession>(std::move(socket), ioc)->run();
self(self);
});
};
do_accept(do_accept); // Start retrieving accept
std::thread io_thread([&ioc]()
{
SPDLOG_LOGGER_INFO(Logger::instance(), "The server starts at http://localhost:" + std::to_string(request_manager_port));
ioc.run();
});
io_thread.join();
return 0;
}