1. yongchao li
  2. clj-test

Commits

yongchao li  committed 20ed9cc

new code

  • Participants
  • Parent commits 7b36002
  • Branches master

Comments (0)

Files changed (3)

File clj-memcached.clj

View file
  • Ignore whitespace
+(ns irc
+  (:import (java.net Socket)
+           (java.io PrintWriter InputStreamReader BufferedReader)))
+
+(def freenode {:name "irc.freenode.net" :port 6667})
+(def user {:name "Nurullah Akkaya" :nick "nakkaya"})
+
+(declare conn-handler)
+
+(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)))))))
+
+(defn login [conn user]
+  (write conn (str "NICK " (:nick user)))
+  (write conn (str "USER " (:nick user) " 0 * :" (:name user))))
+
+(def irc (connect freenode))
+(login irc user)
+(write irc "JOIN #clojure")
+(write irc "QUIT")

File coffee-time/client.clj

View file
  • Ignore whitespace
+(ns coffee-time.client)
+
+(import '(java.net Socket)
+        '(java.io PrintWriter InputStreamReader BufferedReader))
+
+(def socket (atom nil))
+(def in (atom nil))
+(def out (atom nil))
+
+(defn start-client [port]
+  (do
+    (reset! socket (Socket. "localhost" port))
+    (reset! out (PrintWriter. (. @socket getOutputStream) true))
+    (reset! in (BufferedReader. (InputStreamReader. (. @socket getInputStream))))))
+
+(defn send-message [m]
+  (do
+    (. @out println m)
+    (println (. @in readLine))))
+
+(defn stop-client []
+  (do (. @in close)
+      (. @out close)
+      (. @socket close)))
+
+(start-client 1234)

File coffee-time/server.clj

View file
  • Ignore whitespace
+(ns coffee-time.server)
+(import '(java.util StringTokenizer)
+        '(java.net ServerSocket Socket SocketException)
+        '(java.io PrintWriter InputStreamReader BufferedReader))
+
+(def storage (atom {}))
+(def exit? (atom false))
+(def parsing-re #"([A-Z]+)\s*(\S+)\s*(\S*)")
+
+(defn accept-connection [server-socket]
+  (try (. server-socket accept)
+       (catch SocketException e)))
+
+(defmulti operate (fn[command & _]
+                              (keyword command)))
+
+(defmethod operate :GET [c key & _]
+  (if (contains? @storage key)
+    (str key " = " (@storage key))
+    "No such key found"))
+
+(defmethod operate :PUT [c key value & _]
+  (do (swap! storage assoc key value)
+      (str "Value of " key " added")))
+
+(defmethod operate :DELETE [c key & _]
+  (if (contains? @storage key)
+    (do (swap! storage dissoc key)
+        (str "Value of " key " deleted"))
+    "No such key found"))
+
+(defmethod operate :EXIT [& _]
+  (reset! exit? true))
+
+(defmethod operate :default [& _]
+  "Wrong command")
+
+(defn parse-and-operate [s]
+  (->> s
+       (re-find parsing-re)
+       rest
+       (apply operate)))
+
+
+(defn start-server [port]
+  (with-open [s-socket (new ServerSocket port)
+              c-socket (accept-connection s-socket)
+              in (new BufferedReader (new InputStreamReader (. c-socket getInputStream)))
+              out (new PrintWriter (. c-socket getOutputStream) true)]
+             (do (reset! exit? false)
+                 (while (not @exit?)
+                   (->> (. in readLine)
+                        parse-and-operate
+                        (. out println))))))
+(start-server 1234)