updated logger, updated ipc setup
This commit is contained in:
@@ -16,73 +16,85 @@ def threaded(fn):
|
||||
|
||||
|
||||
class IPCServer:
|
||||
''' Create a listener so that other instances send requests back to existing instance. '''
|
||||
def __init__(self, conn_type: str = "socket"):
|
||||
self.is_ipc_alive = False
|
||||
self._conn_type = conn_type
|
||||
self.ipc_authkey = b'app-ipc'
|
||||
self.ipc_timeout = 15.0
|
||||
""" 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
|
||||
|
||||
if conn_type == "socket":
|
||||
self.ipc_address = '/tmp/app-ipc.sock'
|
||||
else:
|
||||
self.ipc_address = '127.0.0.1'
|
||||
self.ipc_port = 8888
|
||||
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
|
||||
|
||||
|
||||
@threaded
|
||||
def create_ipc_server(self) -> None:
|
||||
def create_ipc_listener(self) -> None:
|
||||
if self._conn_type == "socket":
|
||||
if os.path.exists(self.ipc_address):
|
||||
if os.path.exists(self._ipc_address):
|
||||
return
|
||||
|
||||
listener = Listener(address=self.ipc_address, family="AF_UNIX", authkey=self.ipc_authkey)
|
||||
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)
|
||||
else:
|
||||
listener = Listener((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
|
||||
listener = Listener((self._ipc_address, self._ipc_port))
|
||||
|
||||
|
||||
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:
|
||||
event_system.push_gui_event([None, "handle_file_from_ipc", (file,)])
|
||||
|
||||
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()
|
||||
start_time = time.perf_counter()
|
||||
self.handle_message(conn, start_time)
|
||||
|
||||
listener.close()
|
||||
|
||||
def handle_message(self, conn, start_time) -> None:
|
||||
while True:
|
||||
msg = conn.recv()
|
||||
if debug:
|
||||
print(msg)
|
||||
|
||||
if "FILE|" in msg:
|
||||
file = msg.split("FILE|")[1].strip()
|
||||
if file:
|
||||
event_system.push_gui_event([None, "handle_file_from_ipc", (file,)])
|
||||
|
||||
conn.close()
|
||||
break
|
||||
|
||||
|
||||
if msg in ['close connection', 'close server']:
|
||||
conn.close()
|
||||
break
|
||||
|
||||
# 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:
|
||||
if self._conn_type == "socket":
|
||||
conn = Client(address=self.ipc_address, family="AF_UNIX", authkey=self.ipc_authkey)
|
||||
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)
|
||||
else:
|
||||
conn = Client((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
|
||||
conn = Client((self._ipc_address, self._ipc_port))
|
||||
|
||||
conn.send(message)
|
||||
conn.send('close connection')
|
||||
conn.close()
|
||||
except ConnectionRefusedError as e:
|
||||
print("Connection refused...")
|
||||
except Exception as e:
|
||||
print(repr(e))
|
||||
|
||||
Reference in New Issue
Block a user