Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #343, server now process chunks of bytes #346

Merged
merged 2 commits into from
Jan 21, 2024
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
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exclude_lines =
self.fail\(.*\)
except KeyboardInterrupt:
except ImportError:
pragma: no cover
skip coverage
include =
./*
omit =
Expand Down
6 changes: 3 additions & 3 deletions simulators/acu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ def __init__(self, sampling_time=default_sampling_time):
self.command_threads = Queue()

self.status = Array(c_char, 813)
self.status[0:4] = bytes(start_flag, 'raw_unicode_escape')
self.status[0:4] = bytes(start_flag, 'latin-1')
self.status[4:8] = utils.uint_to_bytes(813)
self.status[-4:] = bytes(end_flag, 'raw_unicode_escape')
self.status[-4:] = bytes(end_flag, 'latin-1')

subsystems = []
subsystems.append(self.PS.update_status)
Expand Down Expand Up @@ -137,7 +137,7 @@ def system_stop(self):
command_thread.join()
except Empty:
break
return '$server_shutdown%%%%%'
return super().system_stop()

def _set_default(self):
"""This method resets the received command string to its default value.
Expand Down
2 changes: 1 addition & 1 deletion simulators/acu/axis_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,7 @@ def _validate_mode_command(self, mode_id, parameter_1, parameter_2):
elif mode_id in [3, 4, 5, 7, 8, 52] and axis_state != 3:
received_command_answer = 4
elif mode_id == 15 and axis_state not in [0, 1]:
received_command_answer = 4 # pragma: no cover
received_command_answer = 4 # skip coverage
elif mode_id == 50:
if not self.stowPosOk:
received_command_answer = 4
Expand Down
4 changes: 2 additions & 2 deletions simulators/acu/pointing_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from datetime import datetime, timedelta
try:
import numpy as np
except ImportError as ex: # pragma: no cover
except ImportError as ex: # skip coverage
raise ImportError('The `numpy` package, required for the simulator'
+ ' to run, is missing!') from ex
try:
from scipy import interpolate
except ImportError as ex: # pragma: no cover
except ImportError as ex: # skip coverage
raise ImportError('The `scipy` package, required for the simulator'
+ ' to run, is missing!') from ex
from simulators import utils
Expand Down
5 changes: 1 addition & 4 deletions simulators/gaia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ def _execute(self, msg):

def _error(self, error_code):
error_string = self.errors.get(error_code)
hex_string = codecs.encode(
error_string.encode('raw_unicode_escape'),
'hex'
)
hex_string = codecs.encode(error_string.encode('latin-1'), 'hex')
retval = f'{self.header}ERROR({error_code})[{error_string}]'
retval += f'({hex_string}) {self.cmd_id}{self.tail}'
return retval
Expand Down
8 changes: 4 additions & 4 deletions simulators/minor_servos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import os
try:
from numpy import sign
except ImportError as ex: # pragma: no cover
except ImportError as ex: # skip coverage
raise ImportError('The `numpy` package, required for the simulator'
+ ' to run, is missing!') from ex
try:
from scipy.interpolate import splrep, splev
except ImportError as ex: # pragma: no cover
except ImportError as ex: # skip coverage
raise ImportError('The `scipy` package, required for the simulator'
+ ' to run, is missing!') from ex
from ctypes import c_bool, c_int
Expand Down Expand Up @@ -84,7 +84,7 @@ def __init__(self):
self.control = 1
self.power = 1
self.emergency = 2
self.gregorian_cap = Value(c_int, 0)
self.gregorian_cap = Value(c_int, 1)
self.last_executed_command = 0
self.servos = {
'PFP': PFP(),
Expand Down Expand Up @@ -118,7 +118,7 @@ def system_stop(self):
self.cover_timer.join()
except RuntimeError:
pass
return '$server_shutdown%%%%%'
return super().system_stop()

@staticmethod
def _update(stop, servos):
Expand Down
2 changes: 1 addition & 1 deletion simulators/mscu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __del__(self):
def system_stop(self):
for servo in self.servos.values():
servo.stop()
return '$server_shutdown%%%%%'
return super().system_stop()

def system_setpos_NAK(self):
self.setpos_NAK[1].value = True # The SRP address
Expand Down
27 changes: 12 additions & 15 deletions simulators/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ def _execute_custom_command(self, msg_body):
else:
params = ()
try:
method = getattr(self.system, name)
response = method(*params)
response = getattr(self.system, name)(*params)
if isinstance(response, str):
self.socket.sendto(
response.encode('raw_unicode_escape'),
response.encode('latin-1'),
self.client_address
)
if response == '$server_shutdown%%%%%':
Expand All @@ -83,7 +82,7 @@ def setup(self):
greet_msg = self.system.system_greet()
if greet_msg:
self.socket.sendto(
greet_msg.encode('raw_unicode_escape'),
greet_msg.encode('latin-1'),
self.client_address
)
else: # UDP client
Expand All @@ -101,16 +100,14 @@ def handle(self):
scenario (i.e. some error condition)."""
if not self.connection_oriented: # UDP client
msg, self.socket = self.socket
msg = msg.decode('raw_unicode_escape')
msg += '\n'
msg += b'\n'
self._handle(msg)
else: # TCP client
while True:
try:
msg = self.socket.recv(1)
msg = self.socket.recv(1024)
if not msg:
break
msg = msg.decode('raw_unicode_escape')
self._handle(msg)
except IOError:
break
Expand All @@ -126,6 +123,7 @@ def _handle(self, msg):
"""
response = None
for byte in msg:
byte = chr(byte)
try:
response = self.system.parse(byte)
except ValueError as ex:
Expand All @@ -136,9 +134,9 @@ def _handle(self, msg):
pass
elif response and isinstance(response, str):
try:
response = response.encode('raw_unicode_escape')
response = response.encode('latin-1')
self.socket.sendto(response, self.client_address)
except IOError: # pragma: no cover
except IOError: # skip coverage
# Something went wrong while sending the response,
# probably the client was stopped without closing
# the connection
Expand Down Expand Up @@ -178,14 +176,14 @@ def handle(self):
while True:
try:
if msg:
custom_msg = msg.decode('raw_unicode_escape')
custom_msg = msg
msg = None
else:
custom_msg = self.socket.recv(1024)
custom_msg = custom_msg.decode('raw_unicode_escape')
# Check if the client is sending a custom command
if not custom_msg:
break
custom_msg = custom_msg.decode('latin-1')
if (
custom_msg.startswith(self.custom_header)
and custom_msg.endswith(self.custom_tail)
Expand Down Expand Up @@ -387,15 +385,14 @@ def _send_stop(entry):
pass
sockobj.sendto(b'$system_stop%%%%%', address)
response = sockobj.recv(1024)
response = response.decode('raw_unicode_escape')
if response != '$server_shutdown%%%%%': # pragma: no cover
if response != b'$server_shutdown%%%%%': # skip coverage
logging.warning(
'%s %s %s',
'The server did not answer with the',
'$server_shutdown%%%%% string!',
'The simulator might still be running!'
)
except Exception as ex: # pragma: no cover
except Exception as ex: # skip coverage
logging.debug(ex)
finally:
sockobj.close()
Expand Down
2 changes: 1 addition & 1 deletion simulators/totalpower/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def _get_status(self, ascii_format=False):
status += '111'
status = binary_to_string(status)
if not ascii_format:
return status.encode('raw_unicode_escape')
return status.encode('latin-1')
else:
return ''.join([hex(ord(c))[-2:] for c in status[::-1]])

Expand Down
16 changes: 8 additions & 8 deletions simulators/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def binary_to_string(binary_string, little_endian=True):
return binary_to_bytes(
binary_string,
little_endian
).decode('raw_unicode_escape')
).decode('latin-1')


def bytes_to_int(byte_string, little_endian=True):
Expand Down Expand Up @@ -235,7 +235,7 @@ def string_to_int(string, little_endian=True):
>>> string_to_int('hello', False)
448378203247
"""
return bytes_to_int(string.encode('raw_unicode_escape'), little_endian)
return bytes_to_int(string.encode('latin-1'), little_endian)


def bytes_to_binary(byte_string, little_endian=True):
Expand Down Expand Up @@ -277,7 +277,7 @@ def string_to_binary(string, little_endian=True):
>>> string_to_binary('hi', little_endian=False)
'0110100001101001'
"""
return bytes_to_binary(string.encode('raw_unicode_escape'), little_endian)
return bytes_to_binary(string.encode('latin-1'), little_endian)


def bytes_to_uint(byte_string, little_endian=True):
Expand Down Expand Up @@ -311,7 +311,7 @@ def string_to_uint(string, little_endian=True):
>>> string_to_uint('hi', little_endian=False)
26729
"""
return bytes_to_uint(string.encode('raw_unicode_escape'), little_endian)
return bytes_to_uint(string.encode('latin-1'), little_endian)


def real_to_binary(num, precision=1):
Expand Down Expand Up @@ -403,7 +403,7 @@ def real_to_string(num, precision=1, little_endian=True):

binary_number = real_to_binary(num, precision)
binary_number = binary_to_bytes(binary_number, little_endian=little_endian)
return binary_number.decode('raw_unicode_escape')
return binary_number.decode('latin-1')


def bytes_to_real(bytes_real, precision=1, little_endian=True):
Expand Down Expand Up @@ -459,7 +459,7 @@ def string_to_real(string_real, precision=1, little_endian=True):
>>> round(string_to_real('\x40\x7A\x25\x7D\x2E\x68\x51\x5D', 2, False), 2)
418.34
"""
bytes_real = bytes(string_real, 'raw_unicode_escape')
bytes_real = bytes(string_real, 'latin-1')
return bytes_to_real(bytes_real, precision, little_endian)


Expand Down Expand Up @@ -505,7 +505,7 @@ def int_to_string(val, n_bytes=4, little_endian=True):
val,
n_bytes,
little_endian
).decode('raw_unicode_escape')
).decode('latin-1')


def uint_to_bytes(val, n_bytes=4, little_endian=True):
Expand Down Expand Up @@ -558,7 +558,7 @@ def uint_to_string(val, n_bytes=4, little_endian=True):
val,
n_bytes,
little_endian
).decode('raw_unicode_escape')
).decode('latin-1')


def sign(number):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_acu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2215,8 +2215,8 @@ class TestACUSimulator(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.start_flag = '\x1A\xCF\xFC\x1D'
cls.end_flag = '\xD1\xCF\xFC\xA1'
cls.start_flag = b'\x1A\xCF\xFC\x1D'
cls.end_flag = b'\xD1\xCF\xFC\xA1'
cls.simulator = Simulator('acu')
cls.simulator.start(daemon=True)

Expand All @@ -2230,7 +2230,6 @@ def test_different_statuses(self):
prev = ''
for _ in range(5):
status = s.recv(1024)
status = status.decode('raw_unicode_escape')
self.assertEqual(len(status), 813)
self.assertEqual(status[0:4], self.start_flag)
self.assertEqual(status[-4:], self.end_flag)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_minor_servos.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_status(self):
regex += r'CONTROL=1\|'
regex += r'POWER=1\|'
regex += r'EMERGENCY=2\|'
regex += r'GREGORIAN_CAP=0\|'
regex += r'GREGORIAN_CAP=1\|'
regex += r'LAST_EXECUTED_COMMAND=0'
regex += fr'{tail}$'
for byte in cmd[:-1]:
Expand Down
Loading