| 11 | PORTS = [876, 8765, 5003, 5005] |
| 12 | |
| 13 | class TermNetLauncher: |
| 14 | def __init__(self): |
| 15 | self.root_dir = Path(__file__).parent.absolute() |
| 16 | self.backend_process = None |
| 17 | self.extension_processes = [] |
| 18 | self.ui_process = None |
| 19 | |
| 20 | def cleanup(self, signum=None, frame=None): |
| 21 | """Clean up all processes""" |
| 22 | print("\nShutting down TermNet...") |
| 23 | |
| 24 | if self.ui_process and hasattr(self.ui_process, "poll") and self.ui_process.poll() is None: |
| 25 | self.ui_process.terminate() |
| 26 | |
| 27 | if self.backend_process and self.backend_process.poll() is None: |
| 28 | self.backend_process.terminate() |
| 29 | |
| 30 | for proc in self.extension_processes: |
| 31 | if proc.poll() is None: |
| 32 | proc.terminate() |
| 33 | |
| 34 | time.sleep(1) |
| 35 | |
| 36 | if self.ui_process and hasattr(self.ui_process, "poll") and self.ui_process.poll() is None: |
| 37 | self.ui_process.kill() |
| 38 | if self.backend_process and self.backend_process.poll() is None: |
| 39 | self.backend_process.kill() |
| 40 | for proc in self.extension_processes: |
| 41 | if proc.poll() is None: |
| 42 | proc.kill() |
| 43 | |
| 44 | sys.exit(0) |
| 45 | |
| 46 | def free_ports(self): |
| 47 | """Kill any process already listening on TermNet ports""" |
| 48 | try: |
| 49 | subprocess.run( |
| 50 | ["bash", "-c", |
| 51 | f"sudo lsof -t -iTCP:{','.join(map(str, PORTS))} -sTCP:LISTEN | xargs -r sudo kill -9"], |
| 52 | check=False |
| 53 | ) |
| 54 | except Exception as e: |
| 55 | print(f"Warning: could not auto-kill old processes: {e}") |
| 56 | |
| 57 | def start_backend(self): |
| 58 | """Start the main backend""" |
| 59 | backend_dir = self.root_dir / "backend" |
| 60 | if not backend_dir.exists(): |
| 61 | print(f"Error: Backend directory not found: {backend_dir}") |
| 62 | return False |
| 63 | |
| 64 | print("Starting TermNet backend...") |
| 65 | try: |
| 66 | # show logs instead of swallowing them |
| 67 | self.backend_process = subprocess.Popen( |
| 68 | [sys.executable, "-m", "termnet.main"], |
| 69 | cwd=backend_dir |
| 70 | ) |