2021-04-04 21:34:46 +00:00
|
|
|
from setproctitle import setproctitle
|
2021-04-08 21:05:25 +00:00
|
|
|
from multiprocessing import current_process
|
2021-04-04 21:34:46 +00:00
|
|
|
from time import sleep
|
|
|
|
from os import _exit
|
|
|
|
|
2021-04-08 21:05:25 +00:00
|
|
|
from helpers.logging_manager import LoggingManager
|
2021-04-08 19:53:51 +00:00
|
|
|
|
2021-04-08 21:32:16 +00:00
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
class PlayerHandler:
|
2021-04-04 21:34:46 +00:00
|
|
|
logger: LoggingManager
|
|
|
|
|
2021-04-08 19:53:51 +00:00
|
|
|
def __init__(self, channel_from_q, websocket_to_q, ui_to_q, controller_to_q):
|
2021-04-04 21:34:46 +00:00
|
|
|
|
|
|
|
self.logger = LoggingManager("PlayerHandler")
|
|
|
|
process_title = "PlayerHandler"
|
|
|
|
setproctitle(process_title)
|
2021-04-08 21:05:25 +00:00
|
|
|
current_process().name = process_title
|
2021-04-04 21:34:46 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
|
|
|
|
for channel in range(len(channel_from_q)):
|
|
|
|
try:
|
|
|
|
message = channel_from_q[channel].get_nowait()
|
|
|
|
source = message.split(":")[0]
|
|
|
|
# TODO ENUM
|
2021-04-08 19:53:51 +00:00
|
|
|
if source in ["ALL", "WEBSOCKET"]:
|
2021-04-04 21:34:46 +00:00
|
|
|
websocket_to_q[channel].put(message)
|
2021-04-08 19:53:51 +00:00
|
|
|
if source in ["ALL", "UI"]:
|
2021-04-04 21:34:46 +00:00
|
|
|
if not message.split(":")[1] == "POS":
|
|
|
|
# We don't care about position update spam
|
|
|
|
ui_to_q[channel].put(message)
|
2021-04-08 19:53:51 +00:00
|
|
|
if source in ["ALL", "CONTROLLER"]:
|
2021-04-04 21:34:46 +00:00
|
|
|
controller_to_q[channel].put(message)
|
2021-04-08 21:32:16 +00:00
|
|
|
except Exception:
|
2021-04-04 21:34:46 +00:00
|
|
|
pass
|
|
|
|
|
2021-04-04 22:14:08 +00:00
|
|
|
sleep(0.02)
|
2021-04-04 21:34:46 +00:00
|
|
|
# Catch the handler being killed externally.
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.logger.log.info("Received KeyboardInterupt")
|
|
|
|
except SystemExit:
|
|
|
|
self.logger.log.info("Received SystemExit")
|
|
|
|
except Exception as e:
|
2021-04-08 20:15:15 +00:00
|
|
|
self.logger.log.exception(
|
|
|
|
"Received unexpected exception: {}".format(e))
|
2021-04-04 21:34:46 +00:00
|
|
|
del self.logger
|
|
|
|
_exit(0)
|