1+ --
2+ -- implementation of uvwget example from
3+ -- http://nikhilm.github.io/uvbook/index.html
4+ --
5+
16local curl = require " cURL"
27local uv = require " lluv"
38local ut = require " lluv.utils"
49
510local fprintf = function (f , ...) f :write ((string.format (... ))) end
6- local printf = function (...) fprintf (io.stdout , ... ) end
711
812local stderr = io.stderr
913
10- local trace = false
14+ local trace = false do
1115
1216trace = trace and print or function () end
1317
18+ end
19+
1420local ACTION_NAMES = {
1521 [curl .POLL_IN ] = " POLL_IN" ;
1622 [curl .POLL_INOUT ] = " POLL_INOUT" ;
@@ -19,9 +25,15 @@ local ACTION_NAMES = {
1925 [curl .POLL_REMOVE ] = " POLL_REMOVE" ;
2026}
2127
28+ local POLL_IO_FLAGS = {
29+ [ curl .POLL_IN ] = uv .READABLE ;
30+ [ curl .POLL_OUT ] = uv .WRITABLE ;
31+ [ curl .POLL_INOUT ] = uv .READABLE + uv .WRITABLE ;
32+ }
33+
2234local EVENT_NAMES = {
23- [ uv .READABLE ] = " READABLE" ;
24- [ uv .WRITABLE ] = " WRITABLE" ;
35+ [ uv .READABLE ] = " READABLE" ;
36+ [ uv .WRITABLE ] = " WRITABLE" ;
2537 [ uv .READABLE + uv .WRITABLE ] = " READABLE + WRITABLE" ;
2638}
2739
@@ -31,12 +43,6 @@ local FLAGS = {
3143 [ uv .READABLE + uv .WRITABLE ] = curl .CSELECT_IN + curl .CSELECT_OUT ;
3244}
3345
34- local POLL_IO_FLAGS = {
35- [ curl .POLL_IN ] = uv .READABLE ;
36- [ curl .POLL_OUT ] = uv .WRITABLE ;
37- [ curl .POLL_INOUT ] = uv .READABLE + uv .WRITABLE ;
38- }
39-
4046local Context = ut .class () do
4147
4248function Context :__init (fd )
6672
6773end
6874
69- -- Number of parallel request
70- local MAX_REQUESTS
75+ local MAX_REQUESTS = 64 -- Number of parallel request
7176local timer , multi
7277local qtask = ut .Queue .new () -- wait tasks
7378local qfree = ut .Queue .new () -- avaliable easy handles
74- local qeasy = {} -- all easy handles
79+ local qeasy = {} -- all easy handles
7580
7681local function on_begin (handle , url , num )
7782 local filename = tostring (num ) .. " .download"
@@ -92,9 +97,9 @@ local function on_end(handle, err, url)
9297 handle .data .file = nil
9398
9499 if err then
95- printf ( " %s ERROR - %s\n " , url , tostring (err ));
100+ fprintf ( stderr , " %s ERROR - %s\n " , url , tostring (err ));
96101 else
97- printf ( " %s DONE\n " , url );
102+ fprintf ( stderr , " %s DONE\n " , url );
98103 end
99104end
100105
@@ -103,6 +108,13 @@ local function cleanup()
103108
104109 for i , easy in ipairs (qeasy ) do
105110 multi :remove_handle (easy )
111+ if easy .data then
112+ local context = easy .data .context
113+ if context then context :close () end
114+
115+ local file = easy .data .file
116+ if file then on_end (easy , ' closed' , easy :getinfo_effective_url ()) end
117+ end
106118 easy :close ()
107119 end
108120
@@ -223,12 +235,12 @@ local curl_check_multi_info = function()
223235 proceed_queue ()
224236end
225237
226- on_libuv_poll = function (handle , err , events )
227- trace (" UV::POLL" , handle , err , EVENT_NAMES [events ] or events )
238+ on_libuv_poll = function (poller , err , events )
239+ trace (" UV::POLL" , poller , err , EVENT_NAMES [events ] or events )
228240
229241 local flags = assert (FLAGS [events ], (" unknown event:" .. events ))
230242
231- context = handle .data .context
243+ local context = poller .data .context
232244
233245 multi :socket_action (context :fileno (), flags )
234246
@@ -238,15 +250,13 @@ end
238250on_libuv_timeout = function (timer )
239251 trace (" UV::TIMEOUT" , timer )
240252
241- local running_handles , err = multi :socket_action ()
253+ multi :socket_action ()
242254
243255 curl_check_multi_info ()
244256end
245257
246258end
247259
248- MAX_REQUESTS = 64
249-
250260timer = uv .timer ()
251261
252262multi = curl .multi {
0 commit comments