Added so if ipc is closed, and logout is sent (if already logged in ofc)

This commit is contained in:
Adolfo Gómez García 2019-03-28 11:58:28 +01:00
parent fddc69e0ea
commit f79f659711
2 changed files with 15 additions and 11 deletions

View File

@ -33,11 +33,11 @@ from __future__ import unicode_literals
import socket import socket
import threading import threading
import sys import sys
import six
import traceback import traceback
import pickle import pickle
import errno import errno
import time import time
import six
from udsactor.utils import toUnicode from udsactor.utils import toUnicode
from udsactor.log import logger 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 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 # Allows notifying login/logout from client for linux platform
ALLOW_LOG_METHODS = sys.platform != 'win32' ALLOW_LOG_METHODS = sys.platform != 'win32'
# States for client processor # States for client processor
ST_SECOND_BYTE = 0x01 ST_SECOND_BYTE = 0x01
ST_RECEIVING = 0x02 ST_RECEIVING = 0x02
@ -101,8 +99,9 @@ ST_PROCESS_MESSAGE = 0x02
class ClientProcessor(threading.Thread): class ClientProcessor(threading.Thread):
def __init__(self, parent, clientSocket): def __init__(self, parent, clientSocket):
super(self.__class__, self).__init__() super(ClientProcessor, self).__init__()
self.parent = parent self.parent = parent
self.clientSocket = clientSocket self.clientSocket = clientSocket
self.running = False self.running = False
@ -133,6 +132,7 @@ class ClientProcessor(threading.Thread):
if b == b'': if b == b'':
# Client disconnected # Client disconnected
self.running = False self.running = False
self.processRequest(REQ_LOGOUT, 'CLIENT_CONNECTION_LOST')
break break
buf = six.byte2int(b) # Empty buffer, this is set as non-blocking buf = six.byte2int(b) # Empty buffer, this is set as non-blocking
if state is None: if state is None:
@ -187,8 +187,8 @@ class ClientProcessor(threading.Thread):
try: try:
m = msg[1] if msg[1] is not None else b'' m = msg[1] if msg[1] is not None else b''
l = len(m) ln = len(m)
data = MAGIC + six.int2byte(msg[0]) + six.int2byte(l & 0xFF) + six.int2byte(l >> 8) + m data = MAGIC + six.int2byte(msg[0]) + six.int2byte(ln & 0xFF) + six.int2byte(ln >> 8) + m
try: try:
self.clientSocket.sendall(data) self.clientSocket.sendall(data)
except socket.error as e: except socket.error as e:
@ -208,7 +208,7 @@ class ClientProcessor(threading.Thread):
class ServerIPC(threading.Thread): class ServerIPC(threading.Thread):
def __init__(self, listenPort, clientMessageProcessor=None): def __init__(self, listenPort, clientMessageProcessor=None):
super(self.__class__, self).__init__() super(ServerIPC, self).__init__()
self.port = listenPort self.port = listenPort
self.running = False self.running = False
self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -291,6 +291,7 @@ class ServerIPC(threading.Thread):
class ClientIPC(threading.Thread): class ClientIPC(threading.Thread):
def __init__(self, listenPort): def __init__(self, listenPort):
super(ClientIPC, self).__init__() super(ClientIPC, self).__init__()
self.port = listenPort self.port = listenPort
@ -320,8 +321,8 @@ class ClientIPC(threading.Thread):
if isinstance(data, six.text_type): # Convert to bytes if necessary if isinstance(data, six.text_type): # Convert to bytes if necessary
data = data.encode('utf-8') data = data.encode('utf-8')
l = len(data) ln = len(data)
msg = six.int2byte(msg) + six.int2byte(l & 0xFF) + six.int2byte(l >> 8) + data msg = six.int2byte(msg) + six.int2byte(ln & 0xFF) + six.int2byte(ln >> 8) + data
self.clientSocket.sendall(msg) self.clientSocket.sendall(msg)
def requestInformation(self): def requestInformation(self):

View File

@ -86,6 +86,7 @@ class CommonService(object):
self.httpServer = None self.httpServer = None
self.rebootRequested = False self.rebootRequested = False
self.knownIps = [] self.knownIps = []
self.loggedIn = False
socket.setdefaulttimeout(20) socket.setdefaulttimeout(20)
def reboot(self): def reboot(self):
@ -259,14 +260,16 @@ class CommonService(object):
logger.info('Rest api not ready') logger.info('Rest api not ready')
return 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') res = self.api.login(data).split('\t')
# third parameter, if exists, sets maxSession duration to this. # third parameter, if exists, sets maxSession duration to this.
# First & second parameters are ip & hostname of connection source # First & second parameters are ip & hostname of connection source
if len(res) >= 3: if len(res) >= 3:
self.api.maxSession = int(res[2]) # Third parameter is max session duration 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 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.api.logout(data)
self.onLogout(data) self.onLogout(data)
if msg == ipc.REQ_INFORMATION: if msg == ipc.REQ_INFORMATION: