diff demo/broadcast.ur @ 699:4e260887d8f2

Chat demo
author Adam Chlipala <adamc@hcoop.net>
date Sun, 05 Apr 2009 11:48:55 -0400
parents
children 1a317a707d71
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demo/broadcast.ur	Sun Apr 05 11:48:55 2009 -0400
@@ -0,0 +1,28 @@
+functor Make(M : sig type t end) = struct
+    sequence s
+    table t : {Id : int, Client : client, Channel : channel M.t}
+
+    type topic = int
+
+    val inj : sql_injectable topic = _
+
+    val create = nextval s
+
+    fun subscribe id =
+        cli <- self;
+        ro <- oneOrNoRows (SELECT t.Channel FROM t WHERE t.Id = {[id]} AND t.Client = {[cli]});
+        case ro of
+            None =>
+            ch <- channel;
+            dml (INSERT INTO t (Id, Client, Channel) VALUES ({[id]}, {[cli]}, {[ch]}));
+            return ch
+          | Some r => return r.T.Channel
+
+    fun send id msg =
+        queryI (SELECT t.Channel FROM t WHERE t.Id = {[id]})
+        (fn r => Basis.send r.T.Channel msg)
+
+    fun subscribers id =
+        r <- oneRow (SELECT COUNT( * ) AS N FROM t WHERE t.Id = {[id]});
+        return r.N
+end