connection: Simplify state management

This commit is contained in:
Cole Robinson 2014-09-11 18:16:21 -04:00
parent b398a46e9b
commit bd5e57dbdc
5 changed files with 40 additions and 66 deletions

View File

@ -74,10 +74,9 @@ class vmmConnection(vmmGObject):
"priority-tick": (GObject.SignalFlags.RUN_FIRST, None, [object]),
}
STATE_DISCONNECTED = 0
STATE_CONNECTING = 1
STATE_ACTIVE = 2
STATE_INACTIVE = 3
(_STATE_DISCONNECTED,
_STATE_CONNECTING,
_STATE_ACTIVE) = range(1, 4)
def __init__(self, uri):
vmmGObject.__init__(self)
@ -86,7 +85,7 @@ class vmmConnection(vmmGObject):
if self._uri is None or self._uri.lower() == "xen":
self._uri = "xen:///"
self.state = self.STATE_DISCONNECTED
self._state = self._STATE_DISCONNECTED
self._connectThread = None
self._connectError = None
self._backend = virtinst.VirtualConnection(self._uri)
@ -574,47 +573,27 @@ class vmmConnection(vmmGObject):
###################################
def _change_state(self, newstate):
if self.state != newstate:
self.state = newstate
if self._state != newstate:
self._state = newstate
self.emit("state-changed")
def get_state(self):
return self.state
def is_active(self):
return self._state == self._STATE_ACTIVE
def is_disconnected(self):
return self._state == self._STATE_DISCONNECTED
def is_connecting(self):
return self._state == self._STATE_CONNECTING
def get_state_text(self):
if self.state == self.STATE_DISCONNECTED:
if self.is_disconnected():
return _("Disconnected")
elif self.state == self.STATE_CONNECTING:
elif self.is_connecting():
return _("Connecting")
elif self.state == self.STATE_ACTIVE:
elif self.is_active():
return _("Active")
elif self.state == self.STATE_INACTIVE:
return _("Inactive")
else:
return _("Unknown")
def pause(self):
if self.state != self.STATE_ACTIVE:
return
self._change_state(self.STATE_INACTIVE)
def resume(self):
if self.state != self.STATE_INACTIVE:
return
self._change_state(self.STATE_ACTIVE)
def is_active(self):
return self.state == self.STATE_ACTIVE
def is_paused(self):
return self.state == self.STATE_INACTIVE
def is_disconnected(self):
return self.state == self.STATE_DISCONNECTED
def is_connecting(self):
return self.state == self.STATE_CONNECTING
#################################
# Libvirt object lookup methods #
@ -883,7 +862,7 @@ class vmmConnection(vmmGObject):
self.config.set_conn_autoconnect(self.get_uri(), val)
def close(self):
if self.state != self.STATE_DISCONNECTED:
if not self.is_disconnected():
logging.debug("conn.close() uri=%s", self.get_uri())
self._closing = True
@ -928,7 +907,7 @@ class vmmConnection(vmmGObject):
cleanup(self._vms)
self._vms = {}
self._change_state(self.STATE_DISCONNECTED)
self._change_state(self._STATE_DISCONNECTED)
self._closing = False
def _cleanup(self):
@ -936,11 +915,11 @@ class vmmConnection(vmmGObject):
self._connectError = None
def open(self, sync=False):
if self.state != self.STATE_DISCONNECTED:
if not self.is_disconnected():
return
self._connectError = None
self._change_state(self.STATE_CONNECTING)
self._change_state(self._STATE_CONNECTING)
if sync:
logging.debug("Opening connection synchronously: %s",
@ -980,10 +959,10 @@ class vmmConnection(vmmGObject):
exc = libexc
if not exc:
self.state = self.STATE_ACTIVE
self._state = self._STATE_ACTIVE
break
self.state = self.STATE_DISCONNECTED
self._state = self._STATE_DISCONNECTED
if (libexc and
(libexc.get_error_code() ==
@ -1022,7 +1001,7 @@ class vmmConnection(vmmGObject):
try:
self.idle_emit("state-changed")
if self.state == self.STATE_ACTIVE:
if self.is_active():
logging.debug("libvirt version=%s",
self._backend.local_libvirt_version())
logging.debug("daemon version=%s",
@ -1051,7 +1030,7 @@ class vmmConnection(vmmGObject):
self._connectError = (str(e),
"".join(traceback.format_exc()), False)
if self.state == self.STATE_DISCONNECTED:
if self.is_disconnected():
if self._connectError:
self.idle_emit("connect-error", *self._connectError)
self._connectError = None
@ -1148,7 +1127,7 @@ class vmmConnection(vmmGObject):
main update function: polls for new objects, updates stats, ...
@force: Perform the requested polling even if async events are in use
"""
if self.state != self.STATE_ACTIVE or self._closing:
if not self.is_active() or self._closing:
return
if not pollvm:

View File

@ -246,8 +246,7 @@ class vmmEngine(vmmGObject):
queue.put(auto_conns.pop(0))
def state_change_cb(conn):
if (conn.get_state() == conn.STATE_ACTIVE or
conn.get_state() == conn.STATE_INACTIVE):
if conn.is_active():
add_next_to_queue()
conn.disconnect_by_func(state_change_cb)
@ -283,8 +282,7 @@ class vmmEngine(vmmGObject):
del(self.conns[hvuri]["windowDetails"][connkey])
def _do_conn_changed(self, conn):
if (conn.get_state() == conn.STATE_ACTIVE or
conn.get_state() == conn.STATE_CONNECTING):
if conn.is_active() or conn.is_connecting():
return
hvuri = conn.get_uri()

View File

@ -30,7 +30,6 @@ from virtinst import Interface
from virtManager import uiutil
from virtManager.asyncjob import vmmAsyncJob
from virtManager.connection import vmmConnection
from virtManager.createnet import vmmCreateNetwork
from virtManager.createpool import vmmCreatePool
from virtManager.createvol import vmmCreateVolume
@ -422,7 +421,7 @@ class vmmHost(vmmGObjectUI):
self.memory_usage_graph.set_property("data_array", memory_vector)
def conn_state_changed(self, ignore1=None):
conn_active = (self.conn.get_state() == vmmConnection.STATE_ACTIVE)
conn_active = self.conn.is_active()
self.widget("menu_file_restore_saved").set_sensitive(conn_active)
self.widget("net-add").set_sensitive(conn_active and
self.conn.is_network_capable())

View File

@ -29,7 +29,6 @@ from virtinst import util
from virtManager import vmmenu
from virtManager import uiutil
from virtManager.connection import vmmConnection
from virtManager.baseclass import vmmGObjectUI
from virtManager.graphwidgets import CellRendererSparkline
@ -566,12 +565,12 @@ class vmmManager(vmmGObjectUI):
def close_conn(self, ignore):
conn = self.current_conn()
if conn.get_state() != vmmConnection.STATE_DISCONNECTED:
if not conn.is_disconnected():
conn.close()
def open_conn(self, ignore=None):
conn = self.current_conn()
if conn.get_state() == vmmConnection.STATE_DISCONNECTED:
if conn.is_disconnected():
conn.open()
return True
@ -612,16 +611,16 @@ class vmmManager(vmmGObjectUI):
def _build_conn_hint(self, conn):
hint = conn.get_uri()
if conn.state == conn.STATE_DISCONNECTED:
if conn.is_disconnected():
hint += " (%s)" % _("Double click to connect")
return hint
def _build_conn_markup(self, conn, name):
name = util.xml_escape(name)
text = name
if conn.state == conn.STATE_DISCONNECTED:
if conn.is_disconnected():
text += " - " + _("Not Connected")
elif conn.state == conn.STATE_CONNECTING:
elif conn.is_connecting():
text += " - " + _("Connecting...")
markup = "<span size='smaller'>%s</span>" % text
@ -629,7 +628,7 @@ class vmmManager(vmmGObjectUI):
def _build_conn_color(self, conn):
color = "#000000"
if conn.state == conn.STATE_DISCONNECTED:
if conn.is_disconnected():
color = "#5b5b5b"
return color
@ -666,7 +665,7 @@ class vmmManager(vmmGObjectUI):
row.insert(ROW_HINT, util.xml_escape(hint))
row.insert(ROW_IS_CONN, bool(conn))
row.insert(ROW_IS_CONN_CONNECTED,
bool(conn) and conn.state != conn.STATE_DISCONNECTED)
bool(conn) and not conn.is_disconnected())
row.insert(ROW_IS_VM, bool(vm))
row.insert(ROW_IS_VM_RUNNING, bool(vm) and vm.is_active())
row.insert(ROW_COLOR, color)
@ -864,12 +863,11 @@ class vmmManager(vmmGObjectUI):
if newname:
row[ROW_SORT_KEY] = newname
row[ROW_MARKUP] = self._build_conn_markup(conn, row[ROW_SORT_KEY])
row[ROW_IS_CONN_CONNECTED] = conn.state != conn.STATE_DISCONNECTED
row[ROW_IS_CONN_CONNECTED] = not conn.is_disconnected()
row[ROW_COLOR] = self._build_conn_color(conn)
row[ROW_HINT] = self._build_conn_hint(conn)
if conn.get_state() in [vmmConnection.STATE_DISCONNECTED,
vmmConnection.STATE_CONNECTING]:
if not conn.is_active():
# Connection went inactive, delete any VM child nodes
parent = row.iter
if parent is not None:
@ -963,8 +961,8 @@ class vmmManager(vmmGObjectUI):
else:
# Pop up connection menu
conn = model[_iter][ROW_HANDLE]
disconn = (conn.get_state() == vmmConnection.STATE_DISCONNECTED)
conning = (conn.get_state() == vmmConnection.STATE_CONNECTING)
disconn = conn.is_disconnected()
conning = conn.is_connecting()
self.connmenu_items["create"].set_sensitive(not disconn)
self.connmenu_items["disconnect"].set_sensitive(not (disconn or

View File

@ -403,12 +403,12 @@ class vmmMigrateDialog(vmmGObjectUI):
if destconn.get_driver() != driver:
reason = _("Connection hypervisors do not match.")
elif destconn.get_state() == destconn.STATE_DISCONNECTED:
elif destconn.is_disconnected():
reason = _("Connection is disconnected.")
elif destconn.get_uri() == origuri:
# Same connection
pass
elif destconn.get_state() == destconn.STATE_ACTIVE:
elif destconn.is_active():
# Assumably we can migrate to this connection
can_migrate = True
reason = desturi