From 0a222ab3ba6ccbb959563462740306beaff0e781 Mon Sep 17 00:00:00 2001 From: Naim A Date: Fri, 16 Aug 2013 18:56:27 +0300 Subject: [PATCH] End process with signals. stdin ignored. --- src/main.cpp | 64 ++++++++++++++++++++++++++++++++++++--------- src/multiplatform.h | 2 +- src/udpTracker.cpp | 17 ++++++++++-- src/udpTracker.hpp | 5 ++++ 4 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b2fb64d..7f11f37 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,12 @@ using namespace UDPT; using namespace UDPT::Server; Logger *logger; +static struct { + Settings *settings; + UDPTracker *usi; + WebApp *wa; + HTTPServer *httpserver; +} Instance; static void _print_usage () { @@ -63,8 +69,8 @@ static void _doAPIStart (Settings *settings, WebApp **wa, HTTPServer **srv, Data threads = 1; try { - *srv = new HTTPServer (port, threads); - *wa = new WebApp (*srv, drvr, settings); + *srv = Instance.httpserver = new HTTPServer (port, threads); + *wa = Instance.wa = new WebApp (*srv, drvr, settings); (*wa)->deploy(); } catch (ServerException &e) { @@ -114,10 +120,33 @@ static void _setCWD (char *argv0) } +/** + * Releases resources before exit. + */ +static void _doCleanup () +{ + delete Instance.wa; + delete Instance.httpserver; + delete Instance.usi; + delete Instance.settings; + delete logger; + + memset (&Instance, 0, sizeof(Instance)); + logger = NULL; +} + +static void _signal_handler (int sig) +{ + stringstream ss; + ss << "Signal " << sig << " raised. Terminating..."; + logger->log(Logger::LL_INFO, ss.str()); + _doCleanup(); +} + int main(int argc, char *argv[]) { - Settings *settings = NULL; - UDPTracker *usi = NULL; + Settings *settings; + UDPTracker *usi; string config_file; int r; @@ -131,6 +160,20 @@ int main(int argc, char *argv[]) cout << "Build Date: " << __DATE__ << endl << endl; config_file = "udpt.conf"; + memset(&Instance, 0, sizeof(Instance)); + +#ifdef SIGBREAK + signal(SIGBREAK, &_signal_handler); +#endif +#ifdef SIGTERM + signal(SIGTERM, &_signal_handler); +#endif +#ifdef SIGABRT + signal(SIGABRT, &_signal_handler); +#endif +#ifdef SIGINT + signal(SIGINT, &_signal_handler); +#endif if (argc <= 1) { @@ -144,7 +187,7 @@ int main(int argc, char *argv[]) config_file = argv[1]; // reported in issue #5. } - settings = new Settings (config_file); + settings = Instance.settings = new Settings (config_file); if (!settings->load()) { @@ -173,7 +216,7 @@ int main(int argc, char *argv[]) } logger = new Logger (settings); - usi = new UDPTracker (settings); + usi = Instance.usi = new UDPTracker (settings); HTTPServer *apiSrv = NULL; WebApp *wa = NULL; @@ -199,18 +242,13 @@ int main(int argc, char *argv[]) _doAPIStart(settings, &wa, &apiSrv, usi->conn); - cout << "Press Any key to exit." << endl; + cout << "Hit Control-C to exit." << endl; - cin.get(); + usi->wait(); cleanup: cout << endl << "Goodbye." << endl; - delete usi; - delete settings; - delete apiSrv; - delete wa; - #ifdef WIN32 WSACleanup(); #endif diff --git a/src/multiplatform.h b/src/multiplatform.h index 5a7bf9a..bc2b18d 100644 --- a/src/multiplatform.h +++ b/src/multiplatform.h @@ -24,7 +24,7 @@ #if defined (_WIN32) && !defined (WIN32) #define WIN32 -#elif defined (__APPLE__) +#elif defined (__APPLE__) || defined (__CYGWIN__) #define linux #endif diff --git a/src/udpTracker.cpp b/src/udpTracker.cpp index df4e62f..2514868 100644 --- a/src/udpTracker.cpp +++ b/src/udpTracker.cpp @@ -108,6 +108,19 @@ namespace UDPT delete[] this->threads; } + void UDPTracker::wait() + { +#ifdef WIN32 + WaitForMultipleObjects(this->thread_count, this->threads, TRUE, INFINITE); +#else + int i; + for (i = 0;i < this->thread_count; i++) + { + pthread_join (this->threads[i], NULL); + } +#endif + } + enum UDPTracker::StartStatus UDPTracker::start () { stringstream ss; @@ -144,7 +157,7 @@ namespace UDPT this->isRunning = true; - ss.clear(); + ss.str(""); ss << "Starting maintenance thread (1/" << ((int)this->thread_count) << ")"; logger->log(Logger::LL_INFO, ss.str()); @@ -157,7 +170,7 @@ namespace UDPT for (i = 1;i < this->thread_count; i++) { - ss.clear(); + ss.str(""); ss << "Starting thread (" << (i + 1) << "/" << ((int)this->thread_count) << ")"; logger->log(Logger::LL_INFO, ss.str()); diff --git a/src/udpTracker.hpp b/src/udpTracker.hpp index 31ab872..7a89417 100644 --- a/src/udpTracker.hpp +++ b/src/udpTracker.hpp @@ -125,6 +125,11 @@ namespace UDPT */ enum StartStatus start (); + /** + * Joins all threads, and waits for all of them to terminate. + */ + void wait (); + /** * Destroys resources that were created by constructor * @param usi Instance to destroy.