Skip to content

Commit

Permalink
removed more complicated read and updated cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Damon Snyder committed Nov 28, 2010
1 parent 490b0c1 commit 3cc6056
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
38 changes: 14 additions & 24 deletions src/beanstalk/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,38 @@
; ([port] (java.net.Socket. "localhost" port))
; ([host port] (java.net.Socket. host port)))

(defn create [host port]
(defn- beanstalk-create [host port]
(let [s (java.net.Socket. "localhost" port)]
(struct-map Beanstalk
:socket s
:reader (reader s)
:writer (writer s)
)))

(defn- beanstalk-cmd [s & args]
(if (nil? args)
(str s \return \newline)
(str s " " (.concat (str (reduce #(str %1 " " %2) args))
(str \return \newline)))))

(defn beanstalk
([port] (create "localhost" port))
([host port] (create host port))
([] (create "localhost" 11300)))
([port] (beanstalk-create "localhost" port))
([host port] (beanstalk-create host port))
([] (beanstalk-create "localhost" 11300)))

(defn beanstalk-close [b]
(.close (bsock b)))

(defn beanstalk-write [b msg]
(let [w (bwriter b)]
(do
(. w write msg)
(. w flush))) b)


(defn beanstalk-read [b]
(let [r (breader b)
sb (StringBuilder.)]
(loop [c (.read r)]
(cond
(neg? c) (str sb)
(and (= \newline (char c))
(> (.length sb) 1)
(= (char (.charAt sb (- (.length sb) 1) )) \return))
(str (.substring sb 0 (- (.length sb) 1)))
true (do (.append sb (char c))
(recur (.read r)))))))
(do (. w write (beanstalk-cmd msg)) (. w flush))) b)

(defn beanstalk-read [b]
(binding [*in* (breader b)]
(read-line)))


(def b (beanstalk 11300))
(beanstalk-write b "stats\r\n")
(beanstalk-read b)
;(def b (beanstalk 11300))
;(beanstalk-write b "stats")
;(beanstalk-read b)

4 changes: 2 additions & 2 deletions test/beanstalk/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

(deftest test-beanstalk-write
(let [b (beanstalk)]
(is (not (nil? (beanstalk-write b "stats\r\n"))))))
(is (not (nil? (beanstalk-write b "stats"))))))

(deftest test-beanstalk-read
(is (= "OK" (re-find #"^OK"
(beanstalk-read
(beanstalk-write
(beanstalk) "stats\r\n"))))))
(beanstalk) "stats"))))))

0 comments on commit 3cc6056

Please sign in to comment.