Package x2go :: Module cache
[frames] | no frames]

Source Code for Module x2go.cache

  1  # -*- coding: utf-8 -*- 
  2   
  3  # Copyright (C) 2010-2011 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de> 
  4  # 
  5  # Python X2go is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation; either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Python X2go is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License 
 16  # along with this program; if not, write to the 
 17  # Free Software Foundation, Inc., 
 18  # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 
 19   
 20  """\ 
 21  X2goListSessionCache class - caching X2go session information. 
 22   
 23  """ 
 24  __NAME__ = 'x2gocache-pylib' 
 25   
 26  # modules 
 27  import copy 
 28  import gevent 
 29   
 30  # Python X2go modules 
 31  import log 
 32  import x2go_exceptions 
 33   
34 -class X2goListSessionsCache(object):
35 """\ 36 For non-blocking operations in client applications using Python X2go, it is 37 recommended to enable the L{X2goListSessionsCache}. This can be done by calling 38 the constructor of the L{X2goClient} class. 39 40 The session list and desktop cache gets updated in regular intervals by a threaded 41 L{X2goSessionGuardian} instance. For the session list and desktop list update, the 42 X2go server commands C{x2golistsessions} and C{x2godesktopsessions} are called and 43 the command's stdout is cached in the session list cache. 44 45 Whenever your client application needs access to either the server's session list 46 or the server's desktop list the session cache is queried instead. This assures that 47 the server's session/desktop list is available without delay, even on slow internet 48 connections. 49 50 """ 51 x2go_listsessions_cache = {} 52
53 - def __init__(self, client_instance, logger=None, loglevel=log.loglevel_DEFAULT):
54 """\ 55 @param client_instance: the L{X2goClient} instance that uses this L{X2goListSessionsCache} 56 @type client_instance: C{instance} 57 @param logger: you can pass an L{X2goLogger} object to the L{X2goListSessionsCache} constructor 58 @type logger: C{instance} 59 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be 60 constructed with the given loglevel 61 @type loglevel: C{int} 62 63 """ 64 self.x2go_listsessions_cache = {} 65 self.last_listsessions_cache = {} 66 67 if logger is None: 68 self.logger = log.X2goLogger(loglevel=loglevel) 69 else: 70 self.logger = copy.deepcopy(logger) 71 self.logger.tag = __NAME__ 72 73 self.client_instance = client_instance
74
75 - def delete(self, profile_name):
76 """\ 77 Remove session list from cache for a given profile. 78 79 @param profile_name: name of profile to operate on 80 @type profile_name: C{str} 81 82 """ 83 try: del self.x2go_listsessions_cache[profile_name] 84 except KeyError: pass
85
86 - def check_cache(self):
87 """\ 88 Check if session list cache elements are still valid (i.e. if all corresponding 89 session profiles are still connected). If not so, remove invalid cache entries from 90 the session list cache. 91 92 """ 93 for profile_name in self.x2go_listsessions_cache.keys(): 94 if profile_name not in self.client_instance.client_connected_profiles(return_profile_names=True): 95 del self.x2go_listsessions_cache[profile_name]
96
97 - def update_all(self, update_sessions=True, update_desktops=False):
98 """\ 99 Update L{X2goListSessionsCache} for all connected session profiles. 100 101 @param update_sessions: cache recent session lists from all connected servers 102 @type update_sessions: C{bool} 103 @param update_desktops: cache recent desktop lists from all connected servers 104 @type update_desktops: C{bool} 105 106 """ 107 for profile_name in self.client_instance.client_connected_profiles(return_profile_names=True): 108 self.update(profile_name, update_sessions=update_sessions, update_desktops=update_desktops) 109 110 self.check_cache()
111
112 - def update(self, profile_name, update_sessions=True, update_desktops=False):
113 """\ 114 Update L{X2goListSessionsCache} (i.e. session/desktops) for session profile C{profile_name}. 115 116 @param profile_name: name of profile to update 117 @type profile_name: C{str} 118 @param update_sessions: cache recent session list from server 119 @type update_sessions: C{bool} 120 @param update_desktops: cache recent desktop list from server 121 @type update_desktops: C{bool} 122 123 """ 124 self.last_listsessions_cache = copy.deepcopy(self.x2go_listsessions_cache) 125 control_session = self.client_instance.client_control_session_of_profile_name(profile_name) 126 if not self.x2go_listsessions_cache.has_key(profile_name): 127 self.x2go_listsessions_cache[profile_name] = {'sessions': None, 'desktops': None, } 128 if update_sessions: 129 self._update_sessions(profile_name, control_session) 130 if update_desktops: 131 self._update_desktops(profile_name, control_session)
132
133 - def _update_desktops(self, profile_name, control_session):
134 """\ 135 Update session lists of L{X2goListSessionsCache} for session profile C{profile_name}. 136 137 @param profile_name: name of profile to update 138 @type profile_name: C{str} 139 140 """ 141 try: 142 self.x2go_listsessions_cache[profile_name]['desktops'] = control_session.list_desktops() 143 except x2go_exceptions.X2goControlSessionException, e: 144 try: 145 del self.x2go_listsessions_cache[profile_name] 146 except KeyError: 147 pass 148 raise e
149
150 - def _update_sessions(self, profile_name, control_session):
151 """\ 152 Update desktop list of L{X2goListSessionsCache} for session profile C{profile_name}. 153 154 @param profile_name: name of profile to update 155 @type profile_name: C{str} 156 157 """ 158 try: 159 self.x2go_listsessions_cache[profile_name]['sessions'] = control_session.list_sessions() 160 except x2go_exceptions.X2goControlSessionException, e: 161 try: 162 del self.x2go_listsessions_cache[profile_name] 163 except KeyError: 164 pass 165 raise e
166
167 - def list_sessions(self, session_uuid):
168 """\ 169 Retrieve a session list from the current cache content of L{X2goListSessionsCache} 170 for a given L{X2goSession} instance (specified by its unique session UUID). 171 172 @param session_uuid: unique identifier of session to query cache for 173 @type session_uuid: C{str} 174 @return: a data object containing available session information 175 @rtype: C{X2goServerSessionList*} instance 176 177 """ 178 profile_name = self.client_instance.get_session_profile_name(session_uuid) 179 if self.is_cached(session_uuid=session_uuid): 180 return self.x2go_listsessions_cache[profile_name]['sessions'] 181 else: 182 return None
183
184 - def list_desktops(self, session_uuid):
185 """\ 186 Retrieve a list of available desktop sessions from the current cache content of 187 L{X2goListSessionsCache} for a given L{X2goSession} instance (specified by its 188 unique session UUID). 189 190 @param session_uuid: unique identifier of session to query cache for 191 @type session_uuid: C{str} 192 @return: a list of strings representing X2go desktop sessions available for sharing 193 @rtype: C{list} 194 195 """ 196 profile_name = self.client_instance.get_session_profile_name(session_uuid) 197 if self.is_cached(session_uuid=session_uuid): 198 return self.x2go_listsessions_cache[profile_name]['desktops'] 199 else: 200 return None
201
202 - def is_cached(self, profile_name=None, session_uuid=None, cache_type=None):
203 """\ 204 Check if session list is cached. 205 206 @param profile_name: name of profile to update 207 @type profile_name: C{str} 208 @param session_uuid: unique identifier of session to query cache for 209 @type session_uuid: C{str} 210 211 """ 212 if profile_name is None and session_uuid: 213 profile_name = self.client_instance.get_session_profile_name(session_uuid) 214 _is_profile_cached = self.x2go_listsessions_cache.has_key(profile_name) 215 _is_cache_type_cached = _is_profile_cached and self.x2go_listsessions_cache[profile_name].has_key(cache_type) 216 if cache_type is None: 217 return _is_profile_cached 218 else: 219 return _is_cache_type_cached
220