| 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 | #ifndef QREMOTEOBJECTREPLICA_P_H |
| 41 | #define QREMOTEOBJECTREPLICA_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include "qremoteobjectreplica.h" |
| 55 | |
| 56 | #include "qremoteobjectpendingcall.h" |
| 57 | |
| 58 | #include "qremoteobjectpacket_p.h" |
| 59 | |
| 60 | #include <QtCore/qpointer.h> |
| 61 | #include <QtCore/qvector.h> |
| 62 | #include <QtCore/qdatastream.h> |
| 63 | #include <QtCore/qcompilerdetection.h> |
| 64 | #include <QtCore/qtimer.h> |
| 65 | |
| 66 | QT_BEGIN_NAMESPACE |
| 67 | |
| 68 | class QRemoteObjectReplica; |
| 69 | class QRemoteObjectSource; |
| 70 | class IoDeviceBase; |
| 71 | |
| 72 | class QReplicaImplementationInterface |
| 73 | { |
| 74 | public: |
| 75 | virtual ~QReplicaImplementationInterface() {} |
| 76 | virtual const QVariant getProperty(int i) const = 0; |
| 77 | virtual void setProperties(const QVariantList &) = 0; |
| 78 | virtual void setProperty(int i, const QVariant &) = 0; |
| 79 | virtual bool isInitialized() const = 0; |
| 80 | virtual QRemoteObjectReplica::State state() const = 0; |
| 81 | virtual bool waitForSource(int) = 0; |
| 82 | virtual QRemoteObjectNode *node() const = 0; |
| 83 | |
| 84 | virtual void _q_send(QMetaObject::Call call, int index, const QVariantList &args) = 0; |
| 85 | virtual QRemoteObjectPendingCall _q_sendWithReply(QMetaObject::Call call, int index, const QVariantList &args) = 0; |
| 86 | }; |
| 87 | |
| 88 | class QStubReplicaImplementation final : public QReplicaImplementationInterface |
| 89 | { |
| 90 | public: |
| 91 | explicit QStubReplicaImplementation(); |
| 92 | ~QStubReplicaImplementation() override; |
| 93 | |
| 94 | const QVariant getProperty(int i) const override; |
| 95 | void setProperties(const QVariantList &) override; |
| 96 | void setProperty(int i, const QVariant &) override; |
| 97 | bool isInitialized() const override { return false; } |
| 98 | QRemoteObjectReplica::State state() const override { return QRemoteObjectReplica::State::Uninitialized;} |
| 99 | bool waitForSource(int) override { return false; } |
| 100 | QRemoteObjectNode *node() const override { return nullptr; } |
| 101 | |
| 102 | void _q_send(QMetaObject::Call call, int index, const QVariantList &args) override; |
| 103 | QRemoteObjectPendingCall _q_sendWithReply(QMetaObject::Call call, int index, const QVariantList &args) override; |
| 104 | QVariantList m_propertyStorage; |
| 105 | }; |
| 106 | |
| 107 | class QRemoteObjectReplicaImplementation : public QObject, public QReplicaImplementationInterface |
| 108 | { |
| 109 | public: |
| 110 | explicit QRemoteObjectReplicaImplementation(const QString &name, const QMetaObject *, QRemoteObjectNode *); |
| 111 | ~QRemoteObjectReplicaImplementation() override; |
| 112 | |
| 113 | bool needsDynamicInitialization() const; |
| 114 | |
| 115 | const QVariant getProperty(int i) const override = 0; |
| 116 | void setProperties(const QVariantList &) override = 0; |
| 117 | void setProperty(int i, const QVariant &) override = 0; |
| 118 | virtual bool isShortCircuit() const = 0; |
| 119 | bool isInitialized() const override { return true; } |
| 120 | QRemoteObjectReplica::State state() const override { return QRemoteObjectReplica::State(m_state.loadRelaxed()); } |
| 121 | void setState(QRemoteObjectReplica::State state); |
| 122 | bool waitForSource(int) override { return true; } |
| 123 | virtual bool waitForFinished(const QRemoteObjectPendingCall &, int) { return true; } |
| 124 | virtual void notifyAboutReply(int, const QVariant &) {} |
| 125 | virtual void configurePrivate(QRemoteObjectReplica *); |
| 126 | void emitInitialized(); |
| 127 | void emitNotified(); |
| 128 | QRemoteObjectNode *node() const override { return m_node; } |
| 129 | |
| 130 | void _q_send(QMetaObject::Call call, int index, const QVariantList &args) override = 0; |
| 131 | QRemoteObjectPendingCall _q_sendWithReply(QMetaObject::Call call, int index, const QVariantList &args) override = 0; |
| 132 | |
| 133 | //Dynamic replica functions |
| 134 | virtual void setDynamicMetaObject(const QMetaObject *meta); |
| 135 | virtual void setDynamicProperties(const QVariantList &values); |
| 136 | |
| 137 | QString m_objectName; |
| 138 | const QMetaObject *m_metaObject; |
| 139 | |
| 140 | //Dynamic Replica data |
| 141 | int m_numSignals;//TODO maybe here too |
| 142 | int m_methodOffset; |
| 143 | int m_signalOffset; |
| 144 | int m_propertyOffset; |
| 145 | QRemoteObjectNode *m_node; |
| 146 | QByteArray m_objectSignature; |
| 147 | QAtomicInt m_state; |
| 148 | }; |
| 149 | |
| 150 | class QConnectedReplicaImplementation final : public QRemoteObjectReplicaImplementation |
| 151 | { |
| 152 | public: |
| 153 | explicit QConnectedReplicaImplementation(const QString &name, const QMetaObject *, QRemoteObjectNode *); |
| 154 | ~QConnectedReplicaImplementation() override; |
| 155 | const QVariant getProperty(int i) const override; |
| 156 | void setProperties(const QVariantList &) override; |
| 157 | void setProperty(int i, const QVariant &) override; |
| 158 | bool isShortCircuit() const final { return false; } |
| 159 | bool isInitialized() const override; |
| 160 | bool waitForSource(int timeout) override; |
| 161 | QVector<int> childIndices() const; |
| 162 | void initialize(QVariantList &values); |
| 163 | void configurePrivate(QRemoteObjectReplica *) override; |
| 164 | void requestRemoteObjectSource(); |
| 165 | bool sendCommand(); |
| 166 | QRemoteObjectPendingCall sendCommandWithReply(int serialId); |
| 167 | bool waitForFinished(const QRemoteObjectPendingCall &call, int timeout) override; |
| 168 | void notifyAboutReply(int ackedSerialId, const QVariant &value) override; |
| 169 | void setConnection(IoDeviceBase *conn); |
| 170 | void setDisconnected(); |
| 171 | |
| 172 | void _q_send(QMetaObject::Call call, int index, const QVariantList &args) override; |
| 173 | QRemoteObjectPendingCall _q_sendWithReply(QMetaObject::Call call, int index, const QVariantList& args) override; |
| 174 | |
| 175 | void setDynamicMetaObject(const QMetaObject *meta) override; |
| 176 | void setDynamicProperties(const QVariantList&) override; |
| 177 | QVector<QRemoteObjectReplica *> m_parentsNeedingConnect; |
| 178 | QVariantList m_propertyStorage; |
| 179 | QVector<int> m_childIndices; |
| 180 | QPointer<IoDeviceBase> connectionToSource; |
| 181 | |
| 182 | // pending call data |
| 183 | int m_curSerialId = 1; // 0 is reserved for heartbeat signals |
| 184 | QHash<int, QRemoteObjectPendingCall> m_pendingCalls; |
| 185 | QRemoteObjectPackets::DataStreamPacket m_packet; |
| 186 | QTimer m_heartbeatTimer; |
| 187 | }; |
| 188 | |
| 189 | class QInProcessReplicaImplementation final : public QRemoteObjectReplicaImplementation |
| 190 | { |
| 191 | public: |
| 192 | explicit QInProcessReplicaImplementation(const QString &name, const QMetaObject *, QRemoteObjectNode *); |
| 193 | ~QInProcessReplicaImplementation() override; |
| 194 | |
| 195 | const QVariant getProperty(int i) const override; |
| 196 | void setProperties(const QVariantList &) override; |
| 197 | void setProperty(int i, const QVariant &) override; |
| 198 | bool isShortCircuit() const final { return true; } |
| 199 | |
| 200 | void _q_send(QMetaObject::Call call, int index, const QVariantList &args) override; |
| 201 | QRemoteObjectPendingCall _q_sendWithReply(QMetaObject::Call call, int index, const QVariantList& args) override; |
| 202 | |
| 203 | QPointer<QRemoteObjectSourceBase> connectionToSource; |
| 204 | }; |
| 205 | |
| 206 | QT_END_NAMESPACE |
| 207 | |
| 208 | #endif |
| 209 | |