summaryrefslogtreecommitdiff
path: root/virtManager/connmanager.py
blob: 8866beafe280578eea6fb78a4ef65c4ad0837f03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (C) 2018 Red Hat, Inc.
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.

from virtinst import log

from .baseclass import vmmGObject
from .connection import vmmConnection


class vmmConnectionManager(vmmGObject):
    """
    Tracks the list of connections, emits conn-added and conn-removed
    """
    __gsignals__ = {
        "conn-added": (vmmGObject.RUN_FIRST, None, [object]),
        "conn-removed": (vmmGObject.RUN_FIRST, None, [str]),
    }

    @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:
                conn.close()
                self.emit("conn-removed", uri)
                conn.cleanup()
            except Exception:  # pragma: no cover
                log.exception("Error cleaning up conn=%s", uri)
        self._conns = {}

    @property
    def conns(self):
        return self._conns.copy()

    def add_conn(self, uri):
        if uri in self._conns:
            return self._conns[uri]  # pragma: no cover
        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  # pragma: no cover
        conn = self._conns.pop(uri)
        self.config.remove_conn_uri(uri)
        self.emit("conn-removed", uri)
        conn.cleanup()