-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
128 lines (102 loc) · 3.35 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
#include <atomic>
#include <cstdlib>
#include <thread>
#include <boost/asio/signal_set.hpp>
#include "const.hpp"
#include "handlers.hpp"
#include "listener.hpp"
#include "log.hpp"
#include "option.hpp"
#include "sheath.hpp"
#include "util.hpp"
#include "plog/Initializers/RollingFileInitializer.h"
using namespace btd;
int main(int argc, char* argv[])
{
const std::string logMain(getLogsDir()+"/kedge-main.log");
const std::string logAlert(getLogsDir()+"/kedge-alert.log");
const std::string logWeb(getLogsDir()+"/kedge-web.log");
plog::init(plog::debug, logMain.c_str(), 1024*1024*32, 2);
plog::init<AlertLog>(plog::debug, logAlert.c_str(), 1024*1024*64, 2);
plog::init<WebLog>(plog::debug, logWeb.c_str(), 1024*1024*64, 2);
LOG_DEBUG << "init log " << logMain;
LOG_DEBUG << "init log " << logAlert;
LOG_DEBUG << "init log " << logWeb;
PLOGD_(AlertLog) << "start alert log";
PLOGD_(WebLog) << "start http log";
Option opt;
if (!opt.init_from(argc, argv)) { return EXIT_FAILURE; }
const auto ctx = opt.make_context();
std::thread ctx_start_loader([&ctx] {
ctx->start();
});
const auto caller = std::make_shared<httpCaller>(ctx, opt.webuiRoot);
// main: web server
boost::system::error_code ec;
auto address = net::ip::make_address(opt.httpAddr, ec);
if (ec) {
std::cerr << "invalid addr " << opt.httpAddr << " reason " << ec.message();
return EXIT_FAILURE;
}
auto port = opt.httpPort;
auto const threads = std::max<int>(1, std::thread::hardware_concurrency()/2 -1);
std::cerr << "start http server on " << address << ":" << port << std::endl;
// The io_context is required for all I/O
net::io_context ioc;
// Create and launch a listening port
std::make_shared<listener>(
ioc,
tcp::endpoint{address, port},
caller)->run();
// Capture SIGINT and SIGTERM to perform a clean shutdown
net::signal_set signals(ioc, SIGINT, SIGTERM);
signals.async_wait(
[&ioc](boost::system::error_code const&, int)
{
// Stop the io_context. This will cause run()
// to return immediately, eventually destroying the
// io_context and any remaining handlers in it.
// ioc.stop();
quit = true;
});
// Run the I/O service on the requested number of threads
std::vector<std::thread> tv;
tv.reserve(threads - 1);
for(auto i = threads - 1; i > 0; --i)
tv.emplace_back(
[&ioc]
{
ioc.run();
});
std::thread shth_loader([&ctx, &ioc] {
while (!quit)
{
ctx->doLoop();
}
ioc.stop();
});
std::thread caller_loader([&caller] {
while (!quit)
{
caller->doLoop();
}
});
std::cerr << "http server running" << std::endl;
ioc.run(); // forever
std::cerr << "http server ran ?" << std::endl;
caller->closeWS();
// Block until all the threads exit
std::cerr << "thread joinning";
// Block until all the threads exit
for(auto& t : tv) {
std::cerr << " .";
t.join();
}
std::cerr << '\n';
ctx_start_loader.join();
shth_loader.join();
caller_loader.join();
std::cerr << "http server stopped" << std::endl;
std::cerr << "closing" << std::endl;
return 0;
}