Working (not implemented already the user desktop part) windows actor

This commit is contained in:
Adolfo Gómez García 2014-10-27 18:40:18 +01:00
parent 74148210a9
commit a2d81b138d
7 changed files with 81 additions and 31 deletions

View File

@ -51,10 +51,8 @@ class MyForm(QtGui.QDialog):
logger.debug('Setting configuration parameters in form')
self.ui.host.setText(data.get('host', ''))
self.ui.masterKey.setText(data.get('masterKey', ''))
self.ui.useSSl.setCurrentIndex(
1 if data.get('ssl', False) is True else 0)
self.ui.logLevelComboBox.setCurrentIndex(
data.get('logLevel', 10000) / 10000 - 1)
self.ui.useSSl.setCurrentIndex(1 if data.get('ssl', False) is True else 0)
self.ui.logLevelComboBox.setCurrentIndex(data.get('logLevel', 10000) / 10000 - 1)
def _getCfg(self):
return {
@ -65,8 +63,7 @@ class MyForm(QtGui.QDialog):
}
def textChanged(self):
enableButtons = self.ui.host.text(
) != '' and self.ui.masterKey.text() != ''
enableButtons = self.ui.host.text() != '' and self.ui.masterKey.text() != ''
self.ui.testButton.setEnabled(enableButtons)
self.ui.saveButton.setEnabled(enableButtons)
@ -111,8 +108,6 @@ if __name__ == "__main__":
else:
logger.setLevel(20000)
logger.debug('Configuration: {}'.format(cfg))
myapp = MyForm(cfg)
myapp.show()
sys.exit(app.exec_())

View File

@ -345,8 +345,9 @@ class UDSActorSvc(win32serviceutil.ServiceFramework):
address = (self.knownIps[self.api.mac], random.randrange(32000, 64000))
logger.debug('Starting REST listener at {}'.format(address))
self.httpServer = httpserver.HTTPServerThread(address, self.ipc)
self.httpServer.start()
# And notify it to broker
self.api.notifyComm(self.httpServer.getServerUrl() )
self.api.notifyComm(self.httpServer.getServerUrl())
# ********************************
# * Registers SENS subscriptions *
@ -395,9 +396,21 @@ class UDSActorSvc(win32serviceutil.ServiceFramework):
# Remove IPC threads
if self.ipc is not None:
self.ipc.stop()
try:
self.ipc.stop()
except:
logger.error('Couln\'t stop ipc server')
if self.httpServer is not None:
self.httpServer.stop()
try:
self.httpServer.stop()
except:
logger.error('Couln\'t stop REST server')
if self.api is not None:
try:
self.api.notifyComm(None)
except:
logger.error('Couln\'t remove comms url from broker')
self.notifyStop()

View File

@ -58,27 +58,36 @@ class MessagesProcessor(QtCore.QThread):
self.ipc.stop()
def requestInformation(self):
self.ipc.requestInformation()
info = self.ipc.requestInformation()
logger.debug('Request information: {}'.format(info))
def run(self):
self.running = True
while self.running and self.ipc.running:
msg = self.ipc.getMessage()
if msg is None:
break
msgId, data = msg
if msgId == ipc.MSG_MESSAGE:
self.displayMessage.emit(QtCore.QString.fromUtf8(data))
elif msgId == ipc.MSG_LOGOFF:
self.logoff.emit()
elif msgId == ipc.MSG_SCRIPT:
self.script.emit(QtCore.QString.fromUtf8(data))
elif msgId == ipc.MSG_INFORMATION:
self.information.emit(cPickle.loads(data))
try:
msg = self.ipc.getMessage()
if msg is None:
break
msgId, data = msg
logger.debug('Got Message on User Space: {}:{}'.format(msgId, data))
if msgId == ipc.MSG_MESSAGE:
self.displayMessage.emit(QtCore.QString.fromUtf8(data))
elif msgId == ipc.MSG_LOGOFF:
self.logoff.emit()
elif msgId == ipc.MSG_SCRIPT:
self.script.emit(QtCore.QString.fromUtf8(data))
elif msgId == ipc.MSG_INFORMATION:
self.information.emit(cPickle.loads(data))
except Exception as e:
try:
logger.error('Got error on IPC thread {}'.format(e))
except:
logger.error('Got error on IPC thread (and unicode error??)')
if self.ipc.running is False:
if self.ipc.running is False and self.running == True:
logger.warn('Lost connection with Service, closing program')
self.exit.emit()
self.exit.emit()
class SystemTrayIconApp(QtGui.QSystemTrayIcon):

View File

@ -118,10 +118,15 @@ def testServer():
import random
import requests
import json
import logging
from udsactor import httpserver
from udsactor import ipc
# Disable verify warinings
logging.getLogger("requests").setLevel(logging.ERROR)
requests.packages.urllib3.disable_warnings()
s = ipc.ServerIPC(39188) # I have got the enterprise number for Virtual Cable. This number is not about ports, but as good as any other selection :)
s.start()
@ -172,10 +177,34 @@ def testServer():
s.stop()
client.stop()
def testRemote():
import requests
import json
serverUrl = "https://172.27.0.208:52562/633a1245873848b7b4017c23283bc195"
print serverUrl
res = requests.post(serverUrl + '/message', data=json.dumps({'message': 'Test message'}), headers={'content-type': 'application/json'}, verify=False)
print res
print res.json()
res = requests.post(serverUrl + '/script', data=json.dumps({'script': 'import time\ntime.sleep(1)\nfor v in xrange(10): print "Hello world, this is an script"'}), headers={'content-type': 'application/json'}, verify=False)
print res
print res.json()
res = requests.post(serverUrl + '/script', data=json.dumps({'script': 'print "Hello world, this is an script"', 'user': True}), headers={'content-type': 'application/json'}, verify=False)
print res
print res.json()
res = requests.get(serverUrl + '/information?param1=1&param2=2', headers={'content-type': 'application/json'}, verify=False)
print res
print res.json()
if __name__ == '__main__':
#ipcServer()
#testRest()
#testIdle()
testServer()
#testRemote()

View File

@ -104,8 +104,9 @@ class Api(object):
self.uuid = None
self.url = "{}://{}/rest/actor/".format(('http', 'https')[ssl], self.host)
self.secretKey = uuid.uuid4().get_hex()
# Disable logging requests messages except for warnings, errors, ...
logging.getLogger("requests").setLevel(logging.WARNING)
# Disable logging requests messages except for errors, ...
requests.packages.urllib3.disable_warnings()
logging.getLogger("requests").setLevel(logging.ERROR)
def _getUrl(self, method, key=None, ids=None):
url = self.url + method
@ -166,7 +167,6 @@ class Api(object):
if processData:
data = json.dumps({'data': data})
print data
url = self._getUrl('/'.join([self.uuid, msg]))
return self._request(url, data)['result']

View File

@ -136,6 +136,7 @@ class HTTPServerHandler(SimpleHTTPServer.BaseHTTPServer.BaseHTTPRequestHandler):
def post_logoff(self, params):
logger.debug('Sending LOGOFF to clients')
HTTPServerHandler.ipc.sendLoggofMessage()
return 'ok'
# Alias
post_logout = post_logoff
@ -145,6 +146,7 @@ class HTTPServerHandler(SimpleHTTPServer.BaseHTTPServer.BaseHTTPRequestHandler):
if 'message' not in params:
raise Exception('Invalid message parameters')
HTTPServerHandler.ipc.sendMessageMessage(params['message'])
return 'ok'
def post_script(self, params):
if 'script' not in params:
@ -164,6 +166,7 @@ class HTTPServerHandler(SimpleHTTPServer.BaseHTTPServer.BaseHTTPRequestHandler):
logger.error('Error executing script: {}'.format(e))
th = threading.Thread(target=executor)
th.start()
return 'ok'
def get_information(self, params):
# TODO: Return something useful? :)
@ -187,6 +190,7 @@ class HTTPServerThread(threading.Thread):
return 'https://{}:{}/{}'.format(self.server.server_address[0], self.server.server_address[1], HTTPServerHandler.uuid)
def stop(self):
logger.debug('Stopping REST Service')
self.server.shutdown()
def run(self):

View File

@ -43,9 +43,9 @@ def encoder(data):
def decoder(data):
return data.decode('bz2')
DEBUG = True
DEBUG = False
path = 'Software\\UDSEnterpriseActor'
path = 'Software\\UDSActor'
baseKey = wreg.HKEY_CURRENT_USER if DEBUG is True else wreg.HKEY_LOCAL_MACHINE
def checkPermissions():