Created
September 16, 2010 14:46
-
-
Save bhenry/582538 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns qa.deaf | |
(:use [clojure.contrib | |
[server-socket :only [create-server]] | |
[duck-streams :only [reader writer]]])) | |
(def port 6662) | |
(def count-BYE (ref 0)) | |
(defn listener [input] | |
(cond (= input "BYE") | |
(cond (= 0 @count-BYE) "HI?" | |
(= 1 @count-BYE) "PIE?" | |
(= 2 @count-BYE) "BYE!") | |
(= input (.toUpperCase input)) | |
(format "Not since %d" (+ 1930 (int (rand 20)))) | |
:else | |
(get ["Speak up!" "What?" "Eh?!"] (int (rand 3))))) | |
(defn handle-client [in out] | |
(binding [*in* (reader in) | |
*out* (writer out)] | |
(loop [r (str (read-line))] | |
(println (listener r)) | |
(dosync (if (= r "BYE") | |
(alter count-BYE inc) | |
(ref-set count-BYE 0))) | |
(if (< @count-BYE 3) | |
(recur (str (read-line))))))) | |
(defonce srvr (create-server port #'handle-client)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment