gespielt mit std::unique_ptr und so...

This commit is contained in:
john stone 2014-01-25 23:32:02 +01:00
parent 7b64cb5b76
commit b4337f73b2
5 changed files with 63 additions and 34 deletions

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <memory>
#include <mongoose.h> #include <mongoose.h>
#include "StaticFileHandler.H" #include "StaticFileHandler.H"
@ -12,8 +14,8 @@ public:
// const ??? // const ???
static Routerin * get_instance() static Routerin * get_instance()
{ {
static Routerin * me; static Routerin * me = nullptr;
if ( ! me->hasInstance ) if (!me)
return me = new Routerin(); return me = new Routerin();
return me; return me;
} }
@ -38,8 +40,8 @@ public:
} }
private: private:
explicit Routerin(); explicit Routerin();
StaticFileHandler && sfh; std::unique_ptr<StaticFileHandler> sfh;
bool hasInstance = false; //bool hasInstance = false;
int event_request_route ( struct mg_event *event); int event_request_route ( struct mg_event *event);
int event_newthread_route ( struct mg_event *event); int event_newthread_route ( struct mg_event *event);

View File

@ -14,17 +14,11 @@ using std::endl;
Routerin::Routerin(): Routerin::Routerin():
sfh( UltraSimpleStaticFileHandler()) sfh( new UltraSimpleStaticFileHandler())
{ {
cout << "Konstr Routering" << std::endl; cout << "Konstr Routering" << std::endl;
Routerin::hasInstance = true;
} }
Routerin::~Routerin()
{
Routerin::hasInstance = false;
}
int Routerin::event_request_route(struct mg_event *event){ int Routerin::event_request_route(struct mg_event *event){
const RouterOp *blub=0; const RouterOp *blub=0;
@ -85,7 +79,7 @@ int Routerin::rq_static(char const * const reststr , struct mg_event *event)
{ {
std::cerr << " a static file is reqested!" << endl; std::cerr << " a static file is reqested!" << endl;
std::cerr << " path: " << reststr << endl; std::cerr << " path: " << reststr << endl;
sfh.answer_pathreq(reststr,event->conn); sfh->answer_pathreq(reststr,event->conn);
return 1; return 1;
} }

View File

@ -6,6 +6,7 @@ class UltraSimpleStaticFileHandler :public StaticFileHandler
public: public:
virtual ~UltraSimpleStaticFileHandler(); virtual ~UltraSimpleStaticFileHandler();
virtual int answer_pathreq(const char * const path, struct mg_connection *conn) override; virtual int answer_pathreq(const char * const path, struct mg_connection *conn) override;
UltraSimpleStaticFileHandler(void);
}; };

View File

@ -22,10 +22,19 @@ static const char * hello_world_html = u8R"HERE(
</body> </body>
</html>)HERE"; </html>)HERE";
UltraSimpleStaticFileHandler::~UltraSimpleStaticFileHandler(){}; #include <iostream>
UltraSimpleStaticFileHandler::UltraSimpleStaticFileHandler()
{
std::cout << "hallo ausm construktor von USSFH [" << this << "] "
<< std::endl;
}
int UltraSimpleStaticFileHandler::answer_pathreq(const char * const path, UltraSimpleStaticFileHandler::~UltraSimpleStaticFileHandler(){
std::cout << "Arrrr aus destruktor! und (∀Gute) zum Pinguintag!!" << std::endl;
}
int UltraSimpleStaticFileHandler::answer_pathreq(const char * const path __attribute__((unused)),
struct mg_connection *conn) struct mg_connection *conn)
{ {
mg_printf(conn, "HTTP/1.0 200 OK\r\n" mg_printf(conn, "HTTP/1.0 200 OK\r\n"

View File

@ -2,6 +2,7 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <signal.h>
#include <mongoose.h> #include <mongoose.h>
@ -102,6 +103,20 @@ bool configfile_parsing_action(int& argc, char **argv)
return true; 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) int main(int argc, char **argv)
{ {
@ -109,11 +124,19 @@ int main(int argc, char **argv)
std::cerr << "ERROR ERROR BEEP" << std::endl; std::cerr << "ERROR ERROR BEEP" << std::endl;
exit(1); exit(1);
} }
#ifdef DEBUG #ifdef DEBUG
dump_args(); dump_args();
if (std::atexit(&exithandlerfunc))
exit(-4);
#endif #endif
struct sigaction sa;
sa.sa_handler = signalhandler;
sigaction(SIGINT, &sa ,nullptr);
MongooseHandler m(args_info.port_arg); MongooseHandler m(args_info.port_arg);
if (!args_info.daemonize_flag) { if (!args_info.daemonize_flag) {
while(1) while(1)
getchar(); getchar();