[rygel] core: Implement DLNA's 'auto-destroy'



commit 2991984d0d640157e9be8e7cf703046e98be112e
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Sun Oct 31 00:50:07 2010 +0300

    core: Implement DLNA's 'auto-destroy'
    
    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                   |    1 +
 src/rygel/rygel-http-post.vala          |   13 +++++
 src/rygel/rygel-import-resource.vala    |    4 ++
 src/rygel/rygel-item-creator.vala       |    7 +++
 src/rygel/rygel-item-removal-queue.vala |   86 +++++++++++++++++++++++++++++++
 5 files changed, 111 insertions(+), 0 deletions(-)
---
diff --git a/src/rygel/Makefile.am b/src/rygel/Makefile.am
index d66d0ac..f9f3113 100644
--- a/src/rygel/Makefile.am
+++ b/src/rygel/Makefile.am
@@ -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 \
diff --git a/src/rygel/rygel-http-post.vala b/src/rygel/rygel-http-post.vala
index d326c63..8e7ff9d 100644
--- a/src/rygel/rygel-http-post.vala
+++ b/src/rygel/rygel-http-post.vala
@@ -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);
 
diff --git a/src/rygel/rygel-import-resource.vala b/src/rygel/rygel-import-resource.vala
index ffc338a..669b601 100644
--- a/src/rygel/rygel-import-resource.vala
+++ b/src/rygel/rygel-import-resource.vala
@@ -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 ();
diff --git a/src/rygel/rygel-item-creator.vala b/src/rygel/rygel-item-creator.vala
index 9a510ce..7293a11 100644
--- a/src/rygel/rygel-item-creator.vala
+++ b/src/rygel/rygel-item-creator.vala
@@ -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
index 0000000..a2a6331
--- /dev/null
+++ b/src/rygel/rygel-item-removal-queue.vala
@@ -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> ();
+    }
+}
+



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