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 | #include <QtRemoteObjects/qremoteobjectnode.h> |
22 | |
23 | QT_BEGIN_NAMESPACE |
24 | |
25 | class QtROIoDeviceBasePrivate; |
26 | class QtROClientIoDevicePrivate; |
27 | class QtROExternalIoDevicePrivate; |
28 | |
29 | class Q_REMOTEOBJECTS_EXPORT QtROIoDeviceBase : public QObject |
30 | { |
31 | Q_OBJECT |
32 | Q_DISABLE_COPY(QtROIoDeviceBase) |
33 | |
34 | public: |
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 | |
52 | Q_SIGNALS: |
53 | void readyRead(); |
54 | void disconnected(); |
55 | |
56 | protected: |
57 | explicit QtROIoDeviceBase(QtROIoDeviceBasePrivate &, QObject *parent); |
58 | virtual QString deviceType() const = 0; |
59 | virtual void doClose() = 0; |
60 | |
61 | private: |
62 | Q_DECLARE_PRIVATE(QtROIoDeviceBase) |
63 | friend class QRemoteObjectNodePrivate; |
64 | friend class QConnectedReplicaImplementation; |
65 | friend class QRemoteObjectSourceIo; |
66 | }; |
67 | |
68 | class Q_REMOTEOBJECTS_EXPORT QtROServerIoDevice : public QtROIoDeviceBase |
69 | { |
70 | Q_OBJECT |
71 | Q_DISABLE_COPY(QtROServerIoDevice) |
72 | |
73 | public: |
74 | explicit QtROServerIoDevice(QObject *parent = nullptr); |
75 | |
76 | protected: |
77 | QString deviceType() const override; |
78 | }; |
79 | |
80 | class Q_REMOTEOBJECTS_EXPORT QConnectionAbstractServer : public QObject |
81 | { |
82 | Q_OBJECT |
83 | Q_DISABLE_COPY(QConnectionAbstractServer) |
84 | |
85 | public: |
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 | |
96 | protected: |
97 | virtual QtROServerIoDevice* configureNewConnection() = 0; |
98 | |
99 | Q_SIGNALS: |
100 | void newConnection(); |
101 | }; |
102 | |
103 | class Q_REMOTEOBJECTS_EXPORT QtROClientIoDevice : public QtROIoDeviceBase |
104 | { |
105 | Q_OBJECT |
106 | Q_DISABLE_COPY(QtROClientIoDevice) |
107 | |
108 | public: |
109 | explicit QtROClientIoDevice(QObject *parent = nullptr); |
110 | ~QtROClientIoDevice() override; |
111 | |
112 | void disconnectFromServer(); |
113 | virtual void connectToServer() = 0; |
114 | |
115 | QUrl url() const; |
116 | |
117 | Q_SIGNALS: |
118 | void shouldReconnect(QtROClientIoDevice*); |
119 | void setError(QRemoteObjectNode::ErrorCode); |
120 | |
121 | protected: |
122 | virtual void doDisconnectFromServer() = 0; |
123 | QString deviceType() const override; |
124 | void setUrl(const QUrl &url); |
125 | |
126 | private: |
127 | Q_DECLARE_PRIVATE(QtROClientIoDevice) |
128 | friend class QtROClientFactory; |
129 | }; |
130 | |
131 | class QtROServerFactory |
132 | { |
133 | public: |
134 | Q_REMOTEOBJECTS_EXPORT static QtROServerFactory *instance(); |
135 | |
136 | QConnectionAbstractServer *create(const QUrl &url, QObject *parent = nullptr) |
137 | { |
138 | auto creatorFunc = m_creatorFuncs.value(key: url.scheme()); |
139 | return creatorFunc ? (*creatorFunc)(parent) : nullptr; |
140 | } |
141 | |
142 | template<typename T> |
143 | void registerType(const QString &id) |
144 | { |
145 | m_creatorFuncs[id] = [](QObject *parent) -> QConnectionAbstractServer * { |
146 | return new T(parent); |
147 | }; |
148 | } |
149 | |
150 | bool isValid(const QUrl &url) |
151 | { |
152 | return m_creatorFuncs.contains(key: url.scheme()); |
153 | } |
154 | |
155 | private: |
156 | friend class QtROFactoryLoader; |
157 | QtROServerFactory(); |
158 | |
159 | using CreatorFunc = QConnectionAbstractServer * (*)(QObject *); |
160 | QHash<QString, CreatorFunc> m_creatorFuncs; |
161 | }; |
162 | |
163 | class QtROClientFactory |
164 | { |
165 | public: |
166 | Q_REMOTEOBJECTS_EXPORT static QtROClientFactory *instance(); |
167 | |
168 | /// creates an object from a string |
169 | QtROClientIoDevice *create(const QUrl &url, QObject *parent = nullptr) |
170 | { |
171 | auto creatorFunc = m_creatorFuncs.value(key: url.scheme()); |
172 | if (!creatorFunc) |
173 | return nullptr; |
174 | |
175 | QtROClientIoDevice *res = (*creatorFunc)(parent); |
176 | if (res) |
177 | res->setUrl(url); |
178 | return res; |
179 | } |
180 | |
181 | template<typename T> |
182 | void registerType(const QString &id) |
183 | { |
184 | m_creatorFuncs[id] = [](QObject *parent) -> QtROClientIoDevice * { |
185 | return new T(parent); |
186 | }; |
187 | } |
188 | |
189 | bool isValid(const QUrl &url) |
190 | { |
191 | return m_creatorFuncs.contains(key: url.scheme()); |
192 | } |
193 | |
194 | private: |
195 | friend class QtROFactoryLoader; |
196 | QtROClientFactory(); |
197 | |
198 | using CreatorFunc = QtROClientIoDevice * (*)(QObject *); |
199 | QHash<QString, CreatorFunc> m_creatorFuncs; |
200 | }; |
201 | |
202 | template <typename T> |
203 | inline void qRegisterRemoteObjectsClient(const QString &id) |
204 | { |
205 | QtROClientFactory::instance()->registerType<T>(id); |
206 | } |
207 | |
208 | template <typename T> |
209 | inline void qRegisterRemoteObjectsServer(const QString &id) |
210 | { |
211 | QtROServerFactory::instance()->registerType<T>(id); |
212 | } |
213 | |
214 | QT_END_NAMESPACE |
215 | |
216 | #endif // QCONNECTIONFACTORIES_H |
217 | |