[rygel] core: Simplify HTTPResponse classes' construction



commit df677a5abfd0d38a7c92bcc750629185bfb71fe7
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Thu Mar 17 01:56:21 2011 +0200

    core: Simplify HTTPResponse classes' construction

 src/rygel/rygel-http-gst-response.vala       |   24 +++++---
 src/rygel/rygel-http-identity-handler.vala   |   47 ++-------------
 src/rygel/rygel-http-response.vala           |   13 ++--
 src/rygel/rygel-http-seekable-response.vala  |   42 +++++++++----
 src/rygel/rygel-http-transcode-handler.vala  |    7 +--
 tests/rygel-http-gst-response-test.vala      |   83 ++++++++++++++++++++-----
 tests/rygel-http-response-test.vala          |    2 +-
 tests/rygel-http-seekable-response-test.vala |   84 ++++++++++++++++++++++---
 8 files changed, 199 insertions(+), 103 deletions(-)
---
diff --git a/src/rygel/rygel-http-gst-response.vala b/src/rygel/rygel-http-gst-response.vala
index 817f67c..48ae55e 100644
--- a/src/rygel/rygel-http-gst-response.vala
+++ b/src/rygel/rygel-http-gst-response.vala
@@ -37,18 +37,24 @@ internal class Rygel.HTTPGstResponse : Rygel.HTTPResponse {
     private int64 buffered;
     private bool out_of_sync;
 
-    public HTTPGstResponse (Soup.Server  server,
-                            Soup.Message msg,
-                            string       name,
-                            Element      src,
-                            HTTPSeek?    seek,
-                            Cancellable? cancellable) throws Error {
-        base (server, msg, false, cancellable);
+    public HTTPGstResponse (HTTPGet        request,
+                            HTTPGetHandler request_handler,
+                            Element?       gst_src = null) throws Error {
+        base (request, request_handler, false);
 
         this.msg.response_headers.set_encoding (Soup.Encoding.EOF);
 
-        this.prepare_pipeline (name, src);
-        this.seek = seek;
+        var src = gst_src;
+        if (src == null) {
+            src = request.item.create_stream_source ();
+
+            if (src == null) {
+                throw new HTTPRequestError.NOT_FOUND (_("Not found"));
+            }
+        }
+
+        this.prepare_pipeline ("RygelHTTPGstResponse", src);
+        this.seek = request.seek;
 
         this.buffered = 0;
         this.out_of_sync = false;
diff --git a/src/rygel/rygel-http-identity-handler.vala b/src/rygel/rygel-http-identity-handler.vala
index aa5df4c..9ae2eb6 100644
--- a/src/rygel/rygel-http-identity-handler.vala
+++ b/src/rygel/rygel-http-identity-handler.vala
@@ -73,49 +73,12 @@ internal class Rygel.HTTPIdentityHandler : Rygel.HTTPGetHandler {
     }
 
     private HTTPResponse render_body_real (HTTPGet request) throws Error {
-        if (request.subtitle != null) {
-            return new HTTPSeekableResponse (request.server,
-                                             request.msg,
-                                             request.subtitle.uri,
-                                             request.seek,
-                                             request.subtitle.size,
-                                             this.cancellable);
-        } else if (request.thumbnail != null) {
-            return new HTTPSeekableResponse (request.server,
-                                             request.msg,
-                                             request.thumbnail.uri,
-                                             request.seek,
-                                             request.thumbnail.size,
-                                             this.cancellable);
-        }
-
-        var item = request.item;
-
-        if (item.should_stream ()) {
-            Gst.Element src = item.create_stream_source ();
-            if (src == null) {
-                throw new HTTPRequestError.NOT_FOUND (_("Not found"));
-            }
-
-            return new HTTPGstResponse (request.server,
-                                        request.msg,
-                                        "RygelHTTPGstResponse",
-                                        src,
-                                        request.seek,
-                                        this.cancellable);
+        if (request.subtitle != null ||
+            request.thumbnail != null ||
+            !(request.item.should_stream ())) {
+            return new HTTPSeekableResponse (request, this);
         } else {
-            if (item.uris.size == 0) {
-                throw new HTTPRequestError.NOT_FOUND
-                                        (_("Item '%s' didn't provide a URI"),
-                                         item.id);
-            }
-
-            return new HTTPSeekableResponse (request.server,
-                                             request.msg,
-                                             item.uris.get (0),
-                                             request.seek,
-                                             item.size,
-                                             this.cancellable);
+            return new HTTPGstResponse (request, this);
         }
     }
 }
diff --git a/src/rygel/rygel-http-response.vala b/src/rygel/rygel-http-response.vala
index dde73f7..28bf86c 100644
--- a/src/rygel/rygel-http-response.vala
+++ b/src/rygel/rygel-http-response.vala
@@ -54,13 +54,12 @@ internal abstract class Rygel.HTTPResponse : GLib.Object, Rygel.StateMachine {
         }
     }
 
-    public HTTPResponse (Soup.Server  server,
-                         Soup.Message msg,
-                         bool         partial,
-                         Cancellable? cancellable) {
-        this.server = server;
-        this.msg = msg;
-        this.cancellable = cancellable;
+    public HTTPResponse (HTTPGet        request,
+                         HTTPGetHandler request_handler,
+                         bool           partial) throws Error {
+        this.server = request.server;
+        this.msg = request.msg;
+        this.cancellable = request_handler.cancellable;
 
         if (partial) {
             this.msg.set_status (Soup.KnownStatusCode.PARTIAL_CONTENT);
diff --git a/src/rygel/rygel-http-seekable-response.vala b/src/rygel/rygel-http-seekable-response.vala
index f72ac01..9438272 100644
--- a/src/rygel/rygel-http-seekable-response.vala
+++ b/src/rygel/rygel-http-seekable-response.vala
@@ -34,18 +34,36 @@ internal class Rygel.HTTPSeekableResponse : Rygel.HTTPResponse {
     private uint8[] buffer;
     private size_t total_length;
 
-    public HTTPSeekableResponse (Soup.Server  server,
-                                 Soup.Message msg,
-                                 string       uri,
-                                 HTTPSeek     seek,
-                                 int64        file_length,
-                                 Cancellable? cancellable) {
-        var partial = seek.length < file_length;
-
-        base (server, msg, partial, cancellable);
-
-        this.seek = seek;
-        this.total_length = (size_t) seek.length;
+    public HTTPSeekableResponse (HTTPGet        request,
+                                 HTTPGetHandler request_handler) throws Error {
+        string uri;
+        int64 file_length;
+
+        if (request.subtitle != null) {
+            uri = request.subtitle.uri;
+            file_length = request.subtitle.size;
+        } else if (request.thumbnail != null) {
+            uri = request.thumbnail.uri;
+            file_length = request.thumbnail.size;
+        } else {
+            var item = request.item;
+
+            if (item.uris.size == 0) {
+                throw new HTTPRequestError.NOT_FOUND
+                                        (_("Item '%s' didn't provide a URI"),
+                                         item.id);
+            }
+
+            uri = item.uris.get (0);
+            file_length = item.size;
+        }
+
+        var partial = request.seek.length < file_length;
+
+        base (request, request_handler, partial);
+
+        this.seek = request.seek;
+        this.total_length = (size_t) this.seek.length;
 
         this.buffer = new uint8[HTTPSeekableResponse.BUFFER_LENGTH];
         this.file = File.new_for_uri (uri);
diff --git a/src/rygel/rygel-http-transcode-handler.vala b/src/rygel/rygel-http-transcode-handler.vala
index f85363f..1478879 100644
--- a/src/rygel/rygel-http-transcode-handler.vala
+++ b/src/rygel/rygel-http-transcode-handler.vala
@@ -59,12 +59,7 @@ internal class Rygel.HTTPTranscodeHandler : HTTPGetHandler {
         try {
             src = this.transcoder.create_source (item, src);
 
-            return new HTTPGstResponse (request.server,
-                                        request.msg,
-                                        "RygelHTTPGstResponse",
-                                        src,
-                                        request.seek,
-                                        this.cancellable);
+            return new HTTPGstResponse (request, this, src);
         } catch (GLib.Error err) {
             throw new HTTPRequestError.NOT_FOUND (err.message);
         }
diff --git a/tests/rygel-http-gst-response-test.vala b/tests/rygel-http-gst-response-test.vala
index 0b3217c..72a76ea 100644
--- a/tests/rygel-http-gst-response-test.vala
+++ b/tests/rygel-http-gst-response-test.vala
@@ -25,10 +25,7 @@ using Soup;
 using Gst;
 
 public class Rygel.HTTPGstResponseTest : Rygel.HTTPResponseTest {
-    private static const long BLOCK_SIZE = MAX_BYTES / 16;
-    private static const long MAX_BUFFERS = MAX_BYTES / BLOCK_SIZE;
-
-    private dynamic Element src;
+    private MediaItem item;
 
     public static int main (string[] args) {
         Gst.init (ref args);
@@ -50,28 +47,82 @@ public class Rygel.HTTPGstResponseTest : Rygel.HTTPResponseTest {
         return 0;
     }
 
-    construct {
-        this.src = GstUtils.create_element ("audiotestsrc", null);
-    }
-
     private HTTPGstResponseTest.complete () throws Error {
         base.complete ();
 
-        this.src.blocksize = BLOCK_SIZE;
-        this.src.num_buffers = MAX_BUFFERS;
+        this.item = new MediaItem.fixed_size ();
     }
 
     private HTTPGstResponseTest.abort () throws Error {
         base.abort ();
+
+        this.item = new MediaItem ();
     }
 
     internal override HTTPResponse create_response (Soup.Message msg)
                                                      throws Error {
-        return new HTTPGstResponse (this.server.context.server,
-                                    msg,
-                                    "TestingHTTPGstResponse",
-                                    this.src,
-                                    null,
-                                    this.cancellable);
+        var request = new HTTPGet (this.server.context.server,
+                                   msg,
+                                   this.item,
+                                   this.cancellable);
+        var handler = new HTTPGetHandler (this.cancellable);
+
+        return new HTTPGstResponse (request, handler);
+    }
+}
+
+public class Rygel.HTTPGet : GLib.Object {
+    public Soup.Server server;
+    public Soup.Message msg;
+
+    public Cancellable cancellable;
+
+    public MediaItem item;
+
+    internal HTTPSeek seek;
+
+    public HTTPGet (Soup.Server  server,
+                    Soup.Message msg,
+                    MediaItem    item,
+                    Cancellable? cancellable) {
+        this.server = server;
+        this.msg = msg;
+        this.item = item;
+        this.cancellable = cancellable;
+    }
+}
+
+public class Rygel.HTTPGetHandler : GLib.Object {
+    public Cancellable cancellable;
+
+    public HTTPGetHandler (Cancellable? cancellable) {
+        this.cancellable = cancellable;
+    }
+}
+
+public class Rygel.MediaItem {
+    private static const long BLOCK_SIZE = HTTPResponseTest.MAX_BYTES / 16;
+    private static const long MAX_BUFFERS =
+                                        HTTPResponseTest.MAX_BYTES / BLOCK_SIZE;
+
+    private dynamic Element src;
+
+    public MediaItem () {
+        this.src = GstUtils.create_element ("audiotestsrc", null);
     }
+
+    public MediaItem.fixed_size () {
+        this ();
+
+        this.src.blocksize = BLOCK_SIZE;
+        this.src.num_buffers = MAX_BUFFERS;
+    }
+
+    public Element? create_stream_source () {
+        return this.src;
+    }
+}
+
+public errordomain Rygel.HTTPRequestError {
+    NOT_FOUND = Soup.KnownStatusCode.NOT_FOUND
 }
diff --git a/tests/rygel-http-response-test.vala b/tests/rygel-http-response-test.vala
index b15a30d..02dfba4 100644
--- a/tests/rygel-http-response-test.vala
+++ b/tests/rygel-http-response-test.vala
@@ -31,7 +31,7 @@ public errordomain Rygel.TestError {
 }
 
 public abstract class Rygel.HTTPResponseTest : GLib.Object {
-    protected static const long MAX_BYTES = 1024;
+    public const long MAX_BYTES = 1024;
 
     protected HTTPServer server;
     protected HTTPClient client;
diff --git a/tests/rygel-http-seekable-response-test.vala b/tests/rygel-http-seekable-response-test.vala
index 7c6ccaf..ada26be 100644
--- a/tests/rygel-http-seekable-response-test.vala
+++ b/tests/rygel-http-seekable-response-test.vala
@@ -22,7 +22,7 @@
  */
 
 using Soup;
-using Gst;
+using Gee;
 
 public class Rygel.HTTPSeek : GLib.Object {
     public int64 start { get; private set; }
@@ -38,8 +38,6 @@ public class Rygel.HTTPSeek : GLib.Object {
 }
 
 public class Rygel.HTTPSeekableResponseTest : Rygel.HTTPResponseTest {
-    private static string URI = "file:///tmp/rygel-dummy-test-file";
-
     private File dummy_file;
 
     public static int main (string[] args) {
@@ -82,7 +80,7 @@ public class Rygel.HTTPSeekableResponseTest : Rygel.HTTPResponseTest {
     }
 
     private void create_dummy_file () throws Error {
-        this.dummy_file = File.new_for_uri (URI);
+        this.dummy_file = File.new_for_uri (MediaItem.URI);
         var stream = this.dummy_file.replace (null, false, 0, null);
 
         // Put randon stuff into it
@@ -92,12 +90,78 @@ public class Rygel.HTTPSeekableResponseTest : Rygel.HTTPResponseTest {
     internal override HTTPResponse create_response (Soup.Message msg)
                                                     throws Error {
         var seek = new HTTPSeek (0, 1024);
+        var item = new MediaItem ();
+
+        var request = new HTTPGet (this.server.context.server,
+                                   msg,
+                                   item,
+                                   seek,
+                                   this.cancellable);
+        var handler = new HTTPGetHandler (this.cancellable);
+
+        return new HTTPSeekableResponse (request, handler);
+    }
+}
+
+public class Rygel.HTTPGet : GLib.Object {
+    public Soup.Server server;
+    public Soup.Message msg;
+
+    public Cancellable cancellable;
+
+    public MediaItem item;
+
+    internal HTTPSeek seek;
+
+    public Subtitle subtitle;
+    public Thumbnail thumbnail;
+
+    public HTTPGet (Soup.Server  server,
+                    Soup.Message msg,
+                    MediaItem    item,
+                    HTTPSeek     seek,
+                    Cancellable? cancellable) {
+        this.server = server;
+        this.msg = msg;
+        this.item = item;
+        this.seek = seek;
+        this.cancellable = cancellable;
+    }
+}
+
+public class Rygel.HTTPGetHandler : GLib.Object {
+    public Cancellable cancellable;
 
-        return new HTTPSeekableResponse (this.server.context.server,
-                                         msg,
-                                         this.dummy_file.get_uri (),
-                                         seek,
-                                         1024,
-                                         this.cancellable);
+    public HTTPGetHandler (Cancellable? cancellable) {
+        this.cancellable = cancellable;
     }
 }
+
+public class Rygel.MediaItem {
+    public const string URI = "file:///tmp/rygel-dummy-test-file";
+
+    public string id = "Dummy";
+    public Gee.ArrayList<string> uris;
+    public int64 size = 1024;
+
+    public MediaItem () {
+        this.uris = new ArrayList<string> ();
+
+        this.uris.add (URI);
+    }
+}
+
+public class Rygel.Subtitle {
+    public string uri;
+    public int64 size = -1;   // Size in bytes
+}
+
+public class Rygel.Thumbnail {
+    public string uri;
+
+    public int64 size = -1; // Size in bytes
+}
+
+public errordomain Rygel.HTTPRequestError {
+    NOT_FOUND = Soup.KnownStatusCode.NOT_FOUND
+}



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