diff libervia/server/server.py @ 1506:ce879da7fcf7

server: fix `on_signal` callback
author Goffi <goffi@goffi.org>
date Thu, 23 Mar 2023 17:50:54 +0100
parents a169cbc315f0
children 106bae41f5c8
line wrap: on
line diff
--- a/libervia/server/server.py	Sat Mar 04 18:37:17 2023 +0100
+++ b/libervia/server/server.py	Thu Mar 23 17:50:54 2023 +0100
@@ -134,7 +134,7 @@
         self._checkCallback(dir_path, wrapped_callback, recursive)
 
 
-class LiberviaSession(server.Session):
+class WebSession(server.Session):
     sessionTimeout = C.SESSION_TIMEOUT
 
     def __init__(self, *args, **kwargs):
@@ -1284,7 +1284,7 @@
             self.vhost_root, [server.GzipEncoderFactory()]
         )
         self.site = server.Site(wrapped)
-        self.site.sessionFactory = LiberviaSession
+        self.site.sessionFactory = WebSession
 
     def _bridgeCb(self):
         del self._bridge_retry
@@ -1368,13 +1368,11 @@
         if not profile:
             log.error(f"got signal without profile: {signal_name}, {args}")
             return
-        try:
-            sockets = websockets.LiberviaPageWSProtocol.profile_map[profile]
-        except KeyError:
-            log.debug(f"no socket opened for profile {profile}")
-            return
-        for socket in sockets:
-            socket.send("bridge", {"signal": signal_name, "args": args})
+        session_iface.WebSession.send(
+            profile,
+            "bridge",
+            {"signal": signal_name, "args": args}
+        )
 
     def application_started_handler(
         self,
@@ -1412,37 +1410,37 @@
         register_with_ext_jid = self.waiting_profiles.getRegisterWithExtJid(profile)
         self.waiting_profiles.purgeRequest(profile)
         session = request.getSession()
-        sat_session = session_iface.ISATSession(session)
-        if sat_session.profile:
+        web_session = session_iface.IWebSession(session)
+        if web_session.profile:
             log.error(_("/!\\ Session has already a profile, this should NEVER happen!"))
             raise failure.Failure(exceptions.ConflictError("Already active"))
 
         # XXX: we force string because python D-Bus has its own string type (dbus.String)
         #   which may cause trouble when exposing it to scripts
-        sat_session.profile = str(profile)
+        web_session.profile = str(profile)
         self.prof_connected.add(profile)
         cache_dir = os.path.join(
             self.cache_root_dir, "profiles", regex.pathEscape(profile)
         )
         # FIXME: would be better to have a global /cache URL which redirect to
         #        profile's cache directory, without uuid
-        self.cache_resource.putChild(sat_session.uuid.encode('utf-8'),
+        self.cache_resource.putChild(web_session.uuid.encode('utf-8'),
                                      ProtectedFile(cache_dir))
         log.debug(
             _("profile cache resource added from {uuid} to {path}").format(
-                uuid=sat_session.uuid, path=cache_dir
+                uuid=web_session.uuid, path=cache_dir
             )
         )
 
         def on_expire():
             log.info("Session expired (profile={profile})".format(profile=profile))
-            self.cache_resource.delEntity(sat_session.uuid.encode('utf-8'))
+            self.cache_resource.delEntity(web_session.uuid.encode('utf-8'))
             log.debug(
-                _("profile cache resource {uuid} deleted").format(uuid=sat_session.uuid)
+                _("profile cache resource {uuid} deleted").format(uuid=web_session.uuid)
             )
-            sat_session.on_expire()
-            if sat_session.ws_socket is not None:
-                sat_session.ws_socket.close()
+            web_session.on_expire()
+            if web_session.ws_socket is not None:
+                web_session.ws_socket.close()
             # and now we disconnect the profile
             self.bridgeCall("disconnect", profile)
 
@@ -1450,12 +1448,12 @@
 
         # FIXME: those session infos should be returned by connect or isConnected
         infos = await self.bridgeCall("sessionInfosGet", profile)
-        sat_session.jid = jid.JID(infos["jid"])
-        own_bare_jid_s = sat_session.jid.userhost()
+        web_session.jid = jid.JID(infos["jid"])
+        own_bare_jid_s = web_session.jid.userhost()
         own_id_raw = await self.bridgeCall(
             "identityGet", own_bare_jid_s, [], True, profile)
-        sat_session.identities[own_bare_jid_s] = data_format.deserialise(own_id_raw)
-        sat_session.backend_started = int(infos["started"])
+        web_session.identities[own_bare_jid_s] = data_format.deserialise(own_id_raw)
+        web_session.backend_started = int(infos["started"])
 
         state = C.PROFILE_LOGGED_EXT_JID if register_with_ext_jid else C.PROFILE_LOGGED
         return state
@@ -1544,15 +1542,15 @@
             connect_method = "connect"
 
         # we check if there is not already an active session
-        sat_session = session_iface.ISATSession(request.getSession())
-        if sat_session.profile:
+        web_session = session_iface.IWebSession(request.getSession())
+        if web_session.profile:
             # yes, there is
-            if sat_session.profile != profile:
+            if web_session.profile != profile:
                 # it's a different profile, we need to disconnect it
                 log.warning(_(
                     "{new_profile} requested login, but {old_profile} was already "
                     "connected, disconnecting {old_profile}").format(
-                        old_profile=sat_session.profile, new_profile=profile))
+                        old_profile=web_session.profile, new_profile=profile))
                 self.purgeSession(request)
 
         if self.waiting_profiles.getRequest(profile):
@@ -1589,16 +1587,16 @@
         if connected:
             #  profile is already connected in backend
             # do we have a corresponding session in Libervia?
-            sat_session = session_iface.ISATSession(request.getSession())
-            if sat_session.profile:
+            web_session = session_iface.IWebSession(request.getSession())
+            if web_session.profile:
                 # yes, session is active
-                if sat_session.profile != profile:
+                if web_session.profile != profile:
                     # existing session should have been ended above
                     # so this line should never be reached
                     log.error(_(
                         "session profile [{session_profile}] differs from login "
                         "profile [{profile}], this should not happen!")
-                            .format(session_profile=sat_session.profile, profile=profile))
+                            .format(session_profile=web_session.profile, profile=profile))
                     raise exceptions.InternalError("profile mismatch")
                 defer.returnValue(C.SESSION_ACTIVE)
             log.info(
@@ -1845,8 +1843,8 @@
         session = request.session
         if session is not None:
             log.debug(_("session purge"))
-            sat_session = self.getSessionData(request, session_iface.ISATSession)
-            socket = sat_session.ws_socket
+            web_session = self.getSessionData(request, session_iface.IWebSession)
+            socket = web_session.ws_socket
             if socket is not None:
                 socket.close()
                 session.ws_socket = None
@@ -1878,16 +1876,16 @@
         @param node(unicode): pubsub node
         @return (unicode): affiliation
         """
-        sat_session = self.getSessionData(request, session_iface.ISATSession)
-        if sat_session.profile is None:
+        web_session = self.getSessionData(request, session_iface.IWebSession)
+        if web_session.profile is None:
             raise exceptions.InternalError("profile must be set to use this method")
-        affiliation = sat_session.getAffiliation(service, node)
+        affiliation = web_session.getAffiliation(service, node)
         if affiliation is not None:
             defer.returnValue(affiliation)
         else:
             try:
                 affiliations = yield self.bridgeCall(
-                    "psAffiliationsGet", service.full(), node, sat_session.profile
+                    "psAffiliationsGet", service.full(), node, web_session.profile
                 )
             except Exception as e:
                 log.warning(
@@ -1901,7 +1899,7 @@
                     affiliation = affiliations[node]
                 except KeyError:
                     affiliation = ""
-            sat_session.setAffiliation(service, node, affiliation)
+            web_session.setAffiliation(service, node, affiliation)
             defer.returnValue(affiliation)
 
     ## Websocket (dynamic pages) ##
@@ -1940,7 +1938,7 @@
         self.cache_resource.putChild(service_path.encode('utf-8'),
                                      ProtectedFile(cache_dir))
         self.service_cache_url = "/" + os.path.join(C.CACHE_DIR, service_path)
-        session_iface.SATSession.service_cache_url = self.service_cache_url
+        session_iface.WebSession.service_cache_url = self.service_cache_url
 
         if self.options["connection_type"] in ("https", "both"):
             try:
@@ -2011,7 +2009,7 @@
         return web_util.redirectTo(url.encode(), request)
 
 
-registerAdapter(session_iface.SATSession, server.Session, session_iface.ISATSession)
+registerAdapter(session_iface.WebSession, server.Session, session_iface.IWebSession)
 registerAdapter(
     session_iface.SATGuestSession, server.Session, session_iface.ISATGuestSession
 )