From 8cf0d9591cd7f028b221a91cdde8f155a0fe41dc Mon Sep 17 00:00:00 2001 From: jtkenny <jtkenny@iastate.edu> Date: Thu, 19 Oct 2023 00:13:27 +0200 Subject: [PATCH] starting writing code for a socket connection --- cflib_groundstation/groundstation_socket.py | 55 +++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 cflib_groundstation/groundstation_socket.py diff --git a/cflib_groundstation/groundstation_socket.py b/cflib_groundstation/groundstation_socket.py new file mode 100644 index 000000000..657625833 --- /dev/null +++ b/cflib_groundstation/groundstation_socket.py @@ -0,0 +1,55 @@ +import socket +import os +import collections + +# Set the path for the Unix socket +socket_path = './cflib_groundstation.socket' + +class GroundstationSocket(): + metadata = collections.namedtuple("metadata", ["msg_type", "msg_id", "data_len"]) + def groundstation_connect(self): + # remove the socket file if it already exists + try: + os.unlink(socket_path) + except OSError: + if os.path.exists(socket_path): + raise + + # Create the Unix socket server + server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + print("Opened groundstation socket.") + + # Bind the socket to the path + server.bind(socket_path) + + # Listen for incoming connections + server.listen(1) + + # accept connections + print('Server is listening for incoming connections...') + connection, client_address = server.accept() + + try: + print('Connection from', str(connection).split(", ")[0][-4:]) + + # receive data from the client + while True: + data = connection.recv(1000) + if not data: + break + print('Received data:' + data.decode()) + dataarray = bytearray(data) + + # Send a response back to the client + response = 'Hello from the server!' + connection.sendall(response.encode()) + finally: + # close the connection + connection.close() + # remove the socket file + os.unlink(socket_path) + + + def decodePacket(data): + + -- GitLab