| 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 QREMOTEOBJECTNODE_H |
| 41 | #define QREMOTEOBJECTNODE_H |
| 42 | |
| 43 | #include <QtCore/qsharedpointer.h> |
| 44 | #include <QtCore/qmetaobject.h> |
| 45 | #include <QtRemoteObjects/qtremoteobjectglobal.h> |
| 46 | #include <QtRemoteObjects/qremoteobjectregistry.h> |
| 47 | #include <QtRemoteObjects/qremoteobjectdynamicreplica.h> |
| 48 | |
| 49 | #include <functional> |
| 50 | |
| 51 | QT_BEGIN_NAMESPACE |
| 52 | |
| 53 | class QRemoteObjectReplica; |
| 54 | class SourceApiMap; |
| 55 | class QAbstractItemModel; |
| 56 | class QAbstractItemModelReplica; |
| 57 | class QItemSelectionModel; |
| 58 | class QRemoteObjectAbstractPersistedStorePrivate; |
| 59 | class QRemoteObjectNodePrivate; |
| 60 | class QRemoteObjectHostBasePrivate; |
| 61 | class QRemoteObjectHostPrivate; |
| 62 | class QRemoteObjectRegistryHostPrivate; |
| 63 | |
| 64 | class Q_REMOTEOBJECTS_EXPORT QRemoteObjectAbstractPersistedStore : public QObject |
| 65 | { |
| 66 | Q_OBJECT |
| 67 | |
| 68 | public: |
| 69 | QRemoteObjectAbstractPersistedStore (QObject *parent = nullptr); |
| 70 | virtual ~QRemoteObjectAbstractPersistedStore(); |
| 71 | |
| 72 | virtual void saveProperties(const QString &repName, const QByteArray &repSig, const QVariantList &values) = 0; |
| 73 | virtual QVariantList restoreProperties(const QString &repName, const QByteArray &repSig) = 0; |
| 74 | |
| 75 | protected: |
| 76 | QRemoteObjectAbstractPersistedStore(QRemoteObjectAbstractPersistedStorePrivate &, QObject *parent); |
| 77 | Q_DECLARE_PRIVATE(QRemoteObjectAbstractPersistedStore) |
| 78 | }; |
| 79 | |
| 80 | class Q_REMOTEOBJECTS_EXPORT QRemoteObjectNode : public QObject |
| 81 | { |
| 82 | Q_OBJECT |
| 83 | Q_PROPERTY(QUrl registryUrl READ registryUrl WRITE setRegistryUrl) |
| 84 | Q_PROPERTY(QRemoteObjectAbstractPersistedStore* persistedStore READ persistedStore WRITE setPersistedStore) |
| 85 | Q_PROPERTY(int heartbeatInterval READ heartbeatInterval WRITE setHeartbeatInterval NOTIFY heartbeatIntervalChanged) |
| 86 | |
| 87 | public: |
| 88 | enum ErrorCode{ |
| 89 | NoError, |
| 90 | RegistryNotAcquired, |
| 91 | RegistryAlreadyHosted, |
| 92 | NodeIsNoServer, |
| 93 | ServerAlreadyCreated, |
| 94 | UnintendedRegistryHosting, |
| 95 | OperationNotValidOnClientNode, |
| 96 | SourceNotRegistered, |
| 97 | MissingObjectName, |
| 98 | HostUrlInvalid, |
| 99 | ProtocolMismatch, |
| 100 | ListenFailed |
| 101 | }; |
| 102 | Q_ENUM(ErrorCode) |
| 103 | |
| 104 | QRemoteObjectNode(QObject *parent = nullptr); |
| 105 | QRemoteObjectNode(const QUrl ®istryAddress, QObject *parent = nullptr); |
| 106 | ~QRemoteObjectNode() override; |
| 107 | |
| 108 | Q_INVOKABLE bool connectToNode(const QUrl &address); |
| 109 | void addClientSideConnection(QIODevice *ioDevice); |
| 110 | virtual void setName(const QString &name); |
| 111 | template < class ObjectType > |
| 112 | ObjectType *acquire(const QString &name = QString()) |
| 113 | { |
| 114 | return new ObjectType(this, name); |
| 115 | } |
| 116 | |
| 117 | template<typename T> |
| 118 | QStringList instances() const |
| 119 | { |
| 120 | const QMetaObject *mobj = &T::staticMetaObject; |
| 121 | const int index = mobj->indexOfClassInfo(QCLASSINFO_REMOTEOBJECT_TYPE); |
| 122 | if (index == -1) |
| 123 | return QStringList(); |
| 124 | |
| 125 | const QString typeName = QString::fromLatin1(str: mobj->classInfo(index).value()); |
| 126 | return instances(typeName); |
| 127 | } |
| 128 | QStringList instances(const QString &typeName) const; |
| 129 | |
| 130 | QRemoteObjectDynamicReplica *acquireDynamic(const QString &name); |
| 131 | QAbstractItemModelReplica *acquireModel(const QString &name, QtRemoteObjects::InitialAction action = QtRemoteObjects::FetchRootSize, const QVector<int> &rolesHint = {}); |
| 132 | QUrl registryUrl() const; |
| 133 | virtual bool setRegistryUrl(const QUrl ®istryAddress); |
| 134 | bool waitForRegistry(int timeout = 30000); |
| 135 | const QRemoteObjectRegistry *registry() const; |
| 136 | |
| 137 | QRemoteObjectAbstractPersistedStore *persistedStore() const; |
| 138 | void setPersistedStore(QRemoteObjectAbstractPersistedStore *persistedStore); |
| 139 | |
| 140 | ErrorCode lastError() const; |
| 141 | |
| 142 | int heartbeatInterval() const; |
| 143 | void setHeartbeatInterval(int interval); |
| 144 | |
| 145 | typedef std::function<void (QUrl)> RemoteObjectSchemaHandler; |
| 146 | void registerExternalSchema(const QString &schema, RemoteObjectSchemaHandler handler); |
| 147 | |
| 148 | Q_SIGNALS: |
| 149 | void remoteObjectAdded(const QRemoteObjectSourceLocation &); |
| 150 | void remoteObjectRemoved(const QRemoteObjectSourceLocation &); |
| 151 | |
| 152 | void error(QRemoteObjectNode::ErrorCode errorCode); |
| 153 | void heartbeatIntervalChanged(int heartbeatInterval); |
| 154 | |
| 155 | protected: |
| 156 | QRemoteObjectNode(QRemoteObjectNodePrivate &, QObject *parent); |
| 157 | |
| 158 | void timerEvent(QTimerEvent*) override; |
| 159 | |
| 160 | private: |
| 161 | void initializeReplica(QRemoteObjectReplica *instance, const QString &name = QString()); |
| 162 | void persistProperties(const QString &repName, const QByteArray &repSig, const QVariantList &props); |
| 163 | QVariantList retrieveProperties(const QString &repName, const QByteArray &repSig); |
| 164 | |
| 165 | Q_DECLARE_PRIVATE(QRemoteObjectNode) |
| 166 | friend class QRemoteObjectReplica; |
| 167 | }; |
| 168 | |
| 169 | class Q_REMOTEOBJECTS_EXPORT QRemoteObjectHostBase : public QRemoteObjectNode |
| 170 | { |
| 171 | Q_OBJECT |
| 172 | public: |
| 173 | enum AllowedSchemas { BuiltInSchemasOnly, AllowExternalRegistration }; |
| 174 | Q_ENUM(AllowedSchemas) |
| 175 | ~QRemoteObjectHostBase() override; |
| 176 | void setName(const QString &name) override; |
| 177 | |
| 178 | template <template <typename> class ApiDefinition, typename ObjectType> |
| 179 | bool enableRemoting(ObjectType *object) |
| 180 | { |
| 181 | ApiDefinition<ObjectType> *api = new ApiDefinition<ObjectType>(object); |
| 182 | return enableRemoting(object, api); |
| 183 | } |
| 184 | Q_INVOKABLE bool enableRemoting(QObject *object, const QString &name = QString()); |
| 185 | bool enableRemoting(QAbstractItemModel *model, const QString &name, const QVector<int> roles, QItemSelectionModel *selectionModel = nullptr); |
| 186 | Q_INVOKABLE bool disableRemoting(QObject *remoteObject); |
| 187 | void addHostSideConnection(QIODevice *ioDevice); |
| 188 | |
| 189 | typedef std::function<bool(const QString &, const QString &)> RemoteObjectNameFilter; |
| 190 | bool proxy(const QUrl ®istryUrl, const QUrl &hostUrl={}, |
| 191 | RemoteObjectNameFilter filter=[](const QString &, const QString &) {return true; }); |
| 192 | // ### Qt 6: Fix -> This should only be part of the QRemoteObjectRegistryHost type, since the |
| 193 | // reverse aspect requires the registry. |
| 194 | bool reverseProxy(RemoteObjectNameFilter filter=[](const QString &, const QString &) {return true; }); |
| 195 | |
| 196 | protected: |
| 197 | virtual QUrl hostUrl() const; |
| 198 | virtual bool setHostUrl(const QUrl &hostAddress, AllowedSchemas allowedSchemas=BuiltInSchemasOnly); |
| 199 | QRemoteObjectHostBase(QRemoteObjectHostBasePrivate &, QObject *); |
| 200 | |
| 201 | private: |
| 202 | bool enableRemoting(QObject *object, const SourceApiMap *, QObject *adapter = nullptr); |
| 203 | Q_DECLARE_PRIVATE(QRemoteObjectHostBase) |
| 204 | }; |
| 205 | |
| 206 | class Q_REMOTEOBJECTS_EXPORT QRemoteObjectHost : public QRemoteObjectHostBase |
| 207 | { |
| 208 | Q_OBJECT |
| 209 | Q_PROPERTY(QUrl hostUrl READ hostUrl WRITE setHostUrl NOTIFY hostUrlChanged) |
| 210 | |
| 211 | public: |
| 212 | QRemoteObjectHost(QObject *parent = nullptr); |
| 213 | QRemoteObjectHost(const QUrl &address, const QUrl ®istryAddress = QUrl(), |
| 214 | AllowedSchemas allowedSchemas=BuiltInSchemasOnly, QObject *parent = nullptr); |
| 215 | QRemoteObjectHost(const QUrl &address, QObject *parent); |
| 216 | ~QRemoteObjectHost() override; |
| 217 | QUrl hostUrl() const override; |
| 218 | bool setHostUrl(const QUrl &hostAddress, AllowedSchemas allowedSchemas=BuiltInSchemasOnly) override; |
| 219 | |
| 220 | Q_SIGNALS: |
| 221 | void hostUrlChanged(); |
| 222 | |
| 223 | protected: |
| 224 | QRemoteObjectHost(QRemoteObjectHostPrivate &, QObject *); |
| 225 | |
| 226 | private: |
| 227 | Q_DECLARE_PRIVATE(QRemoteObjectHost) |
| 228 | }; |
| 229 | |
| 230 | class Q_REMOTEOBJECTS_EXPORT QRemoteObjectRegistryHost : public QRemoteObjectHostBase |
| 231 | { |
| 232 | Q_OBJECT |
| 233 | public: |
| 234 | QRemoteObjectRegistryHost(const QUrl ®istryAddress = QUrl(), QObject *parent = nullptr); |
| 235 | ~QRemoteObjectRegistryHost() override; |
| 236 | bool setRegistryUrl(const QUrl ®istryUrl) override; |
| 237 | |
| 238 | protected: |
| 239 | QRemoteObjectRegistryHost(QRemoteObjectRegistryHostPrivate &, QObject *); |
| 240 | |
| 241 | private: |
| 242 | Q_DECLARE_PRIVATE(QRemoteObjectRegistryHost) |
| 243 | }; |
| 244 | |
| 245 | QT_END_NAMESPACE |
| 246 | |
| 247 | #endif |
| 248 | |