e9c7f79bc3
At least it seems working. No dynamic objects are created for now...
153 lines
3.1 KiB
C++
153 lines
3.1 KiB
C++
#include <cstdlib>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <signal.h>
|
|
|
|
#include <mongoose.h>
|
|
|
|
#include "options.h"
|
|
#include "Routerin.H"
|
|
//globally accessible config
|
|
static struct sic_conf args_info;
|
|
|
|
using std::cout;
|
|
|
|
|
|
class MongooseHandler{
|
|
private:
|
|
struct mg_context *ctx;
|
|
//const char *options[];
|
|
unsigned short listenport;
|
|
// = {"listening_ports", "8080", NULL};
|
|
|
|
public:
|
|
|
|
//default listenport:8080
|
|
MongooseHandler(int listenport=8080,std::string baseurl="http://[::1]/"):
|
|
ctx(nullptr),
|
|
listenport(listenport)
|
|
{
|
|
char portstring[8];
|
|
|
|
Routerin *routerin = Routerin::get_instance();
|
|
routerin->set_baseurl(baseurl);
|
|
|
|
std::snprintf(portstring,8,"%d",this->listenport);
|
|
const char *options[] =
|
|
{"listening_ports",portstring,nullptr};
|
|
this->ctx= mg_start(options, routerin->event_route, nullptr);
|
|
}
|
|
|
|
//no copy constructor:
|
|
MongooseHandler( const MongooseHandler &) = delete;
|
|
|
|
//destructor stops the server
|
|
~MongooseHandler(){
|
|
if (this->ctx){
|
|
mg_stop(this->ctx);
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
#ifdef DEBUG
|
|
void dump_args()
|
|
{
|
|
cout << "value of port: " << args_info.port_arg << std::endl;
|
|
cout << "value of daemonize: " <<
|
|
static_cast<bool>(args_info.daemonize_flag) <<
|
|
std::endl;
|
|
|
|
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)
|
|
{
|
|
struct cmd_parser_params *params;
|
|
|
|
//initialize the parameters structure
|
|
params = cmd_parser_params_create();
|
|
|
|
params->check_required = 0;
|
|
|
|
if (cmd_parser_config_file("./siccc.conf", &args_info, params) != 0) {
|
|
cmd_parser_free(&args_info);
|
|
free(params);
|
|
return false;
|
|
}
|
|
|
|
params->initialize = 0;
|
|
params->override = 1;
|
|
params->check_required = 1;
|
|
|
|
//call the command line parser
|
|
if (cmd_parser(argc, argv, &args_info) != 0) {
|
|
cmd_parser_free(&args_info);
|
|
free(params);
|
|
return false;
|
|
}
|
|
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;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
|
|
if ( !configfile_parsing_action(argc, argv) ) {
|
|
std::cerr << "ERROR ERROR BEEP" << std::endl;
|
|
exit(1);
|
|
}
|
|
|
|
|
|
#ifdef DEBUG
|
|
dump_args();
|
|
if (std::atexit(&exithandlerfunc))
|
|
exit(-4);
|
|
#endif
|
|
struct sigaction sa;
|
|
sa.sa_handler = signalhandler;
|
|
sigaction(SIGINT, &sa ,nullptr);
|
|
|
|
MongooseHandler *m = new MongooseHandler(args_info.port_arg);
|
|
|
|
|
|
if (!args_info.daemonize_flag) {
|
|
while(1)
|
|
getchar();
|
|
}
|
|
delete m;
|
|
|
|
return 0;
|
|
}
|