]> matita.cs.unibo.it Git - helm.git/blob - helm/hbugs/broker/hbugs_broker_registry.ml
- fixed helm web page url and copyright notice
[helm.git] / helm / hbugs / broker / hbugs_broker_registry.ml
1 (*
2  * Copyright (C) 2003:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
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.
9  *
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.
14  *
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.
19  *
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,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 open Hbugs_misc;;
30 open Hbugs_types;;
31
32 exception Client_already_in of client_id;;
33 exception Client_not_found of client_id;;
34 exception Musing_already_in of musing_id;;
35 exception Musing_not_found of musing_id;;
36 exception Tutor_already_in of tutor_id;;
37 exception Tutor_not_found of tutor_id;;
38
39 class clients =
40   object (self)
41
42     inherit ThreadSafe.threadSafe
43
44     val urls: (client_id, string) Hashtbl.t = Hashtbl.create 17
45     val subscriptions: (client_id, tutor_id list) Hashtbl.t = Hashtbl.create 17
46
47     method register id url = self#doWriter (lazy (
48       if Hashtbl.mem urls id then
49         raise (Client_already_in id)
50       else begin
51         Hashtbl.add urls id url;
52         Hashtbl.add subscriptions id []
53       end
54     ))
55     method unregister id = self#doWriter (lazy (
56       if Hashtbl.mem urls id then begin
57         Hashtbl.remove urls id;
58         Hashtbl.remove subscriptions id
59       end else
60         raise (Client_not_found id)
61     ))
62     method isAuthenticated id = self#doReader (lazy (
63       Hashtbl.mem urls id
64     ))
65     method subscribe client_id tutor_ids = self#doWriter (lazy (
66       if Hashtbl.mem urls client_id then
67         Hashtbl.replace subscriptions client_id tutor_ids
68       else
69         raise (Client_not_found client_id)
70     ))
71     method getUrl id = self#doReader (lazy (
72       if Hashtbl.mem urls id then
73         Hashtbl.find urls id
74       else
75         raise (Client_not_found id)
76     ))
77     method getSubscription id = self#doReader (lazy (
78       if Hashtbl.mem urls id then
79         Hashtbl.find subscriptions id
80       else
81         raise (Client_not_found id)
82     ))
83
84   end
85
86 class tutors =
87   object (self)
88
89     inherit ThreadSafe.threadSafe
90
91     val tbl: (tutor_id, string * hint_type * string) Hashtbl.t =
92       Hashtbl.create 17
93
94     method register id url hint_type dsc = self#doWriter (lazy (
95       if Hashtbl.mem tbl id then
96         raise (Tutor_already_in id)
97       else
98         Hashtbl.add tbl id (url, hint_type, dsc)
99     ))
100     method unregister id = self#doWriter (lazy (
101       if Hashtbl.mem tbl id then
102         Hashtbl.remove tbl id
103       else
104         raise (Tutor_not_found id)
105     ))
106     method isAuthenticated id = self#doReader (lazy (
107       Hashtbl.mem tbl id
108     ))
109     method exists id = self#doReader (lazy (
110       Hashtbl.mem tbl id
111     ))
112     method getTutor id = self#doReader (lazy (
113       if Hashtbl.mem tbl id then
114         Hashtbl.find tbl id
115       else
116         raise (Tutor_not_found id)
117     ))
118     method getUrl id =
119       let (url, _, _) = self#getTutor id in
120       url
121     method getHintType id =
122       let (_, hint_type, _) = self#getTutor id in
123       hint_type
124     method getDescription id =
125       let (_, _, dsc) = self#getTutor id in
126       dsc
127     method index = self#doReader (lazy (
128       Hashtbl.fold
129         (fun id (url, hint_type, dsc) idx -> (id, dsc) :: idx) tbl []
130     ))
131
132   end
133
134 class musings =
135   object (self)
136
137     inherit ThreadSafe.threadSafe
138
139     val musings: (musing_id, client_id * tutor_id) Hashtbl.t = Hashtbl.create 17
140     val clients: (client_id, musing_id) Hashtbl.t = Hashtbl.create 17
141     val tutors: (tutor_id, musing_id) Hashtbl.t = Hashtbl.create 17
142
143     method register musing_id client_id tutor_id = self#doWriter (lazy (
144       if Hashtbl.mem musings musing_id then
145         raise (Musing_already_in musing_id)
146       else begin
147         Hashtbl.add musings musing_id (client_id, tutor_id);
148         Hashtbl.add clients client_id musing_id;
149         Hashtbl.add tutors tutor_id musing_id
150       end
151     ))
152     method unregister id = self#doWriter (lazy (
153       if Hashtbl.mem musings id then begin
154         Hashtbl.remove musings id;
155         hashtbl_remove_all clients id;
156         hashtbl_remove_all tutors id
157       end
158     ))
159     method getByMusingId id = self#doReader (lazy (
160       try
161         Hashtbl.find musings id
162       with Not_found -> raise (Musing_not_found id)
163     ))
164     method getByClientId id = self#doReader (lazy (
165       Hashtbl.find_all clients id
166     ))
167     method getByTutorId id = self#doReader (lazy (
168       Hashtbl.find_all tutors id
169     ))
170
171   end
172