2022-01-23 22:56:27 +00:00
|
|
|
# Python imports
|
2022-03-03 07:31:48 +00:00
|
|
|
import os, threading, time
|
2022-01-23 22:56:27 +00:00
|
|
|
from multiprocessing.connection import Listener, Client
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
|
def threaded(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-02-25 23:53:58 +00:00
|
|
|
class IPCServer:
|
2022-06-14 23:19:21 +00:00
|
|
|
""" 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
|
|
|
|
self._ipc_authkey = b'app-ipc'
|
|
|
|
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-06-14 23:19:21 +00:00
|
|
|
self._ipc_address = '/tmp/app-ipc.sock'
|
|
|
|
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-03-03 07:31:48 +00:00
|
|
|
|
2022-01-23 22:56:27 +00:00
|
|
|
@threaded
|
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-06-14 23:19:21 +00:00
|
|
|
if os.path.exists(self._ipc_address):
|
2022-03-03 07:31:48 +00:00
|
|
|
return
|
|
|
|
|
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
|
|
|
|
while True:
|
|
|
|
conn = listener.accept()
|
2022-06-14 23:19:21 +00:00
|
|
|
start_time = time.perf_counter()
|
|
|
|
self.handle_message(conn, start_time)
|
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-06-14 23:19:21 +00:00
|
|
|
def handle_message(self, conn, start_time) -> None:
|
|
|
|
while True:
|
|
|
|
msg = conn.recv()
|
|
|
|
if debug:
|
|
|
|
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:
|
|
|
|
event_system.push_gui_event([None, "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))
|