irc-bot/dispatch.ml

141 lines
3.7 KiB
OCaml
Raw Normal View History

2009-02-08 22:26:27 -07:00
type event = Input | Output | Exception
2008-03-15 19:46:06 -06:00
type timer_handler = float -> unit
2009-02-08 22:26:27 -07:00
type fd_handler = Unix.file_descr -> event -> unit
2008-03-14 12:16:04 -06:00
2008-03-15 19:46:06 -06:00
module Timer =
2008-03-14 12:16:04 -06:00
Set.Make (struct
2008-03-15 19:46:06 -06:00
type t = (float * timer_handler)
let compare (time, handler) (time', handler') = compare time time'
2008-03-15 19:46:06 -06:00
end)
2008-03-14 12:16:04 -06:00
2008-03-15 19:46:06 -06:00
module Fd_map =
Map.Make (struct
type t = Unix.file_descr
let compare = compare
end)
2008-03-14 12:16:04 -06:00
type t = {
2009-02-08 22:26:27 -07:00
read_fds : Unix.file_descr list ref;
write_fds : Unix.file_descr list ref;
except_fds : Unix.file_descr list ref;
handlers : fd_handler Fd_map.t ref;
2008-03-15 19:46:06 -06:00
timers : Timer.t ref;
2008-03-14 12:16:04 -06:00
}
(* select(), poll(), and epoll() treat timeout as an upper bound of time
to wait. This fudge factor helps ensure that given no FD activity,
this isn't run in a tight loop as a timer approaches. This value was
determined experimentally on a 1.25GHz G4 PPC to work most of the
time. Your mileage may vary. *)
let timeout_fudge = 0.001
2008-03-14 12:16:04 -06:00
let create size =
2009-02-08 22:26:27 -07:00
{read_fds = ref [];
write_fds = ref [];
except_fds = ref [];
handlers = ref Fd_map.empty;
2008-03-15 19:46:06 -06:00
timers = ref Timer.empty}
2008-03-14 12:16:04 -06:00
let destroy d =
2008-03-15 19:46:06 -06:00
(* Explicitly unreference fds and timers, in case d sticks around *)
2009-02-08 22:26:27 -07:00
d.handlers := Fd_map.empty;
2008-03-15 19:46:06 -06:00
d.timers := Timer.empty
2008-03-14 12:16:04 -06:00
2009-02-08 22:26:27 -07:00
let get_fds d event =
match event with
| Input -> d.read_fds
| Output -> d.write_fds
| Exception -> d.except_fds
2008-03-14 12:16:04 -06:00
let modify d fd events =
2009-02-08 22:26:27 -07:00
let add_event event =
let l = get_fds d event in
let nl = (List.filter ((<>) fd) !l) in
if List.mem event events then
l := fd :: nl
else
l := nl
in
if Fd_map.mem fd !(d.handlers) then
List.iter add_event [Input; Output; Exception]
else
raise Not_found
2008-03-14 12:16:04 -06:00
let set_handler d fd handler =
2009-02-08 22:26:27 -07:00
d.handlers := Fd_map.add fd handler !(d.handlers)
let add d fd handler events =
set_handler d fd handler;
modify d fd events
2008-03-14 12:16:04 -06:00
let delete d fd =
2009-02-08 22:26:27 -07:00
let del_event event =
let l = get_fds d event in
l := (List.filter ((<>) fd) !l)
in
d.handlers := Fd_map.remove fd !(d.handlers);
List.iter del_event [Input; Output; Exception]
2008-03-14 12:16:04 -06:00
let add_timer d handler time =
2008-03-15 19:46:06 -06:00
d.timers := Timer.add (time, handler) !(d.timers)
2008-03-14 12:16:04 -06:00
2008-03-15 19:46:06 -06:00
let delete_timer d time =
2008-03-14 12:16:04 -06:00
let may_remain (time', _) =
time' <> time
in
2008-03-15 19:46:06 -06:00
d.timers := Timer.filter may_remain !(d.timers)
2008-03-14 12:16:04 -06:00
2008-03-15 19:46:06 -06:00
let rec dispatch_timers d now =
if (!(d.timers) != Timer.empty) then
let (time, handler) = Timer.min_elt !(d.timers) in
if now < time then
2008-03-15 19:46:06 -06:00
()
else begin
handler time;
d.timers := Timer.remove (time, handler) !(d.timers);
dispatch_timers d now
end
2008-03-14 12:16:04 -06:00
2009-02-08 22:26:27 -07:00
let rec dispatch_results d (read_ready, write_ready, except_ready) =
let rec dispatch event fd_list =
match fd_list with
| [] ->
()
| fd :: tl ->
let handler = Fd_map.find fd !(d.handlers) in
handler fd event;
dispatch event tl
in
dispatch Input read_ready;
dispatch Output write_ready;
dispatch Exception except_ready
2008-03-14 12:16:04 -06:00
let once d =
let now = Unix.gettimeofday () in
2008-03-14 12:16:04 -06:00
let timeout =
try
2008-03-15 19:46:06 -06:00
let (time, _) = Timer.min_elt !(d.timers) in
let delta = (time -. now +. timeout_fudge) in
max delta 0.0
2008-03-14 12:16:04 -06:00
with Not_found ->
(-1.0)
2008-03-14 12:16:04 -06:00
in
2009-02-08 22:26:27 -07:00
(* select () waits *at most* timeout ms. If you have fds but they're
not
doing anything, multiple calls to once may be required. This is
lame. *)
let result = Unix.select !(d.read_fds) !(d.write_fds) !(d.except_fds) timeout in
dispatch_results d result;
dispatch_timers d (Unix.gettimeofday ())
2008-03-15 19:46:06 -06:00
2008-03-14 12:16:04 -06:00
let rec run d =
2009-02-08 22:26:27 -07:00
if ((!(d.handlers) == Fd_map.empty) &&
2008-03-15 19:46:06 -06:00
(!(d.timers) == Timer.empty)) then
2008-03-14 12:16:04 -06:00
()
else begin
once d;
run d
end