Skip to content

Commit bc365ff

Browse files
michaelaeriksenFelix Exner
authored andcommitted
added support for timeout and max_num_tries
1 parent cd0d426 commit bc365ff

File tree

9 files changed

+40
-23
lines changed

9 files changed

+40
-23
lines changed

include/ur_client_library/comm/pipeline.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class IProducer
173173
/*!
174174
* \brief Set-up functionality of the producers.
175175
*/
176-
virtual void setupProducer()
176+
virtual void setupProducer(size_t max_num_tries = 0, std::chrono::milliseconds reconnection_time = std::chrono::seconds(10))
177177
{
178178
}
179179
/*!
@@ -287,9 +287,9 @@ class Pipeline
287287
stop();
288288
}
289289

290-
void init()
290+
void init(size_t max_num_tries = 0, std::chrono::milliseconds reconnection_time = std::chrono::seconds(10))
291291
{
292-
producer_.setupProducer();
292+
producer_.setupProducer(max_num_tries, reconnection_time);
293293
if (consumer_ != nullptr)
294294
consumer_->setupConsumer();
295295
}

include/ur_client_library/comm/producer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ class URProducer : public IProducer<T>
6060
/*!
6161
* \brief Triggers the stream to connect to the robot.
6262
*/
63-
void setupProducer() override
63+
void setupProducer(size_t max_num_tries = 0, std::chrono::milliseconds reconnection_time = std::chrono::seconds(10)) override
6464
{
65+
stream_.setReconnectionTime(reconnection_time);
6566
timeval tv;
6667
tv.tv_sec = 1;
6768
tv.tv_usec = 0;
6869
stream_.setReceiveTimeout(tv);
69-
if (!stream_.connect())
70+
if (!stream_.connect(max_num_tries))
7071
{
7172
throw UrException("Failed to connect to robot. Please check if the robot is booted and connected.");
7273
}

include/ur_client_library/comm/stream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class URStream : public TCPSocket
5858
*
5959
* \returns True on success, false if connection could not be established
6060
*/
61-
bool connect()
61+
bool connect(size_t max_num_tries = 0)
6262
{
63-
return TCPSocket::setup(host_, port_);
63+
return TCPSocket::setup(host_, port_, max_num_tries);
6464
}
6565

6666
/*!

include/ur_client_library/comm/tcp_socket.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class TCPSocket
5050
private:
5151
std::atomic<int> socket_fd_;
5252
std::atomic<SocketState> state_;
53-
std::chrono::seconds reconnection_time_;
53+
std::chrono::milliseconds reconnection_time_;
5454

5555
protected:
5656
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
@@ -59,7 +59,7 @@ class TCPSocket
5959
}
6060
virtual void setOptions(int socket_fd);
6161

62-
bool setup(std::string& host, int port);
62+
bool setup(std::string& host, int port, size_t max_num_tries = 0);
6363

6464
std::unique_ptr<timeval> recv_timeout_;
6565

@@ -146,7 +146,7 @@ class TCPSocket
146146
*
147147
* \param reconnection_time time in between connection attempts to the server
148148
*/
149-
void setReconnectionTime(std::chrono::seconds reconnection_time)
149+
void setReconnectionTime(std::chrono::milliseconds reconnection_time)
150150
{
151151
reconnection_time_ = reconnection_time;
152152
}

include/ur_client_library/rtde/rtde_client.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class RTDEClient
125125
*
126126
* \returns Success of the handshake
127127
*/
128-
bool init();
128+
bool init(size_t max_num_tries = 0, std::chrono::milliseconds reconnection_time = std::chrono::seconds(10));
129129
/*!
130130
* \brief Triggers the robot to start sending RTDE data packages in the negotiated format.
131131
*
@@ -228,7 +228,7 @@ class RTDEClient
228228
// the robot is booted.
229229
std::vector<std::string> ensureTimestampIsPresent(const std::vector<std::string>& output_recipe) const;
230230

231-
void setupCommunication();
231+
void setupCommunication(size_t max_num_tries = 0, std::chrono::milliseconds reconnection_time = std::chrono::seconds(10));
232232
bool negotiateProtocolVersion(const uint16_t protocol_version);
233233
void queryURControlVersion();
234234
void setupOutputs(const uint16_t protocol_version);

include/ur_client_library/ur/dashboard_client.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ class DashboardClient : public comm::TCPSocket
5555
DashboardClient() = delete;
5656
virtual ~DashboardClient() = default;
5757

58-
const int DASHBOARD_SERVER_PORT = 29999;
58+
static constexpr int DASHBOARD_SERVER_PORT = 29999;
5959

6060
/*!
6161
* \brief Opens a connection to the dashboard server on the host as specified in the constructor.
6262
*
6363
* \returns True on successful connection, false otherwise.
6464
*/
65-
bool connect();
66-
65+
bool connect(size_t max_num_tries = 0, std::chrono::milliseconds reconnection_time = std::chrono::seconds(10));
66+
6767
/*!
6868
* \brief Makes sure no connection to the dashboard server is held inside the object.
6969
*/

src/comm/tcp_socket.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void TCPSocket::setOptions(int socket_fd)
5555
}
5656
}
5757

58-
bool TCPSocket::setup(std::string& host, int port)
58+
bool TCPSocket::setup(std::string& host, int port, size_t max_num_tries)
5959
{
6060
if (state_ == SocketState::Connected)
6161
return false;
@@ -74,6 +74,7 @@ bool TCPSocket::setup(std::string& host, int port)
7474
hints.ai_socktype = SOCK_STREAM;
7575
hints.ai_flags = AI_PASSIVE;
7676

77+
size_t connect_counter = 0;
7778
bool connected = false;
7879
while (!connected)
7980
{
@@ -96,13 +97,23 @@ bool TCPSocket::setup(std::string& host, int port)
9697

9798
freeaddrinfo(result);
9899

100+
if (max_num_tries > 0)
101+
{
102+
if (connect_counter++ >= max_num_tries)
103+
{
104+
URCL_LOG_ERROR("Failed to establish connection for %s:%d after %d tries", host.c_str(), port, max_num_tries);
105+
state_ = SocketState::Invalid;
106+
return false;
107+
}
108+
}
109+
99110
if (!connected)
100111
{
101112
state_ = SocketState::Invalid;
102113
std::stringstream ss;
103114
ss << "Failed to connect to robot on IP " << host_name
104115
<< ". Please check that the robot is booted and reachable on " << host_name << ". Retrying in "
105-
<< reconnection_time_.count() << " seconds";
116+
<< std::chrono::duration_cast<std::chrono::duration<float>>(reconnection_time_).count() << " seconds";
106117
URCL_LOG_ERROR("%s", ss.str().c_str());
107118
std::this_thread::sleep_for(reconnection_time_);
108119
}

src/rtde/rtde_client.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ RTDEClient::~RTDEClient()
6969
disconnect();
7070
}
7171

72-
bool RTDEClient::init()
72+
bool RTDEClient::init(size_t max_num_tries, std::chrono::milliseconds reconnection_time)
7373
{
7474
if (client_state_ > ClientState::UNINITIALIZED)
7575
{
@@ -79,7 +79,7 @@ bool RTDEClient::init()
7979
unsigned int attempts = 0;
8080
while (attempts < MAX_INITIALIZE_ATTEMPTS)
8181
{
82-
setupCommunication();
82+
setupCommunication(max_num_tries, reconnection_time);
8383
if (client_state_ == ClientState::INITIALIZED)
8484
return true;
8585

@@ -92,11 +92,11 @@ bool RTDEClient::init()
9292
throw UrException(ss.str());
9393
}
9494

95-
void RTDEClient::setupCommunication()
95+
void RTDEClient::setupCommunication(size_t max_num_tries, std::chrono::milliseconds reconnection_time)
9696
{
9797
client_state_ = ClientState::INITIALIZING;
9898
// A running pipeline is needed inside setup
99-
pipeline_.init();
99+
pipeline_.init(max_num_tries, reconnection_time);
100100
pipeline_.run();
101101

102102
uint16_t protocol_version = MAX_RTDE_PROTOCOL_VERSION;

src/ur/dashboard_client.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void DashboardClient::rtrim(std::string& str, const std::string& chars)
4747
str.erase(str.find_last_not_of(chars) + 1);
4848
}
4949

50-
bool DashboardClient::connect()
50+
bool DashboardClient::connect(size_t max_num_tries, std::chrono::milliseconds reconnection_time)
5151
{
5252
if (getState() == comm::SocketState::Connected)
5353
{
@@ -61,17 +61,22 @@ bool DashboardClient::connect()
6161

6262
while (not ret_val)
6363
{
64+
TCPSocket::setReconnectionTime(reconnection_time);
6465
// The first read after connection can take more time.
6566
tv.tv_sec = 10;
6667
tv.tv_usec = 0;
6768
TCPSocket::setReceiveTimeout(tv);
6869
try
6970
{
70-
if (TCPSocket::setup(host_, port_))
71+
if (TCPSocket::setup(host_, port_, max_num_tries))
7172
{
7273
URCL_LOG_INFO("%s", read().c_str());
7374
ret_val = true;
7475
}
76+
else
77+
{
78+
return false;
79+
}
7580
}
7681
catch (const TimeoutException&)
7782
{

0 commit comments

Comments
 (0)