SolarFM/src/versions/solarfm-0.0.1/solarfm/utils/ipc_server.py

109 lines
3.6 KiB
Python
Raw Normal View History

# Python imports
2023-02-05 05:38:44 +00:00
import os
import time
from multiprocessing.connection import Client
from multiprocessing.connection import Listener
# Lib imports
2023-02-05 05:38:44 +00:00
from gi.repository import GLib
# Application imports
from .singleton import Singleton
class IPCServer(Singleton):
""" Create a listener so that other SolarFM instances send requests back to existing instance. """
def __init__(self, ipc_address: str = '127.0.0.1', conn_type: str = "socket"):
self.is_ipc_alive = False
self._ipc_port = 4848
self._ipc_address = ipc_address
self._conn_type = conn_type
2022-09-06 02:21:04 +00:00
self._ipc_authkey = b'' + bytes(f'{app_name}-ipc', 'utf-8')
self._ipc_timeout = 15.0
2022-03-03 07:24:59 +00:00
if conn_type == "socket":
2022-09-06 02:21:04 +00:00
self._ipc_address = f'/tmp/{app_name}-ipc.sock'
2022-06-11 01:13:57 +00:00
elif conn_type == "full_network":
self._ipc_address = '0.0.0.0'
2022-06-11 01:13:57 +00:00
elif conn_type == "full_network_unsecured":
self._ipc_authkey = None
self._ipc_address = '0.0.0.0'
2022-06-11 01:13:57 +00:00
elif conn_type == "local_network_unsecured":
self._ipc_authkey = None
2022-03-03 07:24:59 +00:00
2022-09-29 22:22:33 +00:00
self._subscribe_to_events()
2022-10-23 04:53:33 +00:00
2022-09-29 22:22:33 +00:00
def _subscribe_to_events(self):
event_system.subscribe("post_file_to_ipc", self.send_ipc_message)
def create_ipc_listener(self) -> None:
2022-03-03 07:24:59 +00:00
if self._conn_type == "socket":
if os.path.exists(self._ipc_address) and settings.is_dirty_start():
os.unlink(self._ipc_address)
2022-03-03 07:24:59 +00:00
listener = Listener(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
elif "unsecured" not in self._conn_type:
listener = Listener((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
2022-06-11 01:13:57 +00:00
else:
listener = Listener((self._ipc_address, self._ipc_port))
2022-03-03 07:24:59 +00:00
self.is_ipc_alive = True
2022-10-23 04:53:33 +00:00
self._run_ipc_loop(listener)
@daemon_threaded
def _run_ipc_loop(self, listener) -> None:
while True:
2023-02-08 23:52:33 +00:00
try:
conn = listener.accept()
start_time = time.perf_counter()
GLib.idle_add(self._handle_ipc_message, *(conn, start_time,))
except Exception as e:
...
2021-11-28 04:03:00 +00:00
listener.close()
2022-10-23 04:53:33 +00:00
def _handle_ipc_message(self, conn, start_time) -> None:
while True:
msg = conn.recv()
if settings.is_debug():
print(msg)
2021-11-25 08:21:10 +00:00
if "FILE|" in msg:
file = msg.split("FILE|")[1].strip()
if file:
event_system.emit("handle_file_from_ipc", file)
conn.close()
break
2021-11-28 04:03:00 +00:00
if msg in ['close connection', 'close server']:
conn.close()
break
2021-11-28 04:03:00 +00:00
# NOTE: Not perfect but insures we don't lock up the connection for too long.
end_time = time.perf_counter()
if (end_time - start_time) > self._ipc_timeout:
conn.close()
break
def send_ipc_message(self, message: str = "Empty Data...") -> None:
try:
2022-03-03 07:24:59 +00:00
if self._conn_type == "socket":
conn = Client(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
elif "unsecured" not in self._conn_type:
conn = Client((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
2022-06-11 01:13:57 +00:00
else:
conn = Client((self._ipc_address, self._ipc_port))
2022-03-03 07:24:59 +00:00
conn.send(message)
conn.close()
except ConnectionRefusedError as e:
print("Connection refused...")
except Exception as e:
print(repr(e))