Skip to content

Commit 58bac18

Browse files
authored
Merge pull request #87 from nasa/86-client-connection-retry
Client.hh: Added a loop around initial client connection to allow the… closes #86
2 parents 93ef119 + fd9f682 commit 58bac18

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

include/idf/Client.hh

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,36 @@ class Client : public Manageable {
6666

6767
// Establish first available connection.
6868
struct addrinfo *currentHost;
69-
for (currentHost = results; currentHost != NULL; currentHost = currentHost->ai_next) {
70-
if ((socketHandle = socket(currentHost->ai_family,
71-
currentHost->ai_socktype, currentHost->ai_protocol)) == -1) {
72-
continue;
73-
}
74-
75-
if (connect(socketHandle, currentHost->ai_addr, currentHost->ai_addrlen) == 0) {
76-
std::cout << "[Client] Connected to " << serverName << ":" << serverPort << std::endl;
77-
break;
78-
}
79-
80-
::close(socketHandle);
69+
bool connected = false;
70+
71+
for (int ii=0; ii<retryLimit; ++ii) {
72+
for (currentHost = results; currentHost != NULL; currentHost = currentHost->ai_next) {
73+
if ((socketHandle = socket(currentHost->ai_family,
74+
currentHost->ai_socktype, currentHost->ai_protocol)) == -1) {
75+
continue;
76+
}
77+
78+
if (connect(socketHandle, currentHost->ai_addr, currentHost->ai_addrlen) == 0) {
79+
std::cout << "[Client] Connected to " << serverName << ":" << serverPort << std::endl;
80+
connected = true;
81+
break;
82+
}
83+
84+
::close(socketHandle);
85+
86+
}
87+
88+
if (connected) {
89+
break;
90+
} else {
91+
std::cout << "[Client] Connection failed - attempting reconnect to " << serverName << ":" << serverPort << std::endl;
92+
sleep(1);
93+
}
8194
}
8295

8396
freeaddrinfo(results);
8497

85-
if (currentHost == NULL) {
98+
if (not connected) {
8699
stream.str("");
87100
stream << "Failed to connect to " << serverName << ":" << serverPort;
88101
throw IOException(stream.str());
@@ -114,6 +127,15 @@ class Client : public Manageable {
114127
Manageable::close();
115128
}
116129

130+
/**
131+
* sets the retry limit for initial connection
132+
*
133+
* @param limit @copydoc retryLimit
134+
*/
135+
void setRetryLimit(const int limit) {
136+
retryLimit = limit;
137+
}
138+
117139
/**
118140
* sets the host to which this instance will attempt to connect the next
119141
* time open() is called
@@ -150,7 +172,8 @@ class Client : public Manageable {
150172
Client(const T& commandSource, const std::string& host, unsigned short port) :
151173
source(commandSource),
152174
serverName(host),
153-
serverPort(port) {}
175+
serverPort(port),
176+
retryLimit(5) {}
154177

155178
/**
156179
* packs commands from @a source into @a commands
@@ -178,6 +201,9 @@ class Client : public Manageable {
178201
/** the port on which the server is listening */
179202
unsigned short serverPort;
180203

204+
/** Number of times to retry establishing the initial connection */
205+
int retryLimit;
206+
181207
/** the socket */
182208
int socketHandle;
183209

include/idf/Keyboard.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class Keyboard : public virtual InputLayout {
209209
/** , and < */
210210
SingleInput comma;
211211

212-
/** . and > */
212+
/** \. and > */
213213
SingleInput period;
214214

215215
/** / and ? */

include/idf/SerialEr7Orion.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class SerialEr7Orion : public SerialThrustMasterBase, public Er7Orion {
2727
* constructs a new instance whose open() will open the serial port at @a path
2828
*
2929
* @param path @copydoc path
30+
* @param isMale @copydoc male
3031
*/
3132
SerialEr7Orion(const char *path = "/dev/ttyACM0", bool isMale = true);
3233

include/idf/SingleCameraController.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class SingleCameraController : public CameraController {
242242
/**
243243
* creates a new SingleCameraController mapped to @a XBoxOne using appropriate defaults
244244
*
245-
* @param XBoxOne the inputs to use in the default mapping
245+
* @param xBoxOne the inputs to use in the default mapping
246246
*
247247
* @return a new XBoxOne-based camera controller
248248
*/

include/idf/SingleFlightController.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class SingleFlightController : public FlightController {
255255
/**
256256
* creates a new SingleFlightController mapped to @a XBoxOne using appropriate defaults
257257
*
258-
* @param saitekX56Stick the inputs to use in the default mapping
258+
* @param xBoxOne the inputs to use in the default mapping
259259
*
260260
* @return a new XboxOne-based flight controller
261261
*/

include/idf/UsbKeyboard.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class UsbKeyboard : public UsbDevice, public Keyboard {
2828
*
2929
* @param vendor vendor ID
3030
* @param product product ID
31+
* @param interface interface
3132
*/
3233
UsbKeyboard(int vendor, int product, int interface = 0);
3334

0 commit comments

Comments
 (0)