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:
|
|
|
|
''' Create a listener so that other instances send requests back to existing instance. '''
|
2022-03-25 03:11:02 +00:00
|
|
|
def __init__(self, conn_type: str = "socket"):
|
2022-02-25 23:53:58 +00:00
|
|
|
self.is_ipc_alive = False
|
2022-03-03 07:31:48 +00:00
|
|
|
self._conn_type = conn_type
|
2022-02-25 23:53:58 +00:00
|
|
|
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":
|
|
|
|
self.ipc_address = '/tmp/app-ipc.sock'
|
|
|
|
else:
|
|
|
|
self.ipc_address = '127.0.0.1'
|
|
|
|
self.ipc_port = 8888
|
|
|
|
|
2022-01-23 22:56:27 +00:00
|
|
|
@threaded
|
2022-03-25 03:11:02 +00:00
|
|
|
def create_ipc_server(self) -> None:
|
2022-03-03 07:31:48 +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)
|
|
|
|
|
2022-01-23 22:56:27 +00:00
|
|
|
self.is_ipc_alive = True
|
|
|
|
while True:
|
|
|
|
conn = listener.accept()
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
|
|
print(f"New Connection: {listener.last_accepted}")
|
|
|
|
while True:
|
|
|
|
msg = conn.recv()
|
|
|
|
if debug:
|
|
|
|
print(msg)
|
|
|
|
|
|
|
|
if "FILE|" in msg:
|
|
|
|
file = msg.split("FILE|")[1].strip()
|
|
|
|
if file:
|
2022-02-01 06:03:04 +00:00
|
|
|
event_system.push_gui_event([None, "handle_file_from_ipc", (file,)])
|
2022-01-23 22:56:27 +00:00
|
|
|
|
|
|
|
conn.close()
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if msg == 'close connection':
|
|
|
|
conn.close()
|
|
|
|
break
|
|
|
|
if msg == 'close server':
|
|
|
|
conn.close()
|
|
|
|
break
|
|
|
|
|
|
|
|
# NOTE: Not perfect but insures we don't lockup the connection for too long.
|
|
|
|
end_time = time.time()
|
|
|
|
if (end - start) > self.ipc_timeout:
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
listener.close()
|
|
|
|
|
|
|
|
|
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":
|
|
|
|
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)
|
|
|
|
|
2022-01-23 22:56:27 +00:00
|
|
|
conn.send(message)
|
|
|
|
conn.send('close connection')
|
2022-03-25 03:11:02 +00:00
|
|
|
conn.close()
|
2022-01-23 22:56:27 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|