145 lines
4.2 KiB
C++
145 lines
4.2 KiB
C++
// Copyright (2019-2023) Paul Scherrer Institute
|
|
|
|
#include <csignal>
|
|
#include "../common/Logger.h"
|
|
#include "JFJochWriterHttp.h"
|
|
#include "StreamWriter.h"
|
|
#include <getopt.h>
|
|
|
|
static Logger logger("jfjoch_writer");
|
|
|
|
static Pistache::Http::Endpoint *httpEndpoint;
|
|
static StreamWriter *writer;
|
|
volatile static bool quitok = false;
|
|
|
|
void print_usage() {
|
|
logger.Info("Usage ./jfjoch_writer {options} <address of the ZeroMQ data source>");
|
|
logger.Info("");
|
|
logger.Info("Available options:");
|
|
logger.Info("-H<int> | --http_port=<int> HTTP port for statistics");
|
|
logger.Info("-r<int> | --zmq_repub_port=<int> ZeroMQ port for PUSH socket to republish images");
|
|
logger.Info("-f<int> | --zmq_file_port=<int> ZeroMQ port for PUB socket to inform about finalized files");
|
|
logger.Info("");
|
|
}
|
|
|
|
static void sigHandler (int sig){
|
|
switch(sig){
|
|
case SIGINT:
|
|
case SIGQUIT:
|
|
case SIGTERM:
|
|
case SIGHUP:
|
|
default:
|
|
httpEndpoint->shutdown();
|
|
quitok = true;
|
|
writer->Cancel();
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void setUpUnixSignals(std::vector<int> quitSignals) {
|
|
sigset_t blocking_mask;
|
|
sigemptyset(&blocking_mask);
|
|
for (auto sig : quitSignals)
|
|
sigaddset(&blocking_mask, sig);
|
|
|
|
struct sigaction sa;
|
|
sa.sa_handler = sigHandler;
|
|
sa.sa_mask = blocking_mask;
|
|
sa.sa_flags = 0;
|
|
|
|
for (auto sig : quitSignals)
|
|
sigaction(sig, &sa, nullptr);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
RegisterHDF5Filter();
|
|
|
|
int32_t http_port = 5234;
|
|
int32_t zmq_repub_port = -1;
|
|
int32_t zmq_file_port = -1;
|
|
|
|
int c;
|
|
static struct option long_options[] = {
|
|
{"http_port", required_argument, 0, 'H'},
|
|
{"zmq_repub_port", required_argument, 0, 'r'},
|
|
{"zmq_file_port", required_argument, 0, 'f'},
|
|
{0, 0, 0, 0}
|
|
};
|
|
|
|
int option_index = 0;
|
|
int opt;
|
|
while ((opt = getopt_long(argc, argv, "?hH:r:f:",long_options, &option_index)) != -1 ) {
|
|
switch (opt) {
|
|
case 'H':
|
|
http_port = atoi(optarg);
|
|
break;
|
|
case 'r':
|
|
zmq_repub_port = atoi(optarg);
|
|
break;
|
|
case 'f':
|
|
zmq_file_port = atoi(optarg);
|
|
break;
|
|
case '?':
|
|
case 'h':
|
|
print_usage();
|
|
exit(EXIT_SUCCESS);
|
|
default:
|
|
print_usage();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
int first_argc = optind;
|
|
|
|
if ((argc - first_argc != 1)) {
|
|
print_usage();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if ((http_port <= 0) || (http_port >= UINT16_MAX)) {
|
|
logger.Error("Http port must be between 1 - 65534");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
logger.Info("HTTP service listening on port {}", http_port);
|
|
|
|
std::string repub_zmq_addr, file_done_zmq_addr;
|
|
|
|
if ((zmq_file_port < UINT16_MAX) && (zmq_file_port > 0)) {
|
|
file_done_zmq_addr = fmt::format("tcp://0.0.0.0:{:d}", zmq_file_port);
|
|
logger.Info("Information on closed files is published via ZeroMQ PUB socket {:s}", file_done_zmq_addr);
|
|
}
|
|
|
|
if ((zmq_repub_port < UINT16_MAX) && (zmq_repub_port > 0)) {
|
|
repub_zmq_addr = fmt::format("tcp://0.0.0.0:{:d}", zmq_repub_port);
|
|
logger.Info("Images are republished via ZeroMQ PUSH socket {:s}", repub_zmq_addr);
|
|
}
|
|
|
|
ZMQContext context;
|
|
Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(http_port));
|
|
|
|
writer = new StreamWriter(logger, argv[first_argc], repub_zmq_addr, file_done_zmq_addr);
|
|
httpEndpoint = new Pistache::Http::Endpoint(addr);
|
|
|
|
auto router = std::make_shared<Pistache::Rest::Router>();
|
|
|
|
auto opts = Pistache::Http::Endpoint::options().threads(8);
|
|
opts.flags(Pistache::Tcp::Options::ReuseAddr);
|
|
httpEndpoint->init(opts);
|
|
|
|
std::vector<int> sigs{SIGQUIT, SIGINT, SIGTERM, SIGHUP};
|
|
setUpUnixSignals(sigs);
|
|
|
|
std::thread writer_thread([] {
|
|
while (!quitok)
|
|
writer->Run();
|
|
});
|
|
|
|
JFJochWriterHttp writer_http(*writer, router);
|
|
|
|
httpEndpoint->setHandler(router->handler());
|
|
httpEndpoint->serve();
|
|
writer_thread.join();
|
|
logger.Info("Clean stop");
|
|
exit(EXIT_SUCCESS);
|
|
}
|