|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from typing import List, Any |
| 4 | +import time |
| 5 | +import stat |
| 6 | + |
| 7 | +from manager.manager.launcher.launcher_interface import ILauncher, LauncherException |
| 8 | +from manager.manager.docker_thread.docker_thread import DockerThread |
| 9 | +from manager.manager.vnc.vnc_server import Vnc_server |
| 10 | +from manager.libs.process_utils import ( |
| 11 | + wait_for_process_to_start, |
| 12 | + check_gpu_acceleration, |
| 13 | +) |
| 14 | +import subprocess |
| 15 | + |
| 16 | +import logging |
| 17 | + |
| 18 | +class LauncherO3deApi(ILauncher): |
| 19 | + display: str |
| 20 | + internal_port: int |
| 21 | + external_port: int |
| 22 | + height: int |
| 23 | + width: int |
| 24 | + type: str |
| 25 | + module: str |
| 26 | + launch_file: str |
| 27 | + threads: List[Any] = [] |
| 28 | + gz_vnc: Any = Vnc_server() |
| 29 | + |
| 30 | + def run(self, callback): |
| 31 | + DRI_PATH = self.get_dri_path() |
| 32 | + ACCELERATION_ENABLED = self.check_device(DRI_PATH) |
| 33 | + |
| 34 | + #TODO: add run here |
| 35 | + |
| 36 | + xserver_cmd = f"/usr/bin/Xorg -quiet -noreset +extension GLX +extension RANDR +extension RENDER -logfile ./xdummy.log -config ./xorg.conf :0" |
| 37 | + xserver_thread = DockerThread(xserver_cmd) |
| 38 | + xserver_thread.start() |
| 39 | + self.threads.append(xserver_thread) |
| 40 | + |
| 41 | + LevelSelect=f'echo "LoadLevel Levels/{self.launch_file}" > data/workspace/ROS2Demo/autoexec.cfg' |
| 42 | + |
| 43 | + LevelSelect_thread = DockerThread(LevelSelect) |
| 44 | + LevelSelect_thread.start() |
| 45 | + self.threads.append(LevelSelect_thread) |
| 46 | + |
| 47 | + if ACCELERATION_ENABLED: |
| 48 | + # Starts xserver, x11vnc and novnc |
| 49 | + self.gz_vnc.start_vnc_gpu( |
| 50 | + self.display, self.internal_port, self.external_port, DRI_PATH |
| 51 | + ) |
| 52 | + # Write display config |
| 53 | + o3decmd = f'export DISPLAY={self.display}; data/workspace/ROS2Demo/build/linux/bin/profile/ROS2Demo.GameLauncher --forceAdapter="NVIDIA"' |
| 54 | + else: |
| 55 | + # Starts xserver, x11vnc and novnc |
| 56 | + self.gz_vnc.start_vnc(self.display, self.internal_port, self.external_port) |
| 57 | + # Write display config |
| 58 | + o3decmd = f'export DISPLAY={self.display}; data/workspace/ROS2Demo/build/linux/bin/profile/ROS2Demo.GameLauncher --forceAdapter="NVIDIA"' |
| 59 | + |
| 60 | + gzclient_thread = DockerThread(o3decmd) |
| 61 | + gzclient_thread.start() |
| 62 | + self.threads.append(gzclient_thread) |
| 63 | + |
| 64 | + process_name = 'ROS2Demo.GameLauncher' |
| 65 | + wait_for_process_to_start(process_name, timeout=360) |
| 66 | + |
| 67 | + def terminate(self): |
| 68 | + self.gz_vnc.terminate() |
| 69 | + if self.threads is not None: |
| 70 | + for thread in self.threads: |
| 71 | + if thread.is_alive(): |
| 72 | + thread.terminate() |
| 73 | + thread.join() |
| 74 | + self.threads.remove(thread) |
| 75 | + |
| 76 | + # TODO: processes to kill |
| 77 | + to_kill = ["ROS2Demo.GameLauncher"] |
| 78 | + |
| 79 | + kill_cmd = "pkill -9 -f " |
| 80 | + for i in to_kill: |
| 81 | + cmd = kill_cmd + i |
| 82 | + subprocess.call( |
| 83 | + cmd, |
| 84 | + shell=True, |
| 85 | + stdout=subprocess.PIPE, |
| 86 | + bufsize=1024, |
| 87 | + universal_newlines=True, |
| 88 | + ) |
0 commit comments