Converted to use unix socket
This commit is contained in:
parent
f3b222ec1b
commit
1e2fe89126
|
@ -5,9 +5,12 @@ A template project for Python with Gtk applications.
|
||||||
* PyGObject
|
* PyGObject
|
||||||
|
|
||||||
### Note
|
### 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
|
* \_\_builtins\_\_.py
|
||||||
* \_\_main\_\_.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:
|
For the user_config, traverse all the way down and copy the contents to either:
|
||||||
* /usr/share/\<your_app_name_as_all_lowercase\>
|
* /usr/share/\<your_app_name_as_all_lowercase\>
|
||||||
|
|
|
@ -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.
|
|
@ -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
|
|
@ -1,5 +1,5 @@
|
||||||
# Python imports
|
# Python imports
|
||||||
import threading, socket, time
|
import os, threading, time
|
||||||
from multiprocessing.connection import Listener, Client
|
from multiprocessing.connection import Listener, Client
|
||||||
|
|
||||||
# Lib imports
|
# Lib imports
|
||||||
|
@ -17,16 +17,28 @@ def threaded(fn):
|
||||||
|
|
||||||
class IPCServer:
|
class IPCServer:
|
||||||
''' Create a listener so that other instances send requests back to existing instance. '''
|
''' 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.is_ipc_alive = False
|
||||||
|
self._conn_type = conn_type
|
||||||
self.ipc_authkey = b'app-ipc'
|
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_address = '127.0.0.1'
|
||||||
self.ipc_port = 8888
|
self.ipc_port = 8888
|
||||||
self.ipc_timeout = 15.0
|
|
||||||
|
|
||||||
@threaded
|
@threaded
|
||||||
def create_ipc_server(self):
|
def create_ipc_server(self):
|
||||||
|
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)
|
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()
|
||||||
|
@ -64,7 +76,11 @@ class IPCServer:
|
||||||
|
|
||||||
def send_ipc_message(self, message="Empty Data..."):
|
def send_ipc_message(self, message="Empty Data..."):
|
||||||
try:
|
try:
|
||||||
|
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 = 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:
|
||||||
|
|
|
@ -17,8 +17,8 @@ class Main(EventSystem):
|
||||||
def __init__(self, args, unknownargs):
|
def __init__(self, args, unknownargs):
|
||||||
if not debug:
|
if not debug:
|
||||||
event_system.create_ipc_server()
|
event_system.create_ipc_server()
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
time.sleep(0.2)
|
|
||||||
if not trace_debug:
|
if not trace_debug:
|
||||||
if not event_system.is_ipc_alive:
|
if not event_system.is_ipc_alive:
|
||||||
if unknownargs:
|
if unknownargs:
|
||||||
|
|
Loading…
Reference in New Issue