2008-02-08 15:38:31 -07:00
|
|
|
open Unixqueue
|
|
|
|
|
2008-02-27 22:50:27 -07:00
|
|
|
exception Buffer_overrun
|
|
|
|
|
2008-02-08 15:38:31 -07:00
|
|
|
type chat_event =
|
|
|
|
| Send of string
|
|
|
|
| Recv of string
|
2008-03-06 13:54:16 -07:00
|
|
|
| Regex of string
|
2008-02-08 15:38:31 -07:00
|
|
|
|
2008-03-06 13:54:16 -07:00
|
|
|
exception Chat_match of (string * chat_event)
|
2008-02-08 18:11:49 -07:00
|
|
|
exception Chat_timeout of chat_event
|
2008-02-08 15:38:31 -07:00
|
|
|
|
2008-03-06 13:54:16 -07:00
|
|
|
let dbg msg a = prerr_endline msg; a
|
|
|
|
|
2008-02-08 15:38:31 -07:00
|
|
|
let string_of_chat_event e =
|
|
|
|
match e with
|
|
|
|
| Send str ->
|
2008-03-06 13:54:16 -07:00
|
|
|
("Send (\"" ^ (String.escaped str) ^ "\")")
|
2008-02-08 15:38:31 -07:00
|
|
|
| Recv str ->
|
2008-03-06 13:54:16 -07:00
|
|
|
("Recv (\"" ^ (String.escaped str) ^ "\")")
|
|
|
|
| Regex str ->
|
|
|
|
("Regex (\"" ^ (String.escaped str) ^ "\")")
|
2008-02-08 15:38:31 -07:00
|
|
|
|
2008-02-08 18:11:49 -07:00
|
|
|
(** Return true if str starts with substr *)
|
2008-02-08 15:38:31 -07:00
|
|
|
let startswith str substr =
|
|
|
|
let l = String.length substr in
|
|
|
|
if l > String.length str then
|
|
|
|
false
|
|
|
|
else
|
|
|
|
String.sub str 0 l = substr
|
|
|
|
|
|
|
|
|
|
|
|
(** Return all but the first index chars in a string *)
|
|
|
|
let string_after str index =
|
|
|
|
let l = String.length str in
|
|
|
|
String.sub str index (l - index)
|
|
|
|
|
|
|
|
|
|
|
|
(** Read a chunk of bytes from fd *)
|
|
|
|
let read_fd fd =
|
|
|
|
let s = 4096 in
|
|
|
|
let buf = String.create s in
|
|
|
|
let len = Unix.read fd buf 0 s in
|
|
|
|
String.sub buf 0 len
|
|
|
|
|
|
|
|
|
2008-02-27 22:50:27 -07:00
|
|
|
class chat_handler chatscript
|
|
|
|
?(input_timeout=0.1)
|
|
|
|
?(output_timeout = 0.1)
|
|
|
|
?(output_max = 4096)
|
|
|
|
?(input_max = 4096)
|
|
|
|
(ues : unix_event_system) fd =
|
2008-02-08 15:38:31 -07:00
|
|
|
object (self)
|
2008-02-27 22:50:27 -07:00
|
|
|
val g = ues#new_group ()
|
|
|
|
val mutable debug = false
|
|
|
|
|
|
|
|
|
|
|
|
val obuf = String.create output_max
|
|
|
|
val mutable obuf_len = 0
|
2008-02-08 18:11:49 -07:00
|
|
|
|
2008-02-08 15:38:31 -07:00
|
|
|
val mutable script = chatscript
|
2008-02-08 18:11:49 -07:00
|
|
|
val inbuf = Buffer.create 4096
|
2008-02-08 15:38:31 -07:00
|
|
|
|
|
|
|
initializer
|
2008-02-27 22:50:27 -07:00
|
|
|
ues#add_handler g self#handle_event;
|
|
|
|
ues#add_resource g (Wait_in fd, input_timeout);
|
2008-02-08 18:11:49 -07:00
|
|
|
self#run_script ();
|
|
|
|
|
2008-02-27 22:50:27 -07:00
|
|
|
method write data =
|
|
|
|
let data_len = String.length data in
|
|
|
|
if (data_len + obuf_len > output_max) then
|
2008-03-06 13:54:16 -07:00
|
|
|
raise Buffer_overrun;
|
2008-02-27 22:50:27 -07:00
|
|
|
String.blit data 0 obuf obuf_len data_len;
|
|
|
|
obuf_len <- obuf_len + data_len;
|
|
|
|
ues#add_resource g (Wait_out fd, output_timeout)
|
|
|
|
|
|
|
|
method handle_event ues esys e =
|
|
|
|
match e with
|
|
|
|
| Input_arrived (g, fd) ->
|
2008-03-06 13:54:16 -07:00
|
|
|
let data = String.create input_max in
|
|
|
|
let len = Unix.read fd data 0 input_max in
|
|
|
|
if (len > 0) then
|
|
|
|
begin
|
|
|
|
Buffer.add_string inbuf (String.sub data 0 len);
|
|
|
|
self#run_script ()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Unix.close fd;
|
|
|
|
ues#clear g;
|
|
|
|
end
|
2008-02-27 22:50:27 -07:00
|
|
|
| Output_readiness (g, fd) ->
|
2008-03-06 13:54:16 -07:00
|
|
|
let size = obuf_len in
|
|
|
|
let n = Unix.single_write fd obuf 0 size in
|
|
|
|
obuf_len <- obuf_len - n;
|
|
|
|
if (obuf_len = 0) then
|
|
|
|
(* Don't check for output readiness anymore *)
|
|
|
|
begin
|
|
|
|
ues#remove_resource g (Wait_out fd)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
(* Put unwritten output back into the output queue *)
|
|
|
|
begin
|
|
|
|
String.blit obuf n obuf 0 (obuf_len)
|
|
|
|
end
|
2008-02-27 22:50:27 -07:00
|
|
|
| Out_of_band (g, fd) ->
|
2008-03-06 13:54:16 -07:00
|
|
|
raise (Failure "Out of band data")
|
2008-02-27 22:50:27 -07:00
|
|
|
| Timeout (g, op) ->
|
2008-03-06 13:54:16 -07:00
|
|
|
raise (Chat_timeout (List.hd script))
|
2008-02-27 22:50:27 -07:00
|
|
|
| Signal ->
|
2008-03-06 13:54:16 -07:00
|
|
|
raise (Failure "Signal")
|
2008-02-27 22:50:27 -07:00
|
|
|
| Extra exn ->
|
2008-03-06 13:54:16 -07:00
|
|
|
raise (Failure "Extra")
|
2008-02-08 15:38:31 -07:00
|
|
|
|
2008-02-08 18:11:49 -07:00
|
|
|
method run_script () =
|
2008-02-08 15:38:31 -07:00
|
|
|
match script with
|
|
|
|
| [] ->
|
2008-03-06 13:54:16 -07:00
|
|
|
Unix.close fd;
|
|
|
|
ues#clear g
|
2008-02-08 18:11:49 -07:00
|
|
|
| Send buf :: tl ->
|
2008-03-06 13:54:16 -07:00
|
|
|
self#write buf;
|
|
|
|
script <- tl;
|
|
|
|
self#run_script ()
|
2008-02-08 18:11:49 -07:00
|
|
|
| Recv buf :: tl ->
|
2008-03-06 13:54:16 -07:00
|
|
|
let buf_len = String.length buf in
|
|
|
|
let inbuf_str = Buffer.contents inbuf in
|
|
|
|
if (Buffer.length inbuf >= buf_len) then
|
|
|
|
if startswith inbuf_str buf then
|
|
|
|
begin
|
|
|
|
script <- tl;
|
|
|
|
Buffer.clear inbuf;
|
|
|
|
Buffer.add_substring
|
|
|
|
inbuf
|
|
|
|
inbuf_str
|
|
|
|
buf_len
|
|
|
|
((String.length inbuf_str) - buf_len);
|
|
|
|
self#run_script ()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise (Chat_match (inbuf_str, Recv buf))
|
|
|
|
else
|
|
|
|
()
|
|
|
|
| Regex buf :: tl ->
|
|
|
|
let inbuf_str = Buffer.contents inbuf in
|
|
|
|
let matched = Str.string_match (Str.regexp buf) inbuf_str 0 in
|
|
|
|
if (Buffer.length inbuf > 0) then
|
|
|
|
if matched then
|
|
|
|
let match_len = Str.match_end () in
|
|
|
|
script <- tl;
|
|
|
|
Buffer.clear inbuf;
|
|
|
|
Buffer.add_substring
|
|
|
|
inbuf
|
|
|
|
inbuf_str
|
|
|
|
match_len
|
|
|
|
((String.length inbuf_str) - match_len);
|
|
|
|
self#run_script ()
|
|
|
|
else
|
|
|
|
raise (Chat_match (inbuf_str, Regex buf))
|
|
|
|
else
|
|
|
|
()
|
2008-02-08 15:38:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-03-07 13:52:13 -07:00
|
|
|
let chat_create ues script proc =
|
|
|
|
let a,b = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
|
|
|
|
ignore (proc ues a);
|
|
|
|
ignore (new chat_handler script ues b)
|
|
|
|
|
2008-02-08 15:38:31 -07:00
|
|
|
(** Run a chat script
|
|
|
|
|
|
|
|
[chat script proc] will create a Unix domain socket pair, call [proc
|
|
|
|
ues fd] with the event system and one of the sockets, and then run
|
|
|
|
[script] through it.
|
|
|
|
*)
|
|
|
|
|
2008-03-07 13:52:13 -07:00
|
|
|
let chat_run ues =
|
|
|
|
try
|
|
|
|
Unixqueue.run ues;
|
|
|
|
with
|
|
|
|
| Chat_match (got, expected) ->
|
|
|
|
raise (Failure ("Not matched: got \"" ^
|
|
|
|
(String.escaped got) ^
|
|
|
|
"\"\n expected " ^
|
|
|
|
(string_of_chat_event expected)))
|
|
|
|
| Chat_timeout evt ->
|
|
|
|
raise (Failure ("Timeout waiting for " ^
|
|
|
|
(string_of_chat_event evt)))
|
2008-02-08 15:38:31 -07:00
|
|
|
|