diff --git a/cflib_groundstation/groundstation_socket.py b/cflib_groundstation/groundstation_socket.py new file mode 100644 index 0000000000000000000000000000000000000000..65762583344b62036e128c5c09d79e546d901dba --- /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): + +