sic/src/sicmain.cc

147 lines
3.0 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>
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};
2013-11-22 00:02:23 +01:00
2013-11-21 20:43:02 +01:00
public:
//default listenport:8080
MongooseHandler(int listenport=8080):
ctx(nullptr),
listenport(listenport)
{
char portstring[8];
Routerin *routerin = Routerin::get_instance();
std::snprintf(portstring,8,"%d",this->listenport);
const char *options[] =
{"listening_ports",portstring,nullptr};
this->ctx= mg_start(options, routerin->event_route, nullptr);
}
2013-11-22 00:02:23 +01:00
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->ctx){
mg_stop(this->ctx);
}
}
};
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;
// 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;
}
static void signalhandler(int signum){
switch(signum){
case SIGINT:
exit(0);
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)
{
2013-11-07 06:44:08 +01:00
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);
2013-11-07 06:44:08 +01:00
2014-01-25 23:32:02 +01:00
MongooseHandler m(args_info.port_arg);
if (!args_info.daemonize_flag) {
while(1)
getchar();
}
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
}