]> matita.cs.unibo.it Git - helm.git/blob - helm/hbugs/broker/hbugs_broker_registry.ml
adf8bc379f022c765aa932f7934dcd5a475f7eab
[helm.git] / helm / hbugs / broker / hbugs_broker_registry.ml
1 (*
2  *  Copyright (C) 2003, HELM Team.
3  *
4  *  This file is part of HELM, an Hypertextual, Electronic
5  *  Library of Mathematics, developed at the Computer Science
6  *  Department, University of Bologna, Italy.
7  *
8  *  HELM is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU General Public License
10  *  as published by the Free Software Foundation; either version 2
11  *  of the License, or (at your option) any later version.
12  *
13  *  HELM is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with HELM; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  *  MA  02111-1307, USA.
22  *
23  *  For details, see the HELM World-Wide-Web page,
24  *  http://cs.unibo.it/helm/.
25  *)
26
27 open Hbugs_misc;;
28 open Hbugs_types;;
29
30 exception Client_already_in of client_id;;
31 exception Client_not_found of client_id;;
32 exception Musing_already_in of musing_id;;
33 exception Musing_not_found of musing_id;;
34 exception Tutor_already_in of tutor_id;;
35 exception Tutor_not_found of tutor_id;;
36
37 class clients =
38   object (self)
39
40     inherit ThreadSafe.threadSafe
41
42     val urls: (client_id, string) Hashtbl.t = Hashtbl.create 17
43     val subscriptions: (client_id, tutor_id list) Hashtbl.t = Hashtbl.create 17
44
45     method register id url = self#doWriter (lazy (
46       if Hashtbl.mem urls id then
47         raise (Client_already_in id)
48       else begin
49         Hashtbl.add urls id url;
50         Hashtbl.add subscriptions id []
51       end
52     ))
53     method unregister id = self#doWriter (lazy (
54       if Hashtbl.mem urls id then begin
55         Hashtbl.remove urls id;
56         Hashtbl.remove subscriptions id
57       end else
58         raise (Client_not_found id)
59     ))
60     method isAuthenticated id = self#doReader (lazy (
61       Hashtbl.mem urls id
62     ))
63     method subscribe client_id tutor_ids = self#doWriter (lazy (
64       if Hashtbl.mem urls client_id then
65         Hashtbl.replace subscriptions client_id tutor_ids
66       else
67         raise (Client_not_found client_id)
68     ))
69     method getUrl id = self#doReader (lazy (
70       if Hashtbl.mem urls id then
71         Hashtbl.find urls id
72       else
73         raise (Client_not_found id)
74     ))
75     method getSubscription id = self#doReader (lazy (
76       if Hashtbl.mem urls id then
77         Hashtbl.find subscriptions id
78       else
79         raise (Client_not_found id)
80     ))
81
82   end
83
84 class tutors =
85   object (self)
86
87     inherit ThreadSafe.threadSafe
88
89     val tbl: (tutor_id, string * hint_type * string) Hashtbl.t =
90       Hashtbl.create 17
91
92     method register id url hint_type dsc = self#doWriter (lazy (
93       if Hashtbl.mem tbl id then
94         raise (Tutor_already_in id)
95       else
96         Hashtbl.add tbl id (url, hint_type, dsc)
97     ))
98     method unregister id = self#doWriter (lazy (
99       if Hashtbl.mem tbl id then
100         Hashtbl.remove tbl id
101       else
102         raise (Tutor_not_found id)
103     ))
104     method isAuthenticated id = self#doReader (lazy (
105       Hashtbl.mem tbl id
106     ))
107     method exists id = self#doReader (lazy (
108       Hashtbl.mem tbl id
109     ))
110     method getTutor id = self#doReader (lazy (
111       if Hashtbl.mem tbl id then
112         Hashtbl.find tbl id
113       else
114         raise (Tutor_not_found id)
115     ))
116     method getUrl id =
117       let (url, _, _) = self#getTutor id in
118       url
119     method getHintType id =
120       let (_, hint_type, _) = self#getTutor id in
121       hint_type
122     method getDescription id =
123       let (_, _, dsc) = self#getTutor id in
124       dsc
125     method index = self#doReader (lazy (
126       Hashtbl.fold
127         (fun id (url, hint_type, dsc) idx -> (id, dsc) :: idx) tbl []
128     ))
129
130   end
131
132 class musings =
133   object (self)
134
135     inherit ThreadSafe.threadSafe
136
137     val musings: (musing_id, client_id * tutor_id) Hashtbl.t = Hashtbl.create 17
138     val clients: (client_id, musing_id) Hashtbl.t = Hashtbl.create 17
139     val tutors: (tutor_id, musing_id) Hashtbl.t = Hashtbl.create 17
140
141     method register musing_id client_id tutor_id = self#doWriter (lazy (
142       if Hashtbl.mem musings musing_id then
143         raise (Musing_already_in musing_id)
144       else begin
145         Hashtbl.add musings musing_id (client_id, tutor_id);
146         Hashtbl.add clients client_id musing_id;
147         Hashtbl.add tutors tutor_id musing_id
148       end
149     ))
150     method unregister id = self#doWriter (lazy (
151       if Hashtbl.mem musings id then begin
152         Hashtbl.remove musings id;
153         hashtbl_remove_all clients id;
154         hashtbl_remove_all tutors id
155       end
156     ))
157     method getByMusingId id = self#doReader (lazy (
158       try
159         Hashtbl.find musings id
160       with Not_found -> raise (Musing_not_found id)
161     ))
162     method getByClientId id = self#doReader (lazy (
163       Hashtbl.find_all clients id
164     ))
165     method getByTutorId id = self#doReader (lazy (
166       Hashtbl.find_all tutors id
167     ))
168
169   end
170