##// END OF EJS Templates
Display errors of NetworkAcessManager
perrinel -
r426:e9bd57e578b4
parent child
Show More
@@ -1,120 +1,133
1 #include "Network/NetworkController.h"
1 #include "Network/NetworkController.h"
2
2
3 #include <QMutex>
3 #include <QMutex>
4 #include <QNetworkAccessManager>
4 #include <QNetworkAccessManager>
5 #include <QNetworkReply>
5 #include <QNetworkReply>
6 #include <QNetworkRequest>
6 #include <QNetworkRequest>
7 #include <QReadWriteLock>
7 #include <QReadWriteLock>
8 #include <QThread>
8 #include <QThread>
9
9
10 #include <unordered_map>
10 #include <unordered_map>
11
11
12 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
12 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
13
13
14 struct NetworkController::NetworkControllerPrivate {
14 struct NetworkController::NetworkControllerPrivate {
15 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
15 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
16 QMutex m_WorkingMutex;
16 QMutex m_WorkingMutex;
17
17
18 QReadWriteLock m_Lock;
18 QReadWriteLock m_Lock;
19 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
19 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
20 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
20 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
21
21
22 void lockRead() { m_Lock.lockForRead(); }
22 void lockRead() { m_Lock.lockForRead(); }
23 void lockWrite() { m_Lock.lockForWrite(); }
23 void lockWrite() { m_Lock.lockForWrite(); }
24 void unlock() { m_Lock.unlock(); }
24 void unlock() { m_Lock.unlock(); }
25 };
25 };
26
26
27 NetworkController::NetworkController(QObject *parent)
27 NetworkController::NetworkController(QObject *parent)
28 : QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
28 : QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
29 {
29 {
30 }
30 }
31
31
32 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
32 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
33 std::function<void(QNetworkReply *, QUuid)> callback)
33 std::function<void(QNetworkReply *, QUuid)> callback)
34 {
34 {
35 auto reply = impl->m_AccessManager->get(request);
36 qCDebug(LOG_NetworkController()) << tr("NetworkController registered")
35 qCDebug(LOG_NetworkController()) << tr("NetworkController registered")
37 << QThread::currentThread() << reply;
36 << QThread::currentThread()->objectName();
37 auto reply = impl->m_AccessManager->get(request);
38
38
39 // Store the couple reply id
39 // Store the couple reply id
40 impl->lockWrite();
40 impl->lockWrite();
41 impl->m_NetworkReplyToVariableId[reply] = identifier;
41 impl->m_NetworkReplyToVariableId[reply] = identifier;
42 impl->unlock();
42 impl->unlock();
43
43
44 auto onReplyFinished = [reply, this, identifier, callback]() {
44 auto onReplyFinished = [reply, this, identifier, callback]() {
45
45
46 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
46 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
47 << QThread::currentThread() << reply;
47 << QThread::currentThread() << reply;
48 impl->lockRead();
48 impl->lockRead();
49 auto it = impl->m_NetworkReplyToVariableId.find(reply);
49 auto it = impl->m_NetworkReplyToVariableId.find(reply);
50 impl->unlock();
50 impl->unlock();
51 if (it != impl->m_NetworkReplyToVariableId.cend()) {
51 if (it != impl->m_NetworkReplyToVariableId.cend()) {
52 impl->lockWrite();
52 impl->lockWrite();
53 impl->m_NetworkReplyToVariableId.erase(reply);
53 impl->m_NetworkReplyToVariableId.erase(reply);
54 impl->unlock();
54 impl->unlock();
55 // Deletes reply
55 // Deletes reply
56 callback(reply, identifier);
56 callback(reply, identifier);
57 reply->deleteLater();
57 reply->deleteLater();
58
58
59 emit this->replyDownloadProgress(identifier, 0);
59 emit this->replyDownloadProgress(identifier, 0);
60 }
60 }
61
61
62 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished END")
62 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished END")
63 << QThread::currentThread() << reply;
63 << QThread::currentThread() << reply;
64 };
64 };
65
65
66 auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) {
66 auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) {
67
67
68 double progress = (bytesRead * 100.0) / totalBytes;
68 double progress = (bytesRead * 100.0) / totalBytes;
69 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress
69 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress
70 << QThread::currentThread() << reply;
70 << QThread::currentThread() << reply;
71 impl->lockRead();
71 impl->lockRead();
72 auto it = impl->m_NetworkReplyToVariableId.find(reply);
72 auto it = impl->m_NetworkReplyToVariableId.find(reply);
73 impl->unlock();
73 impl->unlock();
74 if (it != impl->m_NetworkReplyToVariableId.cend()) {
74 if (it != impl->m_NetworkReplyToVariableId.cend()) {
75 emit this->replyDownloadProgress(it->second, progress);
75 emit this->replyDownloadProgress(it->second, progress);
76 }
76 }
77 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END")
77 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END")
78 << QThread::currentThread() << reply;
78 << QThread::currentThread() << reply;
79 };
79 };
80
80
81
81
82 connect(reply, &QNetworkReply::finished, this, onReplyFinished);
82 connect(reply, &QNetworkReply::finished, this, onReplyFinished);
83 connect(reply, &QNetworkReply::downloadProgress, this, onReplyProgress);
83 connect(reply, &QNetworkReply::downloadProgress, this, onReplyProgress);
84 qCDebug(LOG_NetworkController()) << tr("NetworkController registered END")
85 << QThread::currentThread()->objectName() << reply;
84 }
86 }
85
87
86 void NetworkController::initialize()
88 void NetworkController::initialize()
87 {
89 {
88 qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread();
90 qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread();
89 impl->m_WorkingMutex.lock();
91 impl->m_WorkingMutex.lock();
90 impl->m_AccessManager = std::make_unique<QNetworkAccessManager>();
92 impl->m_AccessManager = std::make_unique<QNetworkAccessManager>();
93
94
95 auto onReplyErrors = [this](QNetworkReply *reply, const QList<QSslError> &errors) {
96
97 qCCritical(LOG_NetworkController()) << tr("NetworkAcessManager errors: ") << errors;
98
99 };
100
101
102 connect(impl->m_AccessManager.get(), &QNetworkAccessManager::sslErrors, this, onReplyErrors);
103
91 qCDebug(LOG_NetworkController()) << tr("NetworkController init END");
104 qCDebug(LOG_NetworkController()) << tr("NetworkController init END");
92 }
105 }
93
106
94 void NetworkController::finalize()
107 void NetworkController::finalize()
95 {
108 {
96 impl->m_WorkingMutex.unlock();
109 impl->m_WorkingMutex.unlock();
97 }
110 }
98
111
99 void NetworkController::onReplyCanceled(QUuid identifier)
112 void NetworkController::onReplyCanceled(QUuid identifier)
100 {
113 {
101 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
114 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
102 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled")
115 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled")
103 << QThread::currentThread();
116 << QThread::currentThread();
104
117
105
118
106 impl->lockRead();
119 impl->lockRead();
107 auto end = impl->m_NetworkReplyToVariableId.cend();
120 auto end = impl->m_NetworkReplyToVariableId.cend();
108 auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
121 auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
109 impl->unlock();
122 impl->unlock();
110 if (it != end) {
123 if (it != end) {
111 it->first->abort();
124 it->first->abort();
112 }
125 }
113 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END")
126 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END")
114 << QThread::currentThread();
127 << QThread::currentThread();
115 }
128 }
116
129
117 void NetworkController::waitForFinish()
130 void NetworkController::waitForFinish()
118 {
131 {
119 QMutexLocker locker{&impl->m_WorkingMutex};
132 QMutexLocker locker{&impl->m_WorkingMutex};
120 }
133 }
General Comments 0
You need to be logged in to leave comments. Login now