libs/multihttp.lua

Sat, 02 Jan 2010 06:05:26 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 02 Jan 2010 06:05:26 +0000
changeset 3
0521ed5b2598
parent 0
6e60da4625db
child 4
a20981a6d88b
permissions
-rw-r--r--

Set batch.status = "complete" when all downloads are finished

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 then
						if block then
							server.setquitting(true);
						end
						batch.status = "complete";
					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