From f79f659711bc5cc31d632547247c6a0c73fb67ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Thu, 28 Mar 2019 11:58:28 +0100 Subject: [PATCH] Added so if ipc is closed, and logout is sent (if already logged in ofc) --- actors/src/udsactor/ipc.py | 19 ++++++++++--------- actors/src/udsactor/service.py | 7 +++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/actors/src/udsactor/ipc.py b/actors/src/udsactor/ipc.py index fa8e3653..509449a0 100644 --- a/actors/src/udsactor/ipc.py +++ b/actors/src/udsactor/ipc.py @@ -33,11 +33,11 @@ from __future__ import unicode_literals import socket import threading import sys -import six import traceback import pickle import errno import time +import six from udsactor.utils import toUnicode from udsactor.log import logger @@ -89,11 +89,9 @@ REV_DICT = { MAGIC = b'\x55\x44\x53\x00' # UDS in hexa with a padded 0 to the right - # Allows notifying login/logout from client for linux platform ALLOW_LOG_METHODS = sys.platform != 'win32' - # States for client processor ST_SECOND_BYTE = 0x01 ST_RECEIVING = 0x02 @@ -101,8 +99,9 @@ ST_PROCESS_MESSAGE = 0x02 class ClientProcessor(threading.Thread): + def __init__(self, parent, clientSocket): - super(self.__class__, self).__init__() + super(ClientProcessor, self).__init__() self.parent = parent self.clientSocket = clientSocket self.running = False @@ -133,6 +132,7 @@ class ClientProcessor(threading.Thread): if b == b'': # Client disconnected self.running = False + self.processRequest(REQ_LOGOUT, 'CLIENT_CONNECTION_LOST') break buf = six.byte2int(b) # Empty buffer, this is set as non-blocking if state is None: @@ -187,8 +187,8 @@ class ClientProcessor(threading.Thread): try: m = msg[1] if msg[1] is not None else b'' - l = len(m) - data = MAGIC + six.int2byte(msg[0]) + six.int2byte(l & 0xFF) + six.int2byte(l >> 8) + m + ln = len(m) + data = MAGIC + six.int2byte(msg[0]) + six.int2byte(ln & 0xFF) + six.int2byte(ln >> 8) + m try: self.clientSocket.sendall(data) except socket.error as e: @@ -208,7 +208,7 @@ class ClientProcessor(threading.Thread): class ServerIPC(threading.Thread): def __init__(self, listenPort, clientMessageProcessor=None): - super(self.__class__, self).__init__() + super(ServerIPC, self).__init__() self.port = listenPort self.running = False self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -291,6 +291,7 @@ class ServerIPC(threading.Thread): class ClientIPC(threading.Thread): + def __init__(self, listenPort): super(ClientIPC, self).__init__() self.port = listenPort @@ -320,8 +321,8 @@ class ClientIPC(threading.Thread): if isinstance(data, six.text_type): # Convert to bytes if necessary data = data.encode('utf-8') - l = len(data) - msg = six.int2byte(msg) + six.int2byte(l & 0xFF) + six.int2byte(l >> 8) + data + ln = len(data) + msg = six.int2byte(msg) + six.int2byte(ln & 0xFF) + six.int2byte(ln >> 8) + data self.clientSocket.sendall(msg) def requestInformation(self): diff --git a/actors/src/udsactor/service.py b/actors/src/udsactor/service.py index 4af97d7e..046b27ec 100644 --- a/actors/src/udsactor/service.py +++ b/actors/src/udsactor/service.py @@ -86,6 +86,7 @@ class CommonService(object): self.httpServer = None self.rebootRequested = False self.knownIps = [] + self.loggedIn = False socket.setdefaulttimeout(20) def reboot(self): @@ -259,14 +260,16 @@ class CommonService(object): logger.info('Rest api not ready') return - if msg == ipc.REQ_LOGIN: + if msg == ipc.REQ_LOGIN and self.loggedIn is False: + self.loggedIn = True res = self.api.login(data).split('\t') # third parameter, if exists, sets maxSession duration to this. # First & second parameters are ip & hostname of connection source if len(res) >= 3: self.api.maxSession = int(res[2]) # Third parameter is max session duration msg = ipc.REQ_INFORMATION # Senf information, requested or not, to client on login notification - if msg == ipc.REQ_LOGOUT: + if msg == ipc.REQ_LOGOUT and self.loggedIn is True: + self.loggedIn = False self.api.logout(data) self.onLogout(data) if msg == ipc.REQ_INFORMATION: