##// END OF EJS Templates
Merge branch 'feature/AbortDownload' into develop
Merge branch 'feature/AbortDownload' into develop

File last commit:

r423:3698da027722
r424:0d257d28af1c merge
Show More
NetworkController.cpp
120 lines | 4.3 KiB | text/x-c | CppLexer
/ core / src / Network / NetworkController.cpp
Intialization of network controller
r339 #include "Network/NetworkController.h"
#include <QMutex>
Add execute skelleton Network
r386 #include <QNetworkAccessManager>
#include <QNetworkReply>
Implement the network controller to permit the execution of a request...
r389 #include <QNetworkRequest>
Add thread protection on AbortDownload process
r423 #include <QReadWriteLock>
Intialization of network controller
r339 #include <QThread>
Implement the network controller to permit the execution of a request...
r389 #include <unordered_map>
Intialization of network controller
r339 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
struct NetworkController::NetworkControllerPrivate {
Implement the network controller to permit the execution of a request...
r389 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
Intialization of network controller
r339 QMutex m_WorkingMutex;
Add execute skelleton Network
r386
Add thread protection on AbortDownload process
r423 QReadWriteLock m_Lock;
Implement the network controller to permit the execution of a request...
r389 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
Add execute skelleton Network
r386 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
Add thread protection on AbortDownload process
r423
void lockRead() { m_Lock.lockForRead(); }
void lockWrite() { m_Lock.lockForWrite(); }
void unlock() { m_Lock.unlock(); }
Intialization of network controller
r339 };
NetworkController::NetworkController(QObject *parent)
: QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
{
Add execute skelleton Network
r386 }
Implement the network controller to permit the execution of a request...
r389 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
std::function<void(QNetworkReply *, QUuid)> callback)
Add execute skelleton Network
r386 {
Implement the network controller to permit the execution of a request...
r389 auto reply = impl->m_AccessManager->get(request);
Add thread protection on AbortDownload process
r423 qCDebug(LOG_NetworkController()) << tr("NetworkController registered")
<< QThread::currentThread() << reply;
Implement the network controller to permit the execution of a request...
r389
// Store the couple reply id
Add thread protection on AbortDownload process
r423 impl->lockWrite();
Implement the network controller to permit the execution of a request...
r389 impl->m_NetworkReplyToVariableId[reply] = identifier;
Add thread protection on AbortDownload process
r423 impl->unlock();
Implement the network controller to permit the execution of a request...
r389
auto onReplyFinished = [reply, this, identifier, callback]() {
Add thread protection on AbortDownload process
r423 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
<< QThread::currentThread() << reply;
impl->lockRead();
Implement the network controller to permit the execution of a request...
r389 auto it = impl->m_NetworkReplyToVariableId.find(reply);
Add thread protection on AbortDownload process
r423 impl->unlock();
Implement the network controller to permit the execution of a request...
r389 if (it != impl->m_NetworkReplyToVariableId.cend()) {
Add thread protection on AbortDownload process
r423 impl->lockWrite();
impl->m_NetworkReplyToVariableId.erase(reply);
impl->unlock();
// Deletes reply
Implement the network controller to permit the execution of a request...
r389 callback(reply, identifier);
Add thread protection on AbortDownload process
r423 reply->deleteLater();
emit this->replyDownloadProgress(identifier, 0);
Implement the network controller to permit the execution of a request...
r389 }
Add thread protection on AbortDownload process
r423
qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished END")
<< QThread::currentThread() << reply;
Implement the network controller to permit the execution of a request...
r389 };
Add implementation of progress bar on variable inspector connected to...
r401 auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) {
Add execute skelleton Network
r386
Add implementation of progress bar on variable inspector connected to...
r401 double progress = (bytesRead * 100.0) / totalBytes;
Add thread protection on AbortDownload process
r423 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress
<< QThread::currentThread() << reply;
impl->lockRead();
Implement the network controller to permit the execution of a request...
r389 auto it = impl->m_NetworkReplyToVariableId.find(reply);
Add thread protection on AbortDownload process
r423 impl->unlock();
Implement the network controller to permit the execution of a request...
r389 if (it != impl->m_NetworkReplyToVariableId.cend()) {
Add implementation of progress bar on variable inspector connected to...
r401 emit this->replyDownloadProgress(it->second, progress);
Implement the network controller to permit the execution of a request...
r389 }
Add thread protection on AbortDownload process
r423 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END")
<< QThread::currentThread() << reply;
Implement the network controller to permit the execution of a request...
r389 };
Add execute skelleton Network
r386
Implement the network controller to permit the execution of a request...
r389
connect(reply, &QNetworkReply::finished, this, onReplyFinished);
Add implementation of progress bar on variable inspector connected to...
r401 connect(reply, &QNetworkReply::downloadProgress, this, onReplyProgress);
Intialization of network controller
r339 }
void NetworkController::initialize()
{
qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread();
impl->m_WorkingMutex.lock();
Implement the network controller to permit the execution of a request...
r389 impl->m_AccessManager = std::make_unique<QNetworkAccessManager>();
Intialization of network controller
r339 qCDebug(LOG_NetworkController()) << tr("NetworkController init END");
}
void NetworkController::finalize()
{
impl->m_WorkingMutex.unlock();
}
Implement the network controller to permit the execution of a request...
r389 void NetworkController::onReplyCanceled(QUuid identifier)
{
auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
Add thread protection on AbortDownload process
r423 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled")
<< QThread::currentThread();
Implement the network controller to permit the execution of a request...
r389
Add thread protection on AbortDownload process
r423 impl->lockRead();
Implement the network controller to permit the execution of a request...
r389 auto end = impl->m_NetworkReplyToVariableId.cend();
auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
Add thread protection on AbortDownload process
r423 impl->unlock();
Implement the network controller to permit the execution of a request...
r389 if (it != end) {
it->first->abort();
}
Add thread protection on AbortDownload process
r423 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END")
<< QThread::currentThread();
Implement the network controller to permit the execution of a request...
r389 }
Intialization of network controller
r339 void NetworkController::waitForFinish()
{
QMutexLocker locker{&impl->m_WorkingMutex};
}