rpc_connection_worker.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_GUI_INCLUDE_CORBO_GUI_RPC_CONNECTION_WORKER_H_
26 #define SRC_GUI_INCLUDE_CORBO_GUI_RPC_CONNECTION_WORKER_H_
27 
29 
30 #include <QObject>
31 #include <QThread>
32 #include <QTimer>
33 
34 #include <memory>
35 
36 namespace corbo {
37 namespace gui {
38 
39 class RpcConnectionWorker : public QObject
40 {
41  Q_OBJECT
42 
43  public:
44  explicit RpcConnectionWorker(std::shared_ptr<MasterServiceClient> rpc_client, QObject* parent = nullptr) : QObject(parent)
45  {
46  if (rpc_client)
47  _rpc_client = rpc_client;
48  else
49  _rpc_client = std::make_shared<MasterServiceClient>();
50  }
51 
53  {
54  if (_timer) _timer->stop();
55  }
56 
57  public slots:
58 
59  void connectRpc(const QString& address)
60  {
61  _server_address = address;
62 
63  if (!_timer)
64  {
65  _timer = new QTimer(this);
66  connect(_timer, SIGNAL(timeout()), this, SLOT(tryConnect()));
67  _timer->setInterval(1000);
68  }
69 
70  _timer->start(); // this should also stop any previous timer
71  }
72 
73  signals:
74  void connectionResult(std::shared_ptr<MasterServiceClient> client);
75 
76  private slots:
77  void tryConnect()
78  {
79  std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(_server_address.toStdString(), grpc::InsecureChannelCredentials());
80  _rpc_client->setChannel(channel);
81 
82  if (_rpc_client->ping(500))
83  {
84  _timer->stop();
86  }
87  }
88 
89  private:
90  QTimer* _timer = nullptr;
91 
92  QString _server_address;
93 
94  std::shared_ptr<MasterServiceClient> _rpc_client;
95 };
96 
97 } // namespace gui
98 } // namespace corbo
99 
100 #endif // SRC_GUI_INCLUDE_CORBO_GUI_RPC_CONNECTION_WORKER_H_
corbo::gui::RpcConnectionWorker::~RpcConnectionWorker
~RpcConnectionWorker()
Definition: rpc_connection_worker.h:118
corbo::gui::RpcConnectionWorker::RpcConnectionWorker
RpcConnectionWorker(std::shared_ptr< MasterServiceClient > rpc_client, QObject *parent=nullptr)
Definition: rpc_connection_worker.h:110
corbo
Definition: communication/include/corbo-communication/utilities.h:37
main_service_client.h
corbo::gui::RpcConnectionWorker::_server_address
QString _server_address
Definition: rpc_connection_worker.h:158
corbo::gui::RpcConnectionWorker::_timer
QTimer * _timer
Definition: rpc_connection_worker.h:156
corbo::gui::RpcConnectionWorker::connectionResult
void connectionResult(std::shared_ptr< MasterServiceClient > client)
corbo::gui::RpcConnectionWorker::tryConnect
void tryConnect()
Definition: rpc_connection_worker.h:143
corbo::gui::RpcConnectionWorker::connectRpc
void connectRpc(const QString &address)
Definition: rpc_connection_worker.h:125
corbo::gui::RpcConnectionWorker::_rpc_client
std::shared_ptr< MasterServiceClient > _rpc_client
Definition: rpc_connection_worker.h:160


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:06:10