From: Zeeshan Ali (Khattak) Date: Sun, 18 Jan 2009 19:27:35 +0000 (+0000) Subject: Put Interactive response handling into a separate class. X-Git-Tag: RYGEL_0_2~26 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b3efdf2dd816265f78cf878afb557a2eba02c38d;p=profile%2Fivi%2Frygel.git Put Interactive response handling into a separate class. svn path=/trunk/; revision=447 --- diff --git a/src/rygel/Makefile.am b/src/rygel/Makefile.am index d671770..f03c3f2 100644 --- a/src/rygel/Makefile.am +++ b/src/rygel/Makefile.am @@ -41,6 +41,8 @@ BUILT_SOURCES = rygel-1.0.vapi \ 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_SOURCES = rygel-1.0.vapi \ 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-1.0.vapi: rygel-content-directory.vala \ 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 \ diff --git a/src/rygel/rygel-http-server.vala b/src/rygel/rygel-http-server.vala index 7b59470..71aadfd 100644 --- a/src/rygel/rygel-http-server.vala +++ b/src/rygel/rygel-http-server.vala @@ -39,7 +39,7 @@ public class Rygel.HTTPServer : GLib.Object { private string path_root; private GUPnP.Context context; - private List responses; + private List responses; public signal void need_stream_source (MediaItem item, out Element src); @@ -81,7 +81,19 @@ public class Rygel.HTTPServer : GLib.Object { 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 @@ public class Rygel.HTTPServer : GLib.Object { 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 @@ public class Rygel.HTTPServer : GLib.Object { } } -class Rygel.Seek : GLib.Object { +public class Rygel.Seek : GLib.Object { public Format format { get; private set; } private int64 _start; diff --git a/src/rygel/rygel-interactive-response.vala b/src/rygel/rygel-interactive-response.vala new file mode 100644 index 0000000..671dffe --- /dev/null +++ b/src/rygel/rygel-interactive-response.vala @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2008 Zeeshan Ali (Khattak) . + * Copyright (C) 2008 Nokia Corporation, all rights reserved. + * + * Author: Zeeshan Ali (Khattak) + * + * + * 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); + } +} +