add unix socket method

This commit is contained in:
chrys 2018-09-05 23:03:07 +02:00
parent 1fe54c6d97
commit 76122a93ad

View File

@ -8,14 +8,15 @@ from fenrirscreenreader.core import debug
from fenrirscreenreader.core.eventData import fenrirEventType from fenrirscreenreader.core.eventData import fenrirEventType
import time import time
import select import select
import socket
import os, os.path
class remoteManager(): class remoteManager():
def __init__(self): def __init__(self):
pass pass
def initialize(self, environment): def initialize(self, environment):
self.env = 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'].getSettingAsBool('remote', 'enabled'):
if self.env['runtime']['settingsManager'].getSetting('remote', 'method') == 'unix': if self.env['runtime']['settingsManager'].getSetting('remote', 'method') == 'unix':
self.env['runtime']['processManager'].addCustomEventThread(self.unixSocketWatchDog, multiprocess=True) self.env['runtime']['processManager'].addCustomEventThread(self.unixSocketWatchDog, multiprocess=True)
@ -26,15 +27,36 @@ class remoteManager():
self.sock.close() self.sock.close()
self.sock = None self.sock = None
def unixSocketWatchDog(self, active, eventQueue): 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: while active.value == 1:
eventQueue.put({"Type":fenrirEventType.RemoteIncomming, client_sock, client_addr = self.sock.accept()
"Data":'#!#command##say##this is a test' if client_sock:
}) # Check if the client is still connected and if data is available:
eventQueue.put({"Type":fenrirEventType.RemoteIncomming, try:
"Data":'#!#command##interrupt' 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): 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 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.host = '0.0.0.0' self.host = '0.0.0.0'
@ -53,7 +75,7 @@ class remoteManager():
rawdata = client_sock.recv(8129) rawdata = client_sock.recv(8129)
try: try:
data = rawdata.decode("utf-8").rstrip() data = rawdata.decode("utf-8").rstrip()
if data.startswith('#!#'): if data.startswith('#<=>#'):
eventQueue.put({"Type":fenrirEventType.RemoteIncomming, eventQueue.put({"Type":fenrirEventType.RemoteIncomming,
"Data": data "Data": data
}) })
@ -91,17 +113,17 @@ class remoteManager():
if not eventData: if not eventData:
return return
# settings: # settings:
# #!#settings##set##section#setting=value[,section#setting=value] # #<=>#settings##set##section#setting=value[,section#setting=value]
# #!#settings##set##speech#voice=de # #<=>#settings##set##speech#voice=de
# #!#settings##reset # #<=>#settings##reset
# execute command: # execute command:
# #!#command##exec#say##this is a test # #<=>#command##exec#say##this is a test
# #!#command##cancel##say # #<=>#command##cancel##say
if not eventData.startswith('#!#'): if not eventData.startswith('#<=>#'):
return return
if eventData.startswith('#!#settings##'): if eventData.startswith('#<=>#settings##'):
settingsText = eventData[len('#!#settings##'):] settingsText = eventData[len('#<=>#settings##'):]
self.handleSettingsChange(settingsText) self.handleSettingsChange(settingsText)
elif eventData.startswith('#!#command##'): elif eventData.startswith('#<=>#command##'):
commandText = eventData[len('#!#command##'):] commandText = eventData[len('#<=>#command##'):]
self.handleCommandExecution(commandText) self.handleCommandExecution(commandText)