End process with signals. stdin ignored.
This commit is contained in:
parent
7e9ecdb099
commit
0a222ab3ba
64
src/main.cpp
64
src/main.cpp
|
@ -33,6 +33,12 @@ using namespace UDPT;
|
||||||
using namespace UDPT::Server;
|
using namespace UDPT::Server;
|
||||||
|
|
||||||
Logger *logger;
|
Logger *logger;
|
||||||
|
static struct {
|
||||||
|
Settings *settings;
|
||||||
|
UDPTracker *usi;
|
||||||
|
WebApp *wa;
|
||||||
|
HTTPServer *httpserver;
|
||||||
|
} Instance;
|
||||||
|
|
||||||
static void _print_usage ()
|
static void _print_usage ()
|
||||||
{
|
{
|
||||||
|
@ -63,8 +69,8 @@ static void _doAPIStart (Settings *settings, WebApp **wa, HTTPServer **srv, Data
|
||||||
threads = 1;
|
threads = 1;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
*srv = new HTTPServer (port, threads);
|
*srv = Instance.httpserver = new HTTPServer (port, threads);
|
||||||
*wa = new WebApp (*srv, drvr, settings);
|
*wa = Instance.wa = new WebApp (*srv, drvr, settings);
|
||||||
(*wa)->deploy();
|
(*wa)->deploy();
|
||||||
} catch (ServerException &e)
|
} 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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Settings *settings = NULL;
|
Settings *settings;
|
||||||
UDPTracker *usi = NULL;
|
UDPTracker *usi;
|
||||||
string config_file;
|
string config_file;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
@ -131,6 +160,20 @@ int main(int argc, char *argv[])
|
||||||
cout << "Build Date: " << __DATE__ << endl << endl;
|
cout << "Build Date: " << __DATE__ << endl << endl;
|
||||||
|
|
||||||
config_file = "udpt.conf";
|
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)
|
if (argc <= 1)
|
||||||
{
|
{
|
||||||
|
@ -144,7 +187,7 @@ int main(int argc, char *argv[])
|
||||||
config_file = argv[1]; // reported in issue #5.
|
config_file = argv[1]; // reported in issue #5.
|
||||||
}
|
}
|
||||||
|
|
||||||
settings = new Settings (config_file);
|
settings = Instance.settings = new Settings (config_file);
|
||||||
|
|
||||||
if (!settings->load())
|
if (!settings->load())
|
||||||
{
|
{
|
||||||
|
@ -173,7 +216,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
logger = new Logger (settings);
|
logger = new Logger (settings);
|
||||||
usi = new UDPTracker (settings);
|
usi = Instance.usi = new UDPTracker (settings);
|
||||||
|
|
||||||
HTTPServer *apiSrv = NULL;
|
HTTPServer *apiSrv = NULL;
|
||||||
WebApp *wa = NULL;
|
WebApp *wa = NULL;
|
||||||
|
@ -199,18 +242,13 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
_doAPIStart(settings, &wa, &apiSrv, usi->conn);
|
_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:
|
cleanup:
|
||||||
cout << endl << "Goodbye." << endl;
|
cout << endl << "Goodbye." << endl;
|
||||||
|
|
||||||
delete usi;
|
|
||||||
delete settings;
|
|
||||||
delete apiSrv;
|
|
||||||
delete wa;
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
#if defined (_WIN32) && !defined (WIN32)
|
#if defined (_WIN32) && !defined (WIN32)
|
||||||
#define WIN32
|
#define WIN32
|
||||||
#elif defined (__APPLE__)
|
#elif defined (__APPLE__) || defined (__CYGWIN__)
|
||||||
#define linux
|
#define linux
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -108,6 +108,19 @@ namespace UDPT
|
||||||
delete[] this->threads;
|
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 ()
|
enum UDPTracker::StartStatus UDPTracker::start ()
|
||||||
{
|
{
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
|
@ -144,7 +157,7 @@ namespace UDPT
|
||||||
|
|
||||||
this->isRunning = true;
|
this->isRunning = true;
|
||||||
|
|
||||||
ss.clear();
|
ss.str("");
|
||||||
ss << "Starting maintenance thread (1/" << ((int)this->thread_count) << ")";
|
ss << "Starting maintenance thread (1/" << ((int)this->thread_count) << ")";
|
||||||
logger->log(Logger::LL_INFO, ss.str());
|
logger->log(Logger::LL_INFO, ss.str());
|
||||||
|
|
||||||
|
@ -157,7 +170,7 @@ namespace UDPT
|
||||||
|
|
||||||
for (i = 1;i < this->thread_count; i++)
|
for (i = 1;i < this->thread_count; i++)
|
||||||
{
|
{
|
||||||
ss.clear();
|
ss.str("");
|
||||||
ss << "Starting thread (" << (i + 1) << "/" << ((int)this->thread_count) << ")";
|
ss << "Starting thread (" << (i + 1) << "/" << ((int)this->thread_count) << ")";
|
||||||
logger->log(Logger::LL_INFO, ss.str());
|
logger->log(Logger::LL_INFO, ss.str());
|
||||||
|
|
||||||
|
|
|
@ -125,6 +125,11 @@ namespace UDPT
|
||||||
*/
|
*/
|
||||||
enum StartStatus start ();
|
enum StartStatus start ();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins all threads, and waits for all of them to terminate.
|
||||||
|
*/
|
||||||
|
void wait ();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroys resources that were created by constructor
|
* Destroys resources that were created by constructor
|
||||||
* @param usi Instance to destroy.
|
* @param usi Instance to destroy.
|
||||||
|
|
Loading…
Reference in a new issue