[rygel/wip/ruih] ruih: Make ServiceManager a singleton



commit 420de124342ed4b2a3fa9297f0bbfc5f213df88f
Author: Jens Georg <mail jensge org>
Date:   Sun Oct 19 10:16:30 2014 +0200

    ruih: Make ServiceManager a singleton
    
    Service was keeping information that belonged to ServiceManager and
    basically constructing it in its static constructor to create a single
    instance that was watching the file.
    
    This commit moves all static members of Service to ServiceManager,
    making it a full GObject and using the general singleton pattern we use
    in Rygel.
    
    This also fixes an potential issue in multi-homed hosts that the
    static file-monitor would cancelled by Service's instance destructor if
    one network interface went down.
    
    Signed-off-by: Jens Georg <mail jensge org>

 src/librygel-ruih/rygel-ruih-service.vala        |   51 ++------------------
 src/librygel-ruih/rygel-ruih-servicemanager.vala |   57 +++++++++++++++++++++-
 2 files changed, 60 insertions(+), 48 deletions(-)
---
diff --git a/src/librygel-ruih/rygel-ruih-service.vala b/src/librygel-ruih/rygel-ruih-service.vala
index 749f7e0..4241f28 100644
--- a/src/librygel-ruih/rygel-ruih-service.vala
+++ b/src/librygel-ruih/rygel-ruih-service.vala
@@ -39,46 +39,6 @@ internal class Rygel.RuihService: Service {
                     "urn:schemas-upnp-org:service:RemoteUIServer:1";
     public const string DESCRIPTION_PATH =
                     "xml/RemoteUIServerService.xml";
-    public const string UI_LISTING_FILE_NAME = "UIList.xml";
-
-    internal static Cancellable cancellable;
-    private static string ui_listing_full_path;
-    private static FileMonitor ui_file_monitor;
-    private static RuihServiceManager ruih_manager;
-
-    static construct {
-        cancellable = new Cancellable ();
-        var ui_listing_directory = Path.build_filename (
-                Environment.get_user_config_dir (), "Rygel");
-        ui_listing_full_path = Path.build_filename (ui_listing_directory,
-                UI_LISTING_FILE_NAME);
-        DirUtils.create_with_parents (ui_listing_directory, 0755);
-        ruih_manager = new RuihServiceManager ();
-
-        try {
-            ruih_manager.set_ui_list (ui_listing_full_path);
-            var ui_file = File.new_for_path (ui_listing_full_path);
-            ui_file_monitor = ui_file.monitor_file (FileMonitorFlags.NONE,
-                                                        cancellable);
-            ui_file_monitor.changed.connect ((src, dest, event) => {
-                try {
-                        ruih_manager.set_ui_list (ui_listing_full_path);
-                }
-                catch (RuihServiceError e) {
-                   error ("Failed to set UIList for file %s - %s\n", ui_listing_full_path,
-                         e.message);
-                }
-            });
-        }
-        catch (Rygel.RuihServiceError e) {
-            error ("Failed to set initial UI list for file %s - %s\n",
-                  ui_listing_full_path, e.message);
-        }
-        catch (GLib.IOError e) {
-            error ("Failed to monitor the file %s - %s\n", ui_listing_full_path,
-                  e.message);
-        }
-    }
 
     public override void constructed () {
         base.constructed ();
@@ -90,10 +50,6 @@ internal class Rygel.RuihService: Service {
             (this.get_compatible_uis_cb);
     }
 
-    ~RuihService () {
-        cancellable.cancel ();
-    }
-
     /* Browse action implementation */
     private void get_compatible_uis_cb (Service       content_dir,
                             ServiceAction action) {
@@ -105,8 +61,9 @@ internal class Rygel.RuihService: Service {
 
         try
         {
-            string compat_ui = ruih_manager.get_compatible_uis
-                (input_device_profile, input_ui_filter);
+            var manager = RuihServiceManager.get_default ();
+            var compat_ui = manager.get_compatible_uis (input_device_profile,
+                                                        input_ui_filter);
 
             action.set ("UIListing", typeof (string), compat_ui);
             action.return ();
@@ -123,4 +80,4 @@ internal class Rygel.RuihService: Service {
         value.init (typeof (string));
         value.set_string ("");
     }
-}
\ No newline at end of file
+}
diff --git a/src/librygel-ruih/rygel-ruih-servicemanager.vala 
b/src/librygel-ruih/rygel-ruih-servicemanager.vala
index 89c285f..d4777fd 100644
--- a/src/librygel-ruih/rygel-ruih-servicemanager.vala
+++ b/src/librygel-ruih/rygel-ruih-servicemanager.vala
@@ -31,7 +31,7 @@ using Gee;
 using Xml;
 using GLib;
 
-public class Rygel.RuihServiceManager
+public class Rygel.RuihServiceManager : Object
 {
     private static const string DEVICEPROFILE = "deviceprofile";
     private static const string PROTOCOL = "protocol";
@@ -50,6 +50,61 @@ public class Rygel.RuihServiceManager
     private ArrayList<UIElem> ui_list;
     private Object object = null;
 
+    private static RuihServiceManager instance = null;
+
+    internal Cancellable cancellable;
+    private string ui_listing_full_path;
+    private FileMonitor ui_file_monitor;
+    private RuihServiceManager ruih_manager;
+
+    public const string UI_LISTING_FILE_NAME = "UIList.xml";
+
+    public static RuihServiceManager get_default () {
+        if (instance == null) {
+            instance = new RuihServiceManager ();
+        }
+
+        return instance;
+    }
+
+    public override void constructed () {
+        base.constructed ();
+
+        unowned string config_dir = Environment.get_user_config_dir ();
+        this.cancellable = new Cancellable ();
+        var ui_listing_directory = Path.build_filename (config_dir, "Rygel");
+        this.ui_listing_full_path = Path.build_filename (ui_listing_directory,
+                                                         UI_LISTING_FILE_NAME);
+        DirUtils.create_with_parents (ui_listing_directory, 0755);
+
+        try {
+            this.set_ui_list (ui_listing_full_path);
+            var ui_file = File.new_for_path (ui_listing_full_path);
+            this.ui_file_monitor = ui_file.monitor_file (FileMonitorFlags.NONE,
+                                                         cancellable);
+            this.ui_file_monitor.changed.connect ((src, dest, event) => {
+                try {
+                        ruih_manager.set_ui_list (ui_listing_full_path);
+                } catch (RuihServiceError e) {
+                    error ("Failed to set UIList for file %s - %s\n",
+                           ui_listing_full_path,
+                           e.message);
+                }
+            });
+        } catch (Rygel.RuihServiceError e) {
+            error ("Failed to set initial UI list for file %s - %s\n",
+                  this.ui_listing_full_path, e.message);
+        } catch (GLib.IOError e) {
+            error ("Failed to monitor the file %s - %s\n",
+                   this.ui_listing_full_path,
+                   e.message);
+        }
+    }
+
+    ~RuihServiceManager () {
+        this.cancellable.cancel ();
+    }
+
     public void set_ui_list (string ui_list_file_path) throws RuihServiceError {
         lock (object) {
             this.ui_list = new ArrayList<UIElem> ();


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