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): ...@@ -43,14 +43,19 @@ class HFOEnvironment(object):
'''Connect to the server that controls the agent on the specified port. ''' '''Connect to the server that controls the agent on the specified port. '''
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print '[Agent Client] Connecting to Agent Server on port', server_port print '[Agent Client] Connecting to Agent Server on port', server_port
while True: retry = 10
while retry > 0:
try: try:
self.socket.connect(('localhost', server_port)) self.socket.connect(('localhost', server_port))
except: except:
time.sleep(1) time.sleep(1)
retry -= 1
continue continue
else: else:
break break
if retry <= 0:
print '[Agent Client] ERROR Unable to communicate with server'
exit(1)
print '[Agent Client] Connected' print '[Agent Client] Connected'
self.handshakeAgentServer(feature_set) self.handshakeAgentServer(feature_set)
# Get the initial state # Get the initial state
......
...@@ -24,8 +24,8 @@ HFOEnvironment::~HFOEnvironment() { ...@@ -24,8 +24,8 @@ HFOEnvironment::~HFOEnvironment() {
close(sockfd); close(sockfd);
} }
void HFOEnvironment::connectToAgentServer(int server_port, void HFOEnvironment::connectToAgentServer(int server_port,
feature_set_t feature_set) { feature_set_t feature_set) {
std::cout << "[Agent Client] Connecting to Agent Server on port " std::cout << "[Agent Client] Connecting to Agent Server on port "
<< server_port << std::endl; << server_port << std::endl;
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
...@@ -45,9 +45,14 @@ HFOEnvironment::~HFOEnvironment() { ...@@ -45,9 +45,14 @@ HFOEnvironment::~HFOEnvironment() {
server->h_length); server->h_length);
serv_addr.sin_port = htons(server_port); serv_addr.sin_port = htons(server_port);
int status = -1; int status = -1;
while (status < 0) { int retry = 10;
while (status < 0 && retry > 0) {
status = connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); status = connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
sleep(1); sleep(1);
retry--;
}
if (status < 0) {
error("[Agent Client] ERROR Unable to communicate with server");
} }
std::cout << "[Agent Client] Connected" << std::endl; std::cout << "[Agent Client] Connected" << std::endl;
handshakeAgentServer(feature_set); 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