rygel r447 - trunk/src/rygel



Author: zeeshanak
Date: Sun Jan 18 19:27:35 2009
New Revision: 447
URL: http://svn.gnome.org/viewvc/rygel?rev=447&view=rev

Log:
Put Interactive response handling into a separate class.

Added:
   trunk/src/rygel/rygel-interactive-response.vala
Modified:
   trunk/src/rygel/Makefile.am
   trunk/src/rygel/rygel-http-server.vala

Modified: trunk/src/rygel/Makefile.am
==============================================================================
--- trunk/src/rygel/Makefile.am	(original)
+++ trunk/src/rygel/Makefile.am	Sun Jan 18 19:27:35 2009
@@ -41,6 +41,8 @@
                 rygel-http-response.h \
                 rygel-streaming-response.c \
                 rygel-streaming-response.h \
+		rygel-interactive-response.c \
+		rygel-interactive-response.h \
 		rygel-resource-info.h \
 		rygel-resource-info.c \
 		rygel-icon-info.h \
@@ -81,6 +83,8 @@
 		rygel-http-response.h \
 		rygel-streaming-response.c \
 		rygel-streaming-response.h \
+		rygel-interactive-response.c \
+		rygel-interactive-response.h \
 		rygel-resource-info.h \
 		rygel-resource-info.c \
 		rygel-icon-info.h \
@@ -121,6 +125,7 @@
 		rygel-http-server.vala \
 		rygel-http-response.vala \
 		rygel-streaming-response.vala \
+		rygel-interactive-response.vala \
 		rygel-resource-info.vala \
 		rygel-icon-info.vala \
 		rygel-plugin.vala \

Modified: trunk/src/rygel/rygel-http-server.vala
==============================================================================
--- trunk/src/rygel/rygel-http-server.vala	(original)
+++ trunk/src/rygel/rygel-http-server.vala	Sun Jan 18 19:27:35 2009
@@ -39,7 +39,7 @@
     private string path_root;
 
     private GUPnP.Context context;
-    private List<StreamingResponse> responses;
+    private List<HTTPResponse> responses;
 
     public signal void need_stream_source (MediaItem   item,
                                            out Element src);
@@ -81,7 +81,19 @@
         this.responses.append (response);
     }
 
-    private void on_response_ended (StreamingResponse response) {
+    private void serve_uri (string       uri,
+                            Soup.Message msg,
+                            Seek?        seek) throws Error {
+        var response = new InteractiveResponse (this.context.server,
+                                                msg,
+                                                uri,
+                                                seek);
+        response.ended += on_response_ended;
+
+        this.responses.append (response);
+    }
+
+    private void on_response_ended (HTTPResponse response) {
         /* Remove the response from our list. */
         this.responses.remove (response);
     }
@@ -230,46 +242,14 @@
             return;
         }
 
-        File file = File.new_for_uri (uri);
-
-        string contents;
-        size_t file_length;
-
         try {
-           file.load_contents (null,
-                               out contents,
-                               out file_length,
-                               null);
+            this.serve_uri (uri, msg, seek);
         } catch (Error error) {
-            warning ("Failed to load contents from URI: %s: %s\n",
+            warning ("Error in attempting to serve %s: %s",
                      uri,
                      error.message);
             msg.set_status (Soup.KnownStatusCode.NOT_FOUND);
-            return;
-        }
-
-        size_t offset;
-        size_t length;
-        if (seek != null) {
-            offset = (size_t) seek.start;
-            length = (size_t) seek.length;
-
-            assert (offset < file_length);
-            assert (length <= file_length);
-        } else {
-            offset = 0;
-            length = file_length;
         }
-
-        if (seek != null) {
-            msg.set_status (Soup.KnownStatusCode.PARTIAL_CONTENT);
-        } else {
-            msg.set_status (Soup.KnownStatusCode.OK);
-        }
-
-        char *data = (char *) contents + offset;
-
-        msg.response_body.append (Soup.MemoryUse.COPY, data, length);
     }
 
     /* Parses the HTTP Range header on @message and sets:
@@ -352,7 +332,7 @@
         }
 }
 
-class Rygel.Seek : GLib.Object {
+public class Rygel.Seek : GLib.Object {
     public Format format { get; private set; }
 
     private int64 _start;

Added: trunk/src/rygel/rygel-interactive-response.vala
==============================================================================
--- (empty file)
+++ trunk/src/rygel/rygel-interactive-response.vala	Sun Jan 18 19:27:35 2009
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2008 Zeeshan Ali (Khattak) <zeeshanak gnome org>.
+ * Copyright (C) 2008 Nokia Corporation, all rights reserved.
+ *
+ * Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
+ *                               <zeeshan ali nokia com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ */
+
+using Rygel;
+using GUPnP;
+
+public class Rygel.InteractiveResponse : Rygel.HTTPResponse {
+    private Seek seek;
+
+    public InteractiveResponse (Soup.Server  server,
+                                Soup.Message msg,
+                                string       uri,
+                                Seek?        seek) throws Error {
+        base (server, msg, false);
+
+        this.seek = seek;
+
+        if (seek != null) {
+            msg.set_status (Soup.KnownStatusCode.PARTIAL_CONTENT);
+        } else {
+            msg.set_status (Soup.KnownStatusCode.OK);
+        }
+
+        File file = File.new_for_uri (uri);
+
+        file.load_contents_async (null, this.on_contents_loaded);
+    }
+
+    private void on_contents_loaded (GLib.Object source_object,
+                                     GLib.AsyncResult result) {
+        File file = (File) source_object;
+        string contents;
+        size_t file_length;
+
+        try {
+           file.load_contents_finish (result,
+                                      out contents,
+                                      out file_length,
+                                      null);
+        } catch (Error error) {
+            warning ("Failed to load contents from URI: %s: %s\n",
+                     file.get_uri (),
+                     error.message);
+            msg.set_status (Soup.KnownStatusCode.NOT_FOUND);
+            return;
+        }
+
+        size_t offset;
+        size_t length;
+        if (seek != null) {
+            offset = (size_t) seek.start;
+            length = (size_t) seek.length;
+
+            assert (offset < file_length);
+            assert (length <= file_length);
+        } else {
+            offset = 0;
+            length = file_length;
+        }
+
+        char *data = (char *) contents + offset;
+
+        this.push_data (data, length);
+        this.end (false);
+    }
+}
+



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