2016-08-10 16:39:02 +08:00
|
|
|
#include <signal.h>
|
2016-12-16 20:56:36 +08:00
|
|
|
#include <iostream>
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-08-11 11:47:30 +08:00
|
|
|
#include "communication/bolt/v1/server/server.hpp"
|
|
|
|
#include "communication/bolt/v1/server/worker.hpp"
|
2016-08-10 16:39:02 +08:00
|
|
|
|
|
|
|
#include "io/network/socket.hpp"
|
|
|
|
|
|
|
|
#include "logging/default.hpp"
|
|
|
|
#include "logging/streams/stdout.hpp"
|
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
#include "utils/signals/handler.hpp"
|
|
|
|
#include "utils/stacktrace.hpp"
|
2016-08-01 01:58:12 +08:00
|
|
|
#include "utils/terminate_handler.hpp"
|
|
|
|
|
2016-08-10 16:39:02 +08:00
|
|
|
static bolt::Server<bolt::Worker>* serverptr;
|
|
|
|
|
|
|
|
Logger logger;
|
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
static constexpr const char* interface = "0.0.0.0";
|
|
|
|
static constexpr const char* port = "7687";
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
void throw_and_stacktace(std::string message) {
|
|
|
|
Stacktrace stacktrace;
|
2016-12-19 18:02:24 +08:00
|
|
|
logger.info(stacktrace.dump());
|
2016-12-16 20:56:36 +08:00
|
|
|
}
|
2016-08-01 01:58:12 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
int main(void) {
|
|
|
|
// TODO figure out what is the relationship between this and signals
|
|
|
|
// that are configured below
|
|
|
|
std::set_terminate(&terminate_handler);
|
2016-08-01 01:58:12 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
// logger init
|
2016-08-19 08:28:22 +08:00
|
|
|
#ifdef SYNC_LOGGER
|
2016-12-16 20:56:36 +08:00
|
|
|
logging::init_sync();
|
2016-08-19 08:28:22 +08:00
|
|
|
#else
|
2016-12-16 20:56:36 +08:00
|
|
|
logging::init_async();
|
2016-08-19 08:28:22 +08:00
|
|
|
#endif
|
2016-12-16 20:56:36 +08:00
|
|
|
logging::log->pipe(std::make_unique<Stdout>());
|
|
|
|
|
|
|
|
// get Main logger
|
|
|
|
logger = logging::log->logger("Main");
|
|
|
|
logger.info("{}", logging::log->type());
|
|
|
|
|
|
|
|
SignalHandler::register_handler(Signal::SegmentationFault, []() {
|
|
|
|
throw_and_stacktace("SegmentationFault signal raised");
|
|
|
|
exit(1);
|
|
|
|
});
|
2016-08-19 08:28:22 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
SignalHandler::register_handler(Signal::Terminate, []() {
|
|
|
|
throw_and_stacktace("Terminate signal raised");
|
|
|
|
exit(1);
|
|
|
|
});
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
SignalHandler::register_handler(Signal::Abort, []() {
|
|
|
|
throw_and_stacktace("Abort signal raised");
|
|
|
|
exit(1);
|
|
|
|
});
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
io::Socket socket;
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
try {
|
|
|
|
socket = io::Socket::bind(interface, port);
|
|
|
|
} catch (io::NetworkError e) {
|
|
|
|
logger.error("Cannot bind to socket on {} at {}", interface, port);
|
|
|
|
logger.error("{}", e.what());
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
socket.set_non_blocking();
|
|
|
|
socket.listen(1024);
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
logger.info("Listening on {} at {}", interface, port);
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
bolt::Server<bolt::Worker> server(std::move(socket));
|
|
|
|
serverptr = &server;
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
// TODO: N should be configurable
|
|
|
|
auto N = std::thread::hardware_concurrency();
|
|
|
|
logger.info("Starting {} workers", N);
|
|
|
|
server.start(N);
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
logger.info("Shutting down...");
|
2016-08-10 16:39:02 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
return EXIT_SUCCESS;
|
2016-08-01 01:58:12 +08:00
|
|
|
}
|