From 7e961a49bc66dfe6a4625beafda985011ef58ce9 Mon Sep 17 00:00:00 2001
From: "Robin.Mueller" <robin.mueller.m@gmail.com>
Date: Sat, 31 Oct 2020 13:46:28 +0100
Subject: [PATCH] quick fix, choice between what works

and what should work..
---
 core/tmtc_backend.py     | 25 ++++++++++++++++++++-----
 core/tmtc_client_core.py | 33 +++++++++++++++++++++------------
 core/tmtc_frontend.py    |  2 +-
 3 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/core/tmtc_backend.py b/core/tmtc_backend.py
index 322e7a1..1f80b52 100644
--- a/core/tmtc_backend.py
+++ b/core/tmtc_backend.py
@@ -2,7 +2,7 @@ import atexit
 import time
 import logging
 import sys
-from multiprocessing import Process
+from multiprocessing import Process, Value
 from collections import deque
 from config import obsw_config as g
 from config.obsw_definitions import ModeList
@@ -50,8 +50,21 @@ class TmTcHandler:
             LOGGER.info("TM listener will not be started")
         atexit.register(keyboard_interrupt_handler, com_interface=self.communication_interface)
 
-    def prepare_start(self) -> Process:
-        return Process(target=self.perform_operation)
+    @staticmethod
+    def prepare_tmtc_handler_start_in_process(init_mode: ModeList):
+        tmtc_handler_task = Process(target=TmTcHandler.start_tmtc_handler, args=(init_mode, ))
+        return tmtc_handler_task
+
+    @staticmethod
+    def start_tmtc_handler(handler_args: any):
+        tmtc_handler = TmTcHandler(handler_args)
+        tmtc_handler.perform_operation()
+
+    @staticmethod
+    def prepare_tmtc_handler_start(init_mode: ModeList = g.ModeList.ListenerMode):
+        tmtc_handler = TmTcHandler(init_mode)
+        tmtc_task = Process(target=start_tmtc_handler, args=(tmtc_handler, ))
+        return tmtc_handler, tmtc_task
 
     def perform_operation(self):
         """
@@ -96,7 +109,6 @@ class TmTcHandler:
             else:
                 LOGGER.info("send package from gui")
                 pus_packet_tuple = self.single_command_package
-
             sender_and_receiver = SingleCommandSenderReceiver(
                 com_interface=self.communication_interface, tmtc_printer=self.tmtc_printer,
                 tm_listener=self.tm_listener)
@@ -174,4 +186,7 @@ def command_preparation() -> Tuple[bytearray, Union[None, PusTcInfo]]:
     Prepare command for single command testing
     :return:
     """
-    return command_preparation_hook()
\ No newline at end of file
+    return command_preparation_hook()
+
+def start_tmtc_handler(executing_task: TmTcHandler):
+    executing_task.perform_operation()
\ No newline at end of file
diff --git a/core/tmtc_client_core.py b/core/tmtc_client_core.py
index 70dcffe..634c82f 100755
--- a/core/tmtc_client_core.py
+++ b/core/tmtc_client_core.py
@@ -51,22 +51,31 @@ def run_tmtc_client(use_gui: bool):
         set_globals(args)
 
     LOGGER.info("Starting TMTC Handler")
-    if use_gui:
-        tmtc_handler = TmTcHandler()
+
+    # Experimental. Right now, creating the class outside of the process leads to issues..
+    create_task_in_process = False
+
+    tmtc_handler = TmTcHandler
+    tmtc_frontend_task = Process
+
+    if create_task_in_process:
+        # Currently does not work, problems with QEMU / Asyncio
+        if not use_gui:
+            tmtc_handler, tmtc_handler_task = TmTcHandler.prepare_tmtc_handler_start(g.G_MODE_ID)
+        else:
+            tmtc_handler, tmtc_handler_task = TmTcHandler.prepare_tmtc_handler_start()
     else:
-        tmtc_handler = TmTcHandler(g.G_MODE_ID)
+        tmtc_handler_task = TmTcHandler.prepare_tmtc_handler_start_in_process(g.G_MODE_ID)
 
-    # tmtc_handler_task = tmtc_handler.prepare_start()
-    tmtc_frontend_task = Process()
 
-    if use_gui:
-        tmtc_frontend = TmTcFrontend(tmtc_handler)
-        tmtc_frontend_task = tmtc_frontend.prepare_start()
-        tmtc_frontend_task.start()
-    # tmtc_handler_task.start()
-    tmtc_handler.perform_operation()
+    if create_task_in_process:
+        if use_gui:
+            tmtc_frontend = TmTcFrontend(tmtc_handler)
+            tmtc_frontend_task = tmtc_frontend.prepare_start(tmtc_frontend)
+            tmtc_frontend_task.start()
+    tmtc_handler_task.start()
 
-    # tmtc_handler_task.join()
+    tmtc_handler_task.join()
     if use_gui:
         tmtc_frontend_task.join()
 
diff --git a/core/tmtc_frontend.py b/core/tmtc_frontend.py
index c840731..2f5fba9 100644
--- a/core/tmtc_frontend.py
+++ b/core/tmtc_frontend.py
@@ -44,7 +44,7 @@ class TmTcFrontend:
         obsw_config.G_SERVICE = 17
         obsw_config.G_COM_IF = obsw_config.ComInterfaces.QEMU
 
-    def prepare_start(self) -> Process:
+    def prepare_start(self, args: any) -> Process:
         return Process(target=self.start_ui)
 
     def service_index_changed(self, index: int):
-- 
GitLab