Python-With-Gtk-Template/src/utils/ipc_server.py

110 lines
3.6 KiB
Python
Raw Normal View History

2022-01-23 22:56:27 +00:00
# Python imports
2022-12-11 20:52:09 +00:00
import os
import threading
2023-01-29 06:06:52 +00:00
import time
2022-12-11 20:52:09 +00:00
from multiprocessing.connection import Client
from multiprocessing.connection import Listener
2022-01-23 22:56:27 +00:00
# Lib imports
# Application imports
2022-02-25 23:53:58 +00:00
class IPCServer:
2022-10-23 04:26:13 +00:00
""" Create a listener so that other {app_name} instances send requests back to existing instance. """
2022-06-14 23:19:21 +00:00
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-05 23:01:39 +00:00
self._ipc_authkey = b'' + bytes(f'{app_name}-ipc', 'utf-8')
2022-06-14 23:19:21 +00:00
self._ipc_timeout = 15.0
2022-01-23 22:56:27 +00:00
2022-03-03 07:31:48 +00:00
if conn_type == "socket":
2022-09-05 23:01:39 +00:00
self._ipc_address = f'/tmp/{app_name}-ipc.sock'
2022-06-14 23:19:21 +00:00
elif conn_type == "full_network":
self._ipc_address = '0.0.0.0'
elif conn_type == "full_network_unsecured":
self._ipc_authkey = None
self._ipc_address = '0.0.0.0'
elif conn_type == "local_network_unsecured":
self._ipc_authkey = None
2022-10-01 04:35:03 +00:00
self._subscribe_to_events()
def _subscribe_to_events(self):
event_system.subscribe("post_file_to_ipc", self.send_ipc_message)
2022-03-03 07:31:48 +00:00
2022-06-14 23:19:21 +00:00
def create_ipc_listener(self) -> None:
2022-03-03 07:31:48 +00:00
if self._conn_type == "socket":
2022-10-23 04:26:13 +00:00
if os.path.exists(self._ipc_address) and settings.is_dirty_start():
os.unlink(self._ipc_address)
2022-03-03 07:31:48 +00:00
2022-06-14 23:19:21 +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-03-03 07:31:48 +00:00
else:
2022-06-14 23:19:21 +00:00
listener = Listener((self._ipc_address, self._ipc_port))
2022-03-03 07:31:48 +00:00
2022-01-23 22:56:27 +00:00
self.is_ipc_alive = True
2022-10-23 04:26:13 +00:00
self._run_ipc_loop(listener)
@daemon_threaded
def _run_ipc_loop(self, listener) -> None:
2023-03-25 20:57:13 +00:00
# NOTE: Not thread safe if using with Gtk. Need to import GLib and use idle_add
2022-01-23 22:56:27 +00:00
while True:
2023-03-25 20:57:13 +00:00
try:
conn = listener.accept()
start_time = time.perf_counter()
self._handle_ipc_message(conn, start_time)
except Exception as e:
...
2022-01-23 22:56:27 +00:00
2022-06-14 23:19:21 +00:00
listener.close()
2022-01-23 22:56:27 +00:00
2022-12-11 20:52:09 +00:00
def _handle_ipc_message(self, conn, start_time) -> None:
2022-06-14 23:19:21 +00:00
while True:
msg = conn.recv()
2022-10-23 04:26:13 +00:00
if settings.is_debug():
2022-06-14 23:19:21 +00:00
print(msg)
2022-01-23 22:56:27 +00:00
2022-06-14 23:19:21 +00:00
if "FILE|" in msg:
file = msg.split("FILE|")[1].strip()
if file:
2022-10-01 04:35:03 +00:00
event_system.emit("handle_file_from_ipc", file)
2022-01-23 22:56:27 +00:00
2022-06-14 23:19:21 +00:00
conn.close()
break
2022-01-23 22:56:27 +00:00
2022-06-14 23:19:21 +00:00
if msg in ['close connection', 'close server']:
conn.close()
break
2022-01-23 22:56:27 +00:00
2022-06-14 23:19:21 +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
2022-01-23 22:56:27 +00:00
2022-03-25 03:11:02 +00:00
def send_ipc_message(self, message: str = "Empty Data...") -> None:
2022-01-23 22:56:27 +00:00
try:
2022-03-03 07:31:48 +00:00
if self._conn_type == "socket":
2022-06-14 23:19:21 +00:00
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-03-03 07:31:48 +00:00
else:
2022-06-14 23:19:21 +00:00
conn = Client((self._ipc_address, self._ipc_port))
2022-03-03 07:31:48 +00:00
2022-01-23 22:56:27 +00:00
conn.send(message)
2022-03-25 03:11:02 +00:00
conn.close()
2022-06-14 23:19:21 +00:00
except ConnectionRefusedError as e:
print("Connection refused...")
2022-01-23 22:56:27 +00:00
except Exception as e:
print(repr(e))