sic/src/sicmain.cc

182 lines
3.8 KiB
C++
Raw Normal View History

2013-11-07 06:44:08 +01:00
#include <cstdlib>
#include <cstdio>
2013-11-21 20:43:02 +01:00
#include <cstring>
#include <cassert>
2013-11-07 04:27:40 +01:00
#include <iostream>
2014-01-25 23:32:02 +01:00
#include <signal.h>
2013-11-07 04:27:40 +01:00
2013-11-21 20:51:28 +01:00
#include <mongoose.h>
2013-11-21 20:43:02 +01:00
2013-11-07 06:44:08 +01:00
#include "options.h"
2014-01-17 23:47:14 +01:00
#include "Routerin.H"
2013-11-21 20:43:02 +01:00
//globally accessible config
2013-11-07 06:44:08 +01:00
static struct sic_conf args_info;
using std::cout;
2013-11-21 20:43:02 +01:00
class MongooseHandler{
private:
struct mg_context *ctx;
//const char *options[];
unsigned short listenport;
// = {"listening_ports", "8080", NULL};
//
struct mg_server * srv;
2013-11-22 00:02:23 +01:00
2013-11-21 20:43:02 +01:00
public:
//default listenport:8080
2014-05-02 00:54:08 +02:00
MongooseHandler(int listenport=8080):
listenport(listenport),
srv(nullptr)
{
char portstring[8];
mg_handler_t mh = & event_route_wrap;
srv = mg_create_server(NULL, mh);
std::snprintf(portstring,8,"%d",this->listenport);
mg_set_option(srv,"listening_port",portstring);
//this->ctx= mg_start(options, Routerin::event_route, nullptr);
}
2013-11-22 00:02:23 +01:00
void serveloop(const int milisectimeout){
assert(srv);
mg_poll_server(srv,milisectimeout);
return;
}
2013-11-21 20:43:02 +01:00
//no copy constructor:
MongooseHandler( const MongooseHandler &) = delete;
2013-11-22 00:02:23 +01:00
2013-11-21 20:43:02 +01:00
//destructor stops the server
~MongooseHandler(){
if (this->srv){
mg_destroy_server(&(this->srv));
2013-11-21 20:43:02 +01:00
}
}
};
2013-11-22 00:02:23 +01:00
#ifdef DEBUG
void dump_args()
2013-11-07 04:27:40 +01:00
{
2013-11-22 00:02:23 +01:00
cout << "value of port: " << args_info.port_arg << std::endl;
cout << "value of daemonize: " <<
static_cast<bool>(args_info.daemonize_flag) <<
std::endl;
2013-11-07 06:44:08 +01:00
2013-11-22 00:02:23 +01:00
cout << "value of listen_given: "<<
args_info.listen_given << std::endl ;
for (unsigned int i = 0; i < args_info.listen_given; i++)
cout << "value of listen: " <<
args_info.listen_arg[i] <<std::endl;
2013-11-22 00:02:23 +01:00
// if (args_info.saveconf_given) {
// if (cmd_parser_file_save(args_info.conffile_arg,
// &args_info) == EXIT_FAILURE)
//
// else
// cout << "saved configuration file "<<
// args_info.conffile_arg <<
// ", \n" << std::endl;
// }
return;
}
#endif
bool configfile_parsing_action(int& argc, char **argv)
{
2013-11-07 06:44:08 +01:00
struct cmd_parser_params *params;
2013-11-08 07:11:35 +01:00
//initialize the parameters structure
2013-11-07 06:44:08 +01:00
params = cmd_parser_params_create();
2014-01-25 23:32:02 +01:00
params->check_required = 0;
2013-11-22 00:02:23 +01:00
if (cmd_parser_config_file("./siccc.conf", &args_info, params) != 0) {
2014-01-25 23:32:02 +01:00
cmd_parser_free(&args_info);
free(params);
return false;
2013-11-07 06:44:08 +01:00
}
2013-11-22 00:02:23 +01:00
params->initialize = 0;
params->override = 1;
2014-01-25 23:32:02 +01:00
params->check_required = 1;
2013-11-22 00:02:23 +01:00
//call the command line parser
if (cmd_parser(argc, argv, &args_info) != 0) {
2014-01-25 23:32:02 +01:00
cmd_parser_free(&args_info);
free(params);
return false;
2013-11-07 06:44:08 +01:00
}
2014-01-25 23:32:02 +01:00
return true;
}
bool killme;
2014-01-25 23:32:02 +01:00
static void signalhandler(int signum){
switch(signum){
case SIGTERM:
2014-01-25 23:32:02 +01:00
case SIGINT:
killme=true;
2014-01-25 23:32:02 +01:00
break;
default:
std::cout << "called sighandler with signal " << signum << " FUPP! " << std::endl;
}
}
static void exithandlerfunc(){
std::cout << "alles hat ein ende" << std::endl;
2013-11-22 00:02:23 +01:00
}
2013-11-07 06:44:08 +01:00
2013-11-22 00:02:23 +01:00
int main(int argc, char **argv)
{
killme = false;
2014-01-25 23:32:02 +01:00
if ( !configfile_parsing_action(argc, argv) ) {
std::cerr << "ERROR ERROR BEEP" << std::endl;
exit(1);
}
2013-11-22 00:02:23 +01:00
#ifdef DEBUG
2014-01-25 23:32:02 +01:00
dump_args();
if (std::atexit(&exithandlerfunc))
exit(-4);
2013-11-22 00:02:23 +01:00
#endif
2014-01-25 23:32:02 +01:00
struct sigaction sa;
sa.sa_handler = signalhandler;
sigaction(SIGINT, &sa ,nullptr);
sigaction(SIGTERM, &sa ,nullptr);
2013-11-07 06:44:08 +01:00
2014-05-02 00:54:08 +02:00
Routerin * merouterin = Routerin::get_instance();
merouterin->set_baseurl(args_info.baseurl_arg);
merouterin->set_json_path(args_info.janssons_arg);
merouterin->set_file_path(args_info.filedir_arg);
2014-05-02 01:15:14 +02:00
merouterin->start();
2014-05-02 01:04:24 +02:00
std::cerr << "baseurl: "<< merouterin->get_baseurl() << "\n";
std::cerr << "json_path: "<< merouterin->get_json_path() << "\n";
std::cerr << "file_path: "<< merouterin->get_file_path() << "\n";
2014-05-02 00:54:08 +02:00
MongooseHandler *m = new MongooseHandler(args_info.port_arg);
2014-01-25 23:32:02 +01:00
2014-05-02 00:54:08 +02:00
2014-01-25 23:32:02 +01:00
if (!args_info.daemonize_flag) {
while((not killme))
m->serveloop(1000);
2014-01-25 23:32:02 +01:00
}
delete m;
2013-11-07 06:44:08 +01:00
2014-01-25 23:32:02 +01:00
return 0;
2013-11-07 04:27:40 +01:00
}