2016-08-10 16:39:02 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <signal.h>
|
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
void sigint_handler(int s)
|
2016-08-01 01:58:12 +08:00
|
|
|
{
|
2016-08-10 16:39:02 +08:00
|
|
|
auto signal = s == SIGINT ? "SIGINT" : "SIGABRT";
|
|
|
|
|
|
|
|
logger.info("Recieved signal {}", signal);
|
|
|
|
logger.info("Shutting down...");
|
2016-08-01 01:58:12 +08:00
|
|
|
|
2016-08-10 16:39:02 +08:00
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
2016-08-01 01:58:12 +08:00
|
|
|
|
2016-08-10 16:39:02 +08:00
|
|
|
static constexpr const char* interface = "0.0.0.0";
|
|
|
|
static constexpr const char* port = "7687";
|
2016-08-01 01:58:12 +08:00
|
|
|
|
2016-08-10 16:39:02 +08:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
// TODO figure out what is the relationship between this and signals
|
|
|
|
// that are configured below
|
2016-08-01 01:58:12 +08:00
|
|
|
std::set_terminate(&terminate_handler);
|
|
|
|
|
2016-08-15 01:49:56 +08:00
|
|
|
logging::init_sync();
|
2016-08-19 00:47:49 +08:00
|
|
|
// logging::init_async();
|
2016-08-10 16:39:02 +08:00
|
|
|
logging::log->pipe(std::make_unique<Stdout>());
|
|
|
|
logger = logging::log->logger("Main");
|
|
|
|
|
|
|
|
signal(SIGINT, sigint_handler);
|
|
|
|
signal(SIGABRT, sigint_handler);
|
|
|
|
|
|
|
|
io::Socket socket;
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.set_non_blocking();
|
|
|
|
socket.listen(1024);
|
|
|
|
|
|
|
|
logger.info("Listening on {} at {}", interface, port);
|
|
|
|
|
|
|
|
bolt::Server<bolt::Worker> server(std::move(socket));
|
|
|
|
serverptr = &server;
|
|
|
|
|
|
|
|
constexpr size_t N = 1;
|
|
|
|
|
|
|
|
logger.info("Starting {} workers", N);
|
|
|
|
server.start(N);
|
|
|
|
|
|
|
|
logger.info("Shutting down...");
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2016-08-01 01:58:12 +08:00
|
|
|
}
|