Converted to use unix socket

This commit is contained in:
itdominator 2022-03-03 01:31:48 -06:00
parent f3b222ec1b
commit 1e2fe89126
5 changed files with 82 additions and 8 deletions

View File

@ -5,9 +5,12 @@ A template project for Python with Gtk applications.
* PyGObject
### Note
There are two "\<change_me\>" strings that need to be set according to your app's name located at:
There are several "\<change_me\>" strings that need to be set according to your app's name located at:
* \_\_builtins\_\_.py
* \_\_main\_\_.py
* user_config/usr/share/app_name
In addition, check the 'ipc_server.py' \_\_init\_\_ section to change the socket information.
For the user_config, traverse all the way down and copy the contents to either:
* /usr/share/\<your_app_name_as_all_lowercase\>

2
plugins/README.txt Normal file
View File

@ -0,0 +1,2 @@
### Note
Copy the example and rename it to your desired name. The Main class and passed in arguments are required. You don't necessarily need to use the passed in socket_id or event_system.

View File

@ -0,0 +1,53 @@
# Python imports
import sys, threading, subprocess, time
# Gtk imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=False).start()
return wrapper
class Plugin:
def __init__(self, builder, event_system):
self._plugin_name = "Example Plugin"
self._builder = builder
self._event_system = event_system
self._message = None
self._time_out = 5
button = Gtk.Button(label=self._plugin_name)
button.connect("button-release-event", self._do_action)
plugin_list = self._builder.get_object("plugin_socket")
plugin_list.add(button)
plugin_list.show_all()
@threaded
def _do_action(self, widget=None, eve=None):
message = "Hello, World!"
self._event_system.push_gui_event(["some_type", "display_message", ("warning", message, None)])
def set_message(self, data):
self._message = data
def get_plugin_name(self):
return self._plugin_name
def get_socket_id(self):
return self._socket_id
def _run_timeout(self):
timeout = 0
while not self._message and timeout < self._time_out:
time.sleep(1)
timeout += 1

View File

@ -1,5 +1,5 @@
# Python imports
import threading, socket, time
import os, threading, time
from multiprocessing.connection import Listener, Client
# Lib imports
@ -17,16 +17,28 @@ def threaded(fn):
class IPCServer:
''' Create a listener so that other instances send requests back to existing instance. '''
def __init__(self):
def __init__(self, conn_type="socket"):
self.is_ipc_alive = False
self._conn_type = conn_type
self.ipc_authkey = b'app-ipc'
self.ipc_address = '127.0.0.1'
self.ipc_port = 8888
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
@threaded
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
while True:
conn = listener.accept()
@ -64,7 +76,11 @@ class IPCServer:
def send_ipc_message(self, message="Empty Data..."):
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('close connection')
except Exception as e:

View File

@ -17,8 +17,8 @@ class Main(EventSystem):
def __init__(self, args, unknownargs):
if not debug:
event_system.create_ipc_server()
time.sleep(0.1)
time.sleep(0.2)
if not trace_debug:
if not event_system.is_ipc_alive:
if unknownargs: