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; GError *err;
gchar *debug; gchar *debug;
if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) { switch (GST_MESSAGE_TYPE (message)) {
gst_message_parse_error (message, &err, &debug); case GST_MESSAGE_ERROR:
GST_VALIDATE_REPORT (monitor, ERROR_ON_BUS, gst_message_parse_error (message, &err, &debug);
"Got error: %s -- Debug message: %s", err->message, debug); GST_VALIDATE_REPORT (monitor, ERROR_ON_BUS,
g_error_free (err); "Got error: %s -- Debug message: %s", err->message, debug);
g_free (debug); g_error_free (err);
} else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING) { g_free (debug);
gst_message_parse_warning (message, &err, &debug); break;
GST_VALIDATE_REPORT (monitor, WARNING_ON_BUS, case GST_MESSAGE_WARNING:
"Got warning: %s -- Debug message: %s", err->message, debug); gst_message_parse_warning (message, &err, &debug);
g_error_free (err); GST_VALIDATE_REPORT (monitor, WARNING_ON_BUS,
g_free (debug); "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; gulong element_added_id;
guint print_pos_srcid; guint print_pos_srcid;
gboolean stateless; gboolean stateless;
gboolean buffering;
}; };
/** /**

View file

@ -32,18 +32,22 @@ __version__ = "0.1"
__all__ = ["RangeHTTPRequestHandler"] __all__ = ["RangeHTTPRequestHandler"]
import os import os
import sys
import posixpath import posixpath
import BaseHTTPServer import BaseHTTPServer
import urllib import urllib
import cgi import cgi
import shutil import shutil
import mimetypes import mimetypes
import time
try: try:
from cStringIO import StringIO from cStringIO import StringIO
except ImportError: except ImportError:
from StringIO import StringIO from StringIO import StringIO
_bandwidth = 0
class RangeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): class RangeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""Simple HTTP request handler with GET and HEAD commands. """Simple HTTP request handler with GET and HEAD commands.
@ -70,6 +74,11 @@ class RangeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
while chunk > 0: while chunk > 0:
if start_range + chunk > end_range: if start_range + chunk > end_range:
chunk = end_range - start_range chunk = end_range - start_range
if _bandwidth != 0:
time_to_sleep = float(float(chunk) / float(_bandwidth))
time.sleep(time_to_sleep)
try: try:
self.wfile.write(f.read(chunk)) self.wfile.write(f.read(chunk))
except: except:
@ -146,7 +155,6 @@ class RangeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.send_header("Content-Length", end_range - start_range) self.send_header("Content-Length", end_range - start_range)
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers() self.end_headers()
print "Sending Bytes ",start_range, " to ", end_range, "...\n"
return (f, start_range, end_range) return (f, start_range, end_range)
def list_directory(self, path): def list_directory(self, path):
@ -271,4 +279,6 @@ def test(HandlerClass = RangeHTTPRequestHandler,
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) > 2:
_bandwidth = int(sys.argv[2])
test() test()

View file

@ -63,20 +63,22 @@ class HTTPServer(loggable.Loggable):
print "Starting Server" print "Starting Server"
try: try:
self.debug("Lunching twistd server") self.debug("Lunching http server")
cmd = "%s %s %d" % (sys.executable, os.path.join(os.path.dirname(__file__), cmd = "%s %s %d %s" % (sys.executable, os.path.join(os.path.dirname(__file__),
"RangeHTTPServer.py"), "RangeHTTPServer.py"),
self.options.http_server_port) self.options.http_server_port,
self.options.http_bandwith,
)
curdir = os.path.abspath(os.curdir) curdir = os.path.abspath(os.curdir)
os.chdir(self.options.http_server_dir) os.chdir(self.options.http_server_dir)
#cmd = "twistd -no web --path=%s -p %d" % ( #cmd = "twistd -no web --path=%s -p %d" % (
# self.options.http_server_dir, self.options.http_server_port) # 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(" "), self._process = subprocess.Popen(cmd.split(" "),
stderr=self._logsfile, stderr=self._logsfile,
stdout=self._logsfile) stdout=self._logsfile)
os.chdir(curdir) os.chdir(curdir)
self.debug("Lunched twistd server") self.debug("Lunched http server")
# Dirty way to avoid eating to much CPU... # Dirty way to avoid eating to much CPU...
# good enough for us anyway. # good enough for us anyway.
time.sleep(1) 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", http_server_group.add_argument("--http-server-port", dest="http_server_port",
default=8079, default=8079,
help="Port on which to run the http server on localhost") 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", http_server_group.add_argument("-s", "--folder-for-http-server", dest="http_server_dir",
default=None, default=None,
help="Folder in which to create an http server on localhost. Default is PATHS") help="Folder in which to create an http server on localhost. Default is PATHS")