diff --git a/assignment-client/src/AssignmentClient.cpp b/assignment-client/src/AssignmentClient.cpp index f7594e97f2..3e3c7c0609 100644 --- a/assignment-client/src/AssignmentClient.cpp +++ b/assignment-client/src/AssignmentClient.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -36,13 +37,19 @@ int hifiSockAddrMeta = qRegisterMetaType("HifiSockAddr"); AssignmentClient::AssignmentClient(int &argc, char **argv) : QCoreApplication(argc, argv), - _assignmentServerHostname(DEFAULT_ASSIGNMENT_SERVER_HOSTNAME) + _assignmentServerHostname(DEFAULT_ASSIGNMENT_SERVER_HOSTNAME), + _shutdownEventListener(this) { + LogUtils::init(); + setOrganizationName("High Fidelity"); setOrganizationDomain("highfidelity.io"); setApplicationName("assignment-client"); QSettings::setDefaultFormat(QSettings::IniFormat); + installNativeEventFilter(&_shutdownEventListener); + connect(&_shutdownEventListener, SIGNAL(receivedCloseEvent()), SLOT(quit())); + // set the logging target to the the CHILD_TARGET_NAME Logging::setTargetName(ASSIGNMENT_CLIENT_TARGET_NAME); diff --git a/assignment-client/src/AssignmentClient.h b/assignment-client/src/AssignmentClient.h index 9834402dbf..7628aa0a3b 100644 --- a/assignment-client/src/AssignmentClient.h +++ b/assignment-client/src/AssignmentClient.h @@ -14,6 +14,7 @@ #include +#include "ShutdownEventListener.h" #include "ThreadedAssignment.h" class AssignmentClient : public QCoreApplication { @@ -21,6 +22,7 @@ class AssignmentClient : public QCoreApplication { public: AssignmentClient(int &argc, char **argv); static const SharedAssignmentPointer& getCurrentAssignment() { return _currentAssignment; } + private slots: void sendAssignmentRequest(); void readPendingDatagrams(); @@ -30,6 +32,7 @@ private slots: private: Assignment _requestAssignment; static SharedAssignmentPointer _currentAssignment; + ShutdownEventListener _shutdownEventListener; QString _assignmentServerHostname; }; diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 2cd8150e63..56570a4b82 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ 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(), @@ -46,10 +48,16 @@ DomainServer::DomainServer(int argc, char* argv[]) : _cookieSessionHash(), _settingsManager() { + + LogUtils::init(); + setOrganizationName("High Fidelity"); setOrganizationDomain("highfidelity.io"); setApplicationName("domain-server"); QSettings::setDefaultFormat(QSettings::IniFormat); + + installNativeEventFilter(&_shutdownEventListener); + connect(&_shutdownEventListener, SIGNAL(receivedCloseEvent()), SLOT(quit())); qRegisterMetaType("DomainServerWebSessionData"); qRegisterMetaTypeStreamOperators("DomainServerWebSessionData"); diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index 666994c818..10633cc71a 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,7 @@ #include "DomainServerSettingsManager.h" #include "DomainServerWebSessionData.h" +#include "ShutdownEventListener.h" #include "WalletTransaction.h" #include "PendingAssignedNodeData.h" @@ -97,6 +99,8 @@ private: QJsonObject jsonForSocket(const HifiSockAddr& socket); QJsonObject jsonObjectForNode(const SharedNodePointer& node); + + ShutdownEventListener _shutdownEventListener; HTTPManager _httpManager; HTTPSManager* _httpsManager; diff --git a/libraries/shared/src/LogUtils.cpp b/libraries/shared/src/LogUtils.cpp new file mode 100644 index 0000000000..c38f5c8574 --- /dev/null +++ b/libraries/shared/src/LogUtils.cpp @@ -0,0 +1,24 @@ +// +// LogUtils.cpp +// libraries/shared/src +// +// Created by Ryan Huffman on 09/03/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "LogUtils.h" + +void LogUtils::init() { +#ifdef Q_OS_WIN + // Windows applications buffer stdout/err hard when not run from a terminal, + // making assignment clients run from the Stack Manager application not flush + // log messages. + // This will disable the buffering. If this becomes a performance issue, + // an alternative is to call fflush(...) periodically. + setbuf(stdout, NULL); + setbuf(stderr, NULL); +#endif +} diff --git a/libraries/shared/src/LogUtils.h b/libraries/shared/src/LogUtils.h new file mode 100644 index 0000000000..955c33d338 --- /dev/null +++ b/libraries/shared/src/LogUtils.h @@ -0,0 +1,20 @@ +// +// LogUtils.h +// libraries/shared/src +// +// Created by Ryan Huffman on 09/03/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_LogUtils_h +#define hifi_LogUtils_h + +class LogUtils { +public: + static void init(); +}; + +#endif // hifi_LogUtils_h diff --git a/libraries/shared/src/ShutdownEventListener.cpp b/libraries/shared/src/ShutdownEventListener.cpp new file mode 100644 index 0000000000..961d18e793 --- /dev/null +++ b/libraries/shared/src/ShutdownEventListener.cpp @@ -0,0 +1,31 @@ +// +// ShutdownEventListener.cpp +// libraries/shared/src +// +// Created by Ryan Huffman on 09/03/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "ShutdownEventListener.h" + +#ifdef Q_OS_WIN +#include +#endif + +ShutdownEventListener::ShutdownEventListener(QObject* parent) : QObject(parent) { +} + +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(); + } + } +#endif + return true; +} diff --git a/libraries/shared/src/ShutdownEventListener.h b/libraries/shared/src/ShutdownEventListener.h new file mode 100644 index 0000000000..e39770c13d --- /dev/null +++ b/libraries/shared/src/ShutdownEventListener.h @@ -0,0 +1,29 @@ +// +// ShutdownEventListener.h +// libraries/shared/src +// +// Created by Ryan Huffman on 09/03/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_ShutdownEventListener_h +#define hifi_ShutdownEventListener_h + +#include +#include + +class ShutdownEventListener : public QObject, public QAbstractNativeEventFilter { + Q_OBJECT +public: + ShutdownEventListener(QObject* parent = NULL); + + virtual bool nativeEventFilter(const QByteArray& eventType, void* message, long* result); + +signals: + void receivedCloseEvent(); +}; + +#endif // hifi_ShutdownEventListener_h