Debian Codegraph @

src/publishsubscribe/qvaluespacepublisher.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include <qvaluespacepublisher.h>
41
42 #include "qvaluespace_p.h"
43 #include "qvaluespacemanager_p.h"
44
45 #include <QtCore/qmetaobject.h>
46
47 QT_BEGIN_NAMESPACE
48
49 /*!
50 \class QValueSpacePublisher
51 \brief The QValueSpacePublisher class allows applications to publish values in the Value Space.
52 \inmodule QtPublishSubscribe
53 \ingroup publishsubscribe
54
55 When multiple Value Space layers are available QValueSpacePublisher only publishes values to
56 the layer with the highest priority. The layers that QValueSpacePublisher can access can be
57 limited by specifying either a \l {QValueSpace::LayerOption}{filter} or a QUuid at construction
58 time.
59
60 The lifetime of values published in the Value Space is specified by the particular Value Space
61 layer that the value is published in. For details on the different behaviors see
62 QValueSpace::LayerOption.
63
64 Once a value is published all applications in the system will have read access to it via the
65 QValueSpaceSubscriber class.
66
67 Although, logically, the Value Space is a simple collection of
68 hierarchical paths, these paths can conceptually be visualized as a set of
69 objects with attributes. For example, rather than viewing the following list
70 as 12 distinct Value Space paths:
71
72 \code
73 /Device/Network/Interfaces/eth0/Name
74 /Device/Network/Interfaces/eth0/Type
75 /Device/Network/Interfaces/eth0/Status
76 /Device/Network/Interfaces/eth0/BytesSent
77 /Device/Network/Interfaces/eth0/BytesReceived
78 /Device/Network/Interfaces/eth0/Time
79 /Device/Network/Interfaces/ppp0/Name
80 /Device/Network/Interfaces/ppp0/Type
81 /Device/Network/Interfaces/ppp0/Status
82 /Device/Network/Interfaces/ppp0/BytesSent
83 /Device/Network/Interfaces/ppp0/BytesReceived
84 /Device/Network/Interfaces/ppp0/Time
85 \endcode
86
87 it can be thought of as describing two Value Space objects,
88 \c { { /Device/Network/Interfaces/eth0, /Device/Network/Interfaces/ppp0 } },
89 each with the six attributes \c { {Name, Type, Status, BytesSent,
90 BytesReceived, Time} }. The QValueSpacePublisher class encapsulates this
91 abstraction.
92
93 For performance reasons the setting of and removing of attributes is buffered
94 internally by the QValueSpacePublisher and applied as a batch sometime later.
95 Normally this occurs the next time the application enters the Qt event loop,
96 but this behavior should not be relied upon. If an application must
97 synchronize with others, the QValueSpacePublisher::sync() and QValueSpacePublisher::syncAll()
98 functions can be used to force the application of changes. This call is generally unnecessary,
99 and should be used sparingly to prevent unnecessary load on the system.
100
101 \sa QValueSpaceSubscriber
102 */
103
104 /*!
105 \fn void QValueSpacePublisher::interestChanged(const QString &attribute, bool interested)
106
107 Signal that is emitted when interest in \a attribute changes. If \a interested is true at
108 least on QValueSpaceSubscriber is interested in the value of \a attribute.
109 */
110
111 class QValueSpacePublisherPrivate
112 {
113 public:
114 QValueSpacePublisherPrivate(const QString &path, QValueSpace::LayerOptions filter = QValueSpace::UnspecifiedLayer);
115 QValueSpacePublisherPrivate(const QString &path, const QUuid &uuid);
116
117 QString path;
118 QAbstractValueSpaceLayer *layer;
119 QAbstractValueSpaceLayer::Handle handle;
120
121 bool hasSet;
122 bool hasWatch;
123 };
124
125 QValueSpacePublisherPrivate::QValueSpacePublisherPrivate(const QString &_path, QValueSpace::LayerOptions filter)
126 : layer(0)
127 , handle(QAbstractValueSpaceLayer::InvalidHandle)
128 , hasSet(false)
129 , hasWatch(false)
130 {
131 path = qCanonicalPath(_path);
132
133 // check filter for mutually exclusive options
134 if ((filter & QValueSpace::PermanentLayer && filter & QValueSpace::TransientLayer)
135 || (filter & QValueSpace::WritableLayer && filter & QValueSpace::ReadOnlyLayer)) {
136 return;
137 }
138
139 QList<QAbstractValueSpaceLayer *> layers = QValueSpaceManager::instance()->getLayers();
140
141 for (int ii = 0; ii < layers.count(); ++ii) {
142 if ((layers.at(ii)->layerOptions() & filter) == filter) {
143 QAbstractValueSpaceLayer::Handle h =
144 layers.at(ii)->item(QAbstractValueSpaceLayer::InvalidHandle, path);
145
146 if (h != QAbstractValueSpaceLayer::InvalidHandle) {
147 layer = layers.at(ii);
148 handle = h;
149 break;
150 }
151 }
152 }
153 }
154
155 QValueSpacePublisherPrivate::QValueSpacePublisherPrivate(const QString &_path, const QUuid &uuid)
156 : layer(0)
157 , handle(QAbstractValueSpaceLayer::InvalidHandle)
158 , hasSet(false)
159 , hasWatch(false)
160 {
161 path = qCanonicalPath(_path);
162
163 QList<QAbstractValueSpaceLayer *> layers = QValueSpaceManager::instance()->getLayers();
164
165 for (int ii = 0; ii < layers.count(); ++ii) {
166 if (layers.at(ii)->id() == uuid) {
167 layer = layers.at(ii);
168 handle = layer->item(QAbstractValueSpaceLayer::InvalidHandle, path);
169 break;
170 }
171 }
172 }
173
174 /*!
175 Constructs a QValueSpacePublisher with the specified \a parent that publishes values under
176 \a path.
177 */
178 QValueSpacePublisher::QValueSpacePublisher(const QString &path, QObject *parent)
179 : QObject(parent)
180 , d_ptr(new QValueSpacePublisherPrivate(path))
181 {
182 }
183
184 /*!
185 Constructs a QValueSpacePublisher with the specified \a parent that publishes values under
186 \a path. The \a filter parameter is used to limit which layer this QValueSpacePublisher will
187 access.
188
189 The constructed Value Space publisher will access the layer with the highest priority that matches
190 \a filter and for which \a path is a valid path.
191
192 If no suitable layer is found, the constructed QValueSpacePublisher will be unconnected.
193
194 \sa isConnected()
195 */
196 QValueSpacePublisher::QValueSpacePublisher(QValueSpace::LayerOptions filter, const QString &path, QObject *parent)
197 : QObject(parent)
198 , d_ptr(new QValueSpacePublisherPrivate(path, filter))
199 {
200 }
201
202 /*!
203 Constructs a QValueSpacePublisher with the specified \a parent that publishes values under
204 \a path. Only the layer identified by \a uuid will be accessed by this publisher.
205
206 Use of this constructor is not platform agnostic. If possible use one of the constructors that
207 take a QValueSpace::LayerOptions parameter instead.
208
209 If a layer with a matching \a uuid is not found, the constructed QValueSpacePublisher will be
210 unconnected.
211
212 \sa isConnected()
213 */
214 QValueSpacePublisher::QValueSpacePublisher(const QUuid &uuid, const QString &path, QObject *parent)
215 : QObject(parent)
216 , d_ptr(new QValueSpacePublisherPrivate(path, uuid))
217 {
218 }
219
220 /*!
221 Destroys the QValueSpacePublisher. This will remove all values published by this publisher in
222 \l {QValueSpace::TransientLayer}{non-permanent} layers.
223 */
224 QValueSpacePublisher::~QValueSpacePublisher()
225 {
226 if (!isConnected())
227 return;
228
229 if (d_ptr->hasSet && !(d_ptr->layer->layerOptions() & QValueSpace::PermanentLayer))
230 d_ptr->layer->removeSubTree(this, d_ptr->handle);
231
232 if (d_ptr->hasWatch)
233 d_ptr->layer->removeWatches(this, d_ptr->handle);
234
235 delete d_ptr;
236 }
237
238 /*!
239 Returns the path that this QValueSpacePublisher refers to.
240 */
241 QString QValueSpacePublisher::path() const
242 {
243 return d_ptr->path;
244 }
245
246 /*!
247 Returns true if this QValueSpacePublisher is connected to an available layer; otherwise returns
248 false.
249 */
250 bool QValueSpacePublisher::isConnected() const
251 {
252 return (d_ptr->layer && d_ptr->handle != QAbstractValueSpaceLayer::InvalidHandle);
253 }
254
255 /*!
256 Forcibly sync all Value Space publisher using the same layer as this publisher.
257
258 For performance reasons attribute changes are batched internally by QValueSpacePublisher
259 instances. In cases where the visibility of changes must be synchronized with other processes,
260 calling this function will flush these batches. By the time this function returns, all other
261 processes in the system will be able to see the attribute changes.
262
263 Generally, calling this function is unnecessary.
264 */
265 void QValueSpacePublisher::sync()
266 {
267 if (!isConnected()) {
268 qWarning("sync called on unconnected QValueSpacePublisher.");
269 return;
270 }
271
272 d_ptr->layer->sync();
273 }
274
275 /*!
276 Sets the value \a name on the publisher to \a data. If name is empty, this call will set the
277 value of this publisher's path.
278
279 For example:
280
281 \code
282 QValueSpacePublisher publisher("/Device");
283 publisher.setValue("State", "Starting");
284 publisher.sync();
285
286 // QValueSpaceSubscriber("/Device/State").value() == QVariant("Starting")
287 \endcode
288 */
289 void QValueSpacePublisher::setValue(const QString &name, const QVariant &data)
290 {
291 if (!isConnected()) {
292 qWarning("setValue called on unconnected QValueSpacePublisher.");
293 return;
294 }
295
296 d_ptr->hasSet = true;
297 d_ptr->layer->setValue(this, d_ptr->handle, qCanonicalPath(name), data);
298 }
299
300 /*!
301 Removes the value \a name and all sub-attributes from the system.
302
303 For example:
304 \code
305 QValueSpacePublisher publisher("/Device");
306 publisher.setValue("State", "Starting");
307 publisher.setValue("State/Memory", "1000");
308 publisher.sync();
309 // QValueSpaceSubscriber("/Device/State").value() == QVariant("Starting")
310 // QValueSpaceSubscriber("/Device/State/Memory").value() == QVariant("1000")
311
312 publisher.resetValue("State");
313 publisher.sync();
314 // QValueSpaceSubscriber("/Device/State").value() == QVariant();
315 // QValueSpaceSubscriber("/Device/State/Memory").value() == QVariant();
316 \endcode
317 */
318 void QValueSpacePublisher::resetValue(const QString &name)
319 {
320 if (!isConnected()) {
321 qWarning("resetValue called on unconnected QValueSpacePublisher.");
322 return;
323 }
324
325 d_ptr->layer->removeValue(this, d_ptr->handle, qCanonicalPath(name));
326 }
327
328 /*!
329 \reimp
330 */
331 void QValueSpacePublisher::connectNotify(const QMetaMethod &signal)
332 {
333 static const QMetaMethod interestChangedSignal = QMetaMethod::fromSignal(&QValueSpacePublisher::interestChanged);
334 if (!d_ptr->hasWatch && isConnected() && signal == interestChangedSignal) {
335 d_ptr->layer->addWatch(this, d_ptr->handle);
336 d_ptr->hasWatch = true;
337 }
338 }
339
340 QT_END_NAMESPACE