QtPdCom  1.2.0
Process.h
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * Copyright (C) 2009-2022 Florian Pose <fp@igh.de>
4 *
5 * This file is part of the QtPdCom library.
6 *
7 * The QtPdCom library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * The QtPdCom library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the QtPdCom Library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 ****************************************************************************/
21
22#ifndef QTPDCOM_PROCESS_H
23#define QTPDCOM_PROCESS_H
24
25#include <QObject>
26#include <QString>
27#include <QList>
28#include <QSslCertificate>
29#include <QUrl>
30
31#include <pdcom5.h>
32#include <pdcom5/ClientStatistics.h>
33#include <pdcom5/Process.h>
34#include <QFuture>
35
36#ifndef PDCOM_VERSION_CODE
37# error "No PDCOM_VERSION_CODE found."
38#elif \
39 !PDCOM_DEVEL \
40 && (PDCOM_VERSION_CODE < PDCOM_VERSION(5, 0, 0) \
41 || PDCOM_VERSION_CODE >= PDCOM_VERSION(6, 0, 0))
42# error "Invalid PdCom version."
43#endif
44
45#include "Export.h"
46#include "FutureWatchers.h"
47#include "VariableList.h"
48
49class QTranslator;
50class QSslKey;
51
52namespace QtPdCom {
53
54class LoginManager;
55
56/****************************************************************************/
57
61 public QObject, public PdCom::Process
62{
63 Q_OBJECT
64 Q_PROPERTY(bool connected READ isConnected NOTIFY connectionStatusChanged)
65 Q_PROPERTY(ConnectionState connectionState READ getConnectionState NOTIFY connectionStatusChanged)
66 Q_PROPERTY(SslCaMode sslCaMode READ getCaMode WRITE setCaMode NOTIFY sslCaModeChanged)
67 Q_PROPERTY(int port READ getPort NOTIFY connectionStatusChanged)
68 Q_PROPERTY(QString host READ getHost NOTIFY connectionStatusChanged)
69 Q_PROPERTY(QUrl url READ getUrl NOTIFY connectionStatusChanged)
70 Q_PROPERTY(QString applicationName READ getApplicationName WRITE setApplicationName)
71 Q_PROPERTY(QtPdCom::LoginManager * loginManager READ getLoginManager WRITE setLoginManager)
72
73 public:
74 enum class SslCaMode {
75 NoTLS,
76 DefaultCAs ,
77 CustomCAs ,
78 IgnoreCertificate ,
79 };
80 Q_ENUM(SslCaMode)
81
82 Process(QObject *parent = nullptr);
83 virtual ~Process();
84
85 void setApplicationName(const QString &);
86 QString getApplicationName() const;
87 Q_INVOKABLE void connectToHost(const QString &, quint16 = 2345);
88 Q_INVOKABLE void disconnectFromHost();
89
97 ConnectedError
99 };
101
102 ConnectionState getConnectionState() const;
103 bool isConnected() const;
104 const QString &getErrorString() const;
105 QString getPeerName() const;
106 QUrl getUrl() const;
107 int getPort() const;
108 QString getHost() const;
109
110 Q_INVOKABLE void sendBroadcast(const QString &, const QString &attr = "text");
111 quint64 getRxBytes() const;
112 quint64 getTxBytes() const;
113
124 QFuture<PdCom::Variable> find(const QString&);
125 QFuture<VariableList> list(const QString& = "");
126 QFuture<void> pingQt();
127 QFuture<std::vector<PdCom::ClientStatistics>> getClientStatisticsQt();
128
165 template<class Class, class Function>
166 QFutureWatcher<PdCom::Variable>& find(const QString& path, Class *obj, Function&& callback);
167 template<class Class, class Function>
168 QFutureWatcher<VariableList>& list(const QString& path, Class *obj, Function&& callback);
169 template<class Class, class Function>
170 QFutureWatcher<void>& ping(Class *obj, Function&& callback);
171 template<class Class, class Function>
172 QFutureWatcher<std::vector<PdCom::ClientStatistics>>&
173 getClientStatistics(Class *obj, Function&& callback);
174
182 void setCaMode(SslCaMode mode);
188 SslCaMode getCaMode() const;
189
196 void setClientCertificate(const QSslCertificate& cert,const QSslKey& key);
204 void setCustomCAs(QList<QSslCertificate> cas);
205
206 static QtPdCom::Process *getDefaultProcess();
207 static void setDefaultProcess(QtPdCom::Process *);
208
209 PdCom::MessageManagerBase *getMessageManager() const;
210
215 void setLoginManager(LoginManager* lm);
216 LoginManager *getLoginManager() const;
217 private:
218 struct Q_DECL_HIDDEN Impl;
219 std::unique_ptr<Impl> impl;
220
222 std::string applicationName() const override;
223 std::string hostname() const override;
224 int read(char *, int) override;
225 void write(const char *, size_t) override;
226 void flush() override;
227 void connected() override;
228 void broadcastReply(
229 const std::string &message,
230 const std::string &attr,
231 std::chrono::nanoseconds time_ns,
232 const std::string &user) override;
233 void pingReply() override;
234 void findReply(PdCom::Variable const& var) override;
235 void listReply(std::vector<PdCom::Variable> vars, std::vector<std::string> dirs) override;
236 void clientStatisticsReply(std::vector<PdCom::ClientStatistics> statistics) override;
237 void reset();
238
244 const char *signal = 0,
245 const QObject *receiver = 0,
246 const char *method = 0
247 );
248
249 // make setMessageManager() private
250 using PdCom::Process::setMessageManager;
251
252 signals:
258
264
270 void error();
271
273 const QString &message,
274 const QString &attr,
275 std::uint64_t time_ns,
276 const QString &user);
277
279
281
282
283 private slots:
284 void socketConnected();
285 void socketDisconnected();
286 void socketError();
287 void socketRead();
288};
289
290/****************************************************************************/
291
292template<class Class, class Function>
293inline QFutureWatcher<PdCom::Variable>& Process::find(
294 const QString& path, Class *obj, Function&& callback)
295{
296 auto& ans = createWatcher<PdCom::Variable>(obj, callback);
297 ans.setFuture(find(path));
298 return ans;
299}
300
301/****************************************************************************/
302
303template<class Class, class Function>
304inline QFutureWatcher<VariableList>& Process::list(const QString& path, Class *obj, Function&& callback)
305{
306 auto& ans = createWatcher<VariableList>(obj, callback);
307 ans.setFuture(list(path));
308 return ans;
309}
310
311/****************************************************************************/
312
313template<class Class, class Function>
314inline QFutureWatcher<void>& Process::ping(Class *obj, Function&& callback)
315{
316 auto& ans = createWatcher<void>(obj, callback);
317 ans.setFuture(pingQt());
318 return ans;
319}
320
321/****************************************************************************/
322
323template<class Class, class Function>
324inline QFutureWatcher<std::vector<PdCom::ClientStatistics>>&
325Process::getClientStatistics(Class *obj, Function&& callback)
326{
327 auto& ans = createWatcher<std::vector<PdCom::ClientStatistics>>(
328 obj, callback
329 );
330 ans.setFuture(getClientStatisticsQt());
331 return ans;
332}
333
334/****************************************************************************/
335
336} // namespace
337
338#endif
#define QTPDCOM_PUBLIC
Definition: Export.h:30
Definition: LoginManager.h:37
PdCom::Process implementation for Qt.
Definition: Process.h:62
QFuture< std::vector< PdCom::ClientStatistics > > getClientStatisticsQt()
Definition: Process.cpp:326
std::unique_ptr< Impl > impl
Definition: Process.h:219
QFuture< void > pingQt()
Definition: Process.cpp:557
void connectionStatusChanged()
QFuture< PdCom::Variable > find(const QString &)
Find a Variable.
Definition: Process.cpp:295
SslCaMode
Definition: Process.h:74
Q_ENUM(ConnectionState)
void connected() override
void processConnected()
Connection established.
void sslCaModeChanged()
ConnectionState
State of the process connection.
Definition: Process.h:92
@ Disconnected
Process disconnected.
Definition: Process.h:93
@ Connecting
Currently connecting.
Definition: Process.h:94
@ Connected
Process connection established.
Definition: Process.h:95
@ ConnectError
An error happened while connecting.
Definition: Process.h:96
QFuture< VariableList > list(const QString &="")
Wrapper function for Process::list.
Definition: Process.cpp:312
void disconnected()
Disconnected gracefully.
void broadcastReceived(const QString &message, const QString &attr, std::uint64_t time_ns, const QString &user)
void error()
Connection error.
QFutureWatcher< void > & ping(Class *obj, Function &&callback)
Definition: Process.h:314
QFutureWatcher< std::vector< PdCom::ClientStatistics > > & getClientStatistics(Class *obj, Function &&callback)
Definition: Process.h:325
bool disconnect(const char *signal=0, const QObject *receiver=0, const char *method=0)
Disconnect method inherited from QObject.
Definition: BroadcastModel.h:32
Definition: Process.cpp:74