Created
September 23, 2010 07:35
-
-
Save fred-o/593289 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
(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