]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/hbugs/broker/hbugs_broker_registry.ml
- hbugs first draft release, not yet tested
[helm.git] / helm / hbugs / broker / hbugs_broker_registry.ml
diff --git a/helm/hbugs/broker/hbugs_broker_registry.ml b/helm/hbugs/broker/hbugs_broker_registry.ml
new file mode 100644 (file)
index 0000000..adf8bc3
--- /dev/null
@@ -0,0 +1,170 @@
+(*
+ *  Copyright (C) 2003, HELM Team.
+ *
+ *  This file is part of HELM, an Hypertextual, Electronic
+ *  Library of Mathematics, developed at the Computer Science
+ *  Department, University of Bologna, Italy.
+ *
+ *  HELM is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; either version 2
+ *  of the License, or (at your option) any later version.
+ *
+ *  HELM is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with HELM; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ *  MA  02111-1307, USA.
+ *
+ *  For details, see the HELM World-Wide-Web page,
+ *  http://cs.unibo.it/helm/.
+ *)
+
+open Hbugs_misc;;
+open Hbugs_types;;
+
+exception Client_already_in of client_id;;
+exception Client_not_found of client_id;;
+exception Musing_already_in of musing_id;;
+exception Musing_not_found of musing_id;;
+exception Tutor_already_in of tutor_id;;
+exception Tutor_not_found of tutor_id;;
+
+class clients =
+  object (self)
+
+    inherit ThreadSafe.threadSafe
+
+    val urls: (client_id, string) Hashtbl.t = Hashtbl.create 17
+    val subscriptions: (client_id, tutor_id list) Hashtbl.t = Hashtbl.create 17
+
+    method register id url = self#doWriter (lazy (
+      if Hashtbl.mem urls id then
+        raise (Client_already_in id)
+      else begin
+        Hashtbl.add urls id url;
+        Hashtbl.add subscriptions id []
+      end
+    ))
+    method unregister id = self#doWriter (lazy (
+      if Hashtbl.mem urls id then begin
+        Hashtbl.remove urls id;
+        Hashtbl.remove subscriptions id
+      end else
+        raise (Client_not_found id)
+    ))
+    method isAuthenticated id = self#doReader (lazy (
+      Hashtbl.mem urls id
+    ))
+    method subscribe client_id tutor_ids = self#doWriter (lazy (
+      if Hashtbl.mem urls client_id then
+        Hashtbl.replace subscriptions client_id tutor_ids
+      else
+        raise (Client_not_found client_id)
+    ))
+    method getUrl id = self#doReader (lazy (
+      if Hashtbl.mem urls id then
+        Hashtbl.find urls id
+      else
+        raise (Client_not_found id)
+    ))
+    method getSubscription id = self#doReader (lazy (
+      if Hashtbl.mem urls id then
+        Hashtbl.find subscriptions id
+      else
+        raise (Client_not_found id)
+    ))
+
+  end
+
+class tutors =
+  object (self)
+
+    inherit ThreadSafe.threadSafe
+
+    val tbl: (tutor_id, string * hint_type * string) Hashtbl.t =
+      Hashtbl.create 17
+
+    method register id url hint_type dsc = self#doWriter (lazy (
+      if Hashtbl.mem tbl id then
+        raise (Tutor_already_in id)
+      else
+        Hashtbl.add tbl id (url, hint_type, dsc)
+    ))
+    method unregister id = self#doWriter (lazy (
+      if Hashtbl.mem tbl id then
+        Hashtbl.remove tbl id
+      else
+        raise (Tutor_not_found id)
+    ))
+    method isAuthenticated id = self#doReader (lazy (
+      Hashtbl.mem tbl id
+    ))
+    method exists id = self#doReader (lazy (
+      Hashtbl.mem tbl id
+    ))
+    method getTutor id = self#doReader (lazy (
+      if Hashtbl.mem tbl id then
+        Hashtbl.find tbl id
+      else
+        raise (Tutor_not_found id)
+    ))
+    method getUrl id =
+      let (url, _, _) = self#getTutor id in
+      url
+    method getHintType id =
+      let (_, hint_type, _) = self#getTutor id in
+      hint_type
+    method getDescription id =
+      let (_, _, dsc) = self#getTutor id in
+      dsc
+    method index = self#doReader (lazy (
+      Hashtbl.fold
+        (fun id (url, hint_type, dsc) idx -> (id, dsc) :: idx) tbl []
+    ))
+
+  end
+
+class musings =
+  object (self)
+
+    inherit ThreadSafe.threadSafe
+
+    val musings: (musing_id, client_id * tutor_id) Hashtbl.t = Hashtbl.create 17
+    val clients: (client_id, musing_id) Hashtbl.t = Hashtbl.create 17
+    val tutors: (tutor_id, musing_id) Hashtbl.t = Hashtbl.create 17
+
+    method register musing_id client_id tutor_id = self#doWriter (lazy (
+      if Hashtbl.mem musings musing_id then
+        raise (Musing_already_in musing_id)
+      else begin
+        Hashtbl.add musings musing_id (client_id, tutor_id);
+        Hashtbl.add clients client_id musing_id;
+        Hashtbl.add tutors tutor_id musing_id
+      end
+    ))
+    method unregister id = self#doWriter (lazy (
+      if Hashtbl.mem musings id then begin
+        Hashtbl.remove musings id;
+        hashtbl_remove_all clients id;
+        hashtbl_remove_all tutors id
+      end
+    ))
+    method getByMusingId id = self#doReader (lazy (
+      try
+        Hashtbl.find musings id
+      with Not_found -> raise (Musing_not_found id)
+    ))
+    method getByClientId id = self#doReader (lazy (
+      Hashtbl.find_all clients id
+    ))
+    method getByTutorId id = self#doReader (lazy (
+      Hashtbl.find_all tutors id
+    ))
+
+  end
+