Commit 0aaeed5f authored by Matthew Hausknecht's avatar Matthew Hausknecht

Added timeout for connection to server.

parent 0f0a2ce5
......@@ -43,14 +43,19 @@ class HFOEnvironment(object):
'''Connect to the server that controls the agent on the specified port. '''
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print '[Agent Client] Connecting to Agent Server on port', server_port
while True:
retry = 10
while retry > 0:
try:
self.socket.connect(('localhost', server_port))
except:
time.sleep(1)
retry -= 1
continue
else:
break
if retry <= 0:
print '[Agent Client] ERROR Unable to communicate with server'
exit(1)
print '[Agent Client] Connected'
self.handshakeAgentServer(feature_set)
# Get the initial state
......
......@@ -24,7 +24,7 @@ HFOEnvironment::~HFOEnvironment() {
close(sockfd);
}
void HFOEnvironment::connectToAgentServer(int server_port,
void HFOEnvironment::connectToAgentServer(int server_port,
feature_set_t feature_set) {
std::cout << "[Agent Client] Connecting to Agent Server on port "
<< server_port << std::endl;
......@@ -45,9 +45,14 @@ HFOEnvironment::~HFOEnvironment() {
server->h_length);
serv_addr.sin_port = htons(server_port);
int status = -1;
while (status < 0) {
int retry = 10;
while (status < 0 && retry > 0) {
status = connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
sleep(1);
retry--;
}
if (status < 0) {
error("[Agent Client] ERROR Unable to communicate with server");
}
std::cout << "[Agent Client] Connected" << std::endl;
handshakeAgentServer(feature_set);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment