##// END OF EJS Templates
commit
commit

File last commit:

r681:5e6b86368b57
r684:90684a2a46fa
Show More
NetworkController.cpp
143 lines | 5.0 KiB | text/x-c | CppLexer
/ core / src / Network / NetworkController.cpp
Intialization of network controller
r313 #include "Network/NetworkController.h"
#include <QMutex>
Add execute skelleton Network
r356 #include <QNetworkAccessManager>
#include <QNetworkReply>
Implement the network controller to permit the execution of a request...
r359 #include <QNetworkRequest>
Add thread protection on AbortDownload process
r389 #include <QReadWriteLock>
Intialization of network controller
r313 #include <QThread>
Implement the network controller to permit the execution of a request...
r359 #include <unordered_map>
Intialization of network controller
r313 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
struct NetworkController::NetworkControllerPrivate {
Implement the network controller to permit the execution of a request...
r359 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
Implementation of V5 acquisition
r510
void lockRead() { m_Lock.lockForRead(); }
void lockWrite() { m_Lock.lockForWrite(); }
void unlock() { m_Lock.unlock(); }
Intialization of network controller
r313 QMutex m_WorkingMutex;
Add execute skelleton Network
r356
Add thread protection on AbortDownload process
r389 QReadWriteLock m_Lock;
Implement the network controller to permit the execution of a request...
r359 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
Add execute skelleton Network
r356 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
Intialization of network controller
r313 };
NetworkController::NetworkController(QObject *parent)
: QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
{
Add execute skelleton Network
r356 }
Implement the network controller to permit the execution of a request...
r359 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
std::function<void(QNetworkReply *, QUuid)> callback)
Add execute skelleton Network
r356 {
Implement the network controller to permit the execution of a request...
r359 // Store the couple reply id
Add thread protection on AbortDownload process
r389 impl->lockWrite();
Alexandre Leroux
commit
r681 qCInfo(LOG_NetworkController()) << tr("NetworkController registered") << identifier;
auto reply = impl->m_AccessManager->get(request);
Implement the network controller to permit the execution of a request...
r359 impl->m_NetworkReplyToVariableId[reply] = identifier;
Alexandre Leroux
commit
r681 qCInfo(LOG_NetworkController()) << tr("Reply stored") << identifier;
Add thread protection on AbortDownload process
r389 impl->unlock();
Implement the network controller to permit the execution of a request...
r359
auto onReplyFinished = [reply, this, identifier, callback]() {
Alexandre Leroux
commit
r681 qCInfo(LOG_NetworkController()) << tr("NetworkController onReplyFinished") << identifier;
Add thread protection on AbortDownload process
r389 impl->lockRead();
Implement the network controller to permit the execution of a request...
r359 auto it = impl->m_NetworkReplyToVariableId.find(reply);
Add thread protection on AbortDownload process
r389 impl->unlock();
Implement the network controller to permit the execution of a request...
r359 if (it != impl->m_NetworkReplyToVariableId.cend()) {
Add thread protection on AbortDownload process
r389 impl->lockWrite();
impl->m_NetworkReplyToVariableId.erase(reply);
impl->unlock();
// Deletes reply
Implement the network controller to permit the execution of a request...
r359 callback(reply, identifier);
Add thread protection on AbortDownload process
r389 reply->deleteLater();
emit this->replyDownloadProgress(identifier, 0);
Implement the network controller to permit the execution of a request...
r359 }
Add thread protection on AbortDownload process
r389
Alexandre Leroux
temp commit
r680 qCDebug(LOG_NetworkController())
<< tr("NetworkController onReplyFinished END") << QThread::currentThread() << reply;
Implement the network controller to permit the execution of a request...
r359 };
Add implementation of progress bar on variable inspector connected to...
r369 auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) {
Add execute skelleton Network
r356
Add implementation of progress bar on variable inspector connected to...
r369 double progress = (bytesRead * 100.0) / totalBytes;
Add thread protection on AbortDownload process
r389 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress
<< QThread::currentThread() << reply;
impl->lockRead();
Implement the network controller to permit the execution of a request...
r359 auto it = impl->m_NetworkReplyToVariableId.find(reply);
Add thread protection on AbortDownload process
r389 impl->unlock();
Implement the network controller to permit the execution of a request...
r359 if (it != impl->m_NetworkReplyToVariableId.cend()) {
Add implementation of progress bar on variable inspector connected to...
r369 emit this->replyDownloadProgress(it->second, progress);
Implement the network controller to permit the execution of a request...
r359 }
Alexandre Leroux
temp commit
r680 qCDebug(LOG_NetworkController())
<< tr("NetworkController onReplyProgress END") << QThread::currentThread() << reply;
Implement the network controller to permit the execution of a request...
r359 };
Add execute skelleton Network
r356
Implement the network controller to permit the execution of a request...
r359
connect(reply, &QNetworkReply::finished, this, onReplyFinished);
Add implementation of progress bar on variable inspector connected to...
r369 connect(reply, &QNetworkReply::downloadProgress, this, onReplyProgress);
Display errors of NetworkAcessManager
r392 qCDebug(LOG_NetworkController()) << tr("NetworkController registered END")
<< QThread::currentThread()->objectName() << reply;
Intialization of network controller
r313 }
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...
r359 impl->m_AccessManager = std::make_unique<QNetworkAccessManager>();
Display errors of NetworkAcessManager
r392
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);
Intialization of network controller
r313 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...
r359 void NetworkController::onReplyCanceled(QUuid identifier)
{
Alexandre Leroux
commit
r681 // auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
Alexandre Leroux
temp commit
r680 qCDebug(LOG_NetworkController())
<< tr("NetworkController onReplyCanceled") << QThread::currentThread();
Add thread protection on AbortDownload process
r389
Alexandre Leroux
commit
r681 qCInfo(LOG_NetworkController()) << tr("Calls cancel on ") << identifier;
Implement the network controller to permit the execution of a request...
r359
Alexandre Leroux
commit
r681 impl->lockWrite();
for (auto &reply : impl->m_NetworkReplyToVariableId) {
if (reply.second == identifier) {
qCInfo(LOG_NetworkController()) << tr("Cancel on ") << identifier << "applied";
reply.first->abort();
}
Implement the network controller to permit the execution of a request...
r359 }
Alexandre Leroux
commit
r681 impl->unlock();
// auto end = impl->m_NetworkReplyToVariableId.cend();
// auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
// impl->unlock();
// if (it != end) {
// qCInfo(LOG_NetworkController()) << tr("Cancel on ") << identifier << "applied";
// it->first->abort();
// }
Alexandre Leroux
temp commit
r680 qCDebug(LOG_NetworkController())
<< tr("NetworkController onReplyCanceled END") << QThread::currentThread();
Implement the network controller to permit the execution of a request...
r359 }
Intialization of network controller
r313 void NetworkController::waitForFinish()
{
QMutexLocker locker{&impl->m_WorkingMutex};
}