initial version of the ClosureEventSender using std::thread

This commit is contained in:
Stephen Birarda 2017-05-31 19:00:41 -07:00
parent c29091f0b1
commit 1d9d83b157
9 changed files with 125 additions and 3 deletions

View file

@ -144,6 +144,7 @@
#include "InterfaceLogging.h"
#include "LODManager.h"
#include "ModelPackager.h"
#include "networking/ClosureEventSender.h"
#include "networking/HFWebEngineProfile.h"
#include "networking/HFTabletWebEngineProfile.h"
#include "networking/FileTypeProfile.h"
@ -534,6 +535,7 @@ bool setupEssentials(int& argc, char** argv, bool runningMarkerExisted) {
DependencyManager::set<AvatarBookmarks>();
DependencyManager::set<LocationBookmarks>();
DependencyManager::set<Snapshot>();
DependencyManager::set<ClosureEventSender>();
return previousSessionCrashed;
}
@ -1568,6 +1570,10 @@ void Application::aboutToQuit() {
getActiveDisplayPlugin()->deactivate();
// use the ClosureEventSender via an std::thread (to not use QThread while the application is going down)
// to send an event that says the user asked for the app to close
_userQuitThread = std::thread { &ClosureEventSender::sendQuitStart, DependencyManager::get<ClosureEventSender>() };
// Hide Running Scripts dialog so that it gets destroyed in an orderly manner; prevents warnings at shutdown.
DependencyManager::get<OffscreenUi>()->hide("RunningScripts");

View file

@ -13,6 +13,7 @@
#define hifi_Application_h
#include <functional>
#include <thread>
#include <QtCore/QHash>
#include <QtCore/QPointer>
@ -680,6 +681,8 @@ private:
FileScriptingInterface* _fileDownload;
AudioInjector* _snapshotSoundInjector { nullptr };
SharedSoundPointer _snapshotSound;
std::thread _userQuitThread;
};

View file

@ -30,6 +30,7 @@
#include "InterfaceLogging.h"
#include "UserActivityLogger.h"
#include "MainWindow.h"
#include "networking/ClosureEventSender.h"
#ifdef HAS_BUGSPLAT
#include <BugSplat.h>
@ -267,6 +268,12 @@ int main(int argc, const char* argv[]) {
Application::shutdownPlugins();
if (UserActivityLogger::getInstance().isEnabled()) {
// send a quit finished event here to indicate that this session closed cleanly
std::thread quitCompleteThread { &::ClosureEventSender::sendQuitFinish, DependencyManager::get<ClosureEventSender>() };
quitCompleteThread.join();
}
qCDebug(interfaceapp, "Normal exit.");
#if !defined(DEBUG) && !defined(Q_OS_LINUX)
// HACK: exit immediately (don't handle shutdown callbacks) for Release build

View file

@ -0,0 +1,71 @@
//
// ClosureEventSender.cpp
// interface/src/networking
//
// Created by Stephen Birarda on 5/31/17.
// Copyright 2017 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 <QtCore/QEventLoop>
#include <QtCore/QJsonDocument>
#include <QtNetwork/QHttpMultiPart>
#include <QtNetwork/QNetworkReply>
#include <AccountManager.h>
#include <NetworkAccessManager.h>
#include <NetworkingConstants.h>
#include <UserActivityLogger.h>
#include <UUID.h>
#include "ClosureEventSender.h"
QNetworkRequest createNetworkRequest() {
QNetworkRequest request;
QUrl requestURL = NetworkingConstants::METAVERSE_SERVER_URL;
requestURL.setPath(USER_ACTIVITY_URL);
request.setUrl(requestURL);
auto accountManager = DependencyManager::get<AccountManager>();
if (accountManager->hasValidAccessToken()) {
request.setRawHeader(ACCESS_TOKEN_AUTHORIZATION_HEADER,
accountManager->getAccountInfo().getAccessToken().authorizationHeaderValue());
}
request.setRawHeader(METAVERSE_SESSION_ID_HEADER,
uuidStringWithoutCurlyBraces(accountManager->getSessionID()).toLocal8Bit());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
return request;
}
QByteArray postDataForAction(QString action) {
return QString("{\"action\": \"" + action + "\"}").toUtf8();
}
QNetworkReply* replyForAction(QString action) {
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
return networkAccessManager.post(createNetworkRequest(), postDataForAction(action));
}
void ClosureEventSender::sendQuitStart() {
QNetworkReply* reply = replyForAction("quit_start");
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
}
void ClosureEventSender::sendQuitFinish() {
QNetworkReply* reply = replyForAction("quit_finish");
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
}

View file

@ -0,0 +1,35 @@
//
// ClosureEventSender.h
// interface/src/networking
//
// Created by Stephen Birarda on 5/31/17.
// Copyright 2017 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_ClosureEventSender_h
#define hifi_ClosureEventSender_h
#include <QtCore/QString>
#include <QtCore/QUuid>
#include <DependencyManager.h>
class ClosureEventSender : public Dependency {
SINGLETON_DEPENDENCY
public:
void setSessionID(QUuid sessionID) { _sessionID = sessionID; }
void sendQuitStart();
void sendQuitFinish();
void sendCrashEvent();
private:
QUuid _sessionID;
QString _accessToken;
};
#endif // hifi_ClosureEventSender_h

View file

@ -45,7 +45,6 @@ Q_DECLARE_METATYPE(QNetworkAccessManager::Operation)
Q_DECLARE_METATYPE(JSONCallbackParameters)
const QString ACCOUNTS_GROUP = "accounts";
static const auto METAVERSE_SESSION_ID_HEADER = QString("HFM-SessionID").toLocal8Bit();
JSONCallbackParameters::JSONCallbackParameters(QObject* jsonCallbackReceiver, const QString& jsonCallbackMethod,
QObject* errorCallbackReceiver, const QString& errorCallbackMethod,

View file

@ -52,6 +52,7 @@ namespace AccountManagerAuth {
Q_DECLARE_METATYPE(AccountManagerAuth::Type);
const QByteArray ACCESS_TOKEN_AUTHORIZATION_HEADER = "Authorization";
const auto METAVERSE_SESSION_ID_HEADER = QString("HFM-SessionID").toLocal8Bit();
using UserAgentGetter = std::function<QString()>;

View file

@ -20,8 +20,6 @@
#include <DependencyManager.h>
#include "AddressManager.h"
static const QString USER_ACTIVITY_URL = "/api/v1/user_activities";
UserActivityLogger& UserActivityLogger::getInstance() {
static UserActivityLogger sharedInstance;
return sharedInstance;

View file

@ -22,6 +22,8 @@
#include <SettingHandle.h>
#include "AddressManager.h"
const QString USER_ACTIVITY_URL = "/api/v1/user_activities";
class UserActivityLogger : public QObject {
Q_OBJECT