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