[gnome-boxes] Allocate unique SPICE port



commit 6804dec894435e0106d93e6f1e761c536e322dfe
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Thu Nov 17 21:23:56 2011 +0200

    Allocate unique SPICE port
    
    If we leave this for libvirt, it will allocate same port for every
    domain. This patch ensures every domain created gets a new port.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=664294

 src/app.vala        |    2 +-
 src/collection.vala |    2 +-
 src/vm-creator.vala |   24 ++++++++++++++++++++++--
 src/wizard.vala     |    2 +-
 4 files changed, 25 insertions(+), 5 deletions(-)
---
diff --git a/src/app.vala b/src/app.vala
index 09c898d..410ee17 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -36,9 +36,9 @@ private class Boxes.App: Boxes.UI {
     public Properties properties;
     public DisplayPage display_page;
     public string? uri { get; private set; }
+    public Collection collection;
 
     private Clutter.TableLayout box_table;
-    private Collection collection;
     private CollectionView view;
 
     private HashTable<string,GVir.Connection> connections;
diff --git a/src/collection.vala b/src/collection.vala
index 673387d..13688fe 100644
--- a/src/collection.vala
+++ b/src/collection.vala
@@ -8,7 +8,7 @@ private class Boxes.Collection: GLib.Object {
     private Boxes.App app;
     public signal void item_added (CollectionItem item);
 
-    GenericArray<CollectionItem> items;
+    public GenericArray<CollectionItem> items;
 
     construct {
         items = new GenericArray<CollectionItem> ();
diff --git a/src/vm-creator.vala b/src/vm-creator.vala
index e419fab..e51418a 100644
--- a/src/vm-creator.vala
+++ b/src/vm-creator.vala
@@ -5,9 +5,28 @@ using GVir;
 
 private class Boxes.VMCreator {
     private Connection connection;
+    private uint16 spice_port;
 
-    public VMCreator (string uri) throws GLib.Error {
+    public VMCreator (App app, string uri) throws GLib.Error {
         connection = new Connection (uri);
+        spice_port = 5800;
+
+        // Ensure unique spice port
+        foreach (var item in app.collection.items.data) {
+            if (item is LibvirtMachine) {
+                var machine = item as LibvirtMachine;
+                var xmldoc = machine.domain.get_config (0).to_xml();
+
+                var type = extract_xpath (xmldoc, "string(/domain/devices/graphics/@type)", true);
+                var port_str = extract_xpath (xmldoc, "string(/domain/devices/graphics[ type='" + type + "']/@port)");
+                var port = int.parse (port_str);
+
+                if (port > 0)
+                    spice_port = uint16.max (spice_port, (uint16) port);
+            }
+        }
+
+        spice_port++;
     }
 
     public async GVir.Domain create_domain_for_installer (InstallerMedia install_media,
@@ -28,6 +47,7 @@ private class Boxes.VMCreator {
         var target_path = yield create_target_volume (name, resources.storage);
 
         var xml = get_virt_xml (install_media, name, target_path, resources);
+        spice_port++; // So next VM doesn't get the same port
         var config = new GVirConfig.Domain.from_xml (xml);
 
         return connection.create_domain (config);
@@ -79,7 +99,7 @@ private class Boxes.VMCreator {
                "      <mac address='00:11:22:33:44:55'/>\n" +
                "    </interface>\n" +
                "    <input type='tablet' bus='usb'/>\n" +
-               "    <graphics type='spice'/>\n" +
+               "    <graphics type='spice' port='" + spice_port.to_string () + "' />\n" +
                "    <console type='pty'/>\n" +
                "    <video>\n" +
                // FIXME: Should be 'qxl', work-around for a spice bug
diff --git a/src/wizard.vala b/src/wizard.vala
index b5fc136..7feb42d 100644
--- a/src/wizard.vala
+++ b/src/wizard.vala
@@ -194,7 +194,7 @@ private class Boxes.Wizard: Boxes.UI {
         if (client == null) {
             client = new GUdev.Client ({"block"});
             os_db = new OSDatabase ();
-            vm_creator = new VMCreator ("qemu:///session"); // FIXME
+            vm_creator = new VMCreator (app, "qemu:///session"); // FIXME
         }
 
         next_button.sensitive = false;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]