vsys. first checkin
[vsys.git] / fdwatcher.ml
1 open Printf
2 open Globals
3
4 let fdset = ref []
5 let cbtable = Hashtbl.create 1024
6
7 (* The in descriptor is always open. Thanks to the broken semantics of
8  * fifo outputs, the out descriptor must be opened a nouveau whenever we
9  * want to send out data, and so we keep the associated filename as well.
10  * Same with input fifos. Yipee.*)
11
12 let add_fd (evpair:fd_and_fname) (fd_other:fd_and_fname) (callback:fd_and_fname->fd_and_fname->unit) = 
13   let (fname,fd) = evpair in
14   fdset := (fd::!fdset);
15   Hashtbl.replace cbtable fd (callback,(evpair,fd_other))
16
17 let del_fd fd =
18   fdset:=List.filter (fun l->l<>fd) !fdset;
19   flush Pervasives.stdout
20
21 let start_watch () =
22     while (true)
23     do
24               let (fds,_,_) = try Unix.select !fdset [] [] (-1.) 
25               with e->
26                 ([],[],[])
27               in
28                 List.iter (fun elt->
29                              let (func,(evd,fd_other)) = Hashtbl.find cbtable elt in
30                                func evd fd_other) fds
31     done
32