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 Client_already_in of client_id;;
36 exception Client_not_found of client_id;;
37 exception Musing_already_in of musing_id;;
38 exception Musing_not_found of musing_id;;
39 exception Tutor_already_in of tutor_id;;
40 exception Tutor_not_found of tutor_id;;
48 let expire_time = 1800. (* 30 minutes *)
53 inherit ThreadSafe.threadSafe
56 method private doCritical: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
57 method private doWriter: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
58 method private doReader: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
62 val timetable: (client_id, float) Hashtbl.t = Hashtbl.create 17
63 val urls: (client_id, string) Hashtbl.t = Hashtbl.create 17
64 val subscriptions: (client_id, tutor_id list) Hashtbl.t = Hashtbl.create 17
66 (** INVARIANT: each client registered has an entry in 'urls' hash table
67 _and_ in 'subscriptions hash table even if it hasn't yet invoked
70 method register id url = self#doWriter (lazy (
71 if Hashtbl.mem urls id then
72 raise (Client_already_in id)
74 Hashtbl.add urls id url;
75 Hashtbl.add subscriptions id [];
76 Hashtbl.add timetable id (Unix.time ())
79 method private remove id =
80 Hashtbl.remove urls id;
81 Hashtbl.remove subscriptions id;
82 Hashtbl.remove timetable id
83 method unregister id = self#doWriter (lazy (
84 if Hashtbl.mem urls id then
87 raise (Client_not_found id)
89 method isAuthenticated id = self#doReader (lazy (
92 method subscribe client_id tutor_ids = self#doWriter (lazy (
93 if Hashtbl.mem urls client_id then
94 Hashtbl.replace subscriptions client_id tutor_ids
96 raise (Client_not_found client_id)
98 method getUrl id = self#doReader (lazy (
99 if Hashtbl.mem urls id then
102 raise (Client_not_found id)
104 method getSubscription id = self#doReader (lazy (
105 if Hashtbl.mem urls id then
106 Hashtbl.find subscriptions id
108 raise (Client_not_found id)
111 method dump = self#doReader (lazy (
116 (sprintf "<client id=\"%s\" url=\"%s\">\n" id url) ^
117 "<subscriptions>\n" ^
118 (String.concat "\n" (* id's subscriptions *)
120 (fun tutor_id -> sprintf "<tutor id=\"%s\" />\n" tutor_id)
121 (Hashtbl.find subscriptions id))) ^
122 "</subscriptions>\n</client>\n"))
126 method purge = self#doWriter (lazy (
127 let now = Unix.time () in
130 if now -. birthday > expire_time then
140 inherit ThreadSafe.threadSafe
143 method private doCritical: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
144 method private doWriter: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
145 method private doReader: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
149 val timetable: (tutor_id, float) Hashtbl.t = Hashtbl.create 17
150 val tbl: (tutor_id, string * hint_type * string) Hashtbl.t =
153 method register id url hint_type dsc = self#doWriter (lazy (
154 if Hashtbl.mem tbl id then
155 raise (Tutor_already_in id)
157 Hashtbl.add tbl id (url, hint_type, dsc);
158 Hashtbl.add timetable id (Unix.time ())
161 method private remove id =
162 Hashtbl.remove tbl id;
163 Hashtbl.remove timetable id
164 method unregister id = self#doWriter (lazy (
165 if Hashtbl.mem tbl id then
168 raise (Tutor_not_found id)
170 method isAuthenticated id = self#doReader (lazy (
173 method exists id = self#doReader (lazy (
176 method getTutor id = self#doReader (lazy (
177 if Hashtbl.mem tbl id then
180 raise (Tutor_not_found id)
183 let (url, _, _) = self#getTutor id in
185 method getHintType id =
186 let (_, hint_type, _) = self#getTutor id in
188 method getDescription id =
189 let (_, _, dsc) = self#getTutor id in
191 method index = self#doReader (lazy (
193 (fun id (url, hint_type, dsc) idx -> (id, dsc) :: idx) tbl []
196 method dump = self#doReader (lazy (
199 (fun id (url, hint_type, dsc) dump ->
202 "<tutor id=\"%s\" url=\"%s\">\n<hint_type>%s</hint_type>\n<description>%s</description>\n</tutor>"
203 id url hint_type dsc))
207 method purge = self#doWriter (lazy (
208 let now = Unix.time () in
211 if now -. birthday > expire_time then
221 inherit ThreadSafe.threadSafe
224 method private doCritical: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
225 method private doWriter: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
226 method private doReader: 'a. 'a lazy_t -> 'a = fun act -> Lazy.force act
230 val timetable: (musing_id, float) Hashtbl.t = Hashtbl.create 17
231 val musings: (musing_id, client_id * tutor_id) Hashtbl.t = Hashtbl.create 17
232 val clients: (client_id, musing_id list) Hashtbl.t = Hashtbl.create 17
233 val tutors: (tutor_id, musing_id list) Hashtbl.t = Hashtbl.create 17
235 (** INVARIANT: each registered musing <musing_id, client_id, tutor_id> has
236 an entry in 'musings' table, an entry in 'clients' (i.e. one of the
237 musings for client_id is musing_id) table, an entry in 'tutors' table
238 (i.e. one of the musings for tutor_id is musing_id) and an entry in
242 method register musing_id client_id tutor_id = self#doWriter (lazy (
243 if Hashtbl.mem musings musing_id then
244 raise (Musing_already_in musing_id)
246 Hashtbl.add musings musing_id (client_id, tutor_id);
247 (* now add this musing as the first one of musings list for client and
249 Hashtbl.replace clients client_id
251 (try Hashtbl.find clients client_id with Not_found -> []));
252 Hashtbl.replace tutors tutor_id
254 (try Hashtbl.find tutors tutor_id with Not_found -> []));
255 Hashtbl.add timetable musing_id (Unix.time ())
258 method private remove id =
259 (* ASSUMPTION: this method is invoked under a 'writer' lock *)
260 let (client_id, tutor_id) = self#getByMusingId' id in
261 Hashtbl.remove musings id;
262 (* now remove this musing from the list of musings for client and tutor
264 Hashtbl.replace clients client_id
265 (List.filter ((<>) id)
266 (try Hashtbl.find clients client_id with Not_found -> []));
267 Hashtbl.replace tutors tutor_id
268 (List.filter ((<>) id)
269 (try Hashtbl.find tutors tutor_id with Not_found -> []));
270 Hashtbl.remove timetable id
271 method unregister id = self#doWriter (lazy (
272 if Hashtbl.mem musings id then
275 method private getByMusingId' id =
276 (* ASSUMPTION: this method is invoked under a 'reader' lock *)
278 Hashtbl.find musings id
279 with Not_found -> raise (Musing_not_found id)
280 method getByMusingId id = self#doReader (lazy (
281 self#getByMusingId' id
283 method getByClientId id = self#doReader (lazy (
285 Hashtbl.find clients id
288 method getByTutorId id = self#doReader (lazy (
290 Hashtbl.find tutors id
293 method isActive id = self#doReader (lazy (
294 Hashtbl.mem musings id
297 method dump = self#doReader (lazy (
300 (fun mid (cid, tid) dump ->
302 (sprintf "<musing id=\"%s\" client=\"%s\" tutor=\"%s\" />\n"
307 method purge = self#doWriter (lazy (
308 let now = Unix.time () in
311 if now -. birthday > expire_time then