1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """\
21 X2goSessionRegistry class - the X2goClient's session registry backend
22
23 """
24 __NAME__ = 'x2gosessregistry-pylib'
25
26 import os
27 import copy
28 import types
29 import uuid
30 import time
31 import threading
32
33
34 import log
35 import utils
36 import session
37 from x2go_exceptions import *
38
39
40 from x2go.backends.control import X2goControlSession as _X2goControlSession
41 from x2go.backends.terminal import X2goTerminalSession as _X2goTerminalSession
42 from x2go.backends.info import X2goServerSessionInfo as _X2goServerSessionInfo
43 from x2go.backends.info import X2goServerSessionList as _X2goServerSessionList
44 from x2go.backends.proxy import X2goProxy as _X2goProxy
45 from x2go.backends.settings import X2goClientSettings as _X2goClientSettings
46 from x2go.backends.printing import X2goClientPrinting as _X2goClientPrinting
47
48 from defaults import LOCAL_HOME as _LOCAL_HOME
49 from defaults import X2GO_CLIENT_ROOTDIR as _X2GO_CLIENT_ROOTDIR
50 from defaults import X2GO_SESSIONS_ROOTDIR as _X2GO_SESSIONS_ROOTDIR
51 from defaults import X2GO_SSH_ROOTDIR as _X2GO_SSH_ROOTDIR
54 """\
55 This class is utilized by L{X2goClient} instances to maintain a good overview on
56 session status of all associated L{X2goSession} instances.
57
58 """
61 """\
62 @param client_instance: the L{X2goClient} instance that instantiated this L{X2goSessionRegistry} instance.
63 @type client_instance: L{X2goClient} instance
64 @param logger: you can pass an L{X2goLogger} object to the L{X2goClientXConfig} constructor
65 @type logger: C{instance}
66 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be
67 constructed with the given loglevel
68 @type loglevel: C{int}
69
70 """
71 if logger is None:
72 self.logger = log.X2goLogger(loglevel=loglevel)
73 else:
74 self.logger = copy.deepcopy(logger)
75 self.logger.tag = __NAME__
76
77 self.client_instance = client_instance
78
79 self.registry = {}
80 self.control_sessions = {}
81
82 self._last_available_session_registration = None
83 self._skip_auto_registration = False
84
86 """\
87 A list of session registry keys.
88
89 @return: session registry key list
90 @rtype: C{list}
91
92 """
93 return self.registry.keys()
94
96 result = 'X2goSessionRegistry('
97 for p in dir(self):
98 if '__' in p or not p in self.__dict__ or type(p) is types.InstanceType: continue
99 result += p + '=' + str(self.__dict__[p])
100 return result + ')'
101
103 """\
104 Returns the L{X2goSession} instance for a given session UUID hash.
105
106 @param session_uuid: the X2go session's UUID registry hash
107 @type session_uuid: C{str}
108 @return: the corresponding L{X2goSession} instance
109 @rtype: L{X2goSession} instance
110
111 """
112 try:
113 return self.registry[session_uuid]
114 except KeyError:
115 raise X2goSessionRegistryException('No session found for UUID %s' % session_uuid)
116
118 """\
119 This method is used to temporarily skip auto-registration of newly appearing
120 X2go session on the server side. This is necessary during session startups to
121 assure that the session registry does not get filled with session UUID
122 duplicates.
123
124 """
125 self._skip_auto_registration = True
126
128 """\
129 This method is used to temporarily (re-)enable auto-registration of newly appearing
130 X2go session on the server side.
131
132 """
133 self._skip_auto_registration = False
134
135 - def forget(self, session_uuid):
136 """\
137 Forget the complete record for session UUID C{session_uuid}.
138
139 @param session_uuid: the X2go session's UUID registry hash
140 @type session_uuid: C{str}
141
142 """
143 try:
144 del self.registry[session_uuid]
145 self.logger('Forgetting session UUID %s' % session_uuid, loglevel=log.loglevel_DEBUG)
146 except IndexError:
147 pass
148
150 """\
151 Retrieve the profile ID of a given session UUID hash.
152
153 @param session_uuid: the X2go session's UUID registry hash
154 @type session_uuid: C{str}
155 @return: profile ID
156 @rtype: C{str}
157
158 """
159 return self(session_uuid).get_profile_id()
160
162 """\
163 Retrieve the profile name of a given session UUID hash.
164
165 @param session_uuid: the X2go session's UUID registry hash
166 @type session_uuid: C{str}
167 @return: profile name
168 @rtype: C{str}
169
170 """
171 return self(session_uuid).get_profile_name()
172
174 """\
175 Compose a session summary (as Python dictionary).
176
177 @param session_uuid: the X2go session's UUID registry hash
178 @type session_uuid: C{str}
179 @return: session summary dictionary
180 @rtype: C{dict}
181
182 """
183 _session_summary = {}
184 _r = False
185 if session_uuid in [ s() for s in self.registered_sessions() ]:
186 _r = True
187
188 if not status_only:
189 _session_summary['uuid'] = _r and session_uuid or None
190 _session_summary['profile_id'] = _r and self.get_profile_id(session_uuid) or ''
191 _session_summary['profile_name'] = _r and self.get_profile_name(session_uuid) or ''
192 _session_summary['session_name'] = _r and self(session_uuid).get_session_name() or ''
193 _session_summary['control_session'] = _r and self(session_uuid).get_control_session() or None
194 _session_summary['control_params'] = _r and self(session_uuid).control_params or {}
195 _session_summary['terminal_session'] = _r and self(session_uuid).get_terminal_session() or None
196 _session_summary['terminal_params'] = _r and self(session_uuid).terminal_params or {}
197 _session_summary['active_threads'] = _r and bool(self(session_uuid).get_terminal_session()) and self(session_uuid).get_terminal_session().active_threads or []
198 _session_summary['backends'] = {
199 'control': _r and self(session_uuid).control_backend or None,
200 'terminal': _r and self(session_uuid).terminal_backend or None,
201 'info': _r and self(session_uuid).info_backend or None,
202 'list': _r and self(session_uuid).list_backend or None,
203 'proxy': _r and self(session_uuid).proxy_backend or None,
204 }
205
206 if _r:
207 _session_summary['virgin'] = self(session_uuid).virgin
208 _session_summary['connected'] = self(session_uuid).connected
209 _session_summary['running'] = self(session_uuid).running
210 _session_summary['suspended'] = self(session_uuid).suspended
211 _session_summary['terminated'] = self(session_uuid).terminated
212 else:
213 _session_summary['virgin'] = None
214 _session_summary['connected'] = None
215 _session_summary['running'] = None
216 _session_summary['suspended'] = None
217 _session_summary['terminated'] = None
218 return _session_summary
219
220 - def update_status(self, session_uuid=None, profile_name=None, profile_id=None, session_list=None, force_update=False, newly_connected=False):
221 """\
222 Update the session status for L{X2goSession} that is represented by a given session UUID hash,
223 profile name or profile ID.
224
225 @param session_uuid: the X2go session's UUID registry hash
226 @type session_uuid: C{str}
227 @param profile_name: alternatively, a profile name can be specified (the stati of all registered sessions for this session
228 profile will be updated)
229 @type profile_name: C{str}
230 @param profile_id: alternatively, a profile ID can be given (the stati of all registered sessions for this session
231 profile will be updated)
232 @type profile_id: C{str}
233 @param session_list: an optional C{X2goServerSessionList*} instance (as returned by the L{X2goClient.list_sessions()} command can
234 be passed to this method.
235 @type session_list: C{X2goServerSessionList*} instance
236 @param force_update: make sure the session status gets really updated
237 @type force_update: C{bool}
238
239 """
240 if session_uuid and profile_name or session_uuid and profile_id or profile_name and profile_id:
241 raise X2goSessionRegistryException('only one of the possible method parameters is allowed (session_uuid, profile_name or profile_id)')
242 elif session_uuid is None and profile_name is None and profile_id is None:
243 raise X2goSessionRegistryException('at least one of the method parameters session_uuid, profile_name or profile_id must be given')
244
245 if session_uuid:
246 session_uuids = [ session_uuid ]
247 elif profile_name:
248 session_uuids = [ s() for s in self.registered_sessions_of_profile_name(profile_name, return_objects=True) ]
249 elif profile_id:
250 session_uuids = [ s() for s in self.registered_sessions_of_profile_name(self.client_instance.to_profile_name(profile_id), return_objects=True) ]
251
252 for _session_uuid in session_uuids:
253
254 if not self(_session_uuid).update_status(session_list=session_list, force_update=force_update):
255
256 return False
257 _last_status = copy.deepcopy(self(_session_uuid)._last_status)
258 _current_status = copy.deepcopy(self(_session_uuid)._current_status)
259
260
261
262
263 _profile_name = self(_session_uuid).get_profile_name()
264 _session_name = self(_session_uuid).get_session_name()
265
266 if self(_session_uuid).get_server_hostname() != _current_status['server']:
267
268
269 self(_session_uuid).session_cleanup()
270 self(_session_uuid).__del__()
271 if len(self.virgin_sessions_of_profile_name(profile_name)) > 1:
272 del self.registry[_session_uuid]
273
274 elif not _last_status['running'] and _current_status['running'] and not _current_status['faulty']:
275
276 if newly_connected:
277
278 self.client_instance.HOOK_on_found_session_running_after_connect(session_uuid=_session_uuid, profile_name=_profile_name, session_name=_session_name)
279 else:
280
281 if self(_session_uuid).terminal_session:
282 if _last_status['suspended']:
283
284 self.client_instance.HOOK_on_session_has_resumed_by_me(session_uuid=_session_uuid, profile_name=_profile_name, session_name=_session_name)
285 elif _last_status['virgin']:
286
287 self.client_instance.HOOK_on_session_has_started_by_me(session_uuid=_session_uuid, profile_name=_profile_name, session_name=_session_name)
288 else:
289 if _last_status['suspended']:
290
291 self.client_instance.HOOK_on_session_has_resumed_by_other(session_uuid=_session_uuid, profile_name=_profile_name, session_name=_session_name)
292 else:
293
294 self.client_instance.HOOK_on_session_has_started_by_other(session_uuid=_session_uuid, profile_name=_profile_name, session_name=_session_name)
295
296 elif _last_status['connected'] and (not _last_status['suspended'] and _current_status['suspended']) and not _current_status['faulty']:
297
298 self(_session_uuid).session_cleanup()
299 self.client_instance.HOOK_on_session_has_been_suspended(session_uuid=_session_uuid, profile_name=_profile_name, session_name=_session_name)
300 elif _last_status['connected'] and (not _last_status['terminated'] and _current_status['terminated']) and not _current_status['faulty']:
301
302 self.client_instance.HOOK_on_session_has_terminated(session_uuid=_session_uuid, profile_name=_profile_name, session_name=_session_name)
303 try: self(_session_uuid).session_cleanup()
304 except X2goSessionException: pass
305 try: self(_session_uuid).__del__()
306 except X2goSessionException: pass
307 if len(self.virgin_sessions_of_profile_name(profile_name)) > 1:
308 self.forget(_session_uuid)
309
310 return True
311
313 """\
314 Register server-side available X2go sessions with this L{X2goSessionRegistry} instance for a given profile name.
315
316 @param profile_name: session profile name to register available X2go sessions for
317 @type profile_name: C{str}
318 @param session_list: an optional C{X2goServerSessionList*} instance (as returned by the L{X2goClient.list_sessions()} command can
319 be passed to this method.
320 @type session_list: C{X2goServerSessionList*} instance
321
322 """
323 if self._last_available_session_registration is not None:
324 _now = time.time()
325 _time_delta = _now - self._last_available_session_registration
326 if _time_delta < 2:
327 self.logger('registration interval too short (%s), skipping automatic session registration...' % _timedelta, loglevel=log.loglevel_DEBUG)
328 return
329 self._last_available_session_registration = _now
330
331 _connected_sessions = self.connected_sessions_of_profile_name(profile_name=profile_name, return_objects=False)
332 _registered_sessions = self.registered_sessions_of_profile_name(profile_name=profile_name, return_objects=False)
333 _session_names = [ self(s_uuid).session_name for s_uuid in _registered_sessions if self(s_uuid).session_name is not None ]
334
335 if _connected_sessions:
336
337
338 _ctrl_session = self(_connected_sessions[0])
339
340 if session_list is None:
341 session_list = _ctrl_session.list_sessions()
342
343
344
345 self.update_status(profile_name=profile_name, session_list=session_list, force_update=True)
346 for session_name in session_list.keys():
347 if session_name not in _session_names and not self._skip_auto_registration:
348 server = _ctrl_session.get_server_hostname()
349 profile_id = _ctrl_session.get_profile_id()
350
351
352
353 _clone_kwargs = _ctrl_session.__dict__
354 kwargs = {}
355 kwargs.update(self.client_instance.session_profiles.to_session_params(profile_id))
356 kwargs['client_instance'] = self.client_instance
357 kwargs['control_backend'] = _clone_kwargs['control_backend']
358 kwargs['terminal_backend'] = _clone_kwargs['terminal_backend']
359 kwargs['proxy_backend'] = _clone_kwargs['proxy_backend']
360 kwargs['info_backend'] = _clone_kwargs['info_backend']
361 kwargs['list_backend'] = _clone_kwargs['list_backend']
362 kwargs['settings_backend'] = _clone_kwargs['settings_backend']
363 kwargs['printing_backend'] = _clone_kwargs['printing_backend']
364 kwargs['keep_controlsession_alive'] = _clone_kwargs['keep_controlsession_alive']
365 kwargs['client_rootdir'] = _clone_kwargs['client_rootdir']
366 kwargs['sessions_rootdir'] = _clone_kwargs['sessions_rootdir']
367
368 try: del kwargs['server']
369 except: pass
370 try: del kwargs['profile_name']
371 except: pass
372 try: del kwargs['profile_id']
373 except: pass
374
375
376 if not self.has_session_of_session_name(session_name):
377 session_uuid = self.register(server, profile_id, profile_name,
378 session_name=session_name, virgin=False,
379 **kwargs
380 )
381 self(session_uuid).connected = True
382 self.update_status(session_uuid=session_uuid, force_update=True, newly_connected=newly_connected)
383
384 - def register(self, server, profile_id, profile_name,
385 session_name=None,
386 control_backend=_X2goControlSession,
387 terminal_backend=_X2goTerminalSession,
388 info_backend=_X2goServerSessionInfo,
389 list_backend=_X2goServerSessionList,
390 proxy_backend=_X2goProxy,
391 settings_backend=_X2goClientSettings,
392 printing_backend=_X2goClientPrinting,
393 client_rootdir=os.path.join(_LOCAL_HOME,_X2GO_CLIENT_ROOTDIR),
394 sessions_rootdir=os.path.join(_LOCAL_HOME,_X2GO_SESSIONS_ROOTDIR),
395 ssh_rootdir=os.path.join(_LOCAL_HOME,_X2GO_SSH_ROOTDIR),
396 keep_controlsession_alive=True,
397 add_to_known_hosts=False,
398 known_hosts=None,
399 **kwargs):
400 """\
401 Register a new L{X2goSession} instance with this L{X2goSessionRegistry}.
402
403 @param server: hostname of X2go server
404 @type server: C{str}
405 @param profile_id: profile ID
406 @type profile_id: C{str}
407 @param profile_name: profile name
408 @type profile_name: C{str}
409 @param session_name: session name (if available)
410 @type session_name: C{str}
411 @param control_backend: X2go control session backend to use
412 @type control_backend: C{class}
413 @param terminal_backend: X2go terminal session backend to use
414 @type terminal_backend: C{class}
415 @param info_backend: X2go session info backend to use
416 @type info_backend: C{class}
417 @param list_backend: X2go session list backend to use
418 @type list_backend: C{class}
419 @param proxy_backend: X2go proxy backend to use
420 @type proxy_backend: C{class}
421 @param settings_backend: X2go client settings backend to use
422 @type settings_backend: C{class}
423 @param printing_backend: X2go client printing backend to use
424 @type printing_backend: C{class}
425 @param client_rootdir: client base dir (default: ~/.x2goclient)
426 @type client_rootdir: C{str}
427 @param sessions_rootdir: sessions base dir (default: ~/.x2go)
428 @type sessions_rootdir: C{str}
429 @param ssh_rootdir: ssh base dir (default: ~/.ssh)
430 @type ssh_rootdir: C{str}
431 @param keep_controlsession_alive: On last L{X2goSession.disconnect()} keep the associated C{X2goControlSession*} instance alive?
432 @ŧype keep_controlsession_alive: C{bool}
433 @param add_to_known_hosts: Auto-accept server host validity?
434 @type add_to_known_hosts: C{bool}
435 @param known_hosts: the underlying Paramiko/SSH systems C{known_hosts} file
436 @type known_hosts: C{str}
437 @param kwargs: all other options will be passed on to the constructor of the to-be-instantiated L{X2goSession} instance
438 @type C{dict}
439
440 """
441 control_session = None
442 if profile_id in self.control_sessions.keys():
443 control_session = self.control_sessions[profile_id]
444
445
446
447 _virgin_sessions = self.virgin_sessions_of_profile_name(profile_name, return_objects=True)
448 if _virgin_sessions and not session_name:
449
450 session_uuid = _virgin_sessions[0].get_uuid()
451 _params = self.client_instance.session_profiles.to_session_params(profile_id)
452 self(session_uuid).update_params(_params)
453 self(session_uuid).set_server(server)
454 self(session_uuid).set_profile_name(profile_name)
455 self.logger('using already initially-registered yet-unused session %s' % session_uuid, loglevel=log.loglevel_NOTICE)
456 return session_uuid
457
458 session_uuid = self.get_session_of_session_name(session_name)
459 if session_uuid is not None:
460 _params = self.client_instance.session_profiles.to_session_params(profile_id)
461
462 self(session_uuid).update_params(_params)
463 self(session_uuid).set_server(server)
464 self(session_uuid).set_profile_name(profile_name)
465 self.logger('using already registered-by-session-name session %s' % session_uuid, loglevel=log.loglevel_NOTICE)
466 return session_uuid
467
468 s = session.X2goSession(server=server, control_session=control_session,
469 profile_id=profile_id, profile_name=profile_name,
470 session_name=session_name,
471 control_backend=control_backend,
472 terminal_backend=terminal_backend,
473 info_backend=info_backend,
474 list_backend=list_backend,
475 proxy_backend=proxy_backend,
476 settings_backend=settings_backend,
477 printing_backend=printing_backend,
478 client_rootdir=client_rootdir,
479 sessions_rootdir=sessions_rootdir,
480 ssh_rootdir=ssh_rootdir,
481 keep_controlsession_alive=keep_controlsession_alive,
482 add_to_known_hosts=add_to_known_hosts,
483 known_hosts=known_hosts,
484 logger=self.logger, **kwargs)
485
486 session_uuid = s._X2goSession__get_uuid()
487 self.logger('registering X2go session %s...' % profile_name, log.loglevel_NOTICE)
488 self.logger('registering X2go session with UUID %s' % session_uuid, log.loglevel_DEBUG)
489
490 self.registry[session_uuid] = s
491 if profile_id not in self.control_sessions.keys():
492 self.control_sessions[profile_id] = s.get_control_session()
493
494 return session_uuid
495
497 """\
498 Detect if we know about an L{X2goSession} of name C{<session_name>}.
499
500 @param session_name: name of session to be searched for
501 @type session_name: C{str}
502 @return: C{True} if a session of C{<session_name>} has been found
503 @rtype: C{bool}
504
505 """
506 return bool(self.get_session_of_session_name(session_name))
507
509 """\
510 Retrieve the L{X2goSession} instance with session name C{<session_name>}.
511
512 @param session_name: name of session to be retrieved
513 @type session_name: C{str}
514 @param return_object: if C{False} the session UUID hash will be returned, if C{True} the L{X2goSession} instance will be returned
515 @type return_object: C{bool}
516 @return: L{X2goSession} object or its representing session UUID hash
517 @rtype: L{X2goSession} instance or C{str}
518
519 """
520 found_sessions = [ s for s in self.registered_sessions() if s.session_name == session_name and s.session_name is not None ]
521 if len(found_sessions) == 1:
522 session = found_sessions[0]
523 if return_object:
524 return session
525 else:
526 return session.get_uuid()
527 elif len(found_sessions) > 1:
528 raise X2goSessionRegistryException('there should only be one registered session of name ,,%s\'\'' % session_name)
529 else:
530 return None
531
532 - def _sessionsWithState(self, state, return_objects=True, return_profile_names=False, return_profile_ids=False, return_session_names=False):
561
562 - def connected_sessions(self, return_objects=True, return_profile_names=False, return_profile_ids=False, return_session_names=False):
563 """\
564 Retrieve a list of sessions that the underlying L{X2goClient} instances is currently connected to.
565 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
566
567 @param return_objects: return as list of L{X2goSession} instances
568 @type return_objects: C{bool}
569 @param return_profile_names: return as list of profile names
570 @type return_profile_names: C{bool}
571 @param return_profile_ids: return as list of profile IDs
572 @type return_profile_ids: C{bool}
573 @param return_session_names: return as list of X2go session names
574 @type return_session_names: C{bool}
575 @return: a session list (as UUID hashes, objects, profile names/IDs or session names)
576 @rtype: C{list}
577
578 """
579 return self._sessionsWithState('connected', return_objects=return_objects, return_profile_names=return_profile_names, return_profile_ids=return_profile_ids, return_session_names=return_session_names)
580
581 - def associated_sessions(self, return_objects=True, return_profile_names=False, return_profile_ids=False, return_session_names=False):
582 """\
583 Retrieve a list of sessions that are currently associated by an C{X2goTerminalSession*} to the underlying L{X2goClient} instance.
584 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
585
586 @param return_objects: return as list of L{X2goSession} instances
587 @type return_objects: C{bool}
588 @param return_profile_names: return as list of profile names
589 @type return_profile_names: C{bool}
590 @param return_profile_ids: return as list of profile IDs
591 @type return_profile_ids: C{bool}
592 @param return_session_names: return as list of X2go session names
593 @type return_session_names: C{bool}
594 @return: a session list (as UUID hashes, objects, profile names/IDs or session names)
595 @rtype: C{list}
596
597 """
598 return self._sessionsWithState('associated', return_objects=return_objects, return_profile_names=return_profile_names, return_profile_ids=return_profile_ids, return_session_names=return_session_names)
599
600 - def virgin_sessions(self, return_objects=True, return_profile_names=False, return_profile_ids=False, return_session_names=False):
601 """\
602 Retrieve a list of sessions that are currently still in virgin state (not yet connected, associated etc.).
603 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
604
605 @param return_objects: return as list of L{X2goSession} instances
606 @type return_objects: C{bool}
607 @param return_profile_names: return as list of profile names
608 @type return_profile_names: C{bool}
609 @param return_profile_ids: return as list of profile IDs
610 @type return_profile_ids: C{bool}
611 @param return_session_names: return as list of X2go session names
612 @type return_session_names: C{bool}
613 @return: a session list (as UUID hashes, objects, profile names/IDs or session names)
614 @rtype: C{list}
615
616
617 """
618 return self._sessionsWithState('virgin', return_objects=return_objects, return_profile_names=return_profile_names, return_profile_ids=return_profile_ids, return_session_names=return_session_names)
619
620 - def running_sessions(self, return_objects=True, return_profile_names=False, return_profile_ids=False, return_session_names=False):
621 """\
622 Retrieve a list of sessions that are currently in running state.
623 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
624
625 @param return_objects: return as list of L{X2goSession} instances
626 @type return_objects: C{bool}
627 @param return_profile_names: return as list of profile names
628 @type return_profile_names: C{bool}
629 @param return_profile_ids: return as list of profile IDs
630 @type return_profile_ids: C{bool}
631 @param return_session_names: return as list of X2go session names
632 @type return_session_names: C{bool}
633 @return: a session list (as UUID hashes, objects, profile names/IDs or session names)
634 @rtype: C{list}
635
636 """
637 return self._sessionsWithState('running', return_objects=return_objects, return_profile_names=return_profile_names, return_profile_ids=return_profile_ids, return_session_names=return_session_names)
638
639 - def suspended_sessions(self, return_objects=True, return_profile_names=False, return_profile_ids=False, return_session_names=False):
640 """\
641 Retrieve a list of sessions that are currently in suspended state.
642 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
643
644 @param return_objects: return as list of L{X2goSession} instances
645 @type return_objects: C{bool}
646 @param return_profile_names: return as list of profile names
647 @type return_profile_names: C{bool}
648 @param return_profile_ids: return as list of profile IDs
649 @type return_profile_ids: C{bool}
650 @param return_session_names: return as list of X2go session names
651 @type return_session_names: C{bool}
652 @return: a session list (as UUID hashes, objects, profile names/IDs or session names)
653 @rtype: C{list}
654
655 """
656 return self._sessionsWithState('suspended', return_objects=return_objects, return_profile_names=return_profile_names, return_profile_ids=return_profile_ids, return_session_names=return_session_names)
657
658 - def terminated_sessions(self, return_objects=True, return_profile_names=False, return_profile_ids=False, return_session_names=False):
659 """\
660 Retrieve a list of sessions that have terminated recently.
661 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
662
663 @param return_objects: return as list of L{X2goSession} instances
664 @type return_objects: C{bool}
665 @param return_profile_names: return as list of profile names
666 @type return_profile_names: C{bool}
667 @param return_profile_ids: return as list of profile IDs
668 @type return_profile_ids: C{bool}
669 @param return_session_names: return as list of X2go session names
670 @type return_session_names: C{bool}
671 @return: a session list (as UUID hashes, objects, profile names/IDs or session names)
672 @rtype: C{list}
673
674 """
675 return self._sessionsWithState('terminated', return_objects=return_objects, return_profile_names=return_profile_names, return_profile_ids=return_profile_ids, return_session_names=return_session_names)
676
677 @property
679 """\
680 Equals C{True} if the underlying L{X2goClient} instance has any running sessions at hand.
681
682 """
683 return self.running_sessions() and len(self.running_sessions()) > 0
684
685 @property
687 """\
688 Equals C{True} if the underlying L{X2goClient} instance has any suspended sessions at hand.
689
690 """
691 return self.suspended_sessions and len(self.suspended_sessions) > 0
692
693 - def registered_sessions(self, return_objects=True, return_profile_names=False, return_profile_ids=False, return_session_names=False):
694 """\
695 Retrieve a list of all registered sessions.
696 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
697
698 @param return_objects: return as list of L{X2goSession} instances
699 @type return_objects: C{bool}
700 @param return_profile_names: return as list of profile names
701 @type return_profile_names: C{bool}
702 @param return_profile_ids: return as list of profile IDs
703 @type return_profile_ids: C{bool}
704 @param return_session_names: return as list of X2go session names
705 @type return_session_names: C{bool}
706 @return: a session list (as UUID hashes, objects, profile names/IDs or session names)
707 @rtype: C{list}
708
709 """
710 return self._sessionsWithState('registered', return_objects=return_objects, return_profile_names=return_profile_names, return_profile_ids=return_profile_ids, return_session_names=return_session_names)
711
712 - def non_running_sessions(self, return_objects=True, return_profile_names=False, return_profile_ids=False, return_session_names=False):
713 """\
714 Retrieve a list of sessions that are currently _NOT_ in running state.
715 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
716
717 @param return_objects: return as list of L{X2goSession} instances
718 @type return_objects: C{bool}
719 @param return_profile_names: return as list of profile names
720 @type return_profile_names: C{bool}
721 @param return_profile_ids: return as list of profile IDs
722 @type return_profile_ids: C{bool}
723 @param return_session_names: return as list of X2go session names
724 @type return_session_names: C{bool}
725 @return: a session list (as UUID hashes, objects, profile names/IDs or session names)
726 @rtype: C{list}
727
728 """
729 return [ s for s in self.registered_sessions(return_objects=return_objects, return_profile_names=return_profile_names, return_profile_ids=return_profile_ids, return_session_names=return_session_names) if s not in self.running_sessions(return_objects=return_objects, return_profile_names=return_profile_names, return_profile_ids=return_profile_ids, return_session_names=return_session_names) ]
730
732 """\
733 For a given session profile name retrieve a list of sessions that are currently connected to the profile's X2go server.
734 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
735
736 @param profile_name: session profile name
737 @type profile_name: C{str}
738 @param return_objects: return as list of L{X2goSession} instances
739 @type return_objects: C{bool}
740 @param return_session_names: return as list of X2go session names
741 @type return_session_names: C{bool}
742 @return: a session list (as UUID hashes, objects or session names)
743 @rtype: C{list}
744
745 """
746 if return_objects:
747 return self.connected_sessions() and [ s for s in self.connected_sessions() if s.profile_name == profile_name ]
748 elif return_session_names:
749 return self.connected_sessions() and [ s.session_name for s in self.connected_sessions() if s.profile_name == profile_name ]
750 else:
751 return self.connected_sessions() and [ s.get_uuid() for s in self.connected_sessions() if s.profile_name == profile_name ]
752
754 """\
755 For a given session profile name retrieve a list of sessions that are currently associated by an C{X2goTerminalSession*} to this L{X2goClient} instance.
756 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
757
758 @param profile_name: session profile name
759 @type profile_name: C{str}
760 @param return_objects: return as list of L{X2goSession} instances
761 @type return_objects: C{bool}
762 @param return_session_names: return as list of X2go session names
763 @type return_session_names: C{bool}
764 @return: a session list (as UUID hashes, objects or session names)
765 @rtype: C{list}
766
767 """
768 if return_objects:
769 return self.associated_sessions() and [ s for s in self.associated_sessions() if s.profile_name == profile_name ]
770 elif return_session_names:
771 return self.associated_sessions() and [ s.session_name for s in self.associated_sessions() if s.profile_name == profile_name ]
772 else:
773 return self.associated_sessions() and [ s.get_uuid() for s in self.associated_sessions() if s.profile_name == profile_name ]
774
776 """\
777 For a given session profile name retrieve a list of sessions that are currently registered with this L{X2goClient} instance.
778 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
779
780 @param profile_name: session profile name
781 @type profile_name: C{str}
782 @param return_objects: return as list of L{X2goSession} instances
783 @type return_objects: C{bool}
784 @param return_session_names: return as list of X2go session names
785 @type return_session_names: C{bool}
786 @return: a session list (as UUID hashes, objects or session names)
787 @rtype: C{list}
788
789 """
790
791 if return_objects:
792 return self.registered_sessions() and [ s for s in self.registered_sessions() if s.profile_name == profile_name ]
793 elif return_session_names:
794 return self.registered_sessions() and [ s.session_name for s in self.registered_sessions() if s.profile_name == profile_name ]
795 else:
796 return self.registered_sessions() and [ s.get_uuid() for s in self.registered_sessions() if s.profile_name == profile_name ]
797
799 """\
800 For a given session profile name retrieve a list of sessions that are registered with this L{X2goClient} instance but have not
801 yet been started (i.e. sessions that are in virgin state). If none of the C{return_*} options is specified a list of
802 session UUID hashes will be returned.
803
804 @param profile_name: session profile name
805 @type profile_name: C{str}
806 @param return_objects: return as list of L{X2goSession} instances
807 @type return_objects: C{bool}
808 @param return_session_names: return as list of X2go session names
809 @type return_session_names: C{bool}
810 @return: a session list (as UUID hashes, objects or session names)
811 @rtype: C{list}
812
813 """
814 if return_objects:
815 return self.virgin_sessions() and [ s for s in self.virgin_sessions() if s.profile_name == profile_name ]
816 elif return_session_names:
817 return self.virgin_sessions() and [ s.session_name for s in self.virgin_sessions() if s.profile_name == profile_name ]
818 else:
819 return self.virgin_sessions() and [ s.get_uuid() for s in self.virgin_sessions() if s.profile_name == profile_name ]
820
822 """\
823 For a given session profile name retrieve a list of sessions that are currently running.
824 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
825
826 @param profile_name: session profile name
827 @type profile_name: C{str}
828 @param return_objects: return as list of L{X2goSession} instances
829 @type return_objects: C{bool}
830 @param return_session_names: return as list of X2go session names
831 @type return_session_names: C{bool}
832 @return: a session list (as UUID hashes, objects or session names)
833 @rtype: C{list}
834
835 """
836 if return_objects:
837 return self.running_sessions() and [ s for s in self.running_sessions() if s.profile_name == profile_name ]
838 elif return_session_names:
839 return self.running_sessions() and [ s.session_name for s in self.running_sessions() if s.profile_name == profile_name ]
840 else:
841 return self.running_sessions() and [ s.get_uuid() for s in self.running_sessions() if s.profile_name == profile_name ]
842
844 """\
845 For a given session profile name retrieve a list of sessions that are currently in suspended state.
846 If none of the C{return_*} options is specified a list of session UUID hashes will be returned.
847
848 @param profile_name: session profile name
849 @type profile_name: C{str}
850 @param return_objects: return as list of L{X2goSession} instances
851 @type return_objects: C{bool}
852 @param return_session_names: return as list of X2go session names
853 @type return_session_names: C{bool}
854 @return: a session list (as UUID hashes, objects or session names)
855 @rtype: C{list}
856
857 """
858 if return_objects:
859 return self.suspended_sessions() and [ s for s in self.suspended_sessions() if s.profile_name == profile_name ]
860 elif return_session_names:
861 return self.suspended_sessions() and [ s.session_name for s in self.suspended_sessions() if s.profile_name == profile_name ]
862 else:
863 return self.suspended_sessions() and [ s.get_uuid() for s in self.suspended_sessions() if s.profile_name == profile_name ]
864
866 """\
867 For a given session profile name retrieve a the corresponding C{X2goControlSession*} instance.
868
869 @param profile_name: session profile name
870 @type profile_name: C{str}
871 @return: contol session instance
872 @rtype: C{X2goControlSession*} instance
873
874 """
875 _sessions = self.registered_sessions_of_profile_name(profile_name, return_objects=True)
876 if _sessions:
877 session = _sessions[0]
878 return session.control_session
879 return None
880
881 @property
883 """\
884 Equals a list of all currently connected control sessions.
885
886 """
887 return [ c for c in self.control_sessions.values() if c.is_connected() ]
888
890 """\
891 Retrieve a list of all currently connected session profiles.
892
893 @param use_paramiko: send query directly to the Paramiko/SSH layer
894 @type use_paramiko: C{bool}
895 @return: list of connected session profiles
896 @rtype: C{list}
897
898 """
899 if use_paramiko:
900 return [ p for p in self.control_sessions.keys() if self.control_sessions[p].is_connected() ]
901 else:
902 return self.connected_sessions(return_profile_ids=True)
903