From 9612b589a3c5e8dfa07baabc3cefb7db4722d49e Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Wed, 21 Dec 2011 16:51:41 -0800 Subject: [PATCH] moving all services to keyword, remove java md client/worker implementation --- README.md | 6 +++--- src/clj/md_clj/client.clj | 8 ++++---- src/clj/md_clj/worker.clj | 2 +- src/jvm/mdclient.java | 30 ------------------------------ src/jvm/mdclient2.java | 34 ---------------------------------- src/jvm/mdworker.java | 27 --------------------------- test/md_clj/test/echo.clj | 12 ++++++------ test/md_clj/test/http.clj | 6 +++--- test/md_clj/test/reverse.clj | 18 ++++++++++++------ 9 files changed, 29 insertions(+), 114 deletions(-) delete mode 100644 src/jvm/mdclient.java delete mode 100644 src/jvm/mdclient2.java delete mode 100644 src/jvm/mdworker.java diff --git a/README.md b/README.md index 1aaf79f..4a99322 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ Majordomo API already written in they guide, but any language that has See the tests. Most are written as useful examples: * echo - the standard echo scenario served up a few different ways -* reverse - standard and DSL APIs to create some reverse worker examples +* reverse - standard and DSL APIs to create some reverse worker examples, lots of comments too * http - an http frontended parallel echo system ## TODO @@ -150,7 +150,7 @@ See the tests. Most are written as useful examples: ## License -Copyright (C) 2011 Josh Rotenberg -Portions (C) 2011 Arkadiusz Orzechowski +* Copyright (C) 2011 Josh Rotenberg +* Portions (C) 2011 Arkadiusz Orzechowski Distributed under the Eclipse Public License, the same as Clojure. diff --git a/src/clj/md_clj/client.clj b/src/clj/md_clj/client.clj index ad2e712..07860ae 100644 --- a/src/clj/md_clj/client.clj +++ b/src/clj/md_clj/client.clj @@ -23,23 +23,23 @@ (defmulti send! (fn [client service request] (class request))) (defmethod send! ZMsg [client service request] - (.send (:client client) service request)) + (.send (:client client) (name service) request)) (defmethod send! String [client service request] (let [r (ZMsg.) _ (.add r (ZFrame. request))] - (.send (:client client) service r))) + (.send (:client client) (name service) r))) (defmethod send! (Class/forName "[B") [client service request] (let [r (ZMsg.) _ (.add r (ZFrame. request))] - (.send (:client client) service r))) + (.send (:client client) (name service) r))) (defmethod send! ::collection [client service request] (let [r (ZMsg.)] (doseq [s request] (.add r (ZFrame. s))) - (.send (:client client) service r))) + (.send (:client client) (name service) r))) (defn recv "Receive from an asynchronous request." diff --git a/src/clj/md_clj/worker.clj b/src/clj/md_clj/worker.clj index 25ae1bb..4a192ea 100644 --- a/src/clj/md_clj/worker.clj +++ b/src/clj/md_clj/worker.clj @@ -7,7 +7,7 @@ (defn new-worker [service endpoint function] - (Worker. service endpoint *worker-debug* function)) + (Worker. (name service) endpoint *worker-debug* function)) (defn run "Run the worker." diff --git a/src/jvm/mdclient.java b/src/jvm/mdclient.java deleted file mode 100644 index 02ee53a..0000000 --- a/src/jvm/mdclient.java +++ /dev/null @@ -1,30 +0,0 @@ -import org.zeromq.ZMsg; - -/** - * Majordomo Protocol client example. Uses the mdcli API to hide all MDP aspects - * - * @author Arkadiusz Orzechowski - * - */ -public class mdclient { - - public static void main(String[] args) { - boolean verbose = (args.length > 0 && "-v".equals(args[0])); - mdcliapi clientSession = new mdcliapi("tcp://localhost:5555", verbose); - - int count; - for (count = 0; count < 100000; count++) { - ZMsg request = new ZMsg(); - request.addString("Hello world"); - ZMsg reply = clientSession.send("echo", request); - if (reply != null) - reply.destroy(); - else - break; // Interrupt or failure - } - - System.out.printf("%d requests/replies processed\n", count); - clientSession.destroy(); - } - -} diff --git a/src/jvm/mdclient2.java b/src/jvm/mdclient2.java deleted file mode 100644 index 62c6016..0000000 --- a/src/jvm/mdclient2.java +++ /dev/null @@ -1,34 +0,0 @@ -import org.zeromq.ZMsg; - -/** - * Majordomo Protocol client example, asynchronous. Uses the mdcli API to hide - * all MDP aspects - * - * @author Arkadiusz Orzechowski - * - */ -public class mdclient2 { - - public static void main(String[] args) { - boolean verbose = (args.length > 0 && "-v".equals(args[0])); - mdcliapi2 clientSession = new mdcliapi2("tcp://localhost:5555", verbose); - - int count; - for (count = 0; count < 100000; count++) { - ZMsg request = new ZMsg(); - request.addString("Hello world"); - clientSession.send("echo", request); - } - for (count = 0; count < 100000; count++) { - ZMsg reply = clientSession.recv(); - if (reply != null) - reply.destroy(); - else - break; // Interrupt or failure - } - - System.out.printf("%d requests/replies processed\n", count); - clientSession.destroy(); - } - -} diff --git a/src/jvm/mdworker.java b/src/jvm/mdworker.java deleted file mode 100644 index 7f220f5..0000000 --- a/src/jvm/mdworker.java +++ /dev/null @@ -1,27 +0,0 @@ -import org.zeromq.ZMsg; - -/** - * Majordomo Protocol worker example. Uses the mdwrk API to hide all MDP aspects - * - * @author Arkadiusz Orzechowski - * - */ -public class mdworker { - - /** - * @param args - */ - public static void main(String[] args) { - boolean verbose = (args.length > 0 && "-v".equals(args[0])); - mdwrkapi workerSession = new mdwrkapi("tcp://localhost:5555", "echo", verbose); - - ZMsg reply = null; - while (!Thread.currentThread().isInterrupted()) { - ZMsg request = workerSession.receive(reply); - if (request == null) - break; //Interrupted - reply = request; // Echo is complex... :-) - } - workerSession.destroy(); - } -} diff --git a/test/md_clj/test/echo.clj b/test/md_clj/test/echo.clj index 8edc200..75c86fb 100644 --- a/test/md_clj/test/echo.clj +++ b/test/md_clj/test/echo.clj @@ -22,7 +22,7 @@ (deftest echo-test (let [echo-worker (mdw/new-worker - "echo" "tcp://localhost:5555" + :echo "tcp://localhost:5555" (fn [request reply] (doall (map #(.add reply %) (.toArray request))))) echo-client (mdc/new-client "tcp://localhost:5555")] @@ -30,7 +30,7 @@ (future (mdw/run echo-worker)) (time (doseq [x random-strings] - (let [reply (mdc/send! echo-client "echo" x)] + (let [reply (mdc/send! echo-client :echo x)] (is (= x (-> (.toArray reply) first @@ -41,7 +41,7 @@ ;; all the results (deftest echo-async-test (let [echo-async-worker (mdw/new-worker - "echo-async" "tcp://localhost:5555" + :echo-async "tcp://localhost:5555" (fn [request reply] (doall (map #(.add reply %) (.toArray request))))) echo-async-client (mdc/new-client "tcp://localhost:5555" true)] @@ -51,7 +51,7 @@ (doseq [x random-strings] (let [request (ZMsg.) _ (.addString request x)] - (mdc/send! echo-async-client "echo-async" request))) + (mdc/send! echo-async-client :echo-async request))) (doseq [x random-strings] (let [reply (mdc/recv echo-async-client)] @@ -63,7 +63,7 @@ (deftest echo-multi-test (let [echo-workers (repeat 10 (mdw/new-worker - "echo-multi" + :echo-multi "tcp://localhost:5555" (fn [request reply] ;;(Thread/sleep 500) @@ -76,7 +76,7 @@ (time (doseq [x random-strings] - (let [reply (mdc/send! echo-client "echo-multi" x)] + (let [reply (mdc/send! echo-client :echo-multi x)] (is (= x (-> (.toArray reply) first .getData diff --git a/test/md_clj/test/http.clj b/test/md_clj/test/http.clj index 3d46352..4c1ffe2 100644 --- a/test/md_clj/test/http.clj +++ b/test/md_clj/test/http.clj @@ -21,7 +21,7 @@ ;; the echo worker itself (def echo-http-worker - (mdw/new-worker "echo-http" "tcp://localhost:5555" echo-handler)) + (mdw/new-worker :echo-http "tcp://localhost:5555" echo-handler)) ;; the client (def echo-http-md-client (mdc/new-client "tcp://localhost:5555")) @@ -35,7 +35,7 @@ (let [body (slurp* (:body request)) request (ZMsg.) _ (.addString request body) - reply (mdc/send! echo-http-md-client "echo-http" request)] + reply (mdc/send! echo-http-md-client :echo-http request)] (-> (.toArray reply) first .getData @@ -58,7 +58,7 @@ (doseq [x random-strings] (let [request (ZMsg.) _ (.addString request x) - reply (mdc/send! echo-http-md-client "echo-http" request)] + reply (mdc/send! echo-http-md-client :echo-http request)] (is (= x (-> (.toArray reply) first .getData diff --git a/test/md_clj/test/reverse.clj b/test/md_clj/test/reverse.clj index 2500fc0..8406926 100644 --- a/test/md_clj/test/reverse.clj +++ b/test/md_clj/test/reverse.clj @@ -17,11 +17,11 @@ (deftest reverse-test (let [reverse-worker (mdw/new-worker - "reverse" "tcp://localhost:5555" reverse-fn) + :reverse "tcp://localhost:5555" reverse-fn) reverse-client (mdc/new-client "tcp://localhost:5555")] (future (mdw/run reverse-worker)) - (let [reply (mdc/send! reverse-client "reverse" ["bar" "foo"])] + (let [reply (mdc/send! reverse-client :reverse ["bar" "foo"])] (is (= "rab" (-> (.toArray reply) first .getData @@ -70,14 +70,20 @@ "two"]) ;; async clients work similarly with the following exceptions: ;; each element in the collection is sent independently, and all - ;; elements are sent immediately. then the results are collected - ;; and returned in order. + ;; elements are sent before any responses are fetched. responses + ;; should be in the same order requests were sent. this is a handy + ;; way to send a bunch of single item requests at once, but of course + ;; you can also make a worker than handles multiple items at the same + ;; time ;; makes two async requests, one for boof and one for chuh. once ;; both have been sent, calls recv and collects/returns the response reply-one-async (mdc/as-client-async :reverse-one ep ["boof" "chuh"]) - ;; makes three async requests, one for duh, one for [boof], and - ;; one for [what, now]. see the note above regarding workers that + + ;; makes three async requests, one for duh, one for [boof], + ;; and one for [what, now]. this is a decent way to send a + ;; batch of requests that may contain multiple items. + ;; see the note above regarding workers that ;; may need to handle requests with one or more items. reply-more-async (mdc/as-client-async :reverse-more ep ["duh"