NetworkController.cpp
133 lines
| 4.8 KiB
| text/x-c
|
CppLexer
r339 | #include "Network/NetworkController.h" | |||
#include <QMutex> | ||||
r386 | #include <QNetworkAccessManager> | |||
#include <QNetworkReply> | ||||
r389 | #include <QNetworkRequest> | |||
r423 | #include <QReadWriteLock> | |||
r339 | #include <QThread> | |||
r389 | #include <unordered_map> | |||
r339 | Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController") | |||
struct NetworkController::NetworkControllerPrivate { | ||||
r389 | explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {} | |||
r539 | ||||
void lockRead() { m_Lock.lockForRead(); } | ||||
void lockWrite() { m_Lock.lockForWrite(); } | ||||
void unlock() { m_Lock.unlock(); } | ||||
r339 | QMutex m_WorkingMutex; | |||
r386 | ||||
r423 | QReadWriteLock m_Lock; | |||
r389 | std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId; | |||
r386 | std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr}; | |||
r339 | }; | |||
NetworkController::NetworkController(QObject *parent) | ||||
: QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)} | ||||
{ | ||||
r386 | } | |||
r751 | void NetworkController::onProcessRequested(std::shared_ptr<QNetworkRequest> request, | |||
QUuid identifier, | ||||
r389 | std::function<void(QNetworkReply *, QUuid)> callback) | |||
r386 | { | |||
r751 | qCDebug(LOG_NetworkController()) << tr("NetworkController onProcessRequested") | |||
<< QThread::currentThread()->objectName() << &request; | ||||
auto reply = impl->m_AccessManager->get(*request); | ||||
r389 | ||||
// Store the couple reply id | ||||
r423 | impl->lockWrite(); | |||
r389 | impl->m_NetworkReplyToVariableId[reply] = identifier; | |||
r423 | impl->unlock(); | |||
r389 | ||||
r750 | auto onReplyFinished = [request, reply, this, identifier, callback]() { | |||
r389 | ||||
r423 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished") | |||
r751 | << QThread::currentThread() << request.get() << reply; | |||
r423 | impl->lockRead(); | |||
r389 | auto it = impl->m_NetworkReplyToVariableId.find(reply); | |||
r423 | impl->unlock(); | |||
r389 | if (it != impl->m_NetworkReplyToVariableId.cend()) { | |||
r423 | impl->lockWrite(); | |||
impl->m_NetworkReplyToVariableId.erase(reply); | ||||
impl->unlock(); | ||||
// Deletes reply | ||||
r389 | callback(reply, identifier); | |||
r423 | reply->deleteLater(); | |||
r389 | } | |||
r423 | ||||
qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished END") | ||||
<< QThread::currentThread() << reply; | ||||
r389 | }; | |||
r750 | auto onReplyProgress = [reply, request, this](qint64 bytesRead, qint64 totalBytes) { | |||
r386 | ||||
r401 | double progress = (bytesRead * 100.0) / totalBytes; | |||
r423 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress | |||
r751 | << QThread::currentThread() << request.get() << reply; | |||
r423 | impl->lockRead(); | |||
r389 | auto it = impl->m_NetworkReplyToVariableId.find(reply); | |||
r423 | impl->unlock(); | |||
r389 | if (it != impl->m_NetworkReplyToVariableId.cend()) { | |||
r750 | emit this->replyDownloadProgress(it->second, request, progress); | |||
r389 | } | |||
r423 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END") | |||
<< QThread::currentThread() << reply; | ||||
r389 | }; | |||
r386 | ||||
r389 | ||||
connect(reply, &QNetworkReply::finished, this, onReplyFinished); | ||||
r401 | connect(reply, &QNetworkReply::downloadProgress, this, onReplyProgress); | |||
r426 | qCDebug(LOG_NetworkController()) << tr("NetworkController registered END") | |||
<< QThread::currentThread()->objectName() << reply; | ||||
r339 | } | |||
void NetworkController::initialize() | ||||
{ | ||||
qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread(); | ||||
impl->m_WorkingMutex.lock(); | ||||
r389 | impl->m_AccessManager = std::make_unique<QNetworkAccessManager>(); | |||
r426 | ||||
auto onReplyErrors = [this](QNetworkReply *reply, const QList<QSslError> &errors) { | ||||
qCCritical(LOG_NetworkController()) << tr("NetworkAcessManager errors: ") << errors; | ||||
}; | ||||
connect(impl->m_AccessManager.get(), &QNetworkAccessManager::sslErrors, this, onReplyErrors); | ||||
r339 | qCDebug(LOG_NetworkController()) << tr("NetworkController init END"); | |||
} | ||||
void NetworkController::finalize() | ||||
{ | ||||
impl->m_WorkingMutex.unlock(); | ||||
} | ||||
r389 | void NetworkController::onReplyCanceled(QUuid identifier) | |||
{ | ||||
auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; | ||||
r423 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled") | |||
<< QThread::currentThread(); | ||||
r389 | ||||
r423 | impl->lockRead(); | |||
r389 | auto end = impl->m_NetworkReplyToVariableId.cend(); | |||
auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply); | ||||
r423 | impl->unlock(); | |||
r389 | if (it != end) { | |||
it->first->abort(); | ||||
} | ||||
r423 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END") | |||
<< QThread::currentThread(); | ||||
r389 | } | |||
r339 | void NetworkController::waitForFinish() | |||
{ | ||||
QMutexLocker locker{&impl->m_WorkingMutex}; | ||||
} | ||||