Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions external-control-backend/src/request_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ def send_command(self, command: str):
Exception: If the connection to the remote PC could not be established or no data is received.
"""
program = ""
timeout = 5
connection_timeout = 5
receive_timeout = 1.0
receive_timeout_overall = 5
Comment thread
urrsk marked this conversation as resolved.
try:
# Create a socket connection with the robot IP and port number defined above
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.1)
s.connect((self.robotIP, self.port))
s.settimeout(connection_timeout)
try:
s.connect((self.robotIP, self.port))
except socket.timeout:
raise Exception(f"Connection timeout")
s.sendall(command.encode('us-ascii'))
s.settimeout(receive_timeout)
Comment thread
cursor[bot] marked this conversation as resolved.
# Receive script code
raw_data = b""
begin = time.time()
Expand All @@ -73,13 +79,13 @@ def send_command(self, command: str):
if raw_data != b"":
print("Done receiving data")
break
elif time.time() - begin > timeout:
elif time.time() - begin > receive_timeout_overall:
s.close()
raise Exception(f"Connection timeout")
Comment thread
cursor[bot] marked this conversation as resolved.
raise socket.timeout(f"Timeout while receiving data")
Comment thread
cursor[bot] marked this conversation as resolved.
program = raw_data.decode("us-ascii")
s.close()
if not bool(program and program.strip()):
raise Exception(f"Did not receive any script lines")
return program
except Exception as e:
raise Exception(f"Connectivity problem with {self.robotIP}:{self.port}: {e}")
raise Exception(f"Connectivity problem with {self.robotIP}:{self.port}: {e}")
2 changes: 1 addition & 1 deletion external-control-backend/src/simple_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def store_in_cache(cache_key, now, json_str, valid):
if valid:
program_cache[cache_key] = (now, json_str)

@app.route('/<int:port>/<robotIP>/', methods=["GET"])
@app.route('/<int:port>/<robotIP>/', methods=["GET"], strict_slashes=False )
def read_params(port, robotIP):
logger.info(f"Received request for port {port} and robot IP {robotIP}")
cache_key = (port, robotIP)
Expand Down
2 changes: 1 addition & 1 deletion external-control-backend/tests/test_request_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_send_command_timeout(monkeypatch):
rp = RequestProgram(1234, '127.0.0.1')
with pytest.raises(Exception) as exc:
rp.send_command('request_program\n')
assert 'Connectivity problem with 127.0.0.1:1234: Connection timeout' in str(exc.value)
assert 'Connectivity problem with 127.0.0.1:1234: Timeout while receiving data' in str(exc.value)
assert dummy.closed

def test_send_command_connect_error(monkeypatch):
Expand Down
Loading