[X2Go-Commits] [python-x2go] 02/03: broker client: hide all broker specific functionalities from X2GoClient class by moving the broker logic into the broker session profiles backend

git-admin at x2go.org git-admin at x2go.org
Tue Mar 18 01:05:58 CET 2014


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch brokerclient
in repository python-x2go.

commit 16903f56f3abb79655fcb25b3e01885f8ae87e71
Author: Mike Gabriel <mike.gabriel at das-netzwerkteam.de>
Date:   Sat Mar 15 10:02:28 2014 +0100

    broker client: hide all broker specific functionalities from X2GoClient class by moving the broker logic into the broker session profiles backend
---
 x2go/backends/profiles/base.py       |   75 ++++++++++++++++++++++++++++++++++
 x2go/backends/profiles/file.py       |    8 ++++
 x2go/backends/profiles/httpbroker.py |   37 ++++++++++++-----
 x2go/client.py                       |   12 +++---
 4 files changed, 115 insertions(+), 17 deletions(-)

diff --git a/x2go/backends/profiles/base.py b/x2go/backends/profiles/base.py
index 4a59692..8364c93 100644
--- a/x2go/backends/profiles/base.py
+++ b/x2go/backends/profiles/base.py
@@ -92,6 +92,23 @@ class X2GoSessionProfiles():
         _profile_id = self.check_profile_id_or_name(self, profile_id_or_name)
         return self.get_profile_config(profile_id=_profile_id)
 
+    def init_profile_cache(self, profile_id):
+        """\
+        Some session profile backends (e.g. the broker backends cache
+        dynamic session profile data). On new connections, it is
+        recommented to (re-)initialize these caches.
+
+        """
+        return self._init_profile_cache(profile_id)
+
+    def _init_profile_cache(self, profile_id):
+        """\
+        Inherit from this class to (re-)initialize profile ID based
+        cache storage.
+
+        """
+        pass
+
     def populate_session_profiles(self):
         """\
         Load a session profile set from the configuration storage
@@ -564,3 +581,61 @@ class X2GoSessionProfiles():
         """
         return []
 
+    def get_server_hostname(self, profile_id):
+        """\
+        Retrieve host name of the X2Go Server configured in a session profile.
+
+        @param profile_id: the profile's unique ID
+        @type profile_id: C{str}
+
+        @return: the host name of the X2Go Server configured by the session profile
+            of the given profile ID
+        @rtype: C{list}
+
+        """
+        return self._get_server_hostname(profile_id)
+
+    def _get_server_hostname(self, profile_id):
+        """\
+        Inherit from this class and provide a way for actually obtaining
+        a the server host name for a given profile ID.
+
+        @param profile_id: the profile's unique ID
+        @type profile_id: C{str}
+
+        @return: the host name of the X2Go Server configured by the session profile
+            of the given profile ID
+        @rtype: C{list}
+
+        """
+        return u'localhost'
+
+    def get_server_port(self, profile_id):
+        """\
+        Retrieve SSH port of the X2Go Server configured in a session profile.
+
+        @param profile_id: the profile's unique ID
+        @type profile_id: C{str}
+
+        @return: the SSH port of the X2Go Server configured by the session profile
+            of the given profile ID
+        @rtype: C{list}
+
+        """
+        return self._get_server_port(profile_id)
+
+    def _get_server_hostname(self, profile_id):
+        """\
+        Inherit from this class and provide a way for actually obtaining
+        a the server SSH port for a given profile ID.
+
+        @param profile_id: the profile's unique ID
+        @type profile_id: C{str}
+
+        @return: the SSH port of the X2Go Server configured by the session profile
+            of the given profile ID
+        @rtype: C{list}
+
+        """
+        return 22
+
diff --git a/x2go/backends/profiles/file.py b/x2go/backends/profiles/file.py
index 9e7edcf..7147295 100644
--- a/x2go/backends/profiles/file.py
+++ b/x2go/backends/profiles/file.py
@@ -26,6 +26,8 @@ applications.
 """
 __NAME__ = 'x2gosessionprofiles-pylib'
 
+import types
+
 # Python X2Go modules
 from x2go.defaults import X2GO_SESSIONPROFILES_CONFIGFILES as _X2GO_SESSIONPROFILES_CONFIGFILES
 import x2go.backends.profiles.base as base
@@ -108,3 +110,9 @@ class X2GoSessionProfiles(base.X2GoSessionProfiles, inifiles.X2GoIniFile):
 
     def _get_profile_ids(self):
         return [ s for s in self.iniConfig.sections() if s != "none" ]
+
+    def _get_server_hostname(self, profile_id):
+        return self.get_profile_config(profile_id)['host']
+
+    def _get_server_port(self, profile_id):
+        return self.get_profile_config(profile_id)['sshport']
diff --git a/x2go/backends/profiles/httpbroker.py b/x2go/backends/profiles/httpbroker.py
index c2f8f12..e4e3dfd 100644
--- a/x2go/backends/profiles/httpbroker.py
+++ b/x2go/backends/profiles/httpbroker.py
@@ -87,6 +87,8 @@ class X2GoSessionProfiles(base.X2GoSessionProfiles):
         base.X2GoSessionProfiles.__init__(self, session_profile_defaults=session_profile_defaults, logger=logger, loglevel=loglevel)
         self.logger("Using session broker at URL: %s" % self.broker_url, log.loglevel_NOTICE)
 
+        self._broker_profile_cache = {}
+
     def get_broker_username(self):
         return self.broker_username
 
@@ -117,16 +119,23 @@ class X2GoSessionProfiles(base.X2GoSessionProfiles):
             return payload['profiles'] if payload['task'] == 'listprofiles' else {}
 
     def broker_selectsession(self, profile_id):
-        request_data = {
-            'task': 'selectsession',
-            'profile-id': profile_id,
-            'user': self.broker_username,
-            'password': self.broker_password,
-        }
-        r = requests.post(self.broker_url, data=request_data)
-        if r.status_code == 200 and r.headers['content-type'].startswith("text/json"):
-            payload = json.loads(r.text)
-            return payload['selected_session'] if payload['task'] == 'selectsession' else {}
+        if not self._broker_profile_cache.has_key(profile_id) or not self._broker_profile_cache[profile_id]:
+            request_data = {
+                    'task': 'selectsession',
+                'profile-id': profile_id,
+                'user': self.broker_username,
+                'password': self.broker_password,
+            }
+            r = requests.post(self.broker_url, data=request_data)
+            if r.status_code == 200 and r.headers['content-type'].startswith("text/json"):
+                payload = json.loads(r.text)
+                self._broker_profile_cache[profile_id] = payload['selected_session'] if payload['task'] == 'selectsession' else {}
+
+        return self._broker_profile_cache[profile_id]
+
+    def _init_profile_cache(self, profile_id):
+        if self._broker_profile_cache.has_key(profile_id):
+            del self._broker_profile_cache.has_key[profile_id]
 
     def _populate_session_profiles(self):
         """\
@@ -171,3 +180,11 @@ class X2GoSessionProfiles(base.X2GoSessionProfiles):
     def _get_profile_ids(self):
         self.session_profiles.keys()
         return self.session_profiles.keys()
+
+    def _get_server_hostname(self, profile_id):
+        selected_session = self.broker_selectsession(profile_id)
+        return selected_session['server']
+
+    def _get_server_port(self, profile_id):
+        selected_session = self.broker_selectsession(profile_id)
+        return int(selected_session['port'])
diff --git a/x2go/client.py b/x2go/client.py
index d23d807..b7eec91 100644
--- a/x2go/client.py
+++ b/x2go/client.py
@@ -871,7 +871,6 @@ class X2GoClient(object):
 
         """
         # detect profile name and profile id properly
-
         if profile_id and self.session_profiles.has_profile_id(profile_id):
             _p = profile_id
         elif profile_name and self.session_profiles.has_profile_name(profile_name):
@@ -899,6 +898,9 @@ class X2GoClient(object):
 
         if _p:
 
+            # initialize session profile cache
+            self.session_profiles.init_profile_cache(profile_id)
+
             _params = self.session_profiles.to_session_params(profile_id=_profile_id)
             del _params['profile_name']
 
@@ -907,12 +909,8 @@ class X2GoClient(object):
                 if k in kwargs.keys():
                     _params[k] = kwargs[k]
 
-            if hasattr(self.session_profiles, 'broker_selectsession'):
-                selected_session = self.session_profiles.broker_selectsession(_p)
-                server = selected_session['server']
-                _params['port'] = int(selected_session['port'])
-            else:
-                server = _params['server']
+            server = self.session_profiles.get_server_hostname(_p)
+            _params['port'] = self.session_profiles.get_server_port(_p)
             del _params['server']
             _params['client_instance'] = self
 

--
Alioth's /srv/git/_hooks_/post-receive-email on /srv/git/code.x2go.org/python-x2go.git



More information about the x2go-commits mailing list