4 * Copyright (c) 2009, ygrek, <ygrek@autistici.org>
10 let pr fmt
= Printf.ksprintf print_endline fmt
14 match M.remove_finished mt
with
15 | None
-> (*if n > 0 then pr "Removed %u handles" n*) ()
16 | Some _
-> loop (n
+1)
21 while M.perform mt
> 0 do
26 let events_base = Ev.init
()
30 let events = Hashtbl.create
32 in
31 let on_event fd flags
=
32 let event = match flags
with
34 | Ev.WRITE
-> M.EV_OUT
37 let _ = M.action mt fd
event in
41 M.set_socket_function mt
begin fun fd what
->
42 List.iter
(fun ev
-> decr
evs; Ev.del ev
) (Hashtbl.find_all
events fd
); Hashtbl.remove
events fd
;
43 let flags = match what
with
44 | M.POLL_REMOVE
| M.POLL_NONE
-> []
45 | M.POLL_IN
-> [Ev.READ
]
46 | M.POLL_OUT
-> [Ev.WRITE
]
47 | M.POLL_INOUT
-> [Ev.READ
;Ev.WRITE
]
52 let ev = Ev.create
() in
53 Ev.set
events_base ev fd
flags ~persist
:true on_event;
56 Hashtbl.add
events fd
ev
58 let _ = M.action_all mt
in
59 Ev.dispatch
events_base;
66 let out_fd = ref [] in
67 let on_event fd
event =
68 let _ = M.action mt fd
event in
71 M.set_socket_function mt
begin fun fd what
->
72 in_fd := List.filter
((<>) fd
) !in_fd;
73 out_fd := List.filter
((<>) fd
) !out_fd;
75 | M.POLL_REMOVE
| M.POLL_NONE
-> finished mt
76 | M.POLL_IN
-> in_fd := fd
:: !in_fd
77 | M.POLL_OUT
-> out_fd := fd
:: !out_fd
78 | M.POLL_INOUT
-> in_fd := fd
:: !in_fd; out_fd := fd
:: !out_fd
80 let _ = M.action_all mt
in
81 while !in_fd <> [] || !out_fd <> [] do
82 let (fdi
,fdo
,_) = Unix.select
!in_fd !out_fd [] (-1.) in
83 List.iter
(fun fd
-> on_event fd
M.EV_IN
) fdi
;
84 List.iter
(fun fd
-> on_event fd
M.EV_OUT
) fdo
;
88 let input_lines file
=
89 let ch = open_in file
in
91 try while true do lines := input_line
ch :: !lines done; []
92 with End_of_file
-> close_in_noerr
ch; List.rev
!lines
95 let module A
= Array
in
96 let func = ref None
in
101 ["-n",Arg.Set_int
n,"<N> ";
102 "-i",Arg.String
(fun s
-> urls := input_lines s
@ !urls),"<file> read urls from file";
103 "-l",Arg.String
(fun s
-> urls := s
:: !urls),"<url> fetch url";
104 "-m",Arg.String
(function
105 | "wait" -> func := Some
loop_wait
106 | "event" -> func := Some
loop_async
107 | "select" -> func := Some
loop_select
108 | s
-> failwith
(Printf.sprintf
"unknown method : %s" s
)), "<wait|event|select> loop method";
111 Arg.parse
args failwith
"Options :";
113 | [url
] -> A.make
!n url
116 (* if A.length urls = 0 then failwith "Specify urls to download"; *)
118 let h = Curl.init () in
120 Curl.set_writefunction
h String.length
;
124 Printf.printf
"Time: %f Size: %Lu Speed: %.2f KB/s URL: %s\n"
125 (Curl.get_totaltime
h)
126 (Int64.of_float
(Curl.get_sizedownload
h))
127 (Curl.get_speeddownload
h /. 1024.)
128 (Curl.get_effectiveurl
h);
132 let hs = A.map
init urls in
133 let mt = M.create
() in
134 A.iter
(M.add
mt) hs;
141 | None
-> test loop_wait; test loop_select; test loop_async