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.
This commit is contained in:
Thibault Saunier 2014-08-08 12:33:54 +02:00
parent 702992cf7a
commit 2da2c6cc56
5 changed files with 66 additions and 18 deletions

View file

@ -186,18 +186,50 @@ _bus_handler (GstBus * bus, GstMessage * message,
GError *err;
gchar *debug;
if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
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);
} else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING) {
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;
}
}

View file

@ -62,6 +62,7 @@ struct _GstValidateBinMonitor {
gulong element_added_id;
guint print_pos_srcid;
gboolean stateless;
gboolean buffering;
};
/**

View file

@ -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()

View file

@ -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)

View file

@ -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")