libs/multihttp.lua

changeset 0
6e60da4625db
child 3
0521ed5b2598
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libs/multihttp.lua	Sat Jan 02 05:46:52 2010 +0000
@@ -0,0 +1,85 @@
+local http = require "net.http";
+local server = require "net.server";
+local timer = require "util.timer";
+
+local pairs, ipairs, type, setmetatable =
+	pairs, ipairs, type, setmetatable;
+
+module "multihttp"
+__index = _M;
+
+function set_callback(batch, callback)
+	batch.callback = callback;
+end
+
+function set_progress_callback(batch, callback, delay)
+	delay = delay or 1;
+	batch.progress_callback = callback;
+	timer.add_task(delay, function ()
+		if batch.progress_callback then
+			batch.progress_callback(batch);
+			if batch.progress_callback then
+				return delay;
+			end
+		end
+	end);
+end
+
+function add_url(batch, url)
+	batch.urls[url] = true;
+end
+
+function remove_url(batch, url)
+	batch.urls[url] = nil;
+end
+
+function download(batch, block)
+	batch.status = "downloading";
+	local count = 0;
+	for url in pairs(batch.urls) do
+		count = count + 1;
+		batch.urls[url] =
+			http.request(url, nil, function (data, status, request)
+				if batch.callback then
+					batch.downloading_count = batch.downloading_count - 1;
+					batch.callback(url, status, data, request);
+					if batch.downloading_count == 0 and block then
+						server.setquitting(true);
+					end
+				end
+			end);
+	end
+	batch.download_count = count;
+	batch.downloading_count = count;
+	if block then
+		server.loop();
+		server.setquitting(false);
+	end
+	return count;
+end
+
+function progress(batch)
+	local progress = {};
+	for url, request in pairs(batch.urls) do
+		local p = {};
+		p.bytes_downloaded = request.havebodylength;
+		p.bytes_total = request.bodylength;
+		if p.bytes_total then
+			p.percent = 100/(p.bytes_total/p.bytes_downloaded);
+		end
+		progress[url] = p;
+	end
+	return progress;
+end
+
+function new(callback, urls)
+	local batch = { callback = callback, urls = {} };
+	if type(urls) == "table" then
+		for _, url in ipairs(urls) do
+			batch.urls[url] = true;
+		end
+	end
+	return setmetatable(batch, _M);
+end
+
+return _M;

mercurial