QtPdCom  1.3.3
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 
83  public:
84  enum class SslCaMode {
85  NoTLS,
86  DefaultCAs ,
87  CustomCAs ,
88  IgnoreCertificate ,
89  };
90  Q_ENUM(SslCaMode)
91 
92  Process(QObject *parent = nullptr);
93  virtual ~Process();
94 
95  void setApplicationName(const QString &);
96  QString getApplicationName() const;
97  Q_INVOKABLE void connectToHost(const QString &, quint16 = 2345);
98  Q_INVOKABLE void disconnectFromHost();
99 
107  ConnectedError
109  };
110  Q_ENUM(ConnectionState);
111 
112  ConnectionState getConnectionState() const;
113  bool isConnected() const;
114  const QString &getErrorString() const;
115  QString getPeerName() const;
116  QUrl getUrl() const;
117  int getPort() const;
118  QString getHost() const;
119 
120  Q_INVOKABLE void sendBroadcast(const QString &, const QString &attr = "text");
121  quint64 getRxBytes() const;
122  quint64 getTxBytes() const;
123 
134  QFuture<PdCom::Variable> find(const QString&);
135  QFuture<VariableList> list(const QString& = "");
136  QFuture<void> pingQt();
137  QFuture<std::vector<PdCom::ClientStatistics>> getClientStatisticsQt();
138 
175  template<class Class, class Function>
176  QFutureWatcher<PdCom::Variable>& find(const QString& path, Class *obj, Function&& callback);
177  template<class Class, class Function>
178  QFutureWatcher<VariableList>& list(const QString& path, Class *obj, Function&& callback);
179  template<class Class, class Function>
180  QFutureWatcher<void>& ping(Class *obj, Function&& callback);
181  template<class Class, class Function>
182  QFutureWatcher<std::vector<PdCom::ClientStatistics>>&
183  getClientStatistics(Class *obj, Function&& callback);
184 
192  void setCaMode(SslCaMode mode);
198  SslCaMode getCaMode() const;
199 
206  void setClientCertificate(const QSslCertificate& cert,const QSslKey& key);
214  void setCustomCAs(QList<QSslCertificate> cas);
215 
216  static QtPdCom::Process *getDefaultProcess();
217  static void setDefaultProcess(QtPdCom::Process *);
218 
219  PdCom::MessageManagerBase *getMessageManager() const;
220 
225  void setLoginManager(LoginManager* lm);
226  LoginManager *getLoginManager() const;
227  private:
228  struct Q_DECL_HIDDEN Impl;
229  std::unique_ptr<Impl> impl;
230 
232  std::string applicationName() const override;
233  std::string hostname() const override;
234  int read(char *, int) override;
235  void write(const char *, size_t) override;
236  void flush() override;
237  void connected() override;
238  void broadcastReply(
239  const std::string &message,
240  const std::string &attr,
241  std::chrono::nanoseconds time_ns,
242  const std::string &user) override;
243  void pingReply() override;
244  void findReply(PdCom::Variable const& var) override;
245  void listReply(std::vector<PdCom::Variable> vars, std::vector<std::string> dirs) override;
246  void clientStatisticsReply(std::vector<PdCom::ClientStatistics> statistics) override;
247  void reset();
248 
253  bool disconnect(
254  const char *signal = 0,
255  const QObject *receiver = 0,
256  const char *method = 0
257  );
258 
259  // make setMessageManager() private
260  using PdCom::Process::setMessageManager;
261 
262  signals:
267  void processConnected();
268 
273  void disconnected();
274 
280  void error();
281 
282  void broadcastReceived(
283  const QString &message,
284  const QString &attr,
285  std::uint64_t time_ns,
286  const QString &user);
287 
288  void connectionStatusChanged();
289 
290  void sslCaModeChanged();
291 
292 
293  private slots:
294  void socketConnected();
295  void socketDisconnected();
296  void socketError();
297  void socketRead();
298 };
299 
300 /****************************************************************************/
301 
302 template<class Class, class Function>
303 inline QFutureWatcher<PdCom::Variable>& Process::find(
304  const QString& path, Class *obj, Function&& callback)
305 {
306  auto& ans = createWatcher<PdCom::Variable>(obj, callback);
307  ans.setFuture(find(path));
308  return ans;
309 }
310 
311 /****************************************************************************/
312 
313 template<class Class, class Function>
314 inline QFutureWatcher<VariableList>& Process::list(const QString& path, Class *obj, Function&& callback)
315 {
316  auto& ans = createWatcher<VariableList>(obj, callback);
317  ans.setFuture(list(path));
318  return ans;
319 }
320 
321 /****************************************************************************/
322 
323 template<class Class, class Function>
324 inline QFutureWatcher<void>& Process::ping(Class *obj, Function&& callback)
325 {
326  auto& ans = createWatcher<void>(obj, callback);
327  ans.setFuture(pingQt());
328  return ans;
329 }
330 
331 /****************************************************************************/
332 
333 template<class Class, class Function>
334 inline QFutureWatcher<std::vector<PdCom::ClientStatistics>>&
335 Process::getClientStatistics(Class *obj, Function&& callback)
336 {
337  auto& ans = createWatcher<std::vector<PdCom::ClientStatistics>>(
338  obj, callback
339  );
340  ans.setFuture(getClientStatisticsQt());
341  return ans;
342 }
343 
344 /****************************************************************************/
345 
346 } // namespace
347 
348 #endif // QTPDCOM_PROCESS_H
Definition: Process.cpp:74
Process connection established.
Definition: Process.h:105
Currently connecting.
Definition: Process.h:104
PdCom::Process implementation for Qt.
Definition: Process.h:70
An error happened while connecting.
Definition: Process.h:106
QFutureWatcher< std::vector< PdCom::ClientStatistics > > & getClientStatistics(Class *obj, Function &&callback)
Definition: Process.h:335
Definition: BroadcastModel.h:32
ConnectionState
State of the process connection.
Definition: Process.h:102
Q_DECLARE_OPAQUE_POINTER(QtPdCom::LoginManager *)
QFuture< VariableList > list(const QString &="")
Wrapper function for Process::list.
Definition: Process.cpp:328
QFuture< std::vector< PdCom::ClientStatistics > > getClientStatisticsQt()
Definition: Process.cpp:343
#define QTPDCOM_PUBLIC
Definition: Export.h:30
QFuture< void > pingQt()
Definition: Process.cpp:584
Definition: LoginManager.h:36
Process disconnected.
Definition: Process.h:103
std::unique_ptr< Impl > impl
Definition: Process.h:229
QFutureWatcher< void > & ping(Class *obj, Function &&callback)
Definition: Process.h:324
QFuture< PdCom::Variable > find(const QString &)
Find a Variable.
Definition: Process.cpp:310
SslCaMode
Definition: Process.h:84