QtPdCom  1.4.1
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 "QtPdCom1.h"
46 #include "Export.h"
47 #include "FutureWatchers.h"
48 #include "VariableList.h"
49 
50 class QTranslator;
51 class QSslKey;
52 
53 
54 #ifdef QTPDCOM_HAS_LOGIN_MANAGER
55 #include "LoginManager.h"
56 #else
57 namespace QtPdCom {
58 class LoginManager;
59 }
60 
62 #endif
63 
64 namespace QtPdCom {
65 
66 /****************************************************************************/
67 
71  public QObject, public PdCom::Process
72 {
73  Q_OBJECT
74  Q_PROPERTY(bool connected READ isConnected NOTIFY connectionStatusChanged)
75  Q_PROPERTY(ConnectionState connectionState READ getConnectionState NOTIFY connectionStatusChanged)
76  Q_PROPERTY(SslCaMode sslCaMode READ getCaMode WRITE setCaMode NOTIFY sslCaModeChanged)
77  Q_PROPERTY(int port READ getPort NOTIFY connectionStatusChanged)
78  Q_PROPERTY(QString host READ getHost NOTIFY connectionStatusChanged)
79  Q_PROPERTY(QUrl url READ getUrl NOTIFY connectionStatusChanged)
80  Q_PROPERTY(QString applicationName READ getApplicationName WRITE setApplicationName)
81  Q_PROPERTY(QtPdCom::LoginManager * loginManager READ getLoginManager WRITE setLoginManager)
82  Q_PROPERTY(QVariant name READ nameQt NOTIFY connectionStatusChanged)
83  Q_PROPERTY(QVariant version READ versionQt NOTIFY connectionStatusChanged)
84 
85  public:
86  enum class SslCaMode {
87  NoTLS,
88  DefaultCAs ,
89  CustomCAs ,
90  IgnoreCertificate ,
91  };
92  Q_ENUM(SslCaMode)
93 
94  Process(QObject *parent = nullptr);
95  virtual ~Process();
96 
97  void setApplicationName(const QString &);
98  QString getApplicationName() const;
99  Q_INVOKABLE void connectToHost(const QString &, quint16 = 2345);
100  Q_INVOKABLE void disconnectFromHost();
101 
109  ConnectedError
111  };
112  Q_ENUM(ConnectionState);
113 
114  ConnectionState getConnectionState() const;
115  bool isConnected() const;
116  const QString &getErrorString() const;
117  QString getPeerName() const;
118  QUrl getUrl() const;
119  int getPort() const;
120  QString getHost() const;
121 
122  Q_INVOKABLE void sendBroadcast(const QString &, const QString &attr = "text");
123  quint64 getRxBytes() const;
124  quint64 getTxBytes() const;
125 
127  QVariant nameQt() const;
129  QVariant versionQt() const;
130 
141  QFuture<PdCom::Variable> find(const QString&);
142  QFuture<VariableList> list(const QString& = "");
143  QFuture<void> pingQt();
144  QFuture<std::vector<PdCom::ClientStatistics>> getClientStatisticsQt();
145 
182  template<class Class, class Function>
183  QFutureWatcher<PdCom::Variable>& find(const QString& path, Class *obj, Function&& callback);
184  template<class Class, class Function>
185  QFutureWatcher<VariableList>& list(const QString& path, Class *obj, Function&& callback);
186  template<class Class, class Function>
187  QFutureWatcher<void>& ping(Class *obj, Function&& callback);
188  template<class Class, class Function>
189  QFutureWatcher<std::vector<PdCom::ClientStatistics>>&
190  getClientStatistics(Class *obj, Function&& callback);
191 
199  void setCaMode(SslCaMode mode);
205  SslCaMode getCaMode() const;
206 
213  void setClientCertificate(const QSslCertificate& cert,const QSslKey& key);
221  void setCustomCAs(QList<QSslCertificate> cas);
222 
223  static QtPdCom::Process *getDefaultProcess();
224  static void setDefaultProcess(QtPdCom::Process *);
225 
226  PdCom::MessageManagerBase *getMessageManager() const;
227 
232  void setLoginManager(LoginManager* lm);
233  LoginManager *getLoginManager() const;
234  private:
235  struct Q_DECL_HIDDEN Impl;
236  std::unique_ptr<Impl> impl;
237 
239  std::string applicationName() const override;
240  std::string hostname() const override;
241  int read(char *, int) override;
242  void write(const char *, size_t) override;
243  void flush() override;
244  void connected() override;
245  void broadcastReply(
246  const std::string &message,
247  const std::string &attr,
248  std::chrono::nanoseconds time_ns,
249  const std::string &user) override;
250  void pingReply() override;
251  void findReply(PdCom::Variable const& var) override;
252  void listReply(std::vector<PdCom::Variable> vars, std::vector<std::string> dirs) override;
253  void clientStatisticsReply(std::vector<PdCom::ClientStatistics> statistics) override;
254  void reset();
255 
260  bool disconnect(
261  const char *signal = 0,
262  const QObject *receiver = 0,
263  const char *method = 0
264  );
265 
266  // make setMessageManager() private
267  using PdCom::Process::setMessageManager;
268 
269  signals:
274  void processConnected();
275 
280  void disconnected();
281 
287  void error();
288 
289  void broadcastReceived(
290  const QString &message,
291  const QString &attr,
292  std::uint64_t time_ns,
293  const QString &user);
294 
295  void connectionStatusChanged();
296 
297  void sslCaModeChanged();
298 
299 
300  private slots:
301  void socketConnected();
302  void socketDisconnected();
303  void socketError();
304  void socketRead();
305 };
306 
307 /****************************************************************************/
308 
309 template<class Class, class Function>
310 inline QFutureWatcher<PdCom::Variable>& Process::find(
311  const QString& path, Class *obj, Function&& callback)
312 {
313  auto& ans = createWatcher<PdCom::Variable>(obj, callback);
314  ans.setFuture(find(path));
315  return ans;
316 }
317 
318 /****************************************************************************/
319 
320 template<class Class, class Function>
321 inline QFutureWatcher<VariableList>& Process::list(const QString& path, Class *obj, Function&& callback)
322 {
323  auto& ans = createWatcher<VariableList>(obj, callback);
324  ans.setFuture(list(path));
325  return ans;
326 }
327 
328 /****************************************************************************/
329 
330 template<class Class, class Function>
331 inline QFutureWatcher<void>& Process::ping(Class *obj, Function&& callback)
332 {
333  auto& ans = createWatcher<void>(obj, callback);
334  ans.setFuture(pingQt());
335  return ans;
336 }
337 
338 /****************************************************************************/
339 
340 template<class Class, class Function>
341 inline QFutureWatcher<std::vector<PdCom::ClientStatistics>>&
342 Process::getClientStatistics(Class *obj, Function&& callback)
343 {
344  auto& ans = createWatcher<std::vector<PdCom::ClientStatistics>>(
345  obj, callback
346  );
347  ans.setFuture(getClientStatisticsQt());
348  return ans;
349 }
350 
351 /****************************************************************************/
352 
353 } // namespace
354 
355 #endif // QTPDCOM_PROCESS_H
Definition: Process.cpp:74
Process connection established.
Definition: Process.h:107
Currently connecting.
Definition: Process.h:106
PdCom::Process implementation for Qt.
Definition: Process.h:70
An error happened while connecting.
Definition: Process.h:108
QFutureWatcher< std::vector< PdCom::ClientStatistics > > & getClientStatistics(Class *obj, Function &&callback)
Definition: Process.h:342
Definition: BroadcastModel.h:32
ConnectionState
State of the process connection.
Definition: Process.h:104
Q_DECLARE_OPAQUE_POINTER(QtPdCom::LoginManager *)
QFuture< VariableList > list(const QString &="")
Wrapper function for Process::list.
Definition: Process.cpp:348
QFuture< std::vector< PdCom::ClientStatistics > > getClientStatisticsQt()
Definition: Process.cpp:363
#define QTPDCOM_PUBLIC
Definition: Export.h:30
QFuture< void > pingQt()
Definition: Process.cpp:604
Class to handle the Authentication process.
Definition: LoginManager.h:46
Process disconnected.
Definition: Process.h:105
std::unique_ptr< Impl > impl
Definition: Process.h:236
QFutureWatcher< void > & ping(Class *obj, Function &&callback)
Definition: Process.h:331
QFuture< PdCom::Variable > find(const QString &)
Find a Variable.
Definition: Process.cpp:330
SslCaMode
Definition: Process.h:86