almost at the goal
This commit is contained in:
120
pytmtc/pyserver.py
Executable file
120
pytmtc/pyserver.py
Executable file
@ -0,0 +1,120 @@
|
||||
#!/usr/bin/env python3
|
||||
import socket
|
||||
import abc
|
||||
import select
|
||||
import logging
|
||||
from threading import Thread
|
||||
from collections import deque
|
||||
from multiprocessing import Queue
|
||||
from spacepackets.ccsds.spacepacket import parse_space_packets, PacketId
|
||||
from spacepackets.ecss.tc import PacketType
|
||||
|
||||
EXP_ID = 278
|
||||
EXP_APID = 1024 + EXP_ID
|
||||
EXP_PACKET_ID_TM = PacketId(PacketType.TM, True, EXP_APID)
|
||||
EXP_PACKET_ID_TC = PacketId(PacketType.TC, True, EXP_APID)
|
||||
OPSSAT_SERVER_PORT = 4096
|
||||
TMTC_SERVER_PORT = 4097
|
||||
|
||||
|
||||
TC_QUEUE = Queue()
|
||||
TM_QUEUE = Queue()
|
||||
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def main():
|
||||
logging.basicConfig(
|
||||
format="[%(asctime)s] [%(levelname)-5s] %(message)s",
|
||||
level=logging.INFO,
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
print("Starting OPS-SAT ground TMTC server")
|
||||
ops_sat_thread = OpsSatServer()
|
||||
ops_sat_thread.start()
|
||||
tmtc_thread = TmtcServer()
|
||||
tmtc_thread.start()
|
||||
ops_sat_thread.join()
|
||||
tmtc_thread.join()
|
||||
|
||||
|
||||
class BaseServer(Thread):
|
||||
def __init__(self, log_prefix: str, port: int):
|
||||
self.log_prefix = log_prefix
|
||||
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server_addr = ("0.0.0.0", port)
|
||||
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
self.server_socket.bind(server_addr)
|
||||
super().__init__()
|
||||
|
||||
def run(self) -> None:
|
||||
while True:
|
||||
self.server_socket.listen()
|
||||
(conn_socket, conn_addr) = self.server_socket.accept()
|
||||
# conn_socket.setblocking(True)
|
||||
# conn_socket.settimeout(0.2)
|
||||
print(f"{self.log_prefix} TCP client {conn_addr} connected")
|
||||
analysis_deque = deque()
|
||||
while True:
|
||||
(readable, writable, _) = select([conn_socket], [], [], 0.2)
|
||||
try:
|
||||
bytes_recvd = conn_socket.recv(4096)
|
||||
if len(bytes_recvd) > 0:
|
||||
_LOGGER.debug(f"{self.log_prefix} RX RAW: {bytes_recvd}")
|
||||
analysis_deque.append(bytes_recvd)
|
||||
elif len(bytes_recvd) == 0:
|
||||
self.handle_read_bytestream(analysis_deque)
|
||||
break
|
||||
else:
|
||||
print("error receiving data from TCP client")
|
||||
except TimeoutError:
|
||||
if len(analysis_deque) > 0:
|
||||
self.handle_read_bytestream(analysis_deque)
|
||||
self.send_data_to_client(conn_socket)
|
||||
|
||||
@abc.abstractmethod
|
||||
def handle_read_bytestream(self, analysis_deque: deque):
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
def send_data_to_client(self, conn_socket: socket.socket):
|
||||
pass
|
||||
|
||||
|
||||
class OpsSatServer(BaseServer):
|
||||
def __init__(self):
|
||||
super().__init__("[OPS-SAT]", OPSSAT_SERVER_PORT)
|
||||
|
||||
def handle_read_bytestream(self, analysis_deque: deque):
|
||||
parsed_packets = parse_space_packets(analysis_deque, [EXP_PACKET_ID_TM])
|
||||
for packet in parsed_packets:
|
||||
_LOGGER.info(f"{self.log_prefix} RX TM: [{packet.hex(sep=',')}]")
|
||||
TM_QUEUE.put(packet)
|
||||
|
||||
def send_data_to_client(self, conn_socket: socket.socket):
|
||||
while not TC_QUEUE.empty():
|
||||
next_packet = TC_QUEUE.get()
|
||||
_LOGGER.info(f"{self.log_prefix} TX TC [{next_packet.hex(sep=',')}]")
|
||||
conn_socket.sendall(next_packet)
|
||||
|
||||
|
||||
class TmtcServer(BaseServer):
|
||||
def __init__(self):
|
||||
super().__init__("[TMTC]", TMTC_SERVER_PORT)
|
||||
|
||||
def handle_read_bytestream(self, analysis_deque: deque):
|
||||
parsed_packets = parse_space_packets(analysis_deque, [EXP_PACKET_ID_TC])
|
||||
for packet in parsed_packets:
|
||||
_LOGGER.info(f"{self.log_prefix} RX TC: [{packet.hex(sep=',')}]")
|
||||
TC_QUEUE.put(packet)
|
||||
|
||||
def send_data_to_client(self, conn_socket: socket.socket):
|
||||
while not TM_QUEUE.empty():
|
||||
next_packet = TM_QUEUE.get()
|
||||
_LOGGER.info(f"{self.log_prefix} TX TM [{next_packet.hex(sep=',')}]")
|
||||
conn_socket.sendall(next_packet)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -28,9 +28,9 @@ class TcpServer(ComInterface):
|
||||
self._server_socket: Optional[socket.socket] = None
|
||||
self._server_thread = Thread(target=self._server_task, daemon=True)
|
||||
self._connected = False
|
||||
self._conn_start = None
|
||||
self._writing_done = False
|
||||
self._reading_done = False
|
||||
# self._conn_start = None
|
||||
# self._writing_done = False
|
||||
# self._reading_done = False
|
||||
|
||||
@property
|
||||
def connected(self) -> bool:
|
||||
@ -66,55 +66,57 @@ class TcpServer(ComInterface):
|
||||
while True and not self._kill_signal.is_set():
|
||||
try:
|
||||
(conn_socket, conn_addr) = self._server_socket.accept()
|
||||
self.conn_start = time.time()
|
||||
while True:
|
||||
self._handle_connection(conn_socket, conn_addr)
|
||||
if (
|
||||
self._reading_done and self._writing_done
|
||||
) or time.time() - self.conn_start > 0.5:
|
||||
print("reading and writing done")
|
||||
break
|
||||
self._handle_connection(conn_socket, conn_addr)
|
||||
# conn_socket.close()
|
||||
"""
|
||||
if (
|
||||
self._reading_done and self._writing_done
|
||||
) or time.time() - self.conn_start > 0.5:
|
||||
print("reading and writing done")
|
||||
break
|
||||
|
||||
"""
|
||||
except TimeoutError:
|
||||
print("timeout error")
|
||||
continue
|
||||
|
||||
def _handle_connection(self, conn_socket: socket.socket, conn_addr: Any):
|
||||
_LOGGER.info(f"TCP client {conn_addr} connected")
|
||||
(readable, writable, _) = select.select(
|
||||
[conn_socket],
|
||||
[conn_socket],
|
||||
[],
|
||||
0.1,
|
||||
)
|
||||
queue_len = 0
|
||||
|
||||
# TODO: Why is the stupid conn socket never readable?
|
||||
print(f"Writable: {writable}")
|
||||
print(f"Readable: {readable}")
|
||||
if writable and writable[0]:
|
||||
queue_len = 0
|
||||
while True:
|
||||
with self._tc_lock:
|
||||
queue_len = len(self._tc_packet_queue)
|
||||
while queue_len > 0:
|
||||
next_packet = bytes()
|
||||
with self._tc_lock:
|
||||
next_packet = self._tc_packet_queue.popleft()
|
||||
if len(next_packet) > 0:
|
||||
conn_socket.sendall(next_packet)
|
||||
queue_len -= 1
|
||||
self._writing_done = True
|
||||
if readable and readable[0]:
|
||||
print("reading shit")
|
||||
while True:
|
||||
bytes_recvd = conn_socket.recv(4096)
|
||||
if len(bytes_recvd) > 0:
|
||||
print(f"Received bytes from TCP client: {bytes_recvd.decode()}")
|
||||
with self._tm_lock:
|
||||
self._tm_packet_queue.append(bytes_recvd)
|
||||
elif len(bytes_recvd) == 0:
|
||||
self._reading_done = True
|
||||
break
|
||||
else:
|
||||
print("error receiving data from TCP client")
|
||||
outputs = []
|
||||
if queue_len > 0:
|
||||
outputs.append(conn_socket)
|
||||
(readable, writable, _) = select.select(
|
||||
[conn_socket],
|
||||
outputs,
|
||||
[],
|
||||
0.2,
|
||||
)
|
||||
|
||||
if writable and writable[0]:
|
||||
print("writeable")
|
||||
while queue_len > 0:
|
||||
next_packet = bytes()
|
||||
with self._tc_lock:
|
||||
next_packet = self._tc_packet_queue.popleft()
|
||||
if len(next_packet) > 0:
|
||||
conn_socket.sendall(next_packet)
|
||||
queue_len -= 1
|
||||
if readable and readable[0]:
|
||||
print("readable")
|
||||
while True:
|
||||
bytes_recvd = conn_socket.recv(4096)
|
||||
if len(bytes_recvd) > 0:
|
||||
print(f"Received bytes from TCP client: {bytes_recvd.decode()}")
|
||||
with self._tm_lock:
|
||||
self._tm_packet_queue.append(bytes_recvd)
|
||||
elif len(bytes_recvd) == 0:
|
||||
break
|
||||
else:
|
||||
print("error receiving data from TCP client")
|
||||
|
||||
def is_open(self) -> bool:
|
||||
"""Can be used to check whether the communication interface is open. This is useful if
|
||||
|
@ -1,39 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
import socket
|
||||
from spacepackets.ecss.tc import PusTelecommand
|
||||
|
||||
|
||||
EXP_ID = 278
|
||||
APID = 1024 + EXP_ID
|
||||
SEND_PING_ONCE = True
|
||||
|
||||
|
||||
def main():
|
||||
global SEND_PING_ONCE
|
||||
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server_addr = ("localhost", 4096)
|
||||
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
server_socket.bind(server_addr)
|
||||
while True:
|
||||
server_socket.listen()
|
||||
(conn_socket, conn_addr) = server_socket.accept()
|
||||
print(f"TCP client {conn_addr} connected")
|
||||
while True:
|
||||
bytes_recvd = conn_socket.recv(4096)
|
||||
if len(bytes_recvd) > 0:
|
||||
print(f"Received bytes from TCP client: {bytes_recvd}")
|
||||
elif len(bytes_recvd) == 0:
|
||||
break
|
||||
else:
|
||||
print("error receiving data from TCP client")
|
||||
if SEND_PING_ONCE:
|
||||
ping_tc = PusTelecommand(service=17, subservice=1, seq_count=0, apid=APID)
|
||||
conn_socket.sendall(ping_tc.pack())
|
||||
SEND_PING_ONCE = False
|
||||
conn_socket.close()
|
||||
continue
|
||||
# server_socket.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user