libs/multihttp.lua

Sat, 02 Jan 2010 05:46:52 +0000

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

Initial commit

0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local http = require "net.http";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local server = require "net.server";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local timer = require "util.timer";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local pairs, ipairs, type, setmetatable =
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 pairs, ipairs, type, setmetatable;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 module "multihttp"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 __index = _M;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 function set_callback(batch, callback)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 batch.callback = callback;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 function set_progress_callback(batch, callback, delay)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 delay = delay or 1;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 batch.progress_callback = callback;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 timer.add_task(delay, function ()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 if batch.progress_callback then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 batch.progress_callback(batch);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if batch.progress_callback then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 return delay;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 function add_url(batch, url)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 batch.urls[url] = true;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 function remove_url(batch, url)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 batch.urls[url] = nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 function download(batch, block)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 batch.status = "downloading";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local count = 0;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 for url in pairs(batch.urls) do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 count = count + 1;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 batch.urls[url] =
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 http.request(url, nil, function (data, status, request)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 if batch.callback then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 batch.downloading_count = batch.downloading_count - 1;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 batch.callback(url, status, data, request);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if batch.downloading_count == 0 and block then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 server.setquitting(true);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 batch.download_count = count;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 batch.downloading_count = count;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 if block then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 server.loop();
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 server.setquitting(false);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 return count;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 function progress(batch)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local progress = {};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 for url, request in pairs(batch.urls) do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local p = {};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 p.bytes_downloaded = request.havebodylength;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 p.bytes_total = request.bodylength;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if p.bytes_total then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 p.percent = 100/(p.bytes_total/p.bytes_downloaded);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 progress[url] = p;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return progress;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 function new(callback, urls)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local batch = { callback = callback, urls = {} };
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 if type(urls) == "table" then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 for _, url in ipairs(urls) do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 batch.urls[url] = true;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 return setmetatable(batch, _M);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 return _M;

mercurial