diff --git a/src/fenrirscreenreader/core/remoteManager.py b/src/fenrirscreenreader/core/remoteManager.py index 51613544..316777d8 100644 --- a/src/fenrirscreenreader/core/remoteManager.py +++ b/src/fenrirscreenreader/core/remoteManager.py @@ -8,14 +8,15 @@ from fenrirscreenreader.core import debug from fenrirscreenreader.core.eventData import fenrirEventType import time import select +import socket +import os, os.path class remoteManager(): def __init__(self): pass def initialize(self, environment): self.env = environment - self.env['runtime']['processManager'].addCustomEventThread(self.tcpWatchDog, multiprocess=True) - return + if self.env['runtime']['settingsManager'].getSettingAsBool('remote', 'enabled'): if self.env['runtime']['settingsManager'].getSetting('remote', 'method') == 'unix': self.env['runtime']['processManager'].addCustomEventThread(self.unixSocketWatchDog, multiprocess=True) @@ -26,15 +27,36 @@ class remoteManager(): self.sock.close() self.sock = None def unixSocketWatchDog(self, active, eventQueue): + # echo "#<=>#command##exec#say##this is a test" | socat - UNIX-CLIENT:/tmp/fenrir-deamon.sock + if os.path.exists("/tmp/fenrir-deamon.sock"): + os.remove("/tmp/fenrir-deamon.sock") + self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.sock.bind("/tmp/fenrir-deamon.sock") + self.sock.listen(1) while active.value == 1: - eventQueue.put({"Type":fenrirEventType.RemoteIncomming, - "Data":'#!#command##say##this is a test' - }) - eventQueue.put({"Type":fenrirEventType.RemoteIncomming, - "Data":'#!#command##interrupt' - }) + client_sock, client_addr = self.sock.accept() + if client_sock: + # Check if the client is still connected and if data is available: + try: + r, w, e = select.select([client_sock,], [], []) + except select.error: + return + if len(r) > 0: + rawdata = client_sock.recv(8129) + try: + data = rawdata.decode("utf-8").rstrip() + if data.startswith('#<=>#'): + eventQueue.put({"Type":fenrirEventType.RemoteIncomming, + "Data": data + }) + except: + pass + client_sock.close() + if self.sock: + self.sock.close() + self.sock = None def tcpWatchDog(self, active, eventQueue): - import socket + # echo "#<=>#command##exec#say##this is a test" | nc localhost 22447 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.host = '0.0.0.0' @@ -53,7 +75,7 @@ class remoteManager(): rawdata = client_sock.recv(8129) try: data = rawdata.decode("utf-8").rstrip() - if data.startswith('#!#'): + if data.startswith('#<=>#'): eventQueue.put({"Type":fenrirEventType.RemoteIncomming, "Data": data }) @@ -91,17 +113,17 @@ class remoteManager(): if not eventData: return # settings: - # #!#settings##set##section#setting=value[,section#setting=value] - # #!#settings##set##speech#voice=de - # #!#settings##reset + # #<=>#settings##set##section#setting=value[,section#setting=value] + # #<=>#settings##set##speech#voice=de + # #<=>#settings##reset # execute command: - # #!#command##exec#say##this is a test - # #!#command##cancel##say - if not eventData.startswith('#!#'): + # #<=>#command##exec#say##this is a test + # #<=>#command##cancel##say + if not eventData.startswith('#<=>#'): return - if eventData.startswith('#!#settings##'): - settingsText = eventData[len('#!#settings##'):] + if eventData.startswith('#<=>#settings##'): + settingsText = eventData[len('#<=>#settings##'):] self.handleSettingsChange(settingsText) - elif eventData.startswith('#!#command##'): - commandText = eventData[len('#!#command##'):] + elif eventData.startswith('#<=>#command##'): + commandText = eventData[len('#<=>#command##'):] self.handleCommandExecution(commandText)