From 05165d28681dbbed471878d7bfe3d4fae339537a Mon Sep 17 00:00:00 2001 From: Chrys Date: Thu, 5 Sep 2019 23:01:35 +0200 Subject: [PATCH] improve multiprocessing, wait for complete and handle remtoe moregraceful --- config/settings/espeak.settings.conf | 2 +- config/settings/settings-daemon.conf | 2 +- config/settings/settings-pty.conf | 2 +- config/settings/settings.conf | 2 +- config/settings/settings.conf.example | 2 +- config/settings/settings.conf.storm | 2 +- .../settings/speech-dispatcher.settings.conf | 2 +- src/fenrirscreenreader/core/processManager.py | 16 ++-- src/fenrirscreenreader/core/settingsData.py | 2 +- .../remoteDriver/tcpDriver.py | 52 ++++++------ .../remoteDriver/unixDriver.py | 80 +++++++++++-------- .../screenDriver/ptyDriver.py | 8 +- 12 files changed, 94 insertions(+), 78 deletions(-) diff --git a/config/settings/espeak.settings.conf b/config/settings/espeak.settings.conf index c6231b63..ccc7deff 100644 --- a/config/settings/espeak.settings.conf +++ b/config/settings/espeak.settings.conf @@ -195,7 +195,7 @@ driver=unixDriver # tcp port port=22447 # socket filepath -socketpath=/tmp/ +socketFile= # allow settings to overwrite enableSettingsRemote=True # allow commands to be executed diff --git a/config/settings/settings-daemon.conf b/config/settings/settings-daemon.conf index db91df1e..219c9f7f 100644 --- a/config/settings/settings-daemon.conf +++ b/config/settings/settings-daemon.conf @@ -205,7 +205,7 @@ driver=unixDriver # tcp port port=22447 # socket filepath -socketpath=/tmp/ +socketFile= # allow settings to overwrite enableSettingsRemote=True # allow commands to be executed diff --git a/config/settings/settings-pty.conf b/config/settings/settings-pty.conf index db91df1e..219c9f7f 100644 --- a/config/settings/settings-pty.conf +++ b/config/settings/settings-pty.conf @@ -205,7 +205,7 @@ driver=unixDriver # tcp port port=22447 # socket filepath -socketpath=/tmp/ +socketFile= # allow settings to overwrite enableSettingsRemote=True # allow commands to be executed diff --git a/config/settings/settings.conf b/config/settings/settings.conf index 92acac02..22645577 100644 --- a/config/settings/settings.conf +++ b/config/settings/settings.conf @@ -206,7 +206,7 @@ driver=unixDriver # tcp port port=22447 # socket filepath -socketpath=/tmp/ +socketFile= # allow settings to overwrite enableSettingsRemote=True # allow commands to be executed diff --git a/config/settings/settings.conf.example b/config/settings/settings.conf.example index a9b1c738..da7665e3 100644 --- a/config/settings/settings.conf.example +++ b/config/settings/settings.conf.example @@ -206,7 +206,7 @@ driver=unixDriver # tcp port port=22447 # socket filepath -socketpath=/tmp/ +socketFile= # allow settings to overwrite enableSettingsRemote=True # allow commands to be executed diff --git a/config/settings/settings.conf.storm b/config/settings/settings.conf.storm index 382dd946..9970ba8f 100644 --- a/config/settings/settings.conf.storm +++ b/config/settings/settings.conf.storm @@ -151,7 +151,7 @@ driver=unixDriver # tcp port port=22447 # socket filepath -socketpath=/tmp/ +socketFile= # allow settings to overwrite enableSettingsRemote=True # allow commands to be executed diff --git a/config/settings/speech-dispatcher.settings.conf b/config/settings/speech-dispatcher.settings.conf index 9158b196..01d890b7 100644 --- a/config/settings/speech-dispatcher.settings.conf +++ b/config/settings/speech-dispatcher.settings.conf @@ -205,7 +205,7 @@ driver=unixDriver # tcp port port=22447 # socket filepath -socketpath=/tmp/ +socketFile= # allow settings to overwrite enableSettingsRemote=True # allow commands to be executed diff --git a/src/fenrirscreenreader/core/processManager.py b/src/fenrirscreenreader/core/processManager.py index 86811655..b6e6407c 100644 --- a/src/fenrirscreenreader/core/processManager.py +++ b/src/fenrirscreenreader/core/processManager.py @@ -23,15 +23,15 @@ class processManager(): def terminateAllProcesses(self): for proc in self._Processes: - try: - proc.terminate() - except KeyboardInterrupt: - pass - except: - pass - proc.join() + #try: + # proc.terminate() + #except KeyboardInterrupt: + # pass + #except: + # pass + proc.join() for t in self._Threads: - t.join() + t.join() def heartBeatTimer(self, active): try: time.sleep(0.5) diff --git a/src/fenrirscreenreader/core/settingsData.py b/src/fenrirscreenreader/core/settingsData.py index 3052ab9a..81f9e38d 100644 --- a/src/fenrirscreenreader/core/settingsData.py +++ b/src/fenrirscreenreader/core/settingsData.py @@ -84,7 +84,7 @@ settingsData = { 'enabled': True, 'driver': 'unixDriver', 'port': 22447, - 'socketpath':'/tmp/', + 'socketFile':'', 'enableSettingsRemote': True, 'enableCommandRemote': True, }, diff --git a/src/fenrirscreenreader/remoteDriver/tcpDriver.py b/src/fenrirscreenreader/remoteDriver/tcpDriver.py index 9b6e6407..59fe9772 100644 --- a/src/fenrirscreenreader/remoteDriver/tcpDriver.py +++ b/src/fenrirscreenreader/remoteDriver/tcpDriver.py @@ -18,30 +18,36 @@ class driver(remoteDriver): self.env['runtime']['processManager'].addCustomEventThread(self.watchDog, multiprocess=True) def watchDog(self, active, eventQueue): # echo "command 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.fenrirSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.fenrirSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.host = '127.0.0.1' self.port = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'port') - self.sock.bind((self.host, self.port)) - self.sock.listen(1) + self.fenrirSock.bind((self.host, self.port)) + self.fenrirSock.listen(1) while active.value == 1: - 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().lstrip() - eventQueue.put({"Type":fenrirEventType.RemoteIncomming, - "Data": data - }) - except: - pass + try: + r, _, _ = select.select([self.fenrirSock], [], [], 0.8) + except select.error: + break + if r == []: + continue + if self.fenrirSock in r: + client_sock, client_addr = self.fenrirSock.accept() + try: + rawdata = client_sock.recv(8129) + except: + pass + try: + data = rawdata.decode("utf-8").rstrip().lstrip() + eventQueue.put({"Type":fenrirEventType.RemoteIncomming, + "Data": data + }) + except: + pass + try: client_sock.close() - if self.sock: - self.sock.close() - self.sock = None + except: + pass + if self.fenrirSock: + self.fenrirSock.close() + self.fenrirSock = None diff --git a/src/fenrirscreenreader/remoteDriver/unixDriver.py b/src/fenrirscreenreader/remoteDriver/unixDriver.py index 83032e8b..b4ba41c3 100644 --- a/src/fenrirscreenreader/remoteDriver/unixDriver.py +++ b/src/fenrirscreenreader/remoteDriver/unixDriver.py @@ -19,39 +19,49 @@ class driver(remoteDriver): self.env['runtime']['processManager'].addCustomEventThread(self.watchDog, multiprocess=True) def watchDog(self, active, eventQueue): # echo "command say this is a test" | socat - UNIX-CLIENT:/tmp/fenrirscreenreader-deamon.sock - - if self.env['runtime']['settingsManager'].getSetting('screen', 'driver') =='vcsaDriver': - socketpath = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'socketpath') + 'fenrirscreenreader-deamon.sock' - else: - socketpath = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'socketpath') + 'fenrirscreenreader-' + str(os.getpid()) + '.sock' - if os.path.exists(socketpath): - os.remove(socketpath) - self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - self.sock.bind(socketpath) - self.sock.listen(1) - if self.env['runtime']['settingsManager'].getSetting('screen', 'driver') =='vcsaDriver': - os.chmod(socketpath, 0o222) - while active.value == 1: - 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().lstrip() - eventQueue.put({"Type":fenrirEventType.RemoteIncomming, - "Data": data - }) - except: - pass + socketFile = '' + try: + socketFile = self.env['runtime']['settingsManager'].getSetting('remote', 'socketFile') + except: + pass + if socketFile == '': + if self.env['runtime']['settingsManager'].getSetting('screen', 'driver') =='vcsaDriver': + socketFile = '/tmp/fenrirscreenreader-deamon.sock' + else: + socketFile = '/tmp/fenrirscreenreader-' + str(os.getppid()) + '.sock' + if os.path.exists(socketFile): + os.unlink(socketFile) + self.fenrirSock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.fenrirSock.bind(socketFile) + os.chmod(socketFile, 0o222) + self.fenrirSock.listen(1) + while active.value: + # Check if the client is still connected and if data is available: + try: + r, _, _ = select.select([self.fenrirSock], [], [], 0.8) + except select.error: + break + if r == []: + continue + if self.fenrirSock in r: + client_sock, client_addr = self.fenrirSock.accept() + try: + rawdata = client_sock.recv(8129) + except: + pass + try: + data = rawdata.decode("utf-8").rstrip().lstrip() + eventQueue.put({"Type":fenrirEventType.RemoteIncomming, + "Data": data + }) + except: + pass + try: client_sock.close() - - if os.path.exists(socketpath): - os.remove(socketpath) - if self.sock: - self.sock.close() - self.sock = None + except: + pass + if self.fenrirSock: + self.fenrirSock.close() + self.fenrirSock = None + if os.path.exists(socketFile): + os.unlink(socketFile) diff --git a/src/fenrirscreenreader/screenDriver/ptyDriver.py b/src/fenrirscreenreader/screenDriver/ptyDriver.py index 7f0ced69..e61ff3fe 100644 --- a/src/fenrirscreenreader/screenDriver/ptyDriver.py +++ b/src/fenrirscreenreader/screenDriver/ptyDriver.py @@ -179,13 +179,13 @@ class driver(screenDriver): try: msgBytes = self.readAll(sys.stdin.fileno(), len=4096) except (EOFError, OSError): - active.value = False + eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None}) break if self.shortcutType == 'KEY': try: self.injectTextToScreen(msgBytes) except: - active.value = False + eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None}) break else: eventQueue.put({"Type":fenrirEventType.ByteInput, @@ -195,7 +195,7 @@ class driver(screenDriver): try: msgBytes = self.readAll(self.p_out.fileno(), interruptFd=sys.stdin.fileno()) except (EOFError, OSError): - active.value = False + eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None}) break # feed and send event bevore write, the pyte already has the right state # so fenrir already can progress bevore os.write what should give some better reaction time @@ -206,7 +206,7 @@ class driver(screenDriver): self.injectTextToScreen(msgBytes, screen=sys.stdout.fileno()) except Exception as e: # Process died? print(e) - active.value = False + eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None}) finally: os.kill(self.p_pid, signal.SIGTERM) self.p_out.close()