From d991a94451b8f040622cd775d9e4ac89cd579453 Mon Sep 17 00:00:00 2001 From: chrys Date: Sun, 9 Sep 2018 19:08:18 +0200 Subject: [PATCH] split out remote backends --- config/settings/espeak.settings.conf | 8 +- config/settings/settings-daemon.conf | 8 +- config/settings/settings-pty.conf | 8 +- config/settings/settings.conf | 8 +- config/settings/settings.conf.example | 8 +- config/settings/settings.conf.storm | 8 +- .../settings/speech-dispatcher.settings.conf | 8 +- src/fenrirscreenreader/core/outputManager.py | 6 +- src/fenrirscreenreader/core/remoteDriver.py | 16 ++++ src/fenrirscreenreader/core/remoteManager.py | 83 ++----------------- src/fenrirscreenreader/core/runtimeData.py | 1 + src/fenrirscreenreader/core/settingsData.py | 2 +- .../remoteDriver/__init__.py | 0 .../remoteDriver/dummyDriver.py | 12 +++ .../remoteDriver/tcpDriver.py | 47 +++++++++++ .../remoteDriver/unixDriver.py | 57 +++++++++++++ 16 files changed, 170 insertions(+), 110 deletions(-) create mode 100644 src/fenrirscreenreader/core/remoteDriver.py create mode 100755 src/fenrirscreenreader/remoteDriver/__init__.py create mode 100644 src/fenrirscreenreader/remoteDriver/dummyDriver.py create mode 100644 src/fenrirscreenreader/remoteDriver/tcpDriver.py create mode 100644 src/fenrirscreenreader/remoteDriver/unixDriver.py diff --git a/config/settings/espeak.settings.conf b/config/settings/espeak.settings.conf index b5edcc26..e0b331a7 100644 --- a/config/settings/espeak.settings.conf +++ b/config/settings/espeak.settings.conf @@ -188,10 +188,10 @@ highlight=False [remote] enable=True -# connection type -# unix = unix sockets -# tcp = tcp (localhost only) -method=unix +# driver +# unixDriver = unix sockets +# tcpDriver = tcp (localhost only) +driver=unixDriver # tcp port port=22447 # socket filepath diff --git a/config/settings/settings-daemon.conf b/config/settings/settings-daemon.conf index 10d171af..2d48e901 100644 --- a/config/settings/settings-daemon.conf +++ b/config/settings/settings-daemon.conf @@ -198,10 +198,10 @@ highlight=False [remote] enable=True -# connection type -# unix = unix sockets -# tcp = tcp (localhost only) -method=unix +# driver +# unixDriver = unix sockets +# tcpDriver = tcp (localhost only) +driver=unixDriver # tcp port port=22447 # socket filepath diff --git a/config/settings/settings-pty.conf b/config/settings/settings-pty.conf index 10d171af..2d48e901 100644 --- a/config/settings/settings-pty.conf +++ b/config/settings/settings-pty.conf @@ -198,10 +198,10 @@ highlight=False [remote] enable=True -# connection type -# unix = unix sockets -# tcp = tcp (localhost only) -method=unix +# driver +# unixDriver = unix sockets +# tcpDriver = tcp (localhost only) +driver=unixDriver # tcp port port=22447 # socket filepath diff --git a/config/settings/settings.conf b/config/settings/settings.conf index 10d171af..2d48e901 100644 --- a/config/settings/settings.conf +++ b/config/settings/settings.conf @@ -198,10 +198,10 @@ highlight=False [remote] enable=True -# connection type -# unix = unix sockets -# tcp = tcp (localhost only) -method=unix +# driver +# unixDriver = unix sockets +# tcpDriver = tcp (localhost only) +driver=unixDriver # tcp port port=22447 # socket filepath diff --git a/config/settings/settings.conf.example b/config/settings/settings.conf.example index 80039a81..2c48c35f 100644 --- a/config/settings/settings.conf.example +++ b/config/settings/settings.conf.example @@ -199,10 +199,10 @@ highlight=False [remote] enable=True -# connection type -# unix = unix sockets -# tcp = tcp (localhost only) -method=unix +# driver +# unixDriver = unix sockets +# tcpDriver = tcp (localhost only) +driver=unixDriver # tcp port port=22447 # socket filepath diff --git a/config/settings/settings.conf.storm b/config/settings/settings.conf.storm index 65f58552..1103186a 100644 --- a/config/settings/settings.conf.storm +++ b/config/settings/settings.conf.storm @@ -144,10 +144,10 @@ highlight=False [remote] enable=True -# connection type -# unix = unix sockets -# tcp = tcp (localhost only) -method=unix +# driver +# unixDriver = unix sockets +# tcpDriver = tcp (localhost only) +driver=unixDriver # tcp port port=22447 # socket filepath diff --git a/config/settings/speech-dispatcher.settings.conf b/config/settings/speech-dispatcher.settings.conf index 2467a9b3..32337ae8 100644 --- a/config/settings/speech-dispatcher.settings.conf +++ b/config/settings/speech-dispatcher.settings.conf @@ -198,10 +198,10 @@ highlight=False [remote] enable=True -# connection type -# unix = unix sockets -# tcp = tcp (localhost only) -method=unix +# driver +# unixDriver = unix sockets +# tcpDriver = tcp (localhost only) +driver=unixDriver # tcp port port=22447 # socket filepath diff --git a/src/fenrirscreenreader/core/outputManager.py b/src/fenrirscreenreader/core/outputManager.py index 02cb5501..e7450fab 100644 --- a/src/fenrirscreenreader/core/outputManager.py +++ b/src/fenrirscreenreader/core/outputManager.py @@ -14,11 +14,11 @@ class outputManager(): def initialize(self, environment): self.env = environment self.env['runtime']['settingsManager'].loadDriver(\ - self.env['runtime']['settingsManager'].getSetting('speech', 'driver'), 'speechDriver') + self.env['runtime']['settingsManager'].getSetting('speech', 'driver'), 'speechDriver') self.env['runtime']['settingsManager'].loadDriver(\ - self.env['runtime']['settingsManager'].getSetting('sound', 'driver'), 'soundDriver') + self.env['runtime']['settingsManager'].getSetting('sound', 'driver'), 'soundDriver') self.env['runtime']['settingsManager'].loadDriver(\ - self.env['runtime']['settingsManager'].getSetting('braille', 'driver'), 'brailleDriver') + self.env['runtime']['settingsManager'].getSetting('braille', 'driver'), 'brailleDriver') def shutdown(self): self.env['runtime']['settingsManager'].shutdownDriver('soundDriver') self.env['runtime']['settingsManager'].shutdownDriver('speechDriver') diff --git a/src/fenrirscreenreader/core/remoteDriver.py b/src/fenrirscreenreader/core/remoteDriver.py new file mode 100644 index 00000000..2baac894 --- /dev/null +++ b/src/fenrirscreenreader/core/remoteDriver.py @@ -0,0 +1,16 @@ +#!/bin/python +# -*- coding: utf-8 -*- + +# Fenrir TTY screen reader +# By Chrys, Storm Dragon, and contributers. + +from fenrirscreenreader.core import debug + +class remoteDriver(): + def __init__(self): + self._isInitialized = False + def initialize(self, environment): + self.env = environment + self._isInitialized = True + def shutdown(self): + self._isInitialized = False diff --git a/src/fenrirscreenreader/core/remoteManager.py b/src/fenrirscreenreader/core/remoteManager.py index 18ba6207..646293cd 100644 --- a/src/fenrirscreenreader/core/remoteManager.py +++ b/src/fenrirscreenreader/core/remoteManager.py @@ -29,9 +29,6 @@ command interrupt 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): @@ -48,83 +45,11 @@ class remoteManager(): self.resetSettingConst = 'RESET' def initialize(self, environment): self.env = environment - - if self.env['runtime']['settingsManager'].getSettingAsBool('remote', 'enabled'): - if self.env['runtime']['settingsManager'].getSetting('remote', 'method').upper() == 'UNIX': - self.env['runtime']['processManager'].addCustomEventThread(self.unixSocketWatchDog, multiprocess=True) - elif self.env['runtime']['settingsManager'].getSetting('remote', 'method').upper() == 'TCP': - self.env['runtime']['processManager'].addCustomEventThread(self.tcpWatchDog, multiprocess=True) + self.env['runtime']['settingsManager'].loadDriver(\ + self.env['runtime']['settingsManager'].getSetting('remote', 'driver'), 'remoteDriver') def shutdown(self): - if self.sock: - self.sock.close() - self.sock = None - def unixSocketWatchDog(self, active, eventQueue): - # echo "command say this is a test" | socat - UNIX-CLIENT:/tmp/fenrirscreenreader-deamon.sock + self.env['runtime']['settingsManager'].shutdownDriver('remoteDriver') - 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 - client_sock.close() - - if os.path.exists(socketpath): - os.remove(socketpath) - if self.sock: - self.sock.close() - self.sock = None - def tcpWatchDog(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.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) - 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 - client_sock.close() - if self.sock: - self.sock.close() - self.sock = None def handleSettingsChange(self, settingsText): if not self.env['runtime']['settingsManager'].getSettingAsBool('remote', 'enableSettingsRemote'): return @@ -200,6 +125,8 @@ class remoteManager(): if not eventData: return upperEventData = eventData.upper() + self.env['runtime']['debug'].writeDebugOut('remoteManager:handleRemoteIncomming: event: ' + str(eventData),debug.debugLevel.INFO) + if upperEventData.startswith(self.settingConst): settingsText = eventData[len(self.settingConst):] self.handleSettingsChange(settingsText) diff --git a/src/fenrirscreenreader/core/runtimeData.py b/src/fenrirscreenreader/core/runtimeData.py index e0384619..7f756440 100644 --- a/src/fenrirscreenreader/core/runtimeData.py +++ b/src/fenrirscreenreader/core/runtimeData.py @@ -11,6 +11,7 @@ runtimeData = { 'screenDriver': None, 'soundDriver': None, 'inputDriver': None, +'remoteDriver': None, 'brailleDriver': None, 'inputManager': None, 'commandManager': None, diff --git a/src/fenrirscreenreader/core/settingsData.py b/src/fenrirscreenreader/core/settingsData.py index e7ee9c66..d69cc0c8 100644 --- a/src/fenrirscreenreader/core/settingsData.py +++ b/src/fenrirscreenreader/core/settingsData.py @@ -82,7 +82,7 @@ settingsData = { }, 'remote':{ 'enabled': True, - 'method': 'unix', + 'driver': 'unixDriver', 'port': 22447, 'socketpath':'/tmp/', 'enableSettingsRemote': True, diff --git a/src/fenrirscreenreader/remoteDriver/__init__.py b/src/fenrirscreenreader/remoteDriver/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/src/fenrirscreenreader/remoteDriver/dummyDriver.py b/src/fenrirscreenreader/remoteDriver/dummyDriver.py new file mode 100644 index 00000000..4dbba2b8 --- /dev/null +++ b/src/fenrirscreenreader/remoteDriver/dummyDriver.py @@ -0,0 +1,12 @@ +#!/bin/python +# -*- coding: utf-8 -*- + +# Fenrir TTY screen reader +# By Chrys, Storm Dragon, and contributers. + +from fenrirscreenreader.core import debug +from fenrirscreenreader.core.remoteDriver import remoteDriver + +class driver(remoteDriver): + def __init__(self): + remoteDriver.__init__(self) diff --git a/src/fenrirscreenreader/remoteDriver/tcpDriver.py b/src/fenrirscreenreader/remoteDriver/tcpDriver.py new file mode 100644 index 00000000..9b6e6407 --- /dev/null +++ b/src/fenrirscreenreader/remoteDriver/tcpDriver.py @@ -0,0 +1,47 @@ +#!/bin/python +# -*- coding: utf-8 -*- + +# Fenrir TTY screen reader +# By Chrys, Storm Dragon, and contributers. + +from fenrirscreenreader.core import debug +from fenrirscreenreader.core.remoteDriver import remoteDriver +from fenrirscreenreader.core.eventData import fenrirEventType + +import select, socket, os, os.path + +class driver(remoteDriver): + def __init__(self): + remoteDriver.__init__(self) + def initialize(self, environment): + self.env = environment + 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.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) + 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 + client_sock.close() + if self.sock: + self.sock.close() + self.sock = None diff --git a/src/fenrirscreenreader/remoteDriver/unixDriver.py b/src/fenrirscreenreader/remoteDriver/unixDriver.py new file mode 100644 index 00000000..83032e8b --- /dev/null +++ b/src/fenrirscreenreader/remoteDriver/unixDriver.py @@ -0,0 +1,57 @@ +#!/bin/python +# -*- coding: utf-8 -*- + +# Fenrir TTY screen reader +# By Chrys, Storm Dragon, and contributers. + +from fenrirscreenreader.core import debug +from fenrirscreenreader.core.remoteDriver import remoteDriver +from fenrirscreenreader.core.eventData import fenrirEventType + +import select, socket, os, os.path + + +class driver(remoteDriver): + def __init__(self): + remoteDriver.__init__(self) + def initialize(self, environment): + self.env = environment + 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 + client_sock.close() + + if os.path.exists(socketpath): + os.remove(socketpath) + if self.sock: + self.sock.close() + self.sock = None