2018-03-14 20:13:22 +03:00
# Copyright (C) 2018 Red Hat, Inc.
#
2018-04-04 16:35:41 +03:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 22:00:02 +03:00
# See the COPYING file in the top-level directory.
2018-03-14 20:13:22 +03:00
2019-06-17 04:12:39 +03:00
from virtinst import log
2018-03-14 20:13:22 +03:00
from . baseclass import vmmGObject
from . connection import vmmConnection
class vmmConnectionManager ( vmmGObject ) :
"""
Tracks the list of connections , emits conn - added and conn - removed
"""
__gsignals__ = {
2018-03-15 15:10:09 +03:00
" conn-added " : ( vmmGObject . RUN_FIRST , None , [ object ] ) ,
" conn-removed " : ( vmmGObject . RUN_FIRST , None , [ str ] ) ,
2018-03-14 20:13:22 +03:00
}
@classmethod
def get_instance ( cls , * args , * * kwargs ) :
if not cls . _instance :
cls . _instance = vmmConnectionManager ( * args , * * kwargs )
return cls . _instance
def __init__ ( self ) :
vmmGObject . __init__ ( self )
self . _conns = { }
# Load URIs from gsettings
for uri in self . config . get_conn_uris ( ) :
self . add_conn ( uri )
def _cleanup ( self ) :
for conn in self . _conns . values ( ) :
uri = conn . get_uri ( )
try :
2018-03-15 14:43:56 +03:00
conn . close ( )
2018-03-14 20:13:22 +03:00
self . emit ( " conn-removed " , uri )
conn . cleanup ( )
except Exception :
2019-06-17 04:12:39 +03:00
log . exception ( " Error cleaning up conn= %s " , uri )
2018-03-14 20:13:22 +03:00
self . _conns = { }
@property
def conns ( self ) :
return self . _conns . copy ( )
def add_conn ( self , uri ) :
if uri in self . _conns :
return self . _conns [ uri ]
conn = vmmConnection ( uri )
self . _conns [ uri ] = conn
self . config . add_conn_uri ( uri )
self . emit ( " conn-added " , conn )
return conn
def remove_conn ( self , uri ) :
if uri not in self . _conns :
return
conn = self . _conns . pop ( uri )
self . config . remove_conn_uri ( uri )
self . emit ( " conn-removed " , uri )
conn . cleanup ( )