| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
let debug = false |
| 12 |
|
| 13 |
type priority = Low | High |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
let default_async_proofs_worker_priority = Low |
| 18 |
|
| 19 |
let string_of_priority = function Low -> "low" | High -> "high" |
| 20 |
let priority_of_string = function |
| 21 |
| "low" -> Low |
| 22 |
| "high" -> High |
| 23 |
| _ -> raise (Invalid_argument "priority_of_string") |
| 24 |
|
| 25 |
type request = |
| 26 |
| Hello of priority |
| 27 |
| Get of int |
| 28 |
| TryGet of int |
| 29 |
| GiveBack of int |
| 30 |
| Ping |
| 31 |
|
| 32 |
type response = |
| 33 |
| Tokens of int |
| 34 |
| Noluck |
| 35 |
| Pong of int * int * int |
| 36 |
|
| 37 |
exception ParseError |
| 38 |
|
| 39 |
|
| 40 |
let strip_r s = |
| 41 |
let len = String.length s in |
| 42 |
if s.[len - 1] <> '\r' then s else String.sub s 0 (len - 1) |
| 43 |
|
| 44 |
let positive_int_of_string n = |
| 45 |
try |
| 46 |
let n = int_of_string n in |
| 47 |
if n <= 0 then raise ParseError else n |
| 48 |
with Invalid_argument _ | Failure _ -> raise ParseError |
| 49 |
|
| 50 |
let parse_request s = |
| 51 |
if debug then Printf.eprintf "parsing '%s'\n" s; |
| 52 |
match Str.split (Str.regexp " ") (strip_r s) with |
| 53 |
| [ "HELLO"; "LOW" ] -> Hello Low |
| 54 |
| [ "HELLO"; "HIGH" ] -> Hello High |
| 55 |
| [ "GET"; n ] -> Get (positive_int_of_string n) |
| 56 |
| [ "TRYGET"; n ] -> TryGet (positive_int_of_string n) |
| 57 |
| [ "GIVEBACK"; n ] -> GiveBack (positive_int_of_string n) |
| 58 |
| [ "PING" ] -> Ping |
| 59 |
| _ -> raise ParseError |
| 60 |
|
| 61 |
let parse_response s = |
| 62 |
if debug then Printf.eprintf "parsing '%s'\n" s; |
| 63 |
match Str.split (Str.regexp " ") (strip_r s) with |
| 64 |
| [ "TOKENS"; n ] -> Tokens (positive_int_of_string n) |
| 65 |
| [ "NOLUCK" ] -> Noluck |
| 66 |
| [ "PONG"; n; m; p ] -> |
| 67 |
let n = try int_of_string n with Failure _ -> raise ParseError in |
| 68 |
let m = try int_of_string m with Failure _ -> raise ParseError in |
| 69 |
let p = try int_of_string p with Failure _ -> raise ParseError in |
| 70 |
Pong (n,m,p) |
| 71 |
| _ -> raise ParseError |
| 72 |
|
| 73 |
let print_request = function |
| 74 |
| Hello Low -> "HELLO LOW\n" |
| 75 |
| Hello High -> "HELLO HIGH\n" |
| 76 |
| Get n -> Printf.sprintf "GET %d\n" n |
| 77 |
| TryGet n -> Printf.sprintf "TRYGET %d\n" n |
| 78 |
| GiveBack n -> Printf.sprintf "GIVEBACK %d\n" n |
| 79 |
| Ping -> "PING\n" |
| 80 |
|
| 81 |
let print_response = function |
| 82 |
| Tokens n -> Printf.sprintf "TOKENS %d\n" n |
| 83 |
| Noluck -> "NOLUCK\n" |
| 84 |
| Pong (n,m,p) -> Printf.sprintf "PONG %d %d %d\n" n m p |
| 85 |
|
| 86 |
let connect s = |
| 87 |
try |
| 88 |
match Str.split (Str.regexp ":") s with |
| 89 |
| [ h; p ] -> |
| 90 |
let open Unix in |
| 91 |
let s = socket PF_INET SOCK_STREAM 0 in |
| 92 |
connect s (ADDR_INET (inet_addr_of_string h,int_of_string p)); |
| 93 |
Some s |
| 94 |
| _ -> None |
| 95 |
with Unix.Unix_error _ -> None |
| 96 |
|
| 97 |
let manager = ref None |
| 98 |
|
| 99 |
let option_map f = function None -> None | Some x -> Some (f x) |
| 100 |
|
| 101 |
let init p = |
| 102 |
try |
| 103 |
let sock = try Sys.getenv "ROCQWORKMGR_SOCK" with Not_found -> Sys.getenv "COQWORKMGR_SOCK" in |
| 104 |
manager := option_map (fun s -> |
| 105 |
let cout = Unix.out_channel_of_descr s in |
| 106 |
set_binary_mode_out cout true; |
| 107 |
let cin = Unix.in_channel_of_descr s in |
| 108 |
set_binary_mode_in cin true; |
| 109 |
output_string cout (print_request (Hello p)); flush cout; |
| 110 |
cin, cout) (connect sock) |
| 111 |
with Not_found | End_of_file -> () |
| 112 |
|
| 113 |
let with_manager f g = |
| 114 |
try |
| 115 |
match !manager with |
| 116 |
| None -> f () |
| 117 |
| Some (cin, cout) -> g cin cout |
| 118 |
with |
| 119 |
| ParseError | End_of_file -> manager := None; f () |
| 120 |
|
| 121 |
let get n = |
| 122 |
with_manager |
| 123 |
(fun () -> n) |
| 124 |
(fun cin cout -> |
| 125 |
output_string cout (print_request (Get n)); |
| 126 |
flush cout; |
| 127 |
let l = input_line cin in |
| 128 |
match parse_response l with |
| 129 |
| Tokens m -> m |
| 130 |
| _ -> raise (Failure "coqworkmgr protocol error")) |
| 131 |
|
| 132 |
let tryget n = |
| 133 |
with_manager |
| 134 |
(fun () -> Some n) |
| 135 |
(fun cin cout -> |
| 136 |
output_string cout (print_request (TryGet n)); |
| 137 |
flush cout; |
| 138 |
let l = input_line cin in |
| 139 |
match parse_response l with |
| 140 |
| Tokens m -> Some m |
| 141 |
| Noluck -> None |
| 142 |
| _ -> raise (Failure "coqworkmgr protocol error")) |
| 143 |
|
| 144 |
let giveback n = |
| 145 |
with_manager |
| 146 |
(fun () -> ()) |
| 147 |
(fun cin cout -> |
| 148 |
output_string cout (print_request (GiveBack n)); |
| 149 |
flush cout) |