From a6ef961a5585af1209e35d3d70c7be15b53c1e92 Mon Sep 17 00:00:00 2001 From: "Schimon Jehudah, Adv." <sjehuda@yandex.com> Date: Mon, 10 Mar 2025 11:05:35 +0200 Subject: [PATCH] Improve connectivity handling (Thank you. mathieui). --- blasta/http/instance.py | 17 +++++++++-------- blasta/xmpp/instance.py | 8 ++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/blasta/http/instance.py b/blasta/http/instance.py index 97f7f84..954282a 100644 --- a/blasta/http/instance.py +++ b/blasta/http/instance.py @@ -1274,20 +1274,21 @@ class HttpInstance: response: Response, jabber_id: str = Form(...), password: str = Form(...)): + request_url_path = request.url.path function_name = sys._getframe().f_code.co_name - logger.debug(f'{function_name},{jabber_id},Start') + logger.debug(f'{function_name},{request_url_path},Start') if not UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request): # Store a variable in the request's state request.app.state.jabber_id = jabber_id session_key = str(random.random()) request.app.state.session_key = session_key accounts[jabber_id] = XmppInstance(jabber_id + '/blasta', password) - breakpoint() - # accounts[jabber_id].authenticated - # dir(accounts[jabber_id]) - # accounts[jabber_id].failed_auth - # accounts[jabber_id].event_when_connected - sessions[jabber_id] = session_key + if accounts[jabber_id].is_connected: + sessions[jabber_id] = session_key + else: + del accounts[jabber_id] + jabber_id = None + # Check if the user and password are present and valid # If not valid, return "Could not connect to JID" @@ -1348,7 +1349,7 @@ class HttpInstance: message = 'Blasta system message ยป Authorization has failed.' description = 'Connection has failed' path = 'error' - logger.error(f'{function_name},{request_url_path},{description}') + logger.error(f'{function_name},{jabber_id},{description}') response = result_post(request, jabber_id, description, message, path) logger.debug(f'{function_name},{request_url_path},Finish') return response diff --git a/blasta/xmpp/instance.py b/blasta/xmpp/instance.py index 179026d..c41b337 100644 --- a/blasta/xmpp/instance.py +++ b/blasta/xmpp/instance.py @@ -24,22 +24,22 @@ class XmppInstance(ClientXMPP): self.register_plugin('xep_0163') # XEP-0163: Personal Eventing Protocol self.register_plugin('xep_0223') # XEP-0223: Persistent Storage of Private Data via PubSub self.connect() - self.connection_accepted = False + self.is_connected = False def on_connection_failed(self, event): function_name = sys._getframe().f_code.co_name logger.debug(f'{function_name},{event},Start') - self.connection_accepted = False + self.is_connected = False logger.debug(f'{function_name},{event},Finish') def on_failed_auth(self, event): function_name = sys._getframe().f_code.co_name logger.debug(f'{function_name},{event},Start') - self.connection_accepted = False + self.is_connected = False logger.debug(f'{function_name},{event},Finish') def on_session_start(self, event): function_name = sys._getframe().f_code.co_name logger.debug(f'{function_name},{event},Start') - self.connection_accepted = True + self.is_connected = True logger.debug(f'{function_name},{event},Finish')