From 504b749c3acb3b8dc06f9166c04ccff4ea584d67 Mon Sep 17 00:00:00 2001 From: 0xkkl <99228006+0xkkl@users.noreply.github.com> Date: Sat, 9 Nov 2024 17:11:05 -0800 Subject: [PATCH 1/2] Update CF-Handler.py --- CF-Handler.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/CF-Handler.py b/CF-Handler.py index 7d89d5e..5359bd0 100644 --- a/CF-Handler.py +++ b/CF-Handler.py @@ -1,20 +1,75 @@ import subprocess import time +import threading +import sys + +def send_stats_command(process): + """Periodically sends /stats command to the process""" + while process.poll() is None: + try: + process.stdin.write(b"/stats\n") + process.stdin.flush() + except: + break + time.sleep(1200) + +def handle_user_input(process): + """Handles user input and sends it to the process""" + while process.poll() is None: + try: + user_input = input("> ") + if user_input: + process.stdin.write(f"{user_input}\n".encode()) + process.stdin.flush() + except EOFError: + break + except Exception as e: + print(f"Input error: {e}") def run_and_monitor(): try: - # Start the child process print("Starting catflipper-linux...") - process = subprocess.Popen(["./catflipper-linux"]) - process.wait() + process = subprocess.Popen( + ["./catflipper-linux"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + universal_newlines=False + ) + stats_thread = threading.Thread( + target=send_stats_command, + args=(process,), + daemon=True + ) + stats_thread.start() + input_thread = threading.Thread( + target=handle_user_input, + args=(process,), + daemon=True + ) + input_thread.start() + def print_output(): + while process.poll() is None: + line = process.stdout.readline() + if line: + sys.stdout.buffer.write(line) + sys.stdout.buffer.flush() - print("catflipper-linux crashed. Restarting...") + output_thread = threading.Thread( + target=print_output, + daemon=True + ) + output_thread.start() + process.wait() + print("\ncatflipper-linux crashed. Restarting...") time.sleep(2) run_and_monitor() except KeyboardInterrupt: - print("Process monitor interrupted by user.") - process.terminate() # ensure process cleanup on exit + print("\nProcess monitor interrupted by user.") + if process: + process.terminate() + except Exception as e: print(f"Error occurred: {e}") time.sleep(2) From 3a239bf60dd8c3e670653555f548600b30dcd8ea Mon Sep 17 00:00:00 2001 From: 0xkkl <99228006+0xkkl@users.noreply.github.com> Date: Sat, 9 Nov 2024 17:13:27 -0800 Subject: [PATCH 2/2] Update CF-Handler.py --- CF-Handler.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/CF-Handler.py b/CF-Handler.py index 5359bd0..d3b821b 100644 --- a/CF-Handler.py +++ b/CF-Handler.py @@ -3,17 +3,16 @@ import threading import sys -def send_stats_command(process): - """Periodically sends /stats command to the process""" +def autostats(process): while process.poll() is None: try: process.stdin.write(b"/stats\n") process.stdin.flush() except: break - time.sleep(1200) + time.sleep(3600) -def handle_user_input(process): +def user_input(process): """Handles user input and sends it to the process""" while process.poll() is None: try: @@ -37,18 +36,18 @@ def run_and_monitor(): universal_newlines=False ) stats_thread = threading.Thread( - target=send_stats_command, + target=autostats, args=(process,), daemon=True ) stats_thread.start() input_thread = threading.Thread( - target=handle_user_input, + target=user_input, args=(process,), daemon=True ) input_thread.start() - def print_output(): + def output(): while process.poll() is None: line = process.stdout.readline() if line: @@ -56,7 +55,7 @@ def print_output(): sys.stdout.buffer.flush() output_thread = threading.Thread( - target=print_output, + target=output, daemon=True ) output_thread.start()