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/
35 exception Invalid_URL of string;;
37 let do_nothing _ = ();;
39 module SmartHbugs_client_gui =
41 class ['a] oneColumnCList gtree_view ~column_type ~column_title
44 ((Gobject.unsafe_cast gtree_view#as_widget) : Gtk.tree_view Gtk.obj) in
45 let columns = new GTree.column_list in
46 let col = columns#add column_type in
47 let vcol = GTree.view_column ~title:column_title ()
48 ~renderer:(GTree.cell_renderer_text[], ["text",col]) in
49 let store = GTree.list_store columns in
51 inherit GTree.view obj
52 method clear = store#clear
53 method append (v : 'a) =
54 let row = store#append () in
55 store#set ~row ~column:col v;
58 self#set_model (Some (store :> GTree.model)) ;
59 ignore (self#append_column vcol)
62 class ['a,'b] twoColumnsCList gtree_view ~column1_type ~column2_type
63 ~column1_title ~column2_title
66 ((Gobject.unsafe_cast gtree_view#as_widget) : Gtk.tree_view Gtk.obj) in
67 let columns = new GTree.column_list in
68 let col1 = columns#add column1_type in
69 let vcol1 = GTree.view_column ~title:column1_title ()
70 ~renderer:(GTree.cell_renderer_text[], ["text",col1]) in
71 let col2 = columns#add column2_type in
72 let vcol2 = GTree.view_column ~title:column2_title ()
73 ~renderer:(GTree.cell_renderer_text[], ["text",col2]) in
74 let store = GTree.list_store columns in
76 inherit GTree.view obj
77 method clear = store#clear
78 method append (v1 : 'a) (v2 : 'b) =
79 let row = store#append () in
80 store#set ~row ~column:col1 v1;
81 store#set ~row ~column:col2 v2
85 self#set_model (Some (store :> GTree.model)) ;
86 ignore (self#append_column vcol1) ;
87 ignore (self#append_column vcol2) ;
90 class subscribeWindow () =
92 inherit Hbugs_client_gui.subscribeWindow ()
93 val mutable tutorsSmartCList = None
94 method tutorsSmartCList =
95 match tutorsSmartCList with
101 (new twoColumnsCList self#tutorsCList
102 ~column1_type:Gobject.Data.string ~column2_type:Gobject.Data.string
103 ~column1_title:"Id" ~column2_title:"Description")
106 class hbugsMainWindow () =
108 inherit Hbugs_client_gui.hbugsMainWindow ()
109 val mutable subscriptionSmartCList = None
110 val mutable hintsSmartCList = None
111 method subscriptionSmartCList =
112 match subscriptionSmartCList with
115 method hintsSmartCList =
116 match hintsSmartCList with
120 subscriptionSmartCList <-
122 (new oneColumnCList self#subscriptionCList
123 ~column_type:Gobject.Data.string ~column_title:"Description")
127 (new oneColumnCList self#hintsCList
128 ~column_type:Gobject.Data.string ~column_title:"Description")
135 ?(use_hint_callback: hint -> unit = do_nothing)
136 ?(describe_hint_callback: hint -> unit = do_nothing)
137 ?(destroy_callback: unit -> unit = do_nothing)
141 let http_url_RE = Pcre.regexp "^(http://)?(.*):(\\d+)" in
142 let port_of_http_url url =
144 let subs = Pcre.extract ~rex:http_url_RE url in
145 int_of_string subs.(3)
146 with e -> raise (Invalid_URL url)
151 val mainWindow = new SmartHbugs_client_gui.hbugsMainWindow ()
152 val subscribeWindow = new SmartHbugs_client_gui.subscribeWindow ()
153 val messageDialog = new Hbugs_client_gui.messageDialog ()
154 val myOwnId = Hbugs_id_generator.new_client_id ()
155 val mutable use_hint_callback = use_hint_callback
156 val mutable myOwnUrl = "localhost:49082"
157 val mutable brokerUrl = "localhost:49081"
158 val mutable brokerId: broker_id option = None
159 (* all available tutors, saved last time a List_tutors message was sent to
161 val mutable availableTutors: tutor_dsc list = []
162 val mutable statusContext = None
163 val mutable subscribeWindowStatusContext = None
164 val mutable debug = false (* enable/disable debugging buttons *)
165 val mutable hints = [] (* actually available hints *)
169 self#startLocalHttpDaemon ();
170 self#testLocalHttpDaemon ();
172 self#registerToBroker ();
173 self#reconfigDebuggingButtons
175 method show = mainWindow#hbugsMainWindow#show
176 method hide = mainWindow#hbugsMainWindow#misc#hide
178 method setUseHintCallback callback =
179 use_hint_callback <- callback
181 method private debugButtons =
183 (fun (b: GButton.button) -> new GObj.misc_ops b#as_widget)
184 [ mainWindow#startLocalHttpDaemonButton;
185 mainWindow#testLocalHttpDaemonButton; mainWindow#testBrokerButton ]
187 method private initGui =
189 (* GUI: main window *)
191 (* ignore delete events so that hbugs window is closable only using
192 menu; on destroy (e.g. while quitting gTopLevel) self#quit is invoked
195 ignore (mainWindow#hbugsMainWindow#event#connect#delete (fun _ -> true));
196 ignore (mainWindow#hbugsMainWindow#event#connect#destroy
197 (fun _ -> self#quit (); false));
199 (* GUI main window's menu *)
200 mainWindow#toggleDebuggingMenuItem#set_active debug;
201 ignore (mainWindow#toggleDebuggingMenuItem#connect#toggled
204 (* GUI: local HTTP daemon settings *)
205 ignore (mainWindow#clientUrlEntry#connect#changed
206 (fun _ -> myOwnUrl <- mainWindow#clientUrlEntry#text));
207 mainWindow#clientUrlEntry#set_text myOwnUrl;
208 ignore (mainWindow#startLocalHttpDaemonButton#connect#clicked
209 self#startLocalHttpDaemon);
210 ignore (mainWindow#testLocalHttpDaemonButton#connect#clicked
211 self#testLocalHttpDaemon);
213 (* GUI: broker choice *)
214 ignore (mainWindow#brokerUrlEntry#connect#changed
215 (fun _ -> brokerUrl <- mainWindow#brokerUrlEntry#text));
216 mainWindow#brokerUrlEntry#set_text brokerUrl;
217 ignore (mainWindow#testBrokerButton#connect#clicked self#testBroker);
218 mainWindow#clientIdLabel#set_text myOwnId;
220 (* GUI: client registration *)
221 ignore (mainWindow#registerClientButton#connect#clicked
222 self#registerToBroker);
224 (* GUI: subscriptions *)
225 ignore (mainWindow#showSubscriptionWindowButton#connect#clicked
228 subscribeWindow#subscribeWindow#show ()));
230 let get_selected_row_index () =
231 match mainWindow#hintsCList#selection#get_selected_rows with
233 (match GTree.Path.get_indices path with
238 (* GUI: hints list *)
240 let event_ops = new GObj.event_ops mainWindow#hintsCList#as_widget in
241 event_ops#connect#button_press
243 if GdkEvent.get_type event = `TWO_BUTTON_PRESS then
244 use_hint_callback (self#hint (get_selected_row_index ())) ;
247 ignore (mainWindow#hintsCList#selection#connect#changed
249 describe_hint_callback (self#hint (get_selected_row_index ())))) ;
251 (* GUI: main status bar *)
252 let ctxt = mainWindow#mainWindowStatusBar#new_context "0" in
253 statusContext <- Some ctxt;
254 ignore (ctxt#push "Ready");
256 (* GUI: subscription window *)
257 subscribeWindow#tutorsCList#selection#set_mode `MULTIPLE;
258 ignore (subscribeWindow#subscribeWindow#event#connect#delete
259 (fun _ -> subscribeWindow#subscribeWindow#misc#hide (); true));
260 ignore (subscribeWindow#listTutorsButton#connect#clicked self#listTutors);
261 ignore (subscribeWindow#subscribeButton#connect#clicked
262 self#subscribeSelected);
263 ignore (subscribeWindow#subscribeAllButton#connect#clicked
265 (subscribeWindow#tutorsCList#get_column 0)#set_visible false;
266 let ctxt = subscribeWindow#subscribeWindowStatusBar#new_context "0" in
267 subscribeWindowStatusContext <- Some ctxt;
268 ignore (ctxt#push "Ready");
270 (* GUI: message dialog *)
271 ignore (messageDialog#messageDialog#event#connect#delete
272 (fun _ -> messageDialog#messageDialog#misc#hide (); true));
273 ignore (messageDialog#okDialogButton#connect#clicked
274 (fun _ -> messageDialog#messageDialog#misc#hide ()))
276 (* accessory methods *)
278 (** pop up a (modal) dialog window showing msg to the user *)
279 method private showDialog msg =
280 messageDialog#dialogLabel#set_text msg;
281 messageDialog#messageDialog#show ()
282 (** use showDialog to display an hbugs message to the user *)
283 method private showMsgInDialog msg =
284 self#showDialog (Hbugs_messages.string_of_msg msg)
286 (** create a new thread which sends msg to broker, wait for an answer and
287 invoke callback passing response message as argument *)
288 method private sendReq ?(wait = false) ~msg callback =
291 callback (Hbugs_messages.submit_req ~url:(brokerUrl ^ "/act") msg)
293 | (Hbugs_messages.Parse_error (subj, reason)) as e ->
296 "Parse_error, unable to fullfill request. Details follow.
299 (Hbugs_messages.string_of_msg msg) (Printexc.to_string e));
300 | (Unix.Unix_error _) as e ->
303 "Can't connect to HBugs Broker
306 brokerUrl (Printexc.to_string e))
309 (sprintf "hbugsClient#sendReq: Uncaught exception: %s"
310 (Printexc.to_string e))
312 let th = Thread.create thread () in
317 (** check if a broker is authenticated using its broker_id
318 [ Background: during client registration, client save broker_id of its
319 broker, further messages from broker are accepted only if they carry the
321 method private isAuthenticated id =
324 | Some broker_id -> (id = broker_id)
328 method private startLocalHttpDaemon =
329 (* flatten an hint tree to an hint list *)
330 let rec flatten_hint = function
331 | Hints hints -> List.concat (List.map flatten_hint hints)
335 let callback req outchan =
337 (match Hbugs_messages.msg_of_string req#body with
339 Hbugs_messages.respond_msg
340 (Usage "Local Http Daemon up and running!") outchan
341 | Hint (broker_id, hint) ->
342 if self#isAuthenticated broker_id then begin
343 let received_hints = flatten_hint hint in
346 (match h with Hints _ -> assert false | _ -> ());
347 ignore(mainWindow#hintsSmartCList#append(string_of_hint h)))
349 hints <- hints @ received_hints;
350 Hbugs_messages.respond_msg (Wow myOwnId) outchan
351 end else (* msg from unauthorized broker *)
352 Hbugs_messages.respond_exc "forbidden" broker_id outchan
354 Hbugs_messages.respond_exc
355 "unexpected_msg" (Hbugs_messages.string_of_msg msg) outchan)
356 with (Hbugs_messages.Parse_error _) as e ->
357 Hbugs_messages.respond_exc
358 "parse_error" (Printexc.to_string e) outchan
360 let addr = "0.0.0.0" in (* TODO actually user specified "My URL" is used
361 only as a value to be sent to broker, local HTTP
362 daemon will listen on "0.0.0.0", port is parsed
363 from My URL though *)
364 let httpDaemonThread () =
367 ~addr ~port:(port_of_http_url myOwnUrl) ~mode:`Single callback
369 | Invalid_URL url -> self#showDialog (sprintf "Invalid URL: \"%s\"" url)
371 self#showDialog (sprintf "Can't start local HTTP daemon: %s"
372 (Printexc.to_string e))
374 ignore (Thread.create httpDaemonThread ())
376 method private testLocalHttpDaemon () =
379 Hbugs_misc.http_post ~body:(Hbugs_messages.string_of_msg Help)
383 (* self#showDialog msg *)
385 | Hbugs_misc.Malformed_URL url ->
388 "Handshake with local HTTP daemon failed, Invalid URL: \"%s\""
390 | Hbugs_misc.Malformed_HTTP_response res ->
393 "Handshake with local HTTP daemon failed, can't parse HTTP response: \"%s\""
395 | (Unix.Unix_error _) as e ->
398 "Handshake with local HTTP daemon failed, can't connect: \"%s\""
399 (Printexc.to_string e))
401 method private testBroker () =
402 self#sendReq ~msg:Help
408 "Handshake with HBugs Broker failed, unexpected message:\n%s"
409 (Hbugs_messages.string_of_msg unexpected_msg)))
411 method registerToBroker () =
412 (match brokerId with (* undo previous registration, if any *)
413 | Some id -> self#unregisterFromBroker ()
415 self#sendReq ~msg:(Register_client (myOwnId, myOwnUrl))
417 | Client_registered broker_id -> (brokerId <- Some broker_id)
420 (sprintf "Client NOT registered, unexpected message:\n%s"
421 (Hbugs_messages.string_of_msg unexpected_msg)))
423 method unregisterFromBroker () =
424 self#sendReq ~wait:true ~msg:(Unregister_client myOwnId)
426 | Client_unregistered _ -> (brokerId <- None)
427 | unexpected_msg -> ())
430 (sprintf "Client NOT unregistered, unexpected message:\n%s"
431 (Hbugs_messages.string_of_msg unexpected_msg)))
434 method stateChange new_state =
435 mainWindow#hintsSmartCList#clear ();
438 ~msg:(State_change (myOwnId, new_state))
440 | State_accepted _ -> ()
443 (sprintf "State NOT accepted by Hbugs, unexpected message:\n%s"
444 (Hbugs_messages.string_of_msg unexpected_msg)))
446 method hint = List.nth hints
448 method private listTutors () =
449 (* wait is set to true just to make sure that after invoking listTutors
450 "availableTutors" is correctly filled *)
451 self#sendReq ~wait:true ~msg:(List_tutors myOwnId)
453 | Tutor_list (_, descriptions) ->
454 availableTutors <- (* sort accordingly to tutor description *)
455 List.sort (fun (a,b) (c,d) -> compare (b,a) (d,c)) descriptions;
456 subscribeWindow#tutorsSmartCList#clear ();
459 ignore (subscribeWindow#tutorsSmartCList#append id dsc))
463 (sprintf "Can't list tutors, unexpected message:\n%s"
464 (Hbugs_messages.string_of_msg unexpected_msg)))
466 (* low level used by subscribeSelected and subscribeAll *)
467 method private subscribe' tutors_id =
468 self#sendReq ~msg:(Subscribe (myOwnId, tutors_id))
470 | (Subscribed (_, subscribedTutors)) as msg ->
471 let sort = List.sort compare in
472 mainWindow#subscriptionSmartCList#clear ();
476 (mainWindow#subscriptionSmartCList#append
478 List.assoc tutor_id availableTutors
479 with Not_found -> assert false )))
481 subscribeWindow#subscribeWindow#misc#hide ();
482 if sort subscribedTutors <> sort tutors_id then
484 (sprintf "Subscription mismatch\n: %s"
485 (Hbugs_messages.string_of_msg msg))
487 mainWindow#subscriptionSmartCList#clear ();
489 (sprintf "Subscription FAILED, unexpected message:\n%s"
490 (Hbugs_messages.string_of_msg unexpected_msg)))
492 method private subscribeSelected () =
493 let tutorsSmartCList = subscribeWindow#tutorsSmartCList in
497 tutorsSmartCList#model#get
498 ~row:(tutorsSmartCList#model#get_iter p)
499 ~column:tutorsSmartCList#column1)
500 tutorsSmartCList#selection#get_selected_rows
502 self#subscribe' selectedTutors
504 method subscribeAll () =
505 self#listTutors (); (* this fills 'availableTutors' field *)
506 self#subscribe' (List.map fst availableTutors)
508 method private quit () =
509 self#unregisterFromBroker ();
512 (** enable/disable debugging *)
513 method private setDebug value = debug <- value
515 method private reconfigDebuggingButtons =
516 List.iter (* debug value changed, reconfigure buttons *)
517 (fun (b: GObj.misc_ops) -> if debug then b#show () else b#hide ())
520 method private toggleDebug () =
521 self#setDebug (not debug);
522 self#reconfigDebuggingButtons