diff --git a/assignment-client/src/AssignmentClient.cpp b/assignment-client/src/AssignmentClient.cpp index 9972adfda2..386d6e8a1d 100644 --- a/assignment-client/src/AssignmentClient.cpp +++ b/assignment-client/src/AssignmentClient.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "AssignmentFactory.h" @@ -38,7 +39,6 @@ int hifiSockAddrMeta = qRegisterMetaType("HifiSockAddr"); AssignmentClient::AssignmentClient(int &argc, char **argv) : QCoreApplication(argc, argv), - _shutdownEventListener(this), _assignmentServerHostname(DEFAULT_ASSIGNMENT_SERVER_HOSTNAME), _localASPortSharedMem(NULL) { @@ -49,8 +49,12 @@ AssignmentClient::AssignmentClient(int &argc, char **argv) : setApplicationName("assignment-client"); QSettings::setDefaultFormat(QSettings::IniFormat); - installNativeEventFilter(&_shutdownEventListener); - connect(&_shutdownEventListener, SIGNAL(receivedCloseEvent()), SLOT(quit())); + // setup a shutdown event listener to handle SIGTERM or WM_CLOSE for us +#ifdef _WIN32 + installNativeEventFilter(&ShutdownEventListener::getInstance()); +#else + ShutdownEventListener::getInstance(); +#endif // set the logging target to the the CHILD_TARGET_NAME LogHandler::getInstance().setTargetName(ASSIGNMENT_CLIENT_TARGET_NAME); diff --git a/assignment-client/src/AssignmentClient.h b/assignment-client/src/AssignmentClient.h index 566805d67f..053458f136 100644 --- a/assignment-client/src/AssignmentClient.h +++ b/assignment-client/src/AssignmentClient.h @@ -14,7 +14,6 @@ #include -#include "ShutdownEventListener.h" #include "ThreadedAssignment.h" class QSharedMemory; @@ -34,7 +33,6 @@ private slots: private: Assignment _requestAssignment; static SharedAssignmentPointer _currentAssignment; - ShutdownEventListener _shutdownEventListener; QString _assignmentServerHostname; HifiSockAddr _assignmentServerSocket; QSharedMemory* _localASPortSharedMem; diff --git a/assignment-client/src/AssignmentClientMonitor.cpp b/assignment-client/src/AssignmentClientMonitor.cpp index 358b963b4b..45e1f56d53 100644 --- a/assignment-client/src/AssignmentClientMonitor.cpp +++ b/assignment-client/src/AssignmentClientMonitor.cpp @@ -12,6 +12,7 @@ #include #include +#include #include "AssignmentClientMonitor.h" @@ -19,24 +20,19 @@ const char* NUM_FORKS_PARAMETER = "-n"; const QString ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME = "assignment-client-monitor"; -void signalHandler(int param){ - // get the qApp and cast it to an AssignmentClientMonitor - AssignmentClientMonitor* app = qobject_cast(qApp); - - // tell it to stop the child processes and then go down - app->stopChildProcesses(); - app->quit(); -} - AssignmentClientMonitor::AssignmentClientMonitor(int &argc, char **argv, int numAssignmentClientForks) : QCoreApplication(argc, argv) -{ - // be a signal handler for SIGTERM so we can stop our children when we get it - signal(SIGTERM, signalHandler); - +{ // start the Logging class with the parent's target name LogHandler::getInstance().setTargetName(ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME); + // setup a shutdown event listener to handle SIGTERM or WM_CLOSE for us +#ifdef _WIN32 + installNativeEventFilter(&ShutdownEventListener::getInstance()); +#else + ShutdownEventListener::getInstance(); +#endif + _childArguments = arguments(); // remove the parameter for the number of forks so it isn't passed to the child forked processes @@ -52,6 +48,10 @@ AssignmentClientMonitor::AssignmentClientMonitor(int &argc, char **argv, int num } } +AssignmentClientMonitor::~AssignmentClientMonitor() { + stopChildProcesses(); +} + void AssignmentClientMonitor::stopChildProcesses() { QList >::Iterator it = _childProcesses.begin(); diff --git a/assignment-client/src/AssignmentClientMonitor.h b/assignment-client/src/AssignmentClientMonitor.h index 9dbfcc495a..9a7bca9cb3 100644 --- a/assignment-client/src/AssignmentClientMonitor.h +++ b/assignment-client/src/AssignmentClientMonitor.h @@ -24,6 +24,7 @@ class AssignmentClientMonitor : public QCoreApplication { Q_OBJECT public: AssignmentClientMonitor(int &argc, char **argv, int numAssignmentClientForks); + ~AssignmentClientMonitor(); void stopChildProcesses(); private slots: diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 75db0b8022..bf9505671b 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include "DomainServerNodeData.h" @@ -41,7 +42,6 @@ const QString ICE_SERVER_DEFAULT_HOSTNAME = "ice.highfidelity.io"; DomainServer::DomainServer(int argc, char* argv[]) : QCoreApplication(argc, argv), - _shutdownEventListener(this), _httpManager(DOMAIN_SERVER_HTTP_PORT, QString("%1/resources/web/").arg(QCoreApplication::applicationDirPath()), this), _httpsManager(NULL), _allAssignments(), @@ -70,8 +70,12 @@ DomainServer::DomainServer(int argc, char* argv[]) : _settingsManager.setupConfigMap(arguments()); - installNativeEventFilter(&_shutdownEventListener); - connect(&_shutdownEventListener, SIGNAL(receivedCloseEvent()), SLOT(quit())); + // setup a shutdown event listener to handle SIGTERM or WM_CLOSE for us +#ifdef _WIN32 + installNativeEventFilter(&ShutdownEventListener::getInstance()); +#else + ShutdownEventListener::getInstance(); +#endif qRegisterMetaType("DomainServerWebSessionData"); qRegisterMetaTypeStreamOperators("DomainServerWebSessionData"); diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index 2a92c63923..76ab562f74 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -27,7 +27,6 @@ #include "DomainServerSettingsManager.h" #include "DomainServerWebSessionData.h" -#include "ShutdownEventListener.h" #include "WalletTransaction.h" #include "PendingAssignedNodeData.h" @@ -125,8 +124,6 @@ private: QJsonObject jsonForSocket(const HifiSockAddr& socket); QJsonObject jsonObjectForNode(const SharedNodePointer& node); - - ShutdownEventListener _shutdownEventListener; HTTPManager _httpManager; HTTPSManager* _httpsManager; diff --git a/libraries/shared/src/ShutdownEventListener.cpp b/libraries/shared/src/ShutdownEventListener.cpp index 6cd7592b69..94975b2418 100644 --- a/libraries/shared/src/ShutdownEventListener.cpp +++ b/libraries/shared/src/ShutdownEventListener.cpp @@ -9,21 +9,43 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include + #include "ShutdownEventListener.h" #ifdef Q_OS_WIN #include +#else +#include #endif -ShutdownEventListener::ShutdownEventListener(QObject* parent) : QObject(parent) { +ShutdownEventListener& ShutdownEventListener::getInstance() { + static ShutdownEventListener staticInstance; + return staticInstance; } +void signalHandler(int param) { + // tell the qApp it should quit + QMetaObject::invokeMethod(qApp, "quit"); +} + +ShutdownEventListener::ShutdownEventListener(QObject* parent) : + QObject(parent) +{ +#ifndef Q_OS_WIN + // be a signal handler for SIGTERM so we can stop our children when we get it + signal(SIGTERM, signalHandler); +#endif +} + + bool ShutdownEventListener::nativeEventFilter(const QByteArray &eventType, void* msg, long* result) { #ifdef Q_OS_WIN if (eventType == "windows_generic_MSG") { MSG* message = (MSG*)msg; if (message->message == WM_CLOSE) { - emit receivedCloseEvent(); + // tell our registered application to quit + QMetaObject::invokeMethod(qApp, "quit"); } } #endif diff --git a/libraries/shared/src/ShutdownEventListener.h b/libraries/shared/src/ShutdownEventListener.h index e39770c13d..f6d50401b2 100644 --- a/libraries/shared/src/ShutdownEventListener.h +++ b/libraries/shared/src/ShutdownEventListener.h @@ -18,12 +18,11 @@ class ShutdownEventListener : public QObject, public QAbstractNativeEventFilter { Q_OBJECT public: - ShutdownEventListener(QObject* parent = NULL); + static ShutdownEventListener& getInstance(); virtual bool nativeEventFilter(const QByteArray& eventType, void* message, long* result); - -signals: - void receivedCloseEvent(); +private: + ShutdownEventListener(QObject* parent = 0); }; #endif // hifi_ShutdownEventListener_h