@@ -3,12 +3,15 | |||||
3 |
|
3 | |||
4 | #include <QLoggingCategory> |
|
4 | #include <QLoggingCategory> | |
5 | #include <QObject> |
|
5 | #include <QObject> | |
|
6 | #include <QUuid> | |||
6 |
|
7 | |||
7 | #include <Common/spimpl.h> |
|
8 | #include <Common/spimpl.h> | |
|
9 | #include <functional> | |||
8 |
|
10 | |||
9 | Q_DECLARE_LOGGING_CATEGORY(LOG_NetworkController) |
|
11 | Q_DECLARE_LOGGING_CATEGORY(LOG_NetworkController) | |
10 |
|
12 | |||
11 | class QNetworkReply; |
|
13 | class QNetworkReply; | |
|
14 | class QNetworkRequest; | |||
12 |
|
15 | |||
13 | /** |
|
16 | /** | |
14 | * @brief The NetworkController class aims to handle all network connection of SciQlop. |
|
17 | * @brief The NetworkController class aims to handle all network connection of SciQlop. | |
@@ -18,14 +21,17 class NetworkController : public QObject { | |||||
18 | public: |
|
21 | public: | |
19 | explicit NetworkController(QObject *parent = 0); |
|
22 | explicit NetworkController(QObject *parent = 0); | |
20 |
|
23 | |||
21 | void execute(QNetworkReply *reply); |
|
|||
22 |
|
||||
23 |
|
||||
24 | void initialize(); |
|
24 | void initialize(); | |
25 | void finalize(); |
|
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 | signals: |
|
32 | signals: | |
28 | replyToRead(); |
|
33 | void replyFinished(QNetworkReply *reply, QUuid identifier); | |
|
34 | void replyDownloadProgress(QUuid identifier); | |||
29 |
|
35 | |||
30 | private: |
|
36 | private: | |
31 | void waitForFinish(); |
|
37 | void waitForFinish(); |
@@ -3,42 +3,66 | |||||
3 | #include <QMutex> |
|
3 | #include <QMutex> | |
4 | #include <QNetworkAccessManager> |
|
4 | #include <QNetworkAccessManager> | |
5 | #include <QNetworkReply> |
|
5 | #include <QNetworkReply> | |
|
6 | #include <QNetworkRequest> | |||
6 | #include <QThread> |
|
7 | #include <QThread> | |
7 |
|
8 | |||
|
9 | #include <unordered_map> | |||
|
10 | ||||
8 | Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController") |
|
11 | Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController") | |
9 |
|
12 | |||
10 | struct NetworkController::NetworkControllerPrivate { |
|
13 | struct NetworkController::NetworkControllerPrivate { | |
11 | explicit NetworkControllerPrivate(NetworkController *parent) |
|
14 | explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {} | |
12 | : m_WorkingMutex{}, m_AccessManager{std::make_unique<QNetworkAccessManager>()} |
|
|||
13 | { |
|
|||
14 | } |
|
|||
15 | QMutex m_WorkingMutex; |
|
15 | QMutex m_WorkingMutex; | |
16 |
|
16 | |||
|
17 | std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId; | |||
17 | std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr}; |
|
18 | std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr}; | |
18 | }; |
|
19 | }; | |
19 |
|
20 | |||
20 | NetworkController::NetworkController(QObject *parent) |
|
21 | NetworkController::NetworkController(QObject *parent) | |
21 | : QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)} |
|
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 | qCDebug(LOG_NetworkController()) << tr("NetworkController registered") | |
29 | auto content = reply->readAll(); |
|
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); |
|
46 | auto onReplyDownloadProgress = [reply, this]() { | |
35 | connect(impl->m_Reply, &QNetworkReply::aboutToClose, this, replyReadyToRead); |
|
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 | void NetworkController::initialize() |
|
61 | void NetworkController::initialize() | |
39 | { |
|
62 | { | |
40 | qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread(); |
|
63 | qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread(); | |
41 | impl->m_WorkingMutex.lock(); |
|
64 | impl->m_WorkingMutex.lock(); | |
|
65 | impl->m_AccessManager = std::make_unique<QNetworkAccessManager>(); | |||
42 | qCDebug(LOG_NetworkController()) << tr("NetworkController init END"); |
|
66 | qCDebug(LOG_NetworkController()) << tr("NetworkController init END"); | |
43 | } |
|
67 | } | |
44 |
|
68 | |||
@@ -47,6 +71,17 void NetworkController::finalize() | |||||
47 | impl->m_WorkingMutex.unlock(); |
|
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 | void NetworkController::waitForFinish() |
|
85 | void NetworkController::waitForFinish() | |
51 | { |
|
86 | { | |
52 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
87 | QMutexLocker locker{&impl->m_WorkingMutex}; |
@@ -38,7 +38,7 struct VariableController::VariableControllerPrivate { | |||||
38 |
|
38 | |||
39 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > |
|
39 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > | |
40 | m_VariableToProviderMap; |
|
40 | m_VariableToProviderMap; | |
41 |
std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableTo |
|
41 | std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifier; | |
42 | }; |
|
42 | }; | |
43 |
|
43 | |||
44 | VariableController::VariableController(QObject *parent) |
|
44 | VariableController::VariableController(QObject *parent) | |
@@ -120,18 +120,18 void VariableController::createVariable(const QString &name, | |||||
120 | /// in sciqlop |
|
120 | /// in sciqlop | |
121 | auto dateTime = impl->m_TimeController->dateTime(); |
|
121 | auto dateTime = impl->m_TimeController->dateTime(); | |
122 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { |
|
122 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { | |
123 |
auto |
|
123 | auto identifier = QUuid::createUuid(); | |
124 |
|
124 | |||
125 | // store the provider |
|
125 | // store the provider | |
126 | impl->m_VariableToProviderMap[newVariable] = provider; |
|
126 | impl->m_VariableToProviderMap[newVariable] = provider; | |
127 |
impl->m_VariableTo |
|
127 | impl->m_VariableToIdentifier[newVariable] = identifier; | |
128 |
|
128 | |||
129 | auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ]( |
|
129 | auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ]( | |
130 |
QUuid |
|
130 | QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache) | |
131 | { |
|
131 | { | |
132 | if (auto variable = varW.lock()) { |
|
132 | if (auto variable = varW.lock()) { | |
133 |
auto var |
|
133 | auto varIdentifier = impl->m_VariableToIdentifier.at(variable); | |
134 |
if (var |
|
134 | if (varIdentifier == identifier) { | |
135 | impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); |
|
135 | impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); | |
136 | variable->setDataSeries(dataSeriesAcquired); |
|
136 | variable->setDataSeries(dataSeriesAcquired); | |
137 | } |
|
137 | } | |
@@ -173,9 +173,9 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable | |||||
173 |
|
173 | |||
174 | if (!dateTimeListNotInCache.empty()) { |
|
174 | if (!dateTimeListNotInCache.empty()) { | |
175 | // Ask the provider for each data on the dateTimeListNotInCache |
|
175 | // Ask the provider for each data on the dateTimeListNotInCache | |
176 |
auto |
|
176 | auto identifier = impl->m_VariableToIdentifier.at(variable); | |
177 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( |
|
177 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( | |
178 |
|
|
178 | identifier, std::move(dateTimeListNotInCache)); | |
179 | } |
|
179 | } | |
180 | else { |
|
180 | else { | |
181 | emit variable->updated(); |
|
181 | emit variable->updated(); |
@@ -78,7 +78,7 public: | |||||
78 | SqpApplication::SqpApplication(int &argc, char **argv) |
|
78 | SqpApplication::SqpApplication(int &argc, char **argv) | |
79 | : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()} |
|
79 | : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()} | |
80 | { |
|
80 | { | |
81 |
qC |
|
81 | qCDebug(LOG_SqpApplication()) << tr("SqpApplication construction") << QThread::currentThread(); | |
82 |
|
82 | |||
83 | connect(&impl->m_DataSourceControllerThread, &QThread::started, |
|
83 | connect(&impl->m_DataSourceControllerThread, &QThread::started, | |
84 | impl->m_DataSourceController.get(), &DataSourceController::initialize); |
|
84 | impl->m_DataSourceController.get(), &DataSourceController::initialize); |
General Comments 0
You need to be logged in to leave comments.
Login now