1// Copyright (C) 2021 Ford Motor Company
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QCONNECTIONFACTORIES_H
5#define QCONNECTIONFACTORIES_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is part of the QtRO internal API and is not meant to be used
12// in applications. It contains "internal" components, which are exported
13// to allow new QIODevice channels to be added to extend QtRO. Usage of these
14// APIs may make your code source and binary incompatible with future versions
15// of Qt.
16//
17
18#include <QtNetwork/qabstractsocket.h>
19
20#include <QtRemoteObjects/qtremoteobjectglobal.h>
21
22
23QT_BEGIN_NAMESPACE
24
25class QtROIoDeviceBasePrivate;
26class QtROClientIoDevicePrivate;
27class QtROExternalIoDevicePrivate;
28
29class Q_REMOTEOBJECTS_EXPORT QtROIoDeviceBase : public QObject
30{
31 Q_OBJECT
32 Q_DISABLE_COPY(QtROIoDeviceBase)
33
34public:
35 explicit QtROIoDeviceBase(QObject *parent = nullptr);
36 ~QtROIoDeviceBase() override;
37
38 bool read(QtRemoteObjects::QRemoteObjectPacketTypeEnum &, QString &);
39
40 virtual void write(const QByteArray &data);
41 virtual void write(const QByteArray &data, qint64);
42 virtual bool isOpen() const;
43 virtual void close();
44 virtual qint64 bytesAvailable() const;
45 virtual QIODevice *connection() const = 0;
46 void initializeDataStream();
47 bool isClosing() const;
48 void addSource(const QString &);
49 void removeSource(const QString &);
50 QSet<QString> remoteObjects() const;
51
52Q_SIGNALS:
53 void readyRead();
54 void disconnected();
55
56protected:
57 explicit QtROIoDeviceBase(QtROIoDeviceBasePrivate &, QObject *parent);
58 virtual QString deviceType() const = 0;
59 virtual void doClose() = 0;
60
61private:
62 Q_DECLARE_PRIVATE(QtROIoDeviceBase)
63 friend class QRemoteObjectNodePrivate;
64 friend class QConnectedReplicaImplementation;
65 friend class QRemoteObjectSourceIo;
66};
67
68class Q_REMOTEOBJECTS_EXPORT QtROServerIoDevice : public QtROIoDeviceBase
69{
70 Q_OBJECT
71 Q_DISABLE_COPY(QtROServerIoDevice)
72
73public:
74 explicit QtROServerIoDevice(QObject *parent = nullptr);
75
76protected:
77 QString deviceType() const override;
78};
79
80class Q_REMOTEOBJECTS_EXPORT QConnectionAbstractServer : public QObject
81{
82 Q_OBJECT
83 Q_DISABLE_COPY(QConnectionAbstractServer)
84
85public:
86 explicit QConnectionAbstractServer(QObject *parent = nullptr);
87 ~QConnectionAbstractServer() override;
88
89 virtual bool hasPendingConnections() const = 0;
90 QtROServerIoDevice* nextPendingConnection();
91 virtual QUrl address() const = 0;
92 virtual bool listen(const QUrl &address) = 0;
93 virtual QAbstractSocket::SocketError serverError() const = 0;
94 virtual void close() = 0;
95
96protected:
97 virtual QtROServerIoDevice* configureNewConnection() = 0;
98
99Q_SIGNALS:
100 void newConnection();
101};
102
103class Q_REMOTEOBJECTS_EXPORT QtROClientIoDevice : public QtROIoDeviceBase
104{
105 Q_OBJECT
106 Q_DISABLE_COPY(QtROClientIoDevice)
107
108public:
109 explicit QtROClientIoDevice(QObject *parent = nullptr);
110 ~QtROClientIoDevice() override;
111
112 void disconnectFromServer();
113 virtual void connectToServer() = 0;
114
115 QUrl url() const;
116
117Q_SIGNALS:
118 void shouldReconnect(QtROClientIoDevice*);
119
120protected:
121 virtual void doDisconnectFromServer() = 0;
122 QString deviceType() const override;
123 void setUrl(const QUrl &url);
124
125private:
126 Q_DECLARE_PRIVATE(QtROClientIoDevice)
127 friend class QtROClientFactory;
128};
129
130class QtROServerFactory
131{
132public:
133 Q_REMOTEOBJECTS_EXPORT static QtROServerFactory *instance();
134
135 QConnectionAbstractServer *create(const QUrl &url, QObject *parent = nullptr)
136 {
137 auto creatorFunc = m_creatorFuncs.value(key: url.scheme());
138 return creatorFunc ? (*creatorFunc)(parent) : nullptr;
139 }
140
141 template<typename T>
142 void registerType(const QString &id)
143 {
144 m_creatorFuncs[id] = [](QObject *parent) -> QConnectionAbstractServer * {
145 return new T(parent);
146 };
147 }
148
149 bool isValid(const QUrl &url)
150 {
151 return m_creatorFuncs.contains(key: url.scheme());
152 }
153
154private:
155 friend class QtROFactoryLoader;
156 QtROServerFactory();
157
158 using CreatorFunc = QConnectionAbstractServer * (*)(QObject *);
159 QHash<QString, CreatorFunc> m_creatorFuncs;
160};
161
162class QtROClientFactory
163{
164public:
165 Q_REMOTEOBJECTS_EXPORT static QtROClientFactory *instance();
166
167 /// creates an object from a string
168 QtROClientIoDevice *create(const QUrl &url, QObject *parent = nullptr)
169 {
170 auto creatorFunc = m_creatorFuncs.value(key: url.scheme());
171 if (!creatorFunc)
172 return nullptr;
173
174 QtROClientIoDevice *res = (*creatorFunc)(parent);
175 if (res)
176 res->setUrl(url);
177 return res;
178 }
179
180 template<typename T>
181 void registerType(const QString &id)
182 {
183 m_creatorFuncs[id] = [](QObject *parent) -> QtROClientIoDevice * {
184 return new T(parent);
185 };
186 }
187
188 bool isValid(const QUrl &url)
189 {
190 return m_creatorFuncs.contains(key: url.scheme());
191 }
192
193private:
194 friend class QtROFactoryLoader;
195 QtROClientFactory();
196
197 using CreatorFunc = QtROClientIoDevice * (*)(QObject *);
198 QHash<QString, CreatorFunc> m_creatorFuncs;
199};
200
201template <typename T>
202inline void qRegisterRemoteObjectsClient(const QString &id)
203{
204 QtROClientFactory::instance()->registerType<T>(id);
205}
206
207template <typename T>
208inline void qRegisterRemoteObjectsServer(const QString &id)
209{
210 QtROServerFactory::instance()->registerType<T>(id);
211}
212
213QT_END_NAMESPACE
214
215#endif // QCONNECTIONFACTORIES_H
216

source code of qtremoteobjects/src/remoteobjects/qconnectionfactories.h