core: Implement DLNA's 'auto-destroy'
authorZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Sat, 30 Oct 2010 21:50:07 +0000 (00:50 +0300)
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Tue, 2 Nov 2010 15:40:50 +0000 (17:40 +0200)
If an item has been created using DLNA's AnyContainer feature, we are required
to destroy it:

* If no content are pushed to it after 35 seconds of it's creation.
* If content transfer to newly created item fails.

src/rygel/Makefile.am
src/rygel/rygel-http-post.vala
src/rygel/rygel-import-resource.vala
src/rygel/rygel-item-creator.vala
src/rygel/rygel-item-removal-queue.vala [new file with mode: 0644]

index d66d0ac..f9f3113 100644 (file)
@@ -102,6 +102,7 @@ VAPI_SOURCE_FILES = rygel-configuration.vala \
                    rygel-import-resource.vala \
                    rygel-item-creator.vala \
                    rygel-item-destroyer.vala \
+                   rygel-item-removal-queue.vala \
                    rygel-search-expression.vala \
                    rygel-relational-expression.vala \
                    rygel-logical-expression.vala \
index d326c63..8e7ff9d 100644 (file)
@@ -41,6 +41,19 @@ internal class Rygel.HTTPPost : HTTPRequest {
     }
 
     protected override async void handle () throws Error {
+        var queue = ItemRemovalQueue.get_default ();
+        queue.dequeue (this.item);
+
+        try {
+            yield this.handle_real ();
+        } catch (Error error) {
+            yield queue.remove_now (this.item, this.cancellable);
+
+            throw error;
+        }
+    }
+
+    private async void handle_real () throws Error {
         this.msg.got_chunk.connect (this.on_got_chunk);
         this.msg.got_body.connect (this.on_got_body);
 
index ffc338a..669b601 100644 (file)
@@ -127,6 +127,9 @@ internal class Rygel.ImportResource : GLib.Object, Rygel.StateMachine {
         // We can already return the action now
         this.action.return ();
 
+        var queue = ItemRemovalQueue.get_default ();
+        queue.dequeue (this.item);
+
         try {
             var destination_file = File.new_for_uri (this.item.uris[0]);
             var source_file = File.new_for_uri (source_uri);
@@ -145,6 +148,7 @@ internal class Rygel.ImportResource : GLib.Object, Rygel.StateMachine {
         } catch (Error err) {
             warning ("%s", err.message);
             this.status = TransferStatus.ERROR;
+            yield queue.remove_now (this.item, this.cancellable);
         }
 
         this.completed ();
index 9a510ce..7293a11 100644 (file)
@@ -120,6 +120,13 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
 
             // Conclude the successful action
             this.conclude ();
+
+            if (this.container_id == "DLNA.ORG_AnyContainer" &&
+                this.item.place_holder) {
+                var queue = ItemRemovalQueue.get_default ();
+
+                queue.queue (this.item, this.cancellable);
+            }
         } catch (Error err) {
             this.handle_error (err);
         }
diff --git a/src/rygel/rygel-item-removal-queue.vala b/src/rygel/rygel-item-removal-queue.vala
new file mode 100644 (file)
index 0000000..a2a6331
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2010 Nokia Corporation.
+ *
+ * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using Gee;
+
+/**
+ * Queues items for removal after 35 seconds or immediately.
+ */
+internal class Rygel.ItemRemovalQueue: GLib.Object {
+    private const uint TIMEOUT = 35;
+
+    private static ItemRemovalQueue removal_queue;
+
+    private HashMap<string,uint> item_timeouts;
+
+    public static ItemRemovalQueue get_default () {
+        if (unlikely (removal_queue == null)) {
+            removal_queue = new ItemRemovalQueue ();
+        }
+
+        return removal_queue;
+    }
+
+    public void queue (MediaItem item, Cancellable? cancellable) {
+        var timeout = Timeout.add_seconds (TIMEOUT, () => {
+            debug ("Timeout on temporary item '%s'.", item.id);
+            this.remove_now.begin (item, cancellable);
+
+            return false;
+        });
+
+        item_timeouts.set (item.id, timeout);
+    }
+
+    public bool dequeue (MediaItem item) {
+        uint timeout;
+
+        if (item_timeouts.unset (item.id, out timeout)) {
+            Source.remove (timeout);
+
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    public async void remove_now (MediaItem item, Cancellable? cancellable) {
+        item_timeouts.unset (item.id);
+
+        var parent = item.parent as WritableContainer;
+
+        try {
+            yield parent.remove_item (item.id, cancellable);
+
+            debug ("Auto-destroyed item '%s'!", item.id);
+        } catch (Error err) {
+            warning ("Failed to auto-destroy temporary item '%s': %s",
+                     item.id,
+                     err.message);
+        }
+    }
+
+    private ItemRemovalQueue () {
+        item_timeouts = new HashMap<string,uint> ();
+    }
+}
+