2021-11-25 06:44:12 +00:00
|
|
|
# Python imports
|
2022-03-03 07:24:59 +00:00
|
|
|
import os, threading, time
|
2021-11-25 06:44:12 +00:00
|
|
|
from multiprocessing.connection import Listener, Client
|
|
|
|
|
2021-12-07 04:44:39 +00:00
|
|
|
# Lib imports
|
2021-11-25 06:44:12 +00:00
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
|
def threaded(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
2021-12-25 05:58:57 +00:00
|
|
|
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
|
2021-11-25 06:44:12 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2021-12-07 04:44:39 +00:00
|
|
|
|
|
|
|
|
2022-02-25 23:58:11 +00:00
|
|
|
class IPCServer:
|
2022-02-20 07:32:51 +00:00
|
|
|
""" Create a listener so that other SolarFM instances send requests back to existing instance. """
|
2022-03-03 07:24:59 +00:00
|
|
|
def __init__(self, conn_type="socket"):
|
2022-02-25 23:58:11 +00:00
|
|
|
self.is_ipc_alive = False
|
2022-03-03 07:24:59 +00:00
|
|
|
self._conn_type = conn_type
|
2022-02-25 23:58:11 +00:00
|
|
|
self.ipc_authkey = b'solarfm-ipc'
|
|
|
|
self.ipc_timeout = 15.0
|
|
|
|
|
2022-03-03 07:24:59 +00:00
|
|
|
if conn_type == "socket":
|
|
|
|
self.ipc_address = '/tmp/solarfm-ipc.sock'
|
|
|
|
else:
|
|
|
|
self.ipc_address = '127.0.0.1'
|
|
|
|
self.ipc_port = 4848
|
|
|
|
|
2021-11-25 06:44:12 +00:00
|
|
|
|
|
|
|
@threaded
|
|
|
|
def create_ipc_server(self):
|
2022-03-03 07:24:59 +00:00
|
|
|
if self._conn_type == "socket":
|
|
|
|
if os.path.exists(self.ipc_address):
|
|
|
|
return
|
|
|
|
|
|
|
|
listener = Listener(address=self.ipc_address, family="AF_UNIX", authkey=self.ipc_authkey)
|
|
|
|
else:
|
|
|
|
listener = Listener((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
|
|
|
|
|
|
|
|
|
2021-11-25 06:44:12 +00:00
|
|
|
self.is_ipc_alive = True
|
2021-12-25 05:58:57 +00:00
|
|
|
while True:
|
2021-11-28 04:03:00 +00:00
|
|
|
conn = listener.accept()
|
|
|
|
start_time = time.time()
|
|
|
|
|
2021-11-25 06:44:12 +00:00
|
|
|
print(f"New Connection: {listener.last_accepted}")
|
|
|
|
while True:
|
|
|
|
msg = conn.recv()
|
2021-11-25 08:21:10 +00:00
|
|
|
if debug:
|
|
|
|
print(msg)
|
2021-11-25 06:44:12 +00:00
|
|
|
|
|
|
|
if "FILE|" in msg:
|
|
|
|
file = msg.split("FILE|")[1].strip()
|
2021-11-25 08:21:10 +00:00
|
|
|
if file:
|
2022-01-31 06:13:43 +00:00
|
|
|
event_system.push_gui_event([None, "handle_file_from_ipc", (file,)])
|
2021-11-25 08:21:10 +00:00
|
|
|
|
2021-11-25 06:44:12 +00:00
|
|
|
conn.close()
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if msg == 'close connection':
|
|
|
|
conn.close()
|
|
|
|
break
|
|
|
|
if msg == 'close server':
|
|
|
|
conn.close()
|
|
|
|
break
|
2021-11-28 04:03:00 +00:00
|
|
|
|
2022-02-20 06:28:34 +00:00
|
|
|
# NOTE: Not perfect but insures we don't lock up the connection for too long.
|
2021-11-28 04:03:00 +00:00
|
|
|
end_time = time.time()
|
2022-01-24 16:24:55 +00:00
|
|
|
if (end - start) > self.ipc_timeout:
|
2021-11-28 04:03:00 +00:00
|
|
|
conn.close()
|
|
|
|
|
2021-11-25 06:44:12 +00:00
|
|
|
listener.close()
|
|
|
|
|
|
|
|
|
|
|
|
def send_ipc_message(self, message="Empty Data..."):
|
|
|
|
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)
|
|
|
|
else:
|
|
|
|
conn = Client((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
|
|
|
|
|
|
|
|
|
2021-11-25 06:44:12 +00:00
|
|
|
conn.send(message)
|
|
|
|
conn.send('close connection')
|
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|