diff chat/room.clj @ 2:b4de894a1e2e

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 28 Oct 2011 00:03:05 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/chat/room.clj	Fri Oct 28 00:03:05 2011 -0700
     1.3 @@ -0,0 +1,34 @@
     1.4 +(ns chat.room
     1.5 +  (:use [clojure.contrib server-socket duck-streams str-utils])
     1.6 +  (:gen-class))
     1.7 +
     1.8 +
     1.9 +;; CHAT ROOMS
    1.10 +
    1.11 +(def rooms (ref {}))
    1.12 +
    1.13 +(defn get-room! "Returns the chat room with the given name, creating it if necessary." [name]
    1.14 +  (dosync
    1.15 +   (or (@rooms name)
    1.16 +       (let [new-room (agent '())]
    1.17 +	 (do (alter *rooms* assoc name new-room))))))
    1.18 +
    1.19 +(defn say-in-room[room message]
    1.20 +  (doseq [[_ output] room]
    1.21 +    (binding [*out* output]
    1.22 +      (println message))))
    1.23 +
    1.24 +
    1.25 +;; USERS
    1.26 +
    1.27 +(defn user-join[room username output-channel]
    1.28 +  (conj room [username output-channel]))
    1.29 +(defn user-leave[room username]
    1.30 +  (remove #(= (% 0) username) room))
    1.31 +
    1.32 +
    1.33 +(def *username* "Someone")
    1.34 +(defn- join-room [room]
    1.35 +  (send room user-join *username* *out*)
    1.36 +  (send room 
    1.37 +  )