Created
August 9, 2012 04:13
-
-
Save drsnyder/3300872 to your computer and use it in GitHub Desktop.
Protocol with CRLF termination.
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
(def C (string :utf-8 :delimiters " ")) | |
(def K (string :utf-8 :delimiters " ")) | |
(def V (string :utf-8 :delimiters " ")) | |
(def CR (string :utf-8 :delimiters ["\r\n"])) | |
defcodec SET ["SET" CR]) | |
(defcodec STORED ["STORED" CR]) | |
;STORED\r\n | |
(defcodec GET ["GET" CR]) | |
(defcodec GETS ["GETS" CR]) | |
; GET <keys>*\r\n | |
; GETS <keys>*\r\n | |
(defcodec VALUE ["VALUE" CR CR]) | |
(defcodec END ["END" CR]) | |
;VALUE <key> <flags> <bytes> [<cas unique>]\r\n | |
;<data block>\r\n | |
;... | |
;END\r\n | |
(defcodec ERRC (string :utf-8)) | |
(defcodec CMDS | |
(header C | |
(fn [h] | |
(case h | |
"SET" SET | |
"GET" GET | |
"GETS" GETS | |
"STORED" STORED) | |
"VALUE" VALUE) | |
"END" END | |
ERRC))) | |
first)) | |
(defn test-handle [ch ci cmd] | |
(condp = (first cmd) | |
"SET" (enqueue ch ["STORED"]) | |
"GET" (do (enqueue ch ["VALUE" "abc" "123"]) (enqueue ch ["END" ""])) | |
"GETS" (do (enqueue ch ["VALUE" "abc" "123"]) (enqueue ch ["END" ""])) | |
(enqueue ch "error"))) | |
(defn handler | |
[ch ci] | |
(receive-all ch (partial test-handle ch ci))) | |
(def s (start-tcp-server handler {:port 10000 :frame CMDS})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment