Skip to content

Instantly share code, notes, and snippets.

@fred-o
Created September 23, 2010 07:35
Show Gist options
  • Save fred-o/593289 to your computer and use it in GitHub Desktop.
Save fred-o/593289 to your computer and use it in GitHub Desktop.
(ns zmq-test.core
(:import [java.io InputStream])
(:use [org.zeromq.clojure]))
(def *ctx* (make-context 0))
(defn serialize [form]
(.getBytes (with-out-str (pr form))))
(defn deserialize [bytes]
(with-in-str (String. bytes)
(read)))
;; server
(future (let [s (make-socket *ctx* +rep+)]
(bind s "inproc://call")
(loop [msg (recv s)]
(println (str "Received: " (String. msg)))
(send- s (serialize (eval (deserialize msg))))
(recur (recv s)))))
;; client
(defn make-client [addr]
(doto (make-socket *ctx* +req+)
(connect addr)))
(defmacro on [cl form]
`(do (send- ~cl (serialize '~form))
(deserialize (recv ~cl))))
;; example usage
(def *client* (make-client "inproc://call"))
(on *client* (+ 2 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment