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
51QT_BEGIN_NAMESPACE
52
53class QRemoteObjectReplica;
54class SourceApiMap;
55class QAbstractItemModel;
56class QAbstractItemModelReplica;
57class QItemSelectionModel;
58class QRemoteObjectAbstractPersistedStorePrivate;
59class QRemoteObjectNodePrivate;
60class QRemoteObjectHostBasePrivate;
61class QRemoteObjectHostPrivate;
62class QRemoteObjectRegistryHostPrivate;
63
64class Q_REMOTEOBJECTS_EXPORT QRemoteObjectAbstractPersistedStore : public QObject
65{
66 Q_OBJECT
67
68public:
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
75protected:
76 QRemoteObjectAbstractPersistedStore(QRemoteObjectAbstractPersistedStorePrivate &, QObject *parent);
77 Q_DECLARE_PRIVATE(QRemoteObjectAbstractPersistedStore)
78};
79
80class 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
87public:
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 &registryAddress, 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(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 &registryAddress);
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
148Q_SIGNALS:
149 void remoteObjectAdded(const QRemoteObjectSourceLocation &);
150 void remoteObjectRemoved(const QRemoteObjectSourceLocation &);
151
152 void error(QRemoteObjectNode::ErrorCode errorCode);
153 void heartbeatIntervalChanged(int heartbeatInterval);
154
155protected:
156 QRemoteObjectNode(QRemoteObjectNodePrivate &, QObject *parent);
157
158 void timerEvent(QTimerEvent*) override;
159
160private:
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
169class Q_REMOTEOBJECTS_EXPORT QRemoteObjectHostBase : public QRemoteObjectNode
170{
171 Q_OBJECT
172public:
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 bool enableRemoting(QObject *object, const QString &name = QString());
185 bool enableRemoting(QAbstractItemModel *model, const QString &name, const QVector<int> roles, QItemSelectionModel *selectionModel = nullptr);
186 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 &registryUrl, const QUrl &hostUrl={},
191 RemoteObjectNameFilter filter=[](const QString &, const QString &) {return true; });
192 bool reverseProxy(RemoteObjectNameFilter filter=[](const QString &, const QString &) {return true; });
193
194protected:
195 virtual QUrl hostUrl() const;
196 virtual bool setHostUrl(const QUrl &hostAddress, AllowedSchemas allowedSchemas=BuiltInSchemasOnly);
197 QRemoteObjectHostBase(QRemoteObjectHostBasePrivate &, QObject *);
198
199private:
200 bool enableRemoting(QObject *object, const SourceApiMap *, QObject *adapter = nullptr);
201 Q_DECLARE_PRIVATE(QRemoteObjectHostBase)
202};
203
204class Q_REMOTEOBJECTS_EXPORT QRemoteObjectHost : public QRemoteObjectHostBase
205{
206 Q_OBJECT
207public:
208 QRemoteObjectHost(QObject *parent = nullptr);
209 QRemoteObjectHost(const QUrl &address, const QUrl &registryAddress = QUrl(),
210 AllowedSchemas allowedSchemas=BuiltInSchemasOnly, QObject *parent = nullptr);
211 QRemoteObjectHost(const QUrl &address, QObject *parent);
212 ~QRemoteObjectHost() override;
213 QUrl hostUrl() const override;
214 bool setHostUrl(const QUrl &hostAddress, AllowedSchemas allowedSchemas=BuiltInSchemasOnly) override;
215
216protected:
217 QRemoteObjectHost(QRemoteObjectHostPrivate &, QObject *);
218
219private:
220 Q_DECLARE_PRIVATE(QRemoteObjectHost)
221};
222
223class Q_REMOTEOBJECTS_EXPORT QRemoteObjectRegistryHost : public QRemoteObjectHostBase
224{
225 Q_OBJECT
226public:
227 QRemoteObjectRegistryHost(const QUrl &registryAddress = QUrl(), QObject *parent = nullptr);
228 ~QRemoteObjectRegistryHost() override;
229 bool setRegistryUrl(const QUrl &registryUrl) override;
230
231protected:
232 QRemoteObjectRegistryHost(QRemoteObjectRegistryHostPrivate &, QObject *);
233
234private:
235 Q_DECLARE_PRIVATE(QRemoteObjectRegistryHost)
236};
237
238QT_END_NAMESPACE
239
240#endif
241