diff options
author | AL-LCL <alvin@alvinhavel.com> | 2023-05-19 10:39:49 +0200 |
---|---|---|
committer | AL-LCL <alvin@alvinhavel.com> | 2023-05-19 10:39:49 +0200 |
commit | 58ebd3bc0f00c532e97e9a5571471ffab87934ba (patch) | |
tree | 6e099e59af07206df6edf2b0c585d0c5a466d4bd /server/controller.py |
Diffstat (limited to 'server/controller.py')
-rw-r--r-- | server/controller.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/server/controller.py b/server/controller.py new file mode 100644 index 0000000..273f33f --- /dev/null +++ b/server/controller.py @@ -0,0 +1,102 @@ +''' + A intermediary class for general purpose + flow control operations throughout the + program. + + Verified: 2021 February 7 + * Follows PEP8 + * Tested Platforms + * Windows 10 + * Third Party Modules + * eel +''' + +from server.state import ServerStatic, Dynamic +from server.console import Console + +import socket +import time + +if not ServerStatic.TERMINAL: + import eel + + +class Controller: + + @staticmethod + def message(ret_msg, *args, **kwargs): + Dynamic.MESSAGES.append((ret_msg, args, kwargs)) + + # NOTE : Calling this method with ret_msg + # enabled at the same time twice will cause + # erroneous output & should only be called + # from the GUI, as it requires return data + if ret_msg: + try: + while Dynamic.RESULT is None: + time.sleep(.1) + else: + return Dynamic.RESULT + finally: + Dynamic.RESULT = None + + @staticmethod + def exit_program(): + if Dynamic.CLIENTS: + for unique_id in Dynamic.CLIENTS.copy(): + Controller.delete_client(unique_id, False) + + raise SystemExit + + @staticmethod + def add_session(unique_id, allow_raise=True): + try: + assert unique_id in Dynamic.CLIENTS, 'Client Not Found' + assert unique_id not in Dynamic.SESSION, \ + 'Client Already In Session' + + Dynamic.SESSION.add(unique_id) + + if not ServerStatic.TERMINAL: + if eel._websockets != []: + eel.sessionAddEel(unique_id) + except Exception: + if allow_raise: + raise + + @staticmethod + def remove_session(unique_id, allow_raise=True): + try: + Dynamic.SESSION.remove(unique_id) + + if not ServerStatic.TERMINAL: + if eel._websockets != []: + eel.sessionRemoveEel(unique_id) + except Exception: + if allow_raise: + raise + + @staticmethod + def delete_client(unique_id, allow_raise=True): + try: + client = Dynamic.CLIENTS[unique_id] + del Dynamic.CLIENTS[unique_id] + + try: + client.socket.shutdown(socket.SHUT_RDWR) + except Exception: + pass + + client.socket.close() + Controller.remove_session(unique_id, False) + + if not ServerStatic.TERMINAL: + if eel._websockets != []: + eel.clientRemoveEel(unique_id) + + Console.log('Disconnected Client [{} {} {}]\n'.format( + client.connect_ip.pure, ServerStatic.SEPERATOR, + unique_id), log_type=ServerStatic.ACTION) + except Exception: + if allow_raise: + raise |