##// END OF EJS Templates
@@ -0,0 +1,30
1 #ifndef SCIQLOP_NETWORKCONTROLLER_H
2 #define SCIQLOP_NETWORKCONTROLLER_H
3
4 #include <QLoggingCategory>
5 #include <QObject>
6
7 #include <Common/spimpl.h>
8
9 Q_DECLARE_LOGGING_CATEGORY(LOG_NetworkController)
10
11 /**
12 * @brief The NetworkController class aims to handle all network connection of SciQlop.
13 */
14 class NetworkController : public QObject {
15 Q_OBJECT
16 public:
17 explicit NetworkController(QObject *parent = 0);
18
19
20 void initialize();
21 void finalize();
22
23 private:
24 void waitForFinish();
25
26 class NetworkControllerPrivate;
27 spimpl::unique_impl_ptr<NetworkControllerPrivate> impl;
28 };
29
30 #endif // SCIQLOP_NETWORKCONTROLLER_H
@@ -0,0 +1,33
1 #include "Network/NetworkController.h"
2
3 #include <QMutex>
4 #include <QThread>
5
6 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
7
8 struct NetworkController::NetworkControllerPrivate {
9 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
10 QMutex m_WorkingMutex;
11 };
12
13 NetworkController::NetworkController(QObject *parent)
14 : QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
15 {
16 }
17
18 void NetworkController::initialize()
19 {
20 qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread();
21 impl->m_WorkingMutex.lock();
22 qCDebug(LOG_NetworkController()) << tr("NetworkController init END");
23 }
24
25 void NetworkController::finalize()
26 {
27 impl->m_WorkingMutex.unlock();
28 }
29
30 void NetworkController::waitForFinish()
31 {
32 QMutexLocker locker{&impl->m_WorkingMutex};
33 }
@@ -1,42 +1,38
1 1 #ifndef SCIQLOP_IDATAPROVIDER_H
2 2 #define SCIQLOP_IDATAPROVIDER_H
3 3
4 4 #include <memory>
5 5
6 6 #include <QObject>
7 7
8 8 #include <Common/MetaTypes.h>
9 9
10 10 #include <Data/SqpDateTime.h>
11 11
12 12 class DataProviderParameters;
13 13 class IDataSeries;
14 14
15 15 /**
16 16 * @brief The IDataProvider interface aims to declare a data provider.
17 17 *
18 18 * A data provider is an entity that generates data and returns it according to various parameters
19 19 * (time interval, product to retrieve the data, etc.)
20 20 *
21 21 * @sa IDataSeries
22 22 */
23 23 class IDataProvider : public QObject {
24 24
25 25 Q_OBJECT
26 26 public:
27 27 virtual ~IDataProvider() noexcept = default;
28 28
29 virtual std::shared_ptr<IDataSeries>
30 retrieveData(const DataProviderParameters &parameters) const = 0;
31
32
33 29 virtual void requestDataLoading(const QVector<SqpDateTime> &dateTimeList) = 0;
34 30
35 31 signals:
36 32 void dataProvided(std::shared_ptr<IDataSeries> dateSerie, const SqpDateTime &dateTime);
37 33 };
38 34
39 35 // Required for using shared_ptr in signals/slots
40 36 SCIQLOP_REGISTER_META_TYPE(IDATAPROVIDER_PTR_REGISTRY, std::shared_ptr<IDataProvider>)
41 37
42 38 #endif // SCIQLOP_IDATAPROVIDER_H
@@ -1,211 +1,197
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 <QtCore/QItemSelectionModel>
15 15
16 16 #include <unordered_map>
17 17
18 18 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19 19
20 namespace {
21
22 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
23 /// will be deleted when the timerange is recovered from SciQlop
24 std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
25 const SqpDateTime &dateTime) noexcept
26 {
27 auto parameters = DataProviderParameters{dateTime};
28
29 return provider.retrieveData(parameters);
30 }
31
32 } // namespace
33
34 20 struct VariableController::VariableControllerPrivate {
35 21 explicit VariableControllerPrivate(VariableController *parent)
36 22 : m_WorkingMutex{},
37 23 m_VariableModel{new VariableModel{parent}},
38 24 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
39 25 m_VariableCacheController{std::make_unique<VariableCacheController>()}
40 26 {
41 27 }
42 28
43 29 QMutex m_WorkingMutex;
44 30 /// Variable model. The VariableController has the ownership
45 31 VariableModel *m_VariableModel;
46 32 QItemSelectionModel *m_VariableSelectionModel;
47 33
48 34
49 35 TimeController *m_TimeController{nullptr};
50 36 std::unique_ptr<VariableCacheController> m_VariableCacheController;
51 37
52 38 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
53 39 m_VariableToProviderMap;
54 40 };
55 41
56 42 VariableController::VariableController(QObject *parent)
57 43 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
58 44 {
59 45 qCDebug(LOG_VariableController()) << tr("VariableController construction")
60 46 << QThread::currentThread();
61 47 }
62 48
63 49 VariableController::~VariableController()
64 50 {
65 51 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
66 52 << QThread::currentThread();
67 53 this->waitForFinish();
68 54 }
69 55
70 56 VariableModel *VariableController::variableModel() noexcept
71 57 {
72 58 return impl->m_VariableModel;
73 59 }
74 60
75 61 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
76 62 {
77 63 return impl->m_VariableSelectionModel;
78 64 }
79 65
80 66 void VariableController::setTimeController(TimeController *timeController) noexcept
81 67 {
82 68 impl->m_TimeController = timeController;
83 69 }
84 70
85 71 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
86 72 {
87 73 if (!variable) {
88 74 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
89 75 return;
90 76 }
91 77
92 78 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
93 79 // make some treatments before the deletion
94 80 emit variableAboutToBeDeleted(variable);
95 81
96 82 // Deletes provider
97 83 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
98 84 qCDebug(LOG_VariableController())
99 85 << tr("Number of providers deleted for variable %1: %2")
100 86 .arg(variable->name(), QString::number(nbProvidersDeleted));
101 87
102 88 // Clears cache
103 89 impl->m_VariableCacheController->clear(variable);
104 90
105 91 // Deletes from model
106 92 impl->m_VariableModel->deleteVariable(variable);
107 93 }
108 94
109 95 void VariableController::deleteVariables(
110 96 const QVector<std::shared_ptr<Variable> > &variables) noexcept
111 97 {
112 98 for (auto variable : qAsConst(variables)) {
113 99 deleteVariable(variable);
114 100 }
115 101 }
116 102
117 103 void VariableController::createVariable(const QString &name,
118 104 std::shared_ptr<IDataProvider> provider) noexcept
119 105 {
120 106
121 107 if (!impl->m_TimeController) {
122 108 qCCritical(LOG_VariableController())
123 109 << tr("Impossible to create variable: The time controller is null");
124 110 return;
125 111 }
126 112
127 113
128 114 /// @todo : for the moment :
129 115 /// - the provider is only used to retrieve data from the variable for its initialization, but
130 116 /// it will be retained later
131 117 /// - default data are generated for the variable, without taking into account the timerange set
132 118 /// in sciqlop
133 119 auto dateTime = impl->m_TimeController->dateTime();
134 120 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) {
135 121
136 122 // store the provider
137 123 impl->m_VariableToProviderMap[newVariable] = provider;
138 124
139 125 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
140 126 auto dataSeriesAcquired, auto dateTimeToPutInCache)
141 127 {
142 128 if (auto variable = varW.lock()) {
143 129 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
144 130 variable->setDataSeries(dataSeriesAcquired);
145 131 }
146 132 };
147 133
148 134 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
149 135 this->onRequestDataLoading(newVariable, dateTime);
150 136 }
151 137 }
152 138
153 139 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
154 140 {
155 141 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
156 142 << QThread::currentThread()->objectName();
157 143 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
158 144
159 145 for (const auto &selectedRow : qAsConst(selectedRows)) {
160 146 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
161 147 selectedVariable->setDateTime(dateTime);
162 148 this->onRequestDataLoading(selectedVariable, dateTime);
163 149 }
164 150 }
165 151 }
166 152
167 153
168 154 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
169 155 const SqpDateTime &dateTime)
170 156 {
171 157 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
172 158 << QThread::currentThread()->objectName();
173 159 // we want to load data of the variable for the dateTime.
174 160 // First we check if the cache contains some of them.
175 161 // For the other, we ask the provider to give them.
176 162 if (variable) {
177 163
178 164 auto dateTimeListNotInCache
179 165 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
180 166
181 167 if (!dateTimeListNotInCache.empty()) {
182 168 // Ask the provider for each data on the dateTimeListNotInCache
183 169 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
184 170 std::move(dateTimeListNotInCache));
185 171 }
186 172 else {
187 173 emit variable->updated();
188 174 }
189 175 }
190 176 else {
191 177 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
192 178 }
193 179 }
194 180
195 181
196 182 void VariableController::initialize()
197 183 {
198 184 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
199 185 impl->m_WorkingMutex.lock();
200 186 qCDebug(LOG_VariableController()) << tr("VariableController init END");
201 187 }
202 188
203 189 void VariableController::finalize()
204 190 {
205 191 impl->m_WorkingMutex.unlock();
206 192 }
207 193
208 194 void VariableController::waitForFinish()
209 195 {
210 196 QMutexLocker locker{&impl->m_WorkingMutex};
211 197 }
@@ -1,50 +1,52
1 1 #ifndef SCIQLOP_SQPAPPLICATION_H
2 2 #define SCIQLOP_SQPAPPLICATION_H
3 3
4 4 #include "SqpApplication.h"
5 5
6 6 #include <QApplication>
7 7 #include <QLoggingCategory>
8 8
9 9 #include <Common/spimpl.h>
10 10
11 11 Q_DECLARE_LOGGING_CATEGORY(LOG_SqpApplication)
12 12
13 13 #if defined(sqpApp)
14 14 #undef sqpApp
15 15 #endif
16 16 #define sqpApp (static_cast<SqpApplication *>(QCoreApplication::instance()))
17 17
18 18 class DataSourceController;
19 class NetworkController;
19 20 class TimeController;
20 21 class VariableController;
21 22 class VisualizationController;
22 23
23 24 /**
24 25 * @brief The SqpApplication class aims to make the link between SciQlop
25 26 * and its plugins. This is the intermediate class that SciQlop has to use
26 27 * in the way to connect a data source. Please first use load method to initialize
27 28 * a plugin specified by its metadata name (JSON plugin source) then others specifics
28 29 * method will be able to access it.
29 30 * You can load a data source driver plugin then create a data source.
30 31 */
31 32
32 33 class SqpApplication : public QApplication {
33 34 Q_OBJECT
34 35 public:
35 36 explicit SqpApplication(int &argc, char **argv);
36 37 virtual ~SqpApplication();
37 38 void initialize();
38 39
39 40 /// Accessors for the differents sciqlop controllers
40 41 DataSourceController &dataSourceController() noexcept;
42 NetworkController &networkController() noexcept;
41 43 TimeController &timeController() noexcept;
42 44 VariableController &variableController() noexcept;
43 45 VisualizationController &visualizationController() noexcept;
44 46
45 47 private:
46 48 class SqpApplicationPrivate;
47 49 spimpl::unique_impl_ptr<SqpApplicationPrivate> impl;
48 50 };
49 51
50 52 #endif // SCIQLOP_SQPAPPLICATION_H
@@ -1,124 +1,140
1 1 #include "SqpApplication.h"
2 2
3 3 #include <Data/IDataProvider.h>
4 4 #include <DataSource/DataSourceController.h>
5 #include <Network/NetworkController.h>
5 6 #include <QThread>
6 7 #include <Time/TimeController.h>
7 8 #include <Variable/Variable.h>
8 9 #include <Variable/VariableController.h>
9 10 #include <Visualization/VisualizationController.h>
10 11
11 12 Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication")
12 13
13 14 class SqpApplication::SqpApplicationPrivate {
14 15 public:
15 16 SqpApplicationPrivate()
16 17 : m_DataSourceController{std::make_unique<DataSourceController>()},
18 m_NetworkController{std::make_unique<NetworkController>()},
17 19 m_TimeController{std::make_unique<TimeController>()},
18 20 m_VariableController{std::make_unique<VariableController>()},
19 21 m_VisualizationController{std::make_unique<VisualizationController>()}
20 22 {
21 QThread::currentThread()->setObjectName("MainThread");
22 m_DataSourceController->moveToThread(&m_DataSourceControllerThread);
23 m_DataSourceControllerThread.setObjectName("DataSourceControllerThread");
24 m_VariableController->moveToThread(&m_VariableControllerThread);
25 m_VariableControllerThread.setObjectName("VariableControllerThread");
26 m_VisualizationController->moveToThread(&m_VisualizationControllerThread);
27 m_VisualizationControllerThread.setObjectName("VisualizationControllerThread");
28
29 23 // /////////////////////////////// //
30 24 // Connections between controllers //
31 25 // /////////////////////////////// //
32 26
33 27 // VariableController <-> DataSourceController
34 28 connect(m_DataSourceController.get(),
35 29 SIGNAL(variableCreationRequested(const QString &, std::shared_ptr<IDataProvider>)),
36 30 m_VariableController.get(),
37 31 SLOT(createVariable(const QString &, std::shared_ptr<IDataProvider>)));
38 32
39 33 // VariableController <-> VisualizationController
40 34 connect(m_VariableController.get(),
41 35 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)),
42 36 m_VisualizationController.get(),
43 37 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)), Qt::DirectConnection);
44 38
45 39
40 m_DataSourceController->moveToThread(&m_DataSourceControllerThread);
41 m_NetworkController->moveToThread(&m_NetworkControllerThread);
42 m_VariableController->moveToThread(&m_VariableControllerThread);
43 m_VisualizationController->moveToThread(&m_VisualizationControllerThread);
44
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 m_NetworkControllerThread.quit();
57 m_NetworkControllerThread.wait();
58
56 59 m_VariableControllerThread.quit();
57 60 m_VariableControllerThread.wait();
58 61
59 62 m_VisualizationControllerThread.quit();
60 63 m_VisualizationControllerThread.wait();
61 64 }
62 65
63 66 std::unique_ptr<DataSourceController> m_DataSourceController;
64 67 std::unique_ptr<VariableController> m_VariableController;
65 68 std::unique_ptr<TimeController> m_TimeController;
69 std::unique_ptr<NetworkController> m_NetworkController;
66 70 std::unique_ptr<VisualizationController> m_VisualizationController;
67 71 QThread m_DataSourceControllerThread;
72 QThread m_NetworkControllerThread;
68 73 QThread m_VariableControllerThread;
69 74 QThread m_VisualizationControllerThread;
70 75 };
71 76
72 77
73 78 SqpApplication::SqpApplication(int &argc, char **argv)
74 79 : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()}
75 80 {
76 81 qCInfo(LOG_SqpApplication()) << tr("SqpApplication construction");
77 82
78 83 connect(&impl->m_DataSourceControllerThread, &QThread::started,
79 84 impl->m_DataSourceController.get(), &DataSourceController::initialize);
80 85 connect(&impl->m_DataSourceControllerThread, &QThread::finished,
81 86 impl->m_DataSourceController.get(), &DataSourceController::finalize);
82 87
88 connect(&impl->m_NetworkControllerThread, &QThread::started, impl->m_NetworkController.get(),
89 &NetworkController::initialize);
90 connect(&impl->m_NetworkControllerThread, &QThread::finished, impl->m_NetworkController.get(),
91 &NetworkController::finalize);
92
83 93 connect(&impl->m_VariableControllerThread, &QThread::started, impl->m_VariableController.get(),
84 94 &VariableController::initialize);
85 95 connect(&impl->m_VariableControllerThread, &QThread::finished, impl->m_VariableController.get(),
86 96 &VariableController::finalize);
87 97
88 98 connect(&impl->m_VisualizationControllerThread, &QThread::started,
89 99 impl->m_VisualizationController.get(), &VisualizationController::initialize);
90 100 connect(&impl->m_VisualizationControllerThread, &QThread::finished,
91 101 impl->m_VisualizationController.get(), &VisualizationController::finalize);
92 102
93 103 impl->m_DataSourceControllerThread.start();
104 impl->m_NetworkControllerThread.start();
94 105 impl->m_VariableControllerThread.start();
95 106 impl->m_VisualizationControllerThread.start();
96 107 }
97 108
98 109 SqpApplication::~SqpApplication()
99 110 {
100 111 }
101 112
102 113 void SqpApplication::initialize()
103 114 {
104 115 }
105 116
106 117 DataSourceController &SqpApplication::dataSourceController() noexcept
107 118 {
108 119 return *impl->m_DataSourceController;
109 120 }
110 121
122 NetworkController &SqpApplication::networkController() noexcept
123 {
124 return *impl->m_NetworkController;
125 }
126
111 127 TimeController &SqpApplication::timeController() noexcept
112 128 {
113 129 return *impl->m_TimeController;
114 130 }
115 131
116 132 VariableController &SqpApplication::variableController() noexcept
117 133 {
118 134 return *impl->m_VariableController;
119 135 }
120 136
121 137 VisualizationController &SqpApplication::visualizationController() noexcept
122 138 {
123 139 return *impl->m_VisualizationController;
124 140 }
@@ -1,28 +1,26
1 1 #ifndef SCIQLOP_COSINUSPROVIDER_H
2 2 #define SCIQLOP_COSINUSPROVIDER_H
3 3
4 4 #include "MockPluginGlobal.h"
5 5
6 6 #include <Data/IDataProvider.h>
7 7
8 8 #include <QLoggingCategory>
9 9
10 10 Q_DECLARE_LOGGING_CATEGORY(LOG_CosinusProvider)
11 11
12 12 /**
13 13 * @brief The CosinusProvider class is an example of how a data provider can generate data
14 14 */
15 15 class SCIQLOP_MOCKPLUGIN_EXPORT CosinusProvider : public IDataProvider {
16 16 public:
17 /// @sa IDataProvider::retrieveData()
18 std::shared_ptr<IDataSeries>
19 retrieveData(const DataProviderParameters &parameters) const override;
20
21 17 void requestDataLoading(const QVector<SqpDateTime> &dateTimeList) override;
22 18
23 19
24 20 private:
21 /// @sa IDataProvider::retrieveData()
22 std::shared_ptr<IDataSeries> retrieveData(const DataProviderParameters &parameters) const;
25 23 std::shared_ptr<IDataSeries> retrieveDataSeries(const SqpDateTime &dateTime);
26 24 };
27 25
28 26 #endif // SCIQLOP_COSINUSPROVIDER_H
General Comments 0
You need to be logged in to leave comments. Login now