From 693c4f4c5c096e067b7d5621f595e26f2b794824 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 13 Nov 2024 19:41:20 +0100 Subject: src: add user session detection implementation Detect user login and logout for Linux and Windows. Poll the session change in 5 second intervals in a thread. Use the same event socket previously used by the old session detection mechanism to notify a session change. Report an active interactive session through the /refresh response so a new ogserver instance can update the session status. --- src/ogClient.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/ogClient.py') diff --git a/src/ogClient.py b/src/ogClient.py index 51aa33b..2635a3f 100644 --- a/src/ogClient.py +++ b/src/ogClient.py @@ -6,6 +6,7 @@ # Free Software Foundation; either version 3 of the License, or # (at your option) any later version. +import threading import errno import select import socket @@ -26,6 +27,8 @@ class State(Enum): class ogClient: OG_PATH = '/opt/opengnsys/' + SESSION_POLL_INTERVAL = 5 + EVENT_SOCKET_PORT = 55885 def __init__(self, config): self.CONFIG = config @@ -36,7 +39,7 @@ class ogClient: if self.mode in {'linux', 'windows'}: self.event_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.event_sock.setblocking(0) - self.event_sock.bind(('127.0.0.1', 55885)) + self.event_sock.bind(('127.0.0.1', ogClient.EVENT_SOCKET_PORT)) else: self.event_sock = None @@ -49,6 +52,25 @@ class ogClient: self.ogrest = ogRest(self.CONFIG) self.seq = None + self.session_check_thread = threading.Thread(target=self._session_check_loop, daemon=True) + self.session_check_thread.start() + + def _session_check_loop(self): + while True: + session_status = self.ogrest.check_interactive_session_change() + if session_status is True: + message = "session start user" + elif session_status is False: + message = "session stop user" + else: + message = None + + if message: + self.event_sock.sendto(message.encode('utf-8'), + ('127.0.0.1', ogClient.EVENT_SOCKET_PORT)) + + time.sleep(ogClient.SESSION_POLL_INTERVAL) + def get_socket(self): return self.sock -- cgit v1.2.3-18-g5258