Make vmmConnection subclass GObject & use its signal infrastructure

This commit is contained in:
Daniel P. Berrange 2006-06-14 13:52:46 -04:00
parent 2f838a74be
commit 7547140130
5 changed files with 47 additions and 47 deletions

View File

@ -1,4 +1,5 @@
import gobject
import libvirt
from virtManager.stats import vmmStats
@ -6,8 +7,19 @@ from virtManager.manager import vmmManager
from virtManager.details import vmmDetails
from virtManager.console import vmmConsole
class vmmConnection:
class vmmConnection(gobject.GObject):
__gsignals__ = {
"vm-added": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
(str, str,)),
"vm-removed": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
(str,)),
"vm-updated": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
(str,)),
"disconnected": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (str,))
}
def __init__(self, engine, config, uri, readOnly):
self.__gobject_init__()
self.engine = engine
self.config = config
@ -21,7 +33,6 @@ class vmmConnection:
self.windowConsole = {}
self.vms = {}
self.callbacks = { "vm_added": [], "vm_removed": [], "vm_updated": [] }
self.stats = vmmStats(config, self)
def get_stats(self):
@ -38,6 +49,11 @@ class vmmConnection:
self.windowManager = vmmManager(self.config, self)
self.windowManager.show()
def disconnect(self):
self.vmm.disconnect()
self.vmm = None
self.emit("disconnected")
def get_host_info(self):
return self.vmm.getInfo()
@ -56,35 +72,17 @@ class vmmConnection:
def show_open_connection(self):
self.engine.show_open_connection()
def connect_to_signal(self, name, callback):
if not(self.callbacks.has_key(name)):
raise "unknown signal " + name + "requested"
self.callbacks[name].append(callback)
if name == "vm_removed":
def connect(self, name, callback):
gobject.GObject.connect(self, name, callback)
print "Cnnect " + name + " to " + str(callback)
if name == "vm-added":
for uuid in self.vms.keys():
self.notify_vm_added(uuid, self.vms[uuid].name())
def disconnect_from_signal(self, name, callback):
for i in len(self.callbacks[name]):
if self.callbacks[i] == callback:
del self.callbacks[i:i]
def notify_vm_added(self, uuid, name):
for cb in self.callbacks["vm_added"]:
cb(uuid, name)
def notify_vm_removed(self, uuid):
for cb in self.callbacks["vm_removed"]:
cb(uuid)
def notify_vm_updated(self, uuid):
for cb in self.callbacks["vm_updated"]:
cb(uuid)
self.emit("vm-added", uuid, self.vms[uuid].name())
def tick(self):
if self.vmm == None:
return
doms = self.vmm.listDomainsID()
newVms = {}
if doms != None:
@ -95,16 +93,17 @@ class vmmConnection:
for uuid in self.vms.keys():
if not(newVms.has_key(uuid)):
del self.vms[uuid]
self.notify_vm_removed(uuid)
self.emit("vm-removed", uuid)
for uuid in newVms.keys():
if not(self.vms.has_key(uuid)):
self.vms[uuid] = newVms[uuid]
self.notify_vm_added(uuid, newVms[uuid].name())
print "Trying to emit"
self.emit("vm-added",uuid, newVms[uuid].name())
for uuid in self.vms.keys():
self.stats.update(uuid, self.vms[uuid])
self.notify_vm_updated(uuid)
self.emit("vm-updated", uuid)
return 1
@ -118,4 +117,5 @@ class vmmConnection:
uuid.append('-')
return "".join(uuid)
gobject.type_register(vmmConnection)

View File

@ -43,7 +43,7 @@ class vmmConsole:
"on_control_details_clicked": self.control_vm_details,
})
self.connection.connect_to_signal("vm_updated", self.vm_updated)
self.connection.connect("vm-updated", self.vm_updated)
self.refresh_status()
def show(self):
@ -91,7 +91,7 @@ class vmmConsole:
def control_vm_details(self, src):
self.connection.show_details(self.vmuuid)
def vm_updated(self, uuid):
def vm_updated(self, connection, uuid):
if uuid == self.vmuuid:
self.refresh_status()

View File

@ -107,9 +107,9 @@ class vmmDetails:
"on_control_snapshot_clicked": self.control_vm_snapshot,
})
self.connection.connect_to_signal("vm_updated", self.refresh_overview)
self.connection.connect("vm-updated", self.refresh_overview)
self.change_graph_ranges()
self.refresh_overview(vmuuid)
self.refresh_overview(self.connection, vmuuid)
self.hw_selected()
def show(self):
@ -207,7 +207,7 @@ class vmmDetails:
self.lastStatus = status
def refresh_overview(self, vmuuid):
def refresh_overview(self, connection, vmuuid):
if not(vmuuid == self.vmuuid):
return

View File

@ -15,9 +15,9 @@ class vmmManager:
self.connection = connection
self.prepare_vmlist()
self.connection.connect_to_signal("vm_added", self.vm_added)
self.connection.connect_to_signal("vm_removed", self.vm_removed)
self.connection.connect_to_signal("vm_updated", self.vm_updated)
self.connection.connect("vm-added", self.vm_added)
self.connection.connect("vm-removed", self.vm_removed)
self.connection.connect("vm-updated", self.vm_updated)
self.config.on_vmlist_status_visible_changed(self.toggle_status_visible_widget)
self.config.on_vmlist_cpu_usage_visible_changed(self.toggle_cpu_usage_visible_widget)
@ -91,10 +91,10 @@ class vmmManager:
def open_connection(self, src=None):
self.connection.show_open_connection()
def vm_added(self, vmuuid, name):
def vm_added(self, connection, vmuuid, name):
vmlist = self.window.get_widget("vm-list")
model = vmlist.get_model()
print "Added\n"
dup = 0
for row in range(model.iter_n_children(None)):
vm = model.get_value(model.iter_nth_child(None, row), 0)
@ -105,7 +105,7 @@ class vmmManager:
model.append([vmuuid, name])
def vm_removed(self, vmuuid):
def vm_removed(self, connection, vmuuid):
vmlist = self.window.get_widget("vm-list")
model = vmlist.get_model()
@ -116,7 +116,7 @@ class vmmManager:
model.remove(model.iter_nth_child(None, row))
break
def vm_updated(self, vmuuid):
def vm_updated(self, connection, vmuuid):
vmlist = self.window.get_widget("vm-list")
model = vmlist.get_model()

View File

@ -20,14 +20,14 @@ class vmmStats:
}
self.hostinfo = self.connection.get_host_info()
self.connection.connect_to_signal("vm_added", self._vm_added)
self.connection.connect_to_signal("vm_removed", self._vm_removed)
self.connection.connect("vm-added", self._vm_added)
self.connection.connect("vm-removed", self._vm_removed)
def _vm_added(self, vmuuid, name):
def _vm_added(self, connection, vmuuid, name):
self.record[vmuuid] = []
def _vm_removed(self, vmuuid):
def _vm_removed(self, connection, vmuuid):
del self.record[vmuuid]
def update(self, vmuuid, vm):