Created
April 6, 2010 21:17
-
-
Save devn/358134 to your computer and use it in GitHub Desktop.
This file contains 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
(defn connect [server] | |
(let [socket (Socket. (:name server) (:port server)) | |
in (BufferedReader. (InputStreamReader. (.getInputStream socket))) | |
out (PrintWriter. (.getOutputStream socket)) | |
conn (ref {:in in :out out})] | |
(doto (Thread. #(conn-handler conn)) (.start)) | |
conn)) | |
(defn write [conn msg] | |
(doto (:out @conn) | |
(.println (str msg "\r")) | |
(.flush))) | |
(defn conn-handler [conn] | |
(while | |
(nil? (:exit @conn)) | |
(let [msg (.readLine (:in @conn))] | |
(println msg) | |
(cond | |
(re-find #"^ERROR :Closing Link:" msg) | |
(dosync (alter conn merge {:exit true})) | |
(re-find #"^PING" msg) | |
(write conn (str "PONG " (re-find #":.*" msg))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment