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/
33 exception Client_already_in of client_id;;
34 exception Client_not_found of client_id;;
35 exception Musing_already_in of musing_id;;
36 exception Musing_not_found of musing_id;;
37 exception Tutor_already_in of tutor_id;;
38 exception Tutor_not_found of tutor_id;;
46 let expire_time = 1800. (* 30 minutes *)
51 inherit ThreadSafe.threadSafe
54 method private doCritical: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
55 method private doWriter: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
56 method private doReader: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
60 val timetable: (client_id, float) Hashtbl.t = Hashtbl.create 17
61 val urls: (client_id, string) Hashtbl.t = Hashtbl.create 17
62 val subscriptions: (client_id, tutor_id list) Hashtbl.t = Hashtbl.create 17
64 (** INVARIANT: each client registered has an entry in 'urls' hash table
65 _and_ in 'subscriptions hash table even if it hasn't yet invoked
68 method register id url = self#doWriter (lazy (
69 if Hashtbl.mem urls id then
70 raise (Client_already_in id)
72 Hashtbl.add urls id url;
73 Hashtbl.add subscriptions id [];
74 Hashtbl.add timetable id (Unix.time ())
77 method private remove id =
78 Hashtbl.remove urls id;
79 Hashtbl.remove subscriptions id;
80 Hashtbl.remove timetable id
81 method unregister id = self#doWriter (lazy (
82 if Hashtbl.mem urls id then
85 raise (Client_not_found id)
87 method isAuthenticated id = self#doReader (lazy (
90 method subscribe client_id tutor_ids = self#doWriter (lazy (
91 if Hashtbl.mem urls client_id then
92 Hashtbl.replace subscriptions client_id tutor_ids
94 raise (Client_not_found client_id)
96 method getUrl id = self#doReader (lazy (
97 if Hashtbl.mem urls id then
100 raise (Client_not_found id)
102 method getSubscription id = self#doReader (lazy (
103 if Hashtbl.mem urls id then
104 Hashtbl.find subscriptions id
106 raise (Client_not_found id)
109 method dump = self#doReader (lazy (
114 (sprintf "<client id=\"%s\" url=\"%s\">\n" id url) ^
115 "<subscriptions>\n" ^
116 (String.concat "\n" (* id's subscriptions *)
118 (fun tutor_id -> sprintf "<tutor id=\"%s\" />\n" tutor_id)
119 (Hashtbl.find subscriptions id))) ^
120 "</subscriptions>\n</client>\n"))
124 method purge = self#doWriter (lazy (
125 let now = Unix.time () in
128 if now -. birthday > expire_time then
138 inherit ThreadSafe.threadSafe
141 method private doCritical: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
142 method private doWriter: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
143 method private doReader: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
147 val timetable: (tutor_id, float) Hashtbl.t = Hashtbl.create 17
148 val tbl: (tutor_id, string * hint_type * string) Hashtbl.t =
151 method register id url hint_type dsc = self#doWriter (lazy (
152 if Hashtbl.mem tbl id then
153 raise (Tutor_already_in id)
155 Hashtbl.add tbl id (url, hint_type, dsc);
156 Hashtbl.add timetable id (Unix.time ())
159 method private remove id =
160 Hashtbl.remove tbl id;
161 Hashtbl.remove timetable id
162 method unregister id = self#doWriter (lazy (
163 if Hashtbl.mem tbl id then
166 raise (Tutor_not_found id)
168 method isAuthenticated id = self#doReader (lazy (
171 method exists id = self#doReader (lazy (
174 method getTutor id = self#doReader (lazy (
175 if Hashtbl.mem tbl id then
178 raise (Tutor_not_found id)
181 let (url, _, _) = self#getTutor id in
183 method getHintType id =
184 let (_, hint_type, _) = self#getTutor id in
186 method getDescription id =
187 let (_, _, dsc) = self#getTutor id in
189 method index = self#doReader (lazy (
191 (fun id (url, hint_type, dsc) idx -> (id, dsc) :: idx) tbl []
194 method dump = self#doReader (lazy (
197 (fun id (url, hint_type, dsc) dump ->
200 "<tutor id=\"%s\" url=\"%s\">\n<hint_type>%s</hint_type>\n<description>%s</description>\n</tutor>"
201 id url hint_type dsc))
205 method purge = self#doWriter (lazy (
206 let now = Unix.time () in
209 if now -. birthday > expire_time then
219 inherit ThreadSafe.threadSafe
222 method private doCritical: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
223 method private doWriter: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
224 method private doReader: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
228 val timetable: (musing_id, float) Hashtbl.t = Hashtbl.create 17
229 val musings: (musing_id, client_id * tutor_id) Hashtbl.t = Hashtbl.create 17
230 val clients: (client_id, musing_id list) Hashtbl.t = Hashtbl.create 17
231 val tutors: (tutor_id, musing_id list) Hashtbl.t = Hashtbl.create 17
233 (** INVARIANT: each registered musing <musing_id, client_id, tutor_id> has
234 an entry in 'musings' table, an entry in 'clients' (i.e. one of the
235 musings for client_id is musing_id) table, an entry in 'tutors' table
236 (i.e. one of the musings for tutor_id is musing_id) and an entry in
240 method register musing_id client_id tutor_id = self#doWriter (lazy (
241 if Hashtbl.mem musings musing_id then
242 raise (Musing_already_in musing_id)
244 Hashtbl.add musings musing_id (client_id, tutor_id);
245 (* now add this musing as the first one of musings list for client and
247 Hashtbl.replace clients client_id
249 (try Hashtbl.find clients client_id with Not_found -> []));
250 Hashtbl.replace tutors tutor_id
252 (try Hashtbl.find tutors tutor_id with Not_found -> []));
253 Hashtbl.add timetable musing_id (Unix.time ())
256 method private remove id =
257 (* ASSUMPTION: this method is invoked under a 'writer' lock *)
258 let (client_id, tutor_id) = self#getByMusingId' id in
259 Hashtbl.remove musings id;
260 (* now remove this musing from the list of musings for client and tutor
262 Hashtbl.replace clients client_id
263 (List.filter ((<>) id)
264 (try Hashtbl.find clients client_id with Not_found -> []));
265 Hashtbl.replace tutors tutor_id
266 (List.filter ((<>) id)
267 (try Hashtbl.find tutors tutor_id with Not_found -> []));
268 Hashtbl.remove timetable id
269 method unregister id = self#doWriter (lazy (
270 if Hashtbl.mem musings id then
273 method private getByMusingId' id =
274 (* ASSUMPTION: this method is invoked under a 'reader' lock *)
276 Hashtbl.find musings id
277 with Not_found -> raise (Musing_not_found id)
278 method getByMusingId id = self#doReader (lazy (
279 self#getByMusingId' id
281 method getByClientId id = self#doReader (lazy (
283 Hashtbl.find clients id
286 method getByTutorId id = self#doReader (lazy (
288 Hashtbl.find tutors id
291 method isActive id = self#doReader (lazy (
292 Hashtbl.mem musings id
295 method dump = self#doReader (lazy (
298 (fun mid (cid, tid) dump ->
300 (sprintf "<musing id=\"%s\" client=\"%s\" tutor=\"%s\" />\n"
305 method purge = self#doWriter (lazy (
306 let now = Unix.time () in
309 if now -. birthday > expire_time then