1/****************************************************************************
2**
3** Copyright (C) 2017 Ford Motor Company
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtRemoteObjects 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 "qremoteobjectpendingcall.h"
41#include "qremoteobjectpendingcall_p.h"
42
43#include "qremoteobjectreplica_p.h"
44
45#include <QtCore/qcoreapplication.h>
46
47#include <private/qobject_p.h>
48
49QT_BEGIN_NAMESPACE
50
51QRemoteObjectPendingCallData::QRemoteObjectPendingCallData(int serialId, QRemoteObjectReplicaImplementation *replica)
52 : replica(replica)
53 , serialId(serialId)
54 , error(QRemoteObjectPendingCall::InvalidMessage)
55 , watcherHelper(nullptr)
56{
57}
58
59QRemoteObjectPendingCallData::~QRemoteObjectPendingCallData()
60{
61}
62
63void QRemoteObjectPendingCallWatcherHelper::add(QRemoteObjectPendingCallWatcher *watcher)
64{
65 connect(sender: this, signal: &QRemoteObjectPendingCallWatcherHelper::finished, context: watcher, slot: [watcher]() {
66 emit watcher->finished(self: watcher);
67 }, type: Qt::QueuedConnection);
68}
69
70void QRemoteObjectPendingCallWatcherHelper::emitSignals()
71{
72 emit finished();
73}
74
75/*!
76 \class QRemoteObjectPendingCall
77 \inmodule QtRemoteObjects
78 \brief Encapsulates the result of an asynchronous method call.
79*/
80
81QRemoteObjectPendingCall::QRemoteObjectPendingCall()
82 : d(new QRemoteObjectPendingCallData)
83{
84}
85
86QRemoteObjectPendingCall::~QRemoteObjectPendingCall()
87{
88}
89
90QRemoteObjectPendingCall::QRemoteObjectPendingCall(const QRemoteObjectPendingCall& other)
91 : d(other.d)
92{
93}
94
95QRemoteObjectPendingCall::QRemoteObjectPendingCall(QRemoteObjectPendingCallData *dd)
96 : d(dd)
97{
98}
99
100QRemoteObjectPendingCall &QRemoteObjectPendingCall::operator=(const QRemoteObjectPendingCall &other)
101{
102 d = other.d;
103 return *this;
104}
105
106/*!
107 Returns the return value of the remote call.
108
109 returnValue will only be valid when the remote call has finished and there
110 are no \l {error}s.
111*/
112QVariant QRemoteObjectPendingCall::returnValue() const
113{
114 if (!d)
115 return QVariant();
116
117 QMutexLocker locker(&d->mutex);
118 return d->returnValue;
119}
120
121/*!
122 \enum QRemoteObjectPendingCall::Error
123
124 This enum type specifies the possible error values for a remote call:
125
126 \value NoError
127 No error occurred.
128 \value InvalidMessage
129 The default error state prior to the remote call finishing.
130*/
131
132/*!
133 Returns the error, if any, from the remote call.
134*/
135QRemoteObjectPendingCall::Error QRemoteObjectPendingCall::error() const
136{
137 if (!d)
138 return QRemoteObjectPendingCall::InvalidMessage;
139
140 QMutexLocker locker(&d->mutex);
141 return d->error;
142}
143
144/*!
145 Returns true if the remote call has finished, false otherwise.
146
147 A finished call will include a returnValue or \l error.
148*/
149bool QRemoteObjectPendingCall::isFinished() const
150{
151 if (!d)
152 return true; // considered finished
153
154 QMutexLocker locker(&d->mutex);
155 return d->error != InvalidMessage;
156}
157
158/*!
159 Blocks for up to \a timeout milliseconds, until the remote call has finished.
160
161 Returns \c true on success, \c false otherwise.
162*/
163bool QRemoteObjectPendingCall::waitForFinished(int timeout)
164{
165 if (!d)
166 return false;
167
168 if (d->error != QRemoteObjectPendingCall::InvalidMessage)
169 return true; // already finished
170
171 QMutexLocker locker(&d->mutex);
172 if (!d->replica)
173 return false;
174
175 return d->replica->waitForFinished(*this, timeout);
176}
177
178QRemoteObjectPendingCall QRemoteObjectPendingCall::fromCompletedCall(const QVariant &returnValue)
179{
180 QRemoteObjectPendingCallData *data = new QRemoteObjectPendingCallData;
181 data->returnValue = returnValue;
182 data->error = NoError;
183 return QRemoteObjectPendingCall(data);
184}
185
186class QRemoteObjectPendingCallWatcherPrivate: public QObjectPrivate
187{
188public:
189 Q_DECLARE_PUBLIC(QRemoteObjectPendingCallWatcher)
190};
191
192/*!
193 \class QRemoteObjectPendingCallWatcher
194 \inmodule QtRemoteObjects
195 \brief Provides a QObject-based API for watching a QRemoteObjectPendingCall.
196
197 QRemoteObjectPendingCallWatcher provides a signal indicating when a QRemoteObjectPendingCall
198 has finished, allowing for convenient, non-blocking handling of the call.
199*/
200
201QRemoteObjectPendingCallWatcher::QRemoteObjectPendingCallWatcher(const QRemoteObjectPendingCall &call, QObject *parent)
202 : QObject(*new QRemoteObjectPendingCallWatcherPrivate, parent)
203 , QRemoteObjectPendingCall(call)
204{
205 if (d) {
206 QMutexLocker locker(&d->mutex);
207 if (!d->watcherHelper) {
208 d->watcherHelper.reset(other: new QRemoteObjectPendingCallWatcherHelper);
209 if (d->error != QRemoteObjectPendingCall::InvalidMessage) {
210 // cause a signal emission anyways
211 QMetaObject::invokeMethod(obj: d->watcherHelper.data(), member: "finished", type: Qt::QueuedConnection);
212 }
213 }
214 d->watcherHelper->add(watcher: this);
215 }
216}
217
218QRemoteObjectPendingCallWatcher::~QRemoteObjectPendingCallWatcher()
219{
220}
221
222/*!
223 Returns true if the remote call has finished, false otherwise.
224
225 A finished call will include a returnValue or error.
226*/
227bool QRemoteObjectPendingCallWatcher::isFinished() const
228{
229 if (!d)
230 return true; // considered finished
231
232 QMutexLocker locker(&d->mutex);
233 return d->error != QRemoteObjectPendingCall::InvalidMessage;
234}
235
236/*!
237 Blocks until the remote call has finished.
238*/
239void QRemoteObjectPendingCallWatcher::waitForFinished()
240{
241 if (d) {
242 QRemoteObjectPendingCall::waitForFinished();
243
244 // our signals were queued, so deliver them
245 QCoreApplication::sendPostedEvents(receiver: d->watcherHelper.data(), event_type: QEvent::MetaCall);
246 QCoreApplication::sendPostedEvents(receiver: this, event_type: QEvent::MetaCall);
247 }
248}
249
250/*!
251 \fn QRemoteObjectPendingCallWatcher::finished(QRemoteObjectPendingCallWatcher *self)
252
253 This signal is emitted when the remote call has finished. \a self is the pointer to
254 the watcher object that emitted the signal. A finished call will include a
255 returnValue or error.
256*/
257
258/*!
259 \class QRemoteObjectPendingReply
260 \inmodule QtRemoteObjects
261 \brief A templated version of QRemoteObjectPendingCall.
262*/
263
264/*! \fn template <typename T> T QRemoteObjectPendingReply<T>::returnValue() const
265
266 Returns a strongly typed version of the return value of the remote call.
267*/
268
269QT_END_NAMESPACE
270
271#include "moc_qremoteobjectpendingcall.cpp"
272

source code of qtremoteobjects/src/remoteobjects/qremoteobjectpendingcall.cpp