From 2da2c6cc5658c0ecbee515ce3d5789c317c66786 Mon Sep 17 00:00:00 2001 From: Thibault Saunier Date: Fri, 8 Aug 2014 12:33:54 +0200 Subject: [PATCH] validate:launcher: Allow limitating local HTTP server bandwith By default we limit its bandwith to 1MBps which is somehow similare to a good internet connection case. --- validate/gst/validate/gst-validate-bin-monitor.c | 56 +++++++++++++++++++----- validate/gst/validate/gst-validate-bin-monitor.h | 1 + validate/tools/launcher/RangeHTTPServer.py | 12 ++++- validate/tools/launcher/httpserver.py | 12 ++--- validate/tools/launcher/main.py | 3 ++ 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/validate/gst/validate/gst-validate-bin-monitor.c b/validate/gst/validate/gst-validate-bin-monitor.c index c4cbb90..e224e57 100644 --- a/validate/gst/validate/gst-validate-bin-monitor.c +++ b/validate/gst/validate/gst-validate-bin-monitor.c @@ -186,18 +186,50 @@ _bus_handler (GstBus * bus, GstMessage * message, GError *err; gchar *debug; - if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) { - gst_message_parse_error (message, &err, &debug); - GST_VALIDATE_REPORT (monitor, ERROR_ON_BUS, - "Got error: %s -- Debug message: %s", err->message, debug); - g_error_free (err); - g_free (debug); - } else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING) { - gst_message_parse_warning (message, &err, &debug); - GST_VALIDATE_REPORT (monitor, WARNING_ON_BUS, - "Got warning: %s -- Debug message: %s", err->message, debug); - g_error_free (err); - g_free (debug); + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_ERROR: + gst_message_parse_error (message, &err, &debug); + GST_VALIDATE_REPORT (monitor, ERROR_ON_BUS, + "Got error: %s -- Debug message: %s", err->message, debug); + g_error_free (err); + g_free (debug); + break; + case GST_MESSAGE_WARNING: + gst_message_parse_warning (message, &err, &debug); + GST_VALIDATE_REPORT (monitor, WARNING_ON_BUS, + "Got warning: %s -- Debug message: %s", err->message, debug); + g_error_free (err); + g_free (debug); + break; + case GST_MESSAGE_BUFFERING: + { + GstBufferingMode mode; + gint percent; + + gst_message_parse_buffering (message, &percent); + gst_message_parse_buffering_stats (message, &mode, NULL, NULL, NULL); + + if (percent == 100) { + /* a 100% message means buffering is done */ + if (monitor->buffering) { + monitor->print_pos_srcid = + g_timeout_add (PRINT_POSITION_TIMEOUT, + (GSourceFunc) print_position, monitor); + monitor->buffering = FALSE; + } + } else { + /* buffering... */ + if (!monitor->buffering) { + monitor->buffering = TRUE; + if (monitor->print_pos_srcid + && g_source_remove (monitor->print_pos_srcid)) + monitor->print_pos_srcid = 0; + } + } + break; + } + default: + break; } } diff --git a/validate/gst/validate/gst-validate-bin-monitor.h b/validate/gst/validate/gst-validate-bin-monitor.h index e8a1c19..588e190 100644 --- a/validate/gst/validate/gst-validate-bin-monitor.h +++ b/validate/gst/validate/gst-validate-bin-monitor.h @@ -62,6 +62,7 @@ struct _GstValidateBinMonitor { gulong element_added_id; guint print_pos_srcid; gboolean stateless; + gboolean buffering; }; /** diff --git a/validate/tools/launcher/RangeHTTPServer.py b/validate/tools/launcher/RangeHTTPServer.py index 827706d..b87277a 100644 --- a/validate/tools/launcher/RangeHTTPServer.py +++ b/validate/tools/launcher/RangeHTTPServer.py @@ -32,18 +32,22 @@ __version__ = "0.1" __all__ = ["RangeHTTPRequestHandler"] import os +import sys import posixpath import BaseHTTPServer import urllib import cgi import shutil import mimetypes +import time try: from cStringIO import StringIO except ImportError: from StringIO import StringIO +_bandwidth = 0 + class RangeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): """Simple HTTP request handler with GET and HEAD commands. @@ -70,6 +74,11 @@ class RangeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): while chunk > 0: if start_range + chunk > end_range: chunk = end_range - start_range + + if _bandwidth != 0: + time_to_sleep = float(float(chunk) / float(_bandwidth)) + time.sleep(time_to_sleep) + try: self.wfile.write(f.read(chunk)) except: @@ -146,7 +155,6 @@ class RangeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.send_header("Content-Length", end_range - start_range) self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) self.end_headers() - print "Sending Bytes ",start_range, " to ", end_range, "...\n" return (f, start_range, end_range) def list_directory(self, path): @@ -271,4 +279,6 @@ def test(HandlerClass = RangeHTTPRequestHandler, if __name__ == '__main__': + if len(sys.argv) > 2: + _bandwidth = int(sys.argv[2]) test() diff --git a/validate/tools/launcher/httpserver.py b/validate/tools/launcher/httpserver.py index d917749..86c80a7 100644 --- a/validate/tools/launcher/httpserver.py +++ b/validate/tools/launcher/httpserver.py @@ -63,20 +63,22 @@ class HTTPServer(loggable.Loggable): print "Starting Server" try: - self.debug("Lunching twistd server") - cmd = "%s %s %d" % (sys.executable, os.path.join(os.path.dirname(__file__), + self.debug("Lunching http server") + cmd = "%s %s %d %s" % (sys.executable, os.path.join(os.path.dirname(__file__), "RangeHTTPServer.py"), - self.options.http_server_port) + self.options.http_server_port, + self.options.http_bandwith, + ) curdir = os.path.abspath(os.curdir) os.chdir(self.options.http_server_dir) #cmd = "twistd -no web --path=%s -p %d" % ( # self.options.http_server_dir, self.options.http_server_port) - self.debug("Lunching server: %s", cmd) + self.debug("Lunching server: %s (logs in %s)", cmd, self._logsfile) self._process = subprocess.Popen(cmd.split(" "), stderr=self._logsfile, stdout=self._logsfile) os.chdir(curdir) - self.debug("Lunched twistd server") + self.debug("Lunched http server") # Dirty way to avoid eating to much CPU... # good enough for us anyway. time.sleep(1) diff --git a/validate/tools/launcher/main.py b/validate/tools/launcher/main.py index 3c4e8f9..7e64178 100644 --- a/validate/tools/launcher/main.py +++ b/validate/tools/launcher/main.py @@ -272,6 +272,9 @@ user argument, you can thus overrides command line options using that. http_server_group.add_argument("--http-server-port", dest="http_server_port", default=8079, help="Port on which to run the http server on localhost") + http_server_group.add_argument("--http-bandwith-limitation", dest="http_bandwith", + default=1024 * 1024, + help="The artificial bandwith limitation to introduce to the local server (in Bytes/sec) (default: 1 MBps)") http_server_group.add_argument("-s", "--folder-for-http-server", dest="http_server_dir", default=None, help="Folder in which to create an http server on localhost. Default is PATHS") -- 2.7.4