Skip to content

Commit 1d4c0fb

Browse files
author
Felix Exner
committed
Set reconnection_time from setup method
Also deprecated the explicit method for setting up reconnection time.
1 parent b8d6104 commit 1d4c0fb

File tree

7 files changed

+51
-29
lines changed

7 files changed

+51
-29
lines changed

include/ur_client_library/comm/producer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ class URProducer : public IProducer<T>
6767
void setupProducer(const size_t max_num_tries = 0,
6868
const std::chrono::milliseconds reconnection_time = std::chrono::seconds(10)) override
6969
{
70-
stream_.setReconnectionTime(reconnection_time);
7170
timeval tv;
7271
tv.tv_sec = 1;
7372
tv.tv_usec = 0;
7473
stream_.setReceiveTimeout(tv);
75-
if (!stream_.connect(max_num_tries))
74+
if (!stream_.connect(max_num_tries, reconnection_time))
7675
{
7776
throw UrException("Failed to connect to robot. Please check if the robot is booted and connected.");
7877
}

include/ur_client_library/comm/stream.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <sys/socket.h>
2424
#include <sys/types.h>
2525
#include <atomic>
26+
#include <chrono>
2627
#include <mutex>
2728
#include <string>
2829
#include "ur_client_library/log.h"
@@ -58,12 +59,14 @@ class URStream : public TCPSocket
5859
*
5960
* \param max_num_tries Maximum number of connection attempts before counting the connection as
6061
* failed. Unlimited number of attempts when set to 0.
62+
* \param reconnection_time time in between connection attempts to the server
6163
*
6264
* \returns True on success, false if connection could not be established
6365
*/
64-
bool connect(const size_t max_num_tries = 0)
66+
bool connect(const size_t max_num_tries = 0,
67+
const std::chrono::milliseconds reconnection_time = std::chrono::seconds(10))
6568
{
66-
return TCPSocket::setup(host_, port_, max_num_tries);
69+
return TCPSocket::setup(host_, port_, max_num_tries, reconnection_time);
6770
}
6871

6972
/*!

include/ur_client_library/comm/tcp_socket.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <sys/socket.h>
2424
#include <sys/types.h>
2525
#include <atomic>
26+
#include <chrono>
2627
#include <mutex>
2728
#include <string>
2829
#include <memory>
@@ -51,6 +52,7 @@ class TCPSocket
5152
std::atomic<int> socket_fd_;
5253
std::atomic<SocketState> state_;
5354
std::chrono::milliseconds reconnection_time_;
55+
bool reconnection_time_modified_deprecated_ = false;
5456

5557
protected:
5658
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
@@ -59,11 +61,13 @@ class TCPSocket
5961
}
6062
virtual void setOptions(int socket_fd);
6163

62-
bool setup(const std::string& host, const int port, const size_t max_num_tries = 0);
64+
bool setup(const std::string& host, const int port, const size_t max_num_tries = 0,
65+
const std::chrono::milliseconds reconnection_time = DEFAULT_RECONNECTION_TIME);
6366

6467
std::unique_ptr<timeval> recv_timeout_;
6568

6669
public:
70+
static constexpr std::chrono::milliseconds DEFAULT_RECONNECTION_TIME{ 10000 };
6771
/*!
6872
* \brief Creates a TCPSocket object
6973
*/
@@ -146,10 +150,8 @@ class TCPSocket
146150
*
147151
* \param reconnection_time time in between connection attempts to the server
148152
*/
149-
void setReconnectionTime(const std::chrono::milliseconds reconnection_time)
150-
{
151-
reconnection_time_ = reconnection_time;
152-
}
153+
[[deprecated("Reconnection time is passed to setup directly now.")]] void
154+
setReconnectionTime(const std::chrono::milliseconds reconnection_time);
153155
};
154156
} // namespace comm
155157
} // namespace urcl

src/comm/tcp_socket.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <endian.h>
2525
#include <netinet/tcp.h>
2626
#include <unistd.h>
27+
#include <chrono>
2728
#include <cstring>
2829
#include <sstream>
2930
#include <thread>
@@ -55,8 +56,19 @@ void TCPSocket::setOptions(int socket_fd)
5556
}
5657
}
5758

58-
bool TCPSocket::setup(std::string& host, int port, size_t max_num_tries)
59+
bool TCPSocket::setup(const std::string& host, const int port, const size_t max_num_tries,
60+
const std::chrono::milliseconds reconnection_time)
5961
{
62+
// This can be removed once we remove the setReconnectionTime() method
63+
auto reconnection_time_resolved = reconnection_time;
64+
if (reconnection_time_modified_deprecated_)
65+
{
66+
URCL_LOG_WARN("TCPSocket::setup(): Reconnection time was modified using `setReconnectionTime()` which is "
67+
"deprecated. Please change your code to set reconnection_time through the `setup()` method "
68+
"directly. The value passed to this function will be ignored.");
69+
reconnection_time_resolved = reconnection_time_;
70+
}
71+
6072
if (state_ == SocketState::Connected)
6173
return false;
6274

@@ -113,9 +125,9 @@ bool TCPSocket::setup(std::string& host, int port, size_t max_num_tries)
113125
std::stringstream ss;
114126
ss << "Failed to connect to robot on IP " << host_name
115127
<< ". Please check that the robot is booted and reachable on " << host_name << ". Retrying in "
116-
<< std::chrono::duration_cast<std::chrono::duration<float>>(reconnection_time_).count() << " seconds";
128+
<< std::chrono::duration_cast<std::chrono::duration<float>>(reconnection_time_resolved).count() << " seconds";
117129
URCL_LOG_ERROR("%s", ss.str().c_str());
118-
std::this_thread::sleep_for(reconnection_time_);
130+
std::this_thread::sleep_for(reconnection_time_resolved);
119131
}
120132
}
121133
setOptions(socket_fd_);
@@ -221,5 +233,13 @@ void TCPSocket::setReceiveTimeout(const timeval& timeout)
221233
}
222234
}
223235

236+
void TCPSocket::setReconnectionTime(const std::chrono::milliseconds reconnection_time)
237+
{
238+
URCL_LOG_ERROR("Calling setReconnectionTime is deprecated. Reconnection timeout is passed to the setup method "
239+
"directly.");
240+
reconnection_time_ = reconnection_time;
241+
reconnection_time_modified_deprecated_ = true;
242+
}
243+
224244
} // namespace comm
225245
} // namespace urcl

src/ur/dashboard_client.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,13 @@ bool DashboardClient::connect(const size_t max_num_tries, const std::chrono::mil
6161

6262
while (not ret_val)
6363
{
64-
TCPSocket::setReconnectionTime(reconnection_time);
6564
// The first read after connection can take more time.
6665
tv.tv_sec = 10;
6766
tv.tv_usec = 0;
6867
TCPSocket::setReceiveTimeout(tv);
6968
try
7069
{
71-
if (TCPSocket::setup(host_, port_, max_num_tries))
70+
if (TCPSocket::setup(host_, port_, max_num_tries, reconnection_time))
7271
{
7372
URCL_LOG_INFO("%s", read().c_str());
7473
ret_val = true;

tests/test_stream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// -- END LICENSE BLOCK ------------------------------------------------
3030

3131
#include <gtest/gtest.h>
32+
#include <chrono>
3233
#include <condition_variable>
3334

3435
#include <ur_client_library/comm/stream.h>
@@ -323,8 +324,7 @@ TEST_F(StreamTest, write_data_package)
323324
TEST_F(StreamTest, connect_non_connected_robot)
324325
{
325326
comm::URStream<rtde_interface::RTDEPackage> stream("127.0.0.1", 12321);
326-
stream.setReconnectionTime(std::chrono::milliseconds(500));
327-
EXPECT_FALSE(stream.connect(2));
327+
EXPECT_FALSE(stream.connect(2, std::chrono::milliseconds(500)));
328328
}
329329

330330
int main(int argc, char* argv[])

tests/test_tcp_socket.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,23 @@ class TCPSocketTest : public ::testing::Test
103103
class Client : public comm::TCPSocket
104104
{
105105
public:
106-
Client(int port, const std::string& ip = "127.0.0.1", const size_t max_num_tries = 0)
106+
Client(int port, const std::string& ip = "127.0.0.1")
107107
{
108108
port_ = port;
109109
ip_ = ip;
110-
max_num_tries_ = max_num_tries;
111110
}
112111

113-
bool setup()
112+
bool setup(const size_t max_num_tries = 0,
113+
const std::chrono::milliseconds reconnection_time = std::chrono::seconds(10))
114114
{
115-
return TCPSocket::setup(ip_, port_, max_num_tries_);
115+
return TCPSocket::setup(ip_, port_, max_num_tries, reconnection_time);
116116
}
117117

118-
void setupClientBeforeServer()
118+
void setupClientBeforeServer(const size_t max_num_tries = 0,
119+
const std::chrono::milliseconds reconnection_time = std::chrono::seconds(10))
119120
{
120121
done_setting_up_client_ = false;
121-
client_setup_thread_ = std::thread(&Client::setupClient, this, port_);
122+
client_setup_thread_ = std::thread(&Client::setupClient, this, port_, max_num_tries, reconnection_time);
122123
}
123124

124125
bool waitForClientSetupThread()
@@ -149,10 +150,10 @@ class TCPSocketTest : public ::testing::Test
149150
std::thread client_setup_thread_;
150151
int port_;
151152
std::string ip_;
152-
size_t max_num_tries_;
153153
bool done_setting_up_client_;
154154

155-
void setupClient(int port)
155+
void setupClient(int port, const size_t max_num_tries = 0,
156+
std::chrono::milliseconds reconnection_time = std::chrono::seconds(10))
156157
{
157158
std::string ip = "127.0.0.1";
158159
TCPSocket::setup(ip, port);
@@ -205,8 +206,7 @@ TEST_F(TCPSocketTest, setup_client_before_server)
205206
// Make server unavailable
206207
server_.reset();
207208

208-
client_->setReconnectionTime(std::chrono::seconds(1));
209-
client_->setupClientBeforeServer();
209+
client_->setupClientBeforeServer(0, std::chrono::seconds(1));
210210

211211
// Make sure that the client has tried to connect to the server, before creating the server
212212
std::this_thread::sleep_for(std::chrono::seconds(1));
@@ -348,9 +348,8 @@ TEST_F(TCPSocketTest, setup_while_client_is_connected)
348348

349349
TEST_F(TCPSocketTest, connect_non_running_robot)
350350
{
351-
Client client(12321, "127.0.0.1", 2);
352-
client.setReconnectionTime(std::chrono::milliseconds(500));
353-
EXPECT_FALSE(client.setup());
351+
Client client(12321, "127.0.0.1");
352+
EXPECT_FALSE(client.setup(2, std::chrono::milliseconds(500)));
354353
}
355354

356355
int main(int argc, char* argv[])

0 commit comments

Comments
 (0)