##// END OF EJS Templates
Implement the network controller to permit the execution of a request...
perrinel -
r389:13937fa7979a
parent child
Show More
@@ -3,12 +3,15
3 3
4 4 #include <QLoggingCategory>
5 5 #include <QObject>
6 #include <QUuid>
6 7
7 8 #include <Common/spimpl.h>
9 #include <functional>
8 10
9 11 Q_DECLARE_LOGGING_CATEGORY(LOG_NetworkController)
10 12
11 13 class QNetworkReply;
14 class QNetworkRequest;
12 15
13 16 /**
14 17 * @brief The NetworkController class aims to handle all network connection of SciQlop.
@@ -18,14 +21,17 class NetworkController : public QObject {
18 21 public:
19 22 explicit NetworkController(QObject *parent = 0);
20 23
21 void execute(QNetworkReply *reply);
22
23
24 24 void initialize();
25 25 void finalize();
26 26
27 public slots:
28 void onProcessRequested(const QNetworkRequest &request, QUuid identifier,
29 std::function<void(QNetworkReply *, QUuid)> callback);
30 void onReplyCanceled(QUuid identifier);
31
27 32 signals:
28 replyToRead();
33 void replyFinished(QNetworkReply *reply, QUuid identifier);
34 void replyDownloadProgress(QUuid identifier);
29 35
30 36 private:
31 37 void waitForFinish();
@@ -3,42 +3,66
3 3 #include <QMutex>
4 4 #include <QNetworkAccessManager>
5 5 #include <QNetworkReply>
6 #include <QNetworkRequest>
6 7 #include <QThread>
7 8
9 #include <unordered_map>
10
8 11 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
9 12
10 13 struct NetworkController::NetworkControllerPrivate {
11 explicit NetworkControllerPrivate(NetworkController *parent)
12 : m_WorkingMutex{}, m_AccessManager{std::make_unique<QNetworkAccessManager>()}
13 {
14 }
14 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
15 15 QMutex m_WorkingMutex;
16 16
17 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
17 18 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
18 19 };
19 20
20 21 NetworkController::NetworkController(QObject *parent)
21 22 : QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
22 23 {
23
24 24 }
25 25
26 void NetworkController::execute(QNetworkReply *reply)
26 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
27 std::function<void(QNetworkReply *, QUuid)> callback)
27 28 {
28 auto replyReadyToRead =[reply, this] () {
29 auto content = reply->readAll();
29 qCDebug(LOG_NetworkController()) << tr("NetworkController registered")
30 << QThread::currentThread();
31 auto reply = impl->m_AccessManager->get(request);
32
33 // Store the couple reply id
34 impl->m_NetworkReplyToVariableId[reply] = identifier;
30 35
31 emit this->replyToRead();
36 auto onReplyFinished = [reply, this, identifier, callback]() {
37
38 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
39 << QThread::currentThread();
40 auto it = impl->m_NetworkReplyToVariableId.find(reply);
41 if (it != impl->m_NetworkReplyToVariableId.cend()) {
42 callback(reply, identifier);
43 }
32 44 };
33 45
34 connect(impl->m_Reply, &QNetworkReply::finished, this, replyReadyToRead);
35 connect(impl->m_Reply, &QNetworkReply::aboutToClose, this, replyReadyToRead);
46 auto onReplyDownloadProgress = [reply, this]() {
47
48 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyDownloadProgress")
49 << QThread::currentThread();
50 auto it = impl->m_NetworkReplyToVariableId.find(reply);
51 if (it != impl->m_NetworkReplyToVariableId.cend()) {
52 emit this->replyDownloadProgress(it->second);
53 }
54 };
55
56
57 connect(reply, &QNetworkReply::finished, this, onReplyFinished);
58 connect(reply, &QNetworkReply::downloadProgress, this, onReplyDownloadProgress);
36 59 }
37 60
38 61 void NetworkController::initialize()
39 62 {
40 63 qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread();
41 64 impl->m_WorkingMutex.lock();
65 impl->m_AccessManager = std::make_unique<QNetworkAccessManager>();
42 66 qCDebug(LOG_NetworkController()) << tr("NetworkController init END");
43 67 }
44 68
@@ -47,6 +71,17 void NetworkController::finalize()
47 71 impl->m_WorkingMutex.unlock();
48 72 }
49 73
74 void NetworkController::onReplyCanceled(QUuid identifier)
75 {
76 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
77
78 auto end = impl->m_NetworkReplyToVariableId.cend();
79 auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
80 if (it != end) {
81 it->first->abort();
82 }
83 }
84
50 85 void NetworkController::waitForFinish()
51 86 {
52 87 QMutexLocker locker{&impl->m_WorkingMutex};
@@ -38,7 +38,7 struct VariableController::VariableControllerPrivate {
38 38
39 39 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
40 40 m_VariableToProviderMap;
41 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToToken;
41 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifier;
42 42 };
43 43
44 44 VariableController::VariableController(QObject *parent)
@@ -120,18 +120,18 void VariableController::createVariable(const QString &name,
120 120 /// in sciqlop
121 121 auto dateTime = impl->m_TimeController->dateTime();
122 122 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) {
123 auto token = QUuid::createUuid();
123 auto identifier = QUuid::createUuid();
124 124
125 125 // store the provider
126 126 impl->m_VariableToProviderMap[newVariable] = provider;
127 impl->m_VariableToToken[newVariable] = token;
127 impl->m_VariableToIdentifier[newVariable] = identifier;
128 128
129 129 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
130 QUuid token, auto dataSeriesAcquired, auto dateTimeToPutInCache)
130 QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache)
131 131 {
132 132 if (auto variable = varW.lock()) {
133 auto varToken = impl->m_VariableToToken.at(variable);
134 if (varToken == token) {
133 auto varIdentifier = impl->m_VariableToIdentifier.at(variable);
134 if (varIdentifier == identifier) {
135 135 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
136 136 variable->setDataSeries(dataSeriesAcquired);
137 137 }
@@ -173,9 +173,9 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable
173 173
174 174 if (!dateTimeListNotInCache.empty()) {
175 175 // Ask the provider for each data on the dateTimeListNotInCache
176 auto token = impl->m_VariableToToken.at(variable);
176 auto identifier = impl->m_VariableToIdentifier.at(variable);
177 177 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
178 token, std::move(dateTimeListNotInCache));
178 identifier, std::move(dateTimeListNotInCache));
179 179 }
180 180 else {
181 181 emit variable->updated();
@@ -78,7 +78,7 public:
78 78 SqpApplication::SqpApplication(int &argc, char **argv)
79 79 : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()}
80 80 {
81 qCInfo(LOG_SqpApplication()) << tr("SqpApplication construction");
81 qCDebug(LOG_SqpApplication()) << tr("SqpApplication construction") << QThread::currentThread();
82 82
83 83 connect(&impl->m_DataSourceControllerThread, &QThread::started,
84 84 impl->m_DataSourceController.get(), &DataSourceController::initialize);
General Comments 0
You need to be logged in to leave comments. Login now