3 * Stefano Zacchiroli <zack@cs.unibo.it>
4 * for the HELM Team http://helm.cs.unibo.it/
6 * This file is part of HELM, an Hypertextual, Electronic
7 * Library of Mathematics, developed at the Computer Science
8 * Department, University of Bologna, Italy.
10 * HELM is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * HELM is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with HELM; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * For details, see the HELM World-Wide-Web page,
26 * http://helm.cs.unibo.it/
32 let broker_url = "localhost:49081/act";;
34 let init_tutor = Hbugs_id_generator.new_tutor_id;;
36 (** register a tutor to broker *)
37 let register_to_broker id url hint_type dsc =
40 Hbugs_messages.submit_req
41 ~url:broker_url (Register_tutor (id, url, hint_type, dsc))
44 | Tutor_registered id ->
45 prerr_endline (sprintf "Tutor registered, broker id: %s" id);
48 raise (Hbugs_messages.Unexpected_message unexpected_msg))
50 failwith (sprintf "Can't register tutor to broker: uncaught exception: %s"
51 (Printexc.to_string e))
53 (** unregister a tutor from the broker *)
54 let unregister_from_broker id =
55 let res = Hbugs_messages.submit_req ~url:broker_url (Unregister_tutor id) in
57 | Tutor_unregistered _ -> prerr_endline "Tutor unregistered!"
60 (sprintf "Can't unregister from broker, received unexpected msg: %s"
61 (Hbugs_messages.string_of_msg unexpected_msg))
64 (* typecheck a loaded proof *)
65 (* TODO this is a cut and paste from gTopLevel.ml *)
66 let typecheck_loaded_proof metasenv bo ty =
67 let module T = CicTypeChecker in
70 (fun metasenv ((_,context,ty) as conj) ->
71 ignore (T.type_of_aux' metasenv context ty) ;
74 ignore (T.type_of_aux' metasenv [] ty) ;
75 ignore (T.type_of_aux' metasenv [] bo)
78 type xml_kind = Body | Type;;
79 let mk_dtdname ~ask_dtd_to_the_getter dtd =
80 if ask_dtd_to_the_getter then
81 Configuration.getter_url ^ "getdtd?uri=" ^ dtd
83 "http://mowgli.cs.unibo.it/dtd/" ^ dtd
85 (** this function must be the inverse function of GTopLevel.strip_xml_headings
87 let add_xml_headings ~kind s =
88 let dtdname = mk_dtdname ~ask_dtd_to_the_getter:true "cic.dtd" in
91 | Body -> "CurrentProof"
92 | Type -> "ConstantType"
94 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n" ^
95 "<!DOCTYPE " ^ root ^ " SYSTEM \""^ dtdname ^ "\">\n\n" ^
99 let load_state (type_string, body_string, goal) =
101 let ((tmp1, oc1), (tmp2, oc2)) =
102 (Filename.open_temp_file "" "", Filename.open_temp_file "" "")
105 output_string oc1 (add_xml_headings ~kind:Type type_string);
106 output_string oc2 (add_xml_headings ~kind:Body body_string);
107 close_out oc1; close_out oc2;
108 prerr_endline (sprintf "Proof Type available in %s" tmp1);
109 prerr_endline (sprintf "Proof Body available in %s" tmp2);
112 (match CicParser.obj_of_xml tmp1 (Some tmp2) with
113 | Cic.CurrentProof (_,metasenv,bo,ty,_) -> (* TODO il primo argomento e' una URI valida o e' casuale? *)
115 let uri = UriManager.uri_of_string "cic:/foo.con" in
117 typecheck_loaded_proof metasenv bo ty;
119 ((uri, metasenv, bo, ty), goal)
123 Sys.remove tmp1; Sys.remove tmp2;
126 (* tutors creation stuff from now on *)
128 module type HbugsTutor =
130 val start: unit -> unit
133 module type HbugsTutorDescription =
137 val tactic: ProofEngineTypes.tactic
139 val hint_type: hint_type
140 val description: string
143 module BuildTutor (Dsc: HbugsTutorDescription) : HbugsTutor =
145 let broker_id = ref None
146 let my_own_id = init_tutor ()
147 let my_own_addr, my_own_port = Dsc.addr, Dsc.port
148 let my_own_url = sprintf "%s:%d" my_own_addr my_own_port
150 let is_authenticated id =
151 match !broker_id with
153 | Some broker_id -> id = broker_id
155 (* thread who do the dirty work *)
156 let slave (state, musing_id) =
157 prerr_endline (sprintf "Hi, I'm the slave for musing %s" musing_id);
158 let (proof, goal) = load_state state in
161 ignore (Dsc.tactic (proof, goal));
167 (my_own_id, musing_id, (if success then Eureka Dsc.hint else Sorry))
169 ignore (Hbugs_messages.submit_req ~url:broker_url answer);
171 (sprintf "Bye, I've completed my duties (success = %b)" success)
174 (* hashtbl mapping musings ids to PID of threads doing the related (dirty)
176 let slaves = Hashtbl.create 17 in
178 prerr_endline "ignoring request from unauthorized broker";
179 Exception ("forbidden", "")
181 function (* _the_ callback *)
182 | Start_musing (broker_id, state) ->
183 if is_authenticated broker_id then begin
184 prerr_endline "received Start_musing";
185 let new_musing_id = Hbugs_id_generator.new_musing_id () in
187 (sprintf "starting a new musing (id = %s)" new_musing_id);
188 (* let slave_thread = Thread.create slave (state, new_musing_id) in *)
190 Hbugs_deity.create slave (state, new_musing_id)
192 Hashtbl.add slaves new_musing_id slave_thread;
193 Musing_started (my_own_id, new_musing_id)
194 end else (* broker unauthorized *)
196 | Abort_musing (broker_id, musing_id) ->
197 if is_authenticated broker_id then begin
198 (try (* kill thread responsible for "musing_id" *)
199 let slave_thread = Hashtbl.find slaves musing_id in
200 Hbugs_deity.kill slave_thread;
201 Hashtbl.remove slaves musing_id
203 | Hbugs_deity.Can_t_kill (pid, reason) ->
204 prerr_endline (sprintf "Unable to kill slave %d: %s" pid reason)
206 prerr_endline (sprintf
207 "Can't find slave corresponding to musing %s, can't kill it"
209 Musing_aborted (my_own_id, musing_id)
210 end else (* broker unauthorized *)
213 Exception ("unexpected_msg",
214 Hbugs_messages.string_of_msg unexpected_msg)
216 let callback (req: Http_types.request) outchan =
218 let req_msg = Hbugs_messages.msg_of_string req#body in
219 let answer = hbugs_callback req_msg in
220 Http_daemon.respond ~body:(Hbugs_messages.string_of_msg answer) outchan
221 with Hbugs_messages.Parse_error (subj, reason) ->
223 ~body:(Hbugs_messages.string_of_msg
224 (Exception ("parse_error", reason)))
229 Sys.catch_break true;
230 at_exit (fun () -> unregister_from_broker my_own_id);
232 Some (register_to_broker
233 my_own_id my_own_url Dsc.hint_type Dsc.description);
235 ~addr:my_own_addr ~port:my_own_port ~mode:`Thread callback
236 with Sys.Break -> () (* exit nicely, invoking at_exit functions *)