##// END OF EJS Templates
Implement the network controller to permit the execution of a request...
perrinel -
r389:13937fa7979a
parent child
Show More
@@ -1,37 +1,43
1 1 #ifndef SCIQLOP_NETWORKCONTROLLER_H
2 2 #define SCIQLOP_NETWORKCONTROLLER_H
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.
15 18 */
16 19 class NetworkController : public QObject {
17 20 Q_OBJECT
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();
32 38
33 39 class NetworkControllerPrivate;
34 40 spimpl::unique_impl_ptr<NetworkControllerPrivate> impl;
35 41 };
36 42
37 43 #endif // SCIQLOP_NETWORKCONTROLLER_H
@@ -1,53 +1,88
1 1 #include "Network/NetworkController.h"
2 2
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;
35
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 }
44 };
45
46 auto onReplyDownloadProgress = [reply, this]() {
30 47
31 emit this->replyToRead();
32 };
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 };
33 55
34 connect(impl->m_Reply, &QNetworkReply::finished, this, replyReadyToRead);
35 connect(impl->m_Reply, &QNetworkReply::aboutToClose, this, replyReadyToRead);
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
45 69 void NetworkController::finalize()
46 70 {
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};
53 88 }
@@ -1,205 +1,205
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableCacheController.h>
3 3 #include <Variable/VariableController.h>
4 4 #include <Variable/VariableModel.h>
5 5
6 6 #include <Data/DataProviderParameters.h>
7 7 #include <Data/IDataProvider.h>
8 8 #include <Data/IDataSeries.h>
9 9 #include <Time/TimeController.h>
10 10
11 11 #include <QDateTime>
12 12 #include <QMutex>
13 13 #include <QThread>
14 14 #include <QUuid>
15 15 #include <QtCore/QItemSelectionModel>
16 16
17 17 #include <unordered_map>
18 18
19 19 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
20 20
21 21 struct VariableController::VariableControllerPrivate {
22 22 explicit VariableControllerPrivate(VariableController *parent)
23 23 : m_WorkingMutex{},
24 24 m_VariableModel{new VariableModel{parent}},
25 25 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
26 26 m_VariableCacheController{std::make_unique<VariableCacheController>()}
27 27 {
28 28 }
29 29
30 30 QMutex m_WorkingMutex;
31 31 /// Variable model. The VariableController has the ownership
32 32 VariableModel *m_VariableModel;
33 33 QItemSelectionModel *m_VariableSelectionModel;
34 34
35 35
36 36 TimeController *m_TimeController{nullptr};
37 37 std::unique_ptr<VariableCacheController> m_VariableCacheController;
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)
45 45 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
46 46 {
47 47 qCDebug(LOG_VariableController()) << tr("VariableController construction")
48 48 << QThread::currentThread();
49 49 }
50 50
51 51 VariableController::~VariableController()
52 52 {
53 53 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
54 54 << QThread::currentThread();
55 55 this->waitForFinish();
56 56 }
57 57
58 58 VariableModel *VariableController::variableModel() noexcept
59 59 {
60 60 return impl->m_VariableModel;
61 61 }
62 62
63 63 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
64 64 {
65 65 return impl->m_VariableSelectionModel;
66 66 }
67 67
68 68 void VariableController::setTimeController(TimeController *timeController) noexcept
69 69 {
70 70 impl->m_TimeController = timeController;
71 71 }
72 72
73 73 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
74 74 {
75 75 if (!variable) {
76 76 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
77 77 return;
78 78 }
79 79
80 80 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
81 81 // make some treatments before the deletion
82 82 emit variableAboutToBeDeleted(variable);
83 83
84 84 // Deletes provider
85 85 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
86 86 qCDebug(LOG_VariableController())
87 87 << tr("Number of providers deleted for variable %1: %2")
88 88 .arg(variable->name(), QString::number(nbProvidersDeleted));
89 89
90 90 // Clears cache
91 91 impl->m_VariableCacheController->clear(variable);
92 92
93 93 // Deletes from model
94 94 impl->m_VariableModel->deleteVariable(variable);
95 95 }
96 96
97 97 void VariableController::deleteVariables(
98 98 const QVector<std::shared_ptr<Variable> > &variables) noexcept
99 99 {
100 100 for (auto variable : qAsConst(variables)) {
101 101 deleteVariable(variable);
102 102 }
103 103 }
104 104
105 105 void VariableController::createVariable(const QString &name,
106 106 std::shared_ptr<IDataProvider> provider) noexcept
107 107 {
108 108
109 109 if (!impl->m_TimeController) {
110 110 qCCritical(LOG_VariableController())
111 111 << tr("Impossible to create variable: The time controller is null");
112 112 return;
113 113 }
114 114
115 115
116 116 /// @todo : for the moment :
117 117 /// - the provider is only used to retrieve data from the variable for its initialization, but
118 118 /// it will be retained later
119 119 /// - default data are generated for the variable, without taking into account the timerange set
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 }
138 138 }
139 139 };
140 140
141 141 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
142 142 this->onRequestDataLoading(newVariable, dateTime);
143 143 }
144 144 }
145 145
146 146 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
147 147 {
148 148 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
149 149 << QThread::currentThread()->objectName();
150 150 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
151 151
152 152 for (const auto &selectedRow : qAsConst(selectedRows)) {
153 153 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
154 154 selectedVariable->setDateTime(dateTime);
155 155 this->onRequestDataLoading(selectedVariable, dateTime);
156 156 }
157 157 }
158 158 }
159 159
160 160
161 161 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
162 162 const SqpDateTime &dateTime)
163 163 {
164 164 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
165 165 << QThread::currentThread()->objectName();
166 166 // we want to load data of the variable for the dateTime.
167 167 // First we check if the cache contains some of them.
168 168 // For the other, we ask the provider to give them.
169 169 if (variable) {
170 170
171 171 auto dateTimeListNotInCache
172 172 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
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();
182 182 }
183 183 }
184 184 else {
185 185 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
186 186 }
187 187 }
188 188
189 189
190 190 void VariableController::initialize()
191 191 {
192 192 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
193 193 impl->m_WorkingMutex.lock();
194 194 qCDebug(LOG_VariableController()) << tr("VariableController init END");
195 195 }
196 196
197 197 void VariableController::finalize()
198 198 {
199 199 impl->m_WorkingMutex.unlock();
200 200 }
201 201
202 202 void VariableController::waitForFinish()
203 203 {
204 204 QMutexLocker locker{&impl->m_WorkingMutex};
205 205 }
@@ -1,140 +1,140
1 1 #include "SqpApplication.h"
2 2
3 3 #include <Data/IDataProvider.h>
4 4 #include <DataSource/DataSourceController.h>
5 5 #include <Network/NetworkController.h>
6 6 #include <QThread>
7 7 #include <Time/TimeController.h>
8 8 #include <Variable/Variable.h>
9 9 #include <Variable/VariableController.h>
10 10 #include <Visualization/VisualizationController.h>
11 11
12 12 Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication")
13 13
14 14 class SqpApplication::SqpApplicationPrivate {
15 15 public:
16 16 SqpApplicationPrivate()
17 17 : m_DataSourceController{std::make_unique<DataSourceController>()},
18 18 m_NetworkController{std::make_unique<NetworkController>()},
19 19 m_TimeController{std::make_unique<TimeController>()},
20 20 m_VariableController{std::make_unique<VariableController>()},
21 21 m_VisualizationController{std::make_unique<VisualizationController>()}
22 22 {
23 23 // /////////////////////////////// //
24 24 // Connections between controllers //
25 25 // /////////////////////////////// //
26 26
27 27 // VariableController <-> DataSourceController
28 28 connect(m_DataSourceController.get(),
29 29 SIGNAL(variableCreationRequested(const QString &, std::shared_ptr<IDataProvider>)),
30 30 m_VariableController.get(),
31 31 SLOT(createVariable(const QString &, std::shared_ptr<IDataProvider>)));
32 32
33 33 // VariableController <-> VisualizationController
34 34 connect(m_VariableController.get(),
35 35 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)),
36 36 m_VisualizationController.get(),
37 37 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)), Qt::DirectConnection);
38 38
39 39
40 40 m_DataSourceController->moveToThread(&m_DataSourceControllerThread);
41 41 m_NetworkController->moveToThread(&m_NetworkControllerThread);
42 42 m_VariableController->moveToThread(&m_VariableControllerThread);
43 43 m_VisualizationController->moveToThread(&m_VisualizationControllerThread);
44 44
45 45
46 46 // Additionnal init
47 47 m_VariableController->setTimeController(m_TimeController.get());
48 48 }
49 49
50 50 virtual ~SqpApplicationPrivate()
51 51 {
52 52 qCInfo(LOG_SqpApplication()) << tr("SqpApplicationPrivate destruction");
53 53 m_DataSourceControllerThread.quit();
54 54 m_DataSourceControllerThread.wait();
55 55
56 56 m_NetworkControllerThread.quit();
57 57 m_NetworkControllerThread.wait();
58 58
59 59 m_VariableControllerThread.quit();
60 60 m_VariableControllerThread.wait();
61 61
62 62 m_VisualizationControllerThread.quit();
63 63 m_VisualizationControllerThread.wait();
64 64 }
65 65
66 66 std::unique_ptr<DataSourceController> m_DataSourceController;
67 67 std::unique_ptr<VariableController> m_VariableController;
68 68 std::unique_ptr<TimeController> m_TimeController;
69 69 std::unique_ptr<NetworkController> m_NetworkController;
70 70 std::unique_ptr<VisualizationController> m_VisualizationController;
71 71 QThread m_DataSourceControllerThread;
72 72 QThread m_NetworkControllerThread;
73 73 QThread m_VariableControllerThread;
74 74 QThread m_VisualizationControllerThread;
75 75 };
76 76
77 77
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);
85 85 connect(&impl->m_DataSourceControllerThread, &QThread::finished,
86 86 impl->m_DataSourceController.get(), &DataSourceController::finalize);
87 87
88 88 connect(&impl->m_NetworkControllerThread, &QThread::started, impl->m_NetworkController.get(),
89 89 &NetworkController::initialize);
90 90 connect(&impl->m_NetworkControllerThread, &QThread::finished, impl->m_NetworkController.get(),
91 91 &NetworkController::finalize);
92 92
93 93 connect(&impl->m_VariableControllerThread, &QThread::started, impl->m_VariableController.get(),
94 94 &VariableController::initialize);
95 95 connect(&impl->m_VariableControllerThread, &QThread::finished, impl->m_VariableController.get(),
96 96 &VariableController::finalize);
97 97
98 98 connect(&impl->m_VisualizationControllerThread, &QThread::started,
99 99 impl->m_VisualizationController.get(), &VisualizationController::initialize);
100 100 connect(&impl->m_VisualizationControllerThread, &QThread::finished,
101 101 impl->m_VisualizationController.get(), &VisualizationController::finalize);
102 102
103 103 impl->m_DataSourceControllerThread.start();
104 104 impl->m_NetworkControllerThread.start();
105 105 impl->m_VariableControllerThread.start();
106 106 impl->m_VisualizationControllerThread.start();
107 107 }
108 108
109 109 SqpApplication::~SqpApplication()
110 110 {
111 111 }
112 112
113 113 void SqpApplication::initialize()
114 114 {
115 115 }
116 116
117 117 DataSourceController &SqpApplication::dataSourceController() noexcept
118 118 {
119 119 return *impl->m_DataSourceController;
120 120 }
121 121
122 122 NetworkController &SqpApplication::networkController() noexcept
123 123 {
124 124 return *impl->m_NetworkController;
125 125 }
126 126
127 127 TimeController &SqpApplication::timeController() noexcept
128 128 {
129 129 return *impl->m_TimeController;
130 130 }
131 131
132 132 VariableController &SqpApplication::variableController() noexcept
133 133 {
134 134 return *impl->m_VariableController;
135 135 }
136 136
137 137 VisualizationController &SqpApplication::visualizationController() noexcept
138 138 {
139 139 return *impl->m_VisualizationController;
140 140 }
General Comments 0
You need to be logged in to leave comments. Login now