Converted to use unix socket

This commit is contained in:
itdominator 2022-03-03 01:24:59 -06:00
parent 32f061ff76
commit 8e242f5475
2 changed files with 25 additions and 7 deletions

View File

@ -1,5 +1,5 @@
# Python imports # Python imports
import threading, time import os, threading, time
from multiprocessing.connection import Listener, Client from multiprocessing.connection import Listener, Client
# Lib imports # Lib imports
@ -17,17 +17,30 @@ def threaded(fn):
class IPCServer: class IPCServer:
""" Create a listener so that other SolarFM instances send requests back to existing instance. """ """ Create a listener so that other SolarFM instances send requests back to existing instance. """
def __init__(self): def __init__(self, conn_type="socket"):
self.is_ipc_alive = False self.is_ipc_alive = False
self._conn_type = conn_type
self.ipc_authkey = b'solarfm-ipc' self.ipc_authkey = b'solarfm-ipc'
self.ipc_address = '127.0.0.1'
self.ipc_port = 4848
self.ipc_timeout = 15.0 self.ipc_timeout = 15.0
if conn_type == "socket":
self.ipc_address = '/tmp/solarfm-ipc.sock'
else:
self.ipc_address = '127.0.0.1'
self.ipc_port = 4848
@threaded @threaded
def create_ipc_server(self): def create_ipc_server(self):
listener = Listener((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey) 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)
self.is_ipc_alive = True self.is_ipc_alive = True
while True: while True:
conn = listener.accept() conn = listener.accept()
@ -65,7 +78,12 @@ class IPCServer:
def send_ipc_message(self, message="Empty Data..."): def send_ipc_message(self, message="Empty Data..."):
try: try:
conn = Client((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey) 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)
conn.send(message) conn.send(message)
conn.send('close connection') conn.send('close connection')
except Exception as e: except Exception as e:

View File

@ -17,7 +17,7 @@ class Main(EventSystem):
def __init__(self, args, unknownargs): def __init__(self, args, unknownargs):
if not trace_debug: if not trace_debug:
event_system.create_ipc_server() event_system.create_ipc_server()
time.sleep(0.2) # Make sure everything's up before proceeding. time.sleep(0.1)
if not event_system.is_ipc_alive: if not event_system.is_ipc_alive:
if unknownargs: if unknownargs: