>From 2f6b06f3bb7a9bb7b22efc9de9557c5da6aac178 Mon Sep 17 00:00:00 2001
From: Kamil Shakirov <kamils80@gmail.com>
Date: Fri, 1 Oct 2010 14:25:18 +0700
Subject: [PATCH] Translated hwclient, hwserver, wuclient, wuserver and zversion examples into
 Common Lisp.

---
 examples/Common Lisp/.gitignore    |    9 ++++++
 examples/Common Lisp/build         |   21 ++++++++++++++
 examples/Common Lisp/build.lisp    |   18 ++++++++++++
 examples/Common Lisp/c             |   25 +++++++++++++++++
 examples/Common Lisp/hwclient.lisp |   43 ++++++++++++++++++++++-------
 examples/Common Lisp/hwserver.lisp |   44 +++++++++++++++++++++++-------
 examples/Common Lisp/wuclient.lisp |   53 +++++++++++++++++++++++++++++-------
 examples/Common Lisp/wuserver.lisp |   51 +++++++++++++++++++++++++++-------
 examples/Common Lisp/zhelpers.lisp |   40 +++++++++++++++++++++++++++
 examples/Common Lisp/zversion.lisp |   25 ++++++++++-------
 examples/build                     |   22 +++++++++------
 11 files changed, 292 insertions(+), 59 deletions(-)
 create mode 100644 examples/Common Lisp/.gitignore
 create mode 100755 examples/Common Lisp/build
 create mode 100644 examples/Common Lisp/build.lisp
 create mode 100755 examples/Common Lisp/c
 create mode 100644 examples/Common Lisp/zhelpers.lisp

diff --git a/examples/Common Lisp/.gitignore b/examples/Common Lisp/.gitignore
new file mode 100644
index 0000000..23d0a05
--- /dev/null
+++ b/examples/Common Lisp/.gitignore	
@@ -0,0 +1,9 @@
+# ignore file
+
+/hwclient
+/hwserver
+/wuclient
+/wuserver
+/zversion
+
+*.fas*
diff --git a/examples/Common Lisp/build b/examples/Common Lisp/build
new file mode 100755
index 0000000..61cba15
--- /dev/null
+++ b/examples/Common Lisp/build	
@@ -0,0 +1,21 @@
+#! /bin/sh
+#
+#   Examples build helper
+#   Syntax: build all | clean
+#
+if [ /$1/ = /all/ ]; then
+    echo "Building Common Lisp examples..."
+    for MAIN in `egrep -l main *.lisp`; do
+        ./c `basename $MAIN .lisp`
+    done
+elif [ /$1/ = /clean/ ]; then
+    echo "Cleaning Common Lisp examples directory..."
+    rm -f *.fas* core
+    for MAIN in `egrep -l main *.lisp`; do
+        rm -f `basename $MAIN .lisp`
+    done
+elif [ -f $1.lisp ]; then
+    ./c $1
+else
+    echo "syntax: build all | clean"
+fi
diff --git a/examples/Common Lisp/build.lisp b/examples/Common Lisp/build.lisp
new file mode 100644
index 0000000..5b7e89d
--- /dev/null
+++ b/examples/Common Lisp/build.lisp	
@@ -0,0 +1,18 @@
+;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; -*-
+;;;
+;;;  Application build script
+;;;
+;;; Kamil Shakirov <kamils80@gmail.com>
+;;;
+
+(defpackage #:zguide.build
+  (:nicknames #:build)
+  (:use #:cl)
+  (:export
+   #:build))
+
+(in-package :zguide.build)
+
+(defun build (app-name app-entry)
+  #+sbcl (sb-ext:save-lisp-and-die app-name :executable t :toplevel app-entry)
+  #+ccl (ccl:save-application app-name :prepend-kernel t :toplevel-function app-entry))
diff --git a/examples/Common Lisp/c b/examples/Common Lisp/c
new file mode 100755
index 0000000..e65a401
--- /dev/null
+++ b/examples/Common Lisp/c	
@@ -0,0 +1,25 @@
+#! /bin/sh
+#
+#   c - Common Lisp compile command
+#
+
+if [ ! -n "${LISP+x}" ]; then
+    LISP=sbcl
+fi
+
+LISP_OPTS=
+
+case $LISP in
+    sbcl)
+        LISP_OPTS="--load build.lisp --load zhelpers.lisp --load $1.lisp --eval"
+        ;;
+    ccl)
+        LISP_OPTS="--load build.lisp --load zhelpers.lisp --load $1.lisp --eval"
+        ;;
+    *)
+        echo "'LISP=$LISP' is not supported!"
+        exit 1
+        ;;
+esac
+
+$LISP $LISP_OPTS "(build:build \"$1\" #'$1:main)"
diff --git a/examples/Common Lisp/hwclient.lisp b/examples/Common Lisp/hwclient.lisp
index 0ae6998..e0d005f 100644
--- a/examples/Common Lisp/hwclient.lisp	
+++ b/examples/Common Lisp/hwclient.lisp	
@@ -1,13 +1,36 @@
-No-one has translated the hwclient example into Common Lisp yet.  Be the first to create
-hwclient in Common Lisp and get one free Internet!  If you're the author of the Common Lisp
-binding, this is a great way to get people to use 0MQ in Common Lisp.
+;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; -*-
+;;;
+;;;  Hello World client in Common Lisp
+;;;  Connects REQ socket to tcp://localhost:5555
+;;;  Sends "Hello" to server, expects "World" back
+;;;
+;;; Kamil Shakirov <kamils80@gmail.com>
+;;;
 
-To submit a new translation email it to zeromq-dev@lists.zeromq.org.  Please:
+(defpackage #:zguide.hwclient
+  (:nicknames #:hwclient)
+  (:use #:cl #:zhelpers)
+  (:export #:main))
 
-* Stick to identical functionality and naming used in examples so that readers
-  can easily compare languages.
-* You MUST place your name as author in the examples so readers can contact you.
-* You MUST state in the email that you license your code under the MIT/X11
-  license.
+(in-package :zguide.hwclient)
 
-Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev.
+(defun main ()
+  ;; Prepare our context and socket
+  (zmq:with-context (context 1)
+    (zmq:with-socket (socket context zmq:req)
+      (format t "Connecting to hello world server...~%")
+      (zmq:connect socket "tcp://localhost:5555")
+
+      ;; Do 10 requests, waiting each time for a response
+      (dotimes (request-nbr 10)
+        (let ((request (make-instance 'zmq:msg :data "Hello")))
+          (format t "Sending request ~D...~%" request-nbr)
+          (zmq:send socket request))
+
+        ;; Get the reply
+        (let ((response (make-instance 'zmq:msg)))
+          (zmq:recv socket response)
+          (format t "Received reply ~D: [~A]~%"
+                  request-nbr (zmq:msg-data-as-string response))))))
+
+  (cleanup))
diff --git a/examples/Common Lisp/hwserver.lisp b/examples/Common Lisp/hwserver.lisp
index a3f4344..3371905 100644
--- a/examples/Common Lisp/hwserver.lisp	
+++ b/examples/Common Lisp/hwserver.lisp	
@@ -1,13 +1,37 @@
-No-one has translated the hwserver example into Common Lisp yet.  Be the first to create
-hwserver in Common Lisp and get one free Internet!  If you're the author of the Common Lisp
-binding, this is a great way to get people to use 0MQ in Common Lisp.
+;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; -*-
+;;;
+;;;  Hello World server in Common Lisp
+;;;  Binds REP socket to tcp://*:5555
+;;;  Expects "Hello" from client, replies with "World"
+;;;
+;;; Kamil Shakirov <kamils80@gmail.com>
+;;;
 
-To submit a new translation email it to zeromq-dev@lists.zeromq.org.  Please:
+(defpackage #:zguide.hwserver
+  (:nicknames #:hwserver)
+  (:use #:cl #:zhelpers)
+  (:export #:main))
 
-* Stick to identical functionality and naming used in examples so that readers
-  can easily compare languages.
-* You MUST place your name as author in the examples so readers can contact you.
-* You MUST state in the email that you license your code under the MIT/X11
-  license.
+(in-package :zguide.hwserver)
 
-Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev.
+(defun main ()
+  ;; Prepare our context and socket
+  (zmq:with-context (context 1)
+    (zmq:with-socket (socket context zmq:rep)
+      (zmq:bind socket "tcp://*:5555")
+
+      (loop
+        (let ((request (make-instance 'zmq:msg)))
+          ;; Wait for next request from client
+          (zmq:recv socket request)
+          (format t "Received request: [~A]~%"
+                  (zmq:msg-data-as-string request))
+
+          ;; Do some 'work'
+          (sleep 1)
+
+          ;; Send reply back to client
+          (let ((reply (make-instance 'zmq:msg :data "World")))
+            (zmq:send socket reply))))))
+
+  (cleanup))
diff --git a/examples/Common Lisp/wuclient.lisp b/examples/Common Lisp/wuclient.lisp
index 3204ffb..e60bc32 100644
--- a/examples/Common Lisp/wuclient.lisp	
+++ b/examples/Common Lisp/wuclient.lisp	
@@ -1,13 +1,46 @@
-No-one has translated the wuclient example into Common Lisp yet.  Be the first to create
-wuclient in Common Lisp and get one free Internet!  If you're the author of the Common Lisp
-binding, this is a great way to get people to use 0MQ in Common Lisp.
+;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; -*-
+;;;
+;;;  Weather update client in Common Lisp
+;;;  Connects SUB socket to tcp://localhost:5556
+;;;  Collects weather updates and finds avg temp in zipcode
+;;;
+;;; Kamil Shakirov <kamils80@gmail.com>
+;;;
 
-To submit a new translation email it to zeromq-dev@lists.zeromq.org.  Please:
+(defpackage #:zguide.wuclient
+  (:nicknames #:wuclient)
+  (:use #:cl #:zhelpers)
+  (:export #:main))
 
-* Stick to identical functionality and naming used in examples so that readers
-  can easily compare languages.
-* You MUST place your name as author in the examples so readers can contact you.
-* You MUST state in the email that you license your code under the MIT/X11
-  license.
+(in-package :zguide.wuclient)
 
-Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev.
+(defun main ()
+  (zmq:with-context (context 1)
+    (format t "Collecting updates from weather server...~%")
+
+    ;; Socket to talk to server
+    (zmq:with-socket (subscriber context zmq:sub)
+      (zmq:connect subscriber "tcp://localhost:5556")
+
+      ;; Subscribe to zipcode, default is NYC, 10001
+      (let ((filter (or (nth 1 (cmd-args)) "10001")))
+        (zmq:setsockopt subscriber zmq:subscribe filter)
+
+        ;; Process 100 updates
+        (let ((number-updates 100)
+              (total-temp 0.0))
+
+          (dotimes (update-nbr number-updates)
+            (let ((update (make-instance 'zmq:msg)))
+              (zmq:recv subscriber update)
+
+              (destructuring-bind (zipcode_ temperature relhumidity_)
+                  (split-sequence:split-sequence #\Space (zmq:msg-data-as-string update))
+
+                (declare (ignore zipcode_ relhumidity_))
+                (incf total-temp (parse-integer temperature)))))
+
+          (format t "Average temperature for zipcode ~A was ~FF~%"
+                  filter (/ total-temp number-updates))))))
+
+  (cleanup))
diff --git a/examples/Common Lisp/wuserver.lisp b/examples/Common Lisp/wuserver.lisp
index 6189023..a8b2637 100644
--- a/examples/Common Lisp/wuserver.lisp	
+++ b/examples/Common Lisp/wuserver.lisp	
@@ -1,13 +1,44 @@
-No-one has translated the wuserver example into Common Lisp yet.  Be the first to create
-wuserver in Common Lisp and get one free Internet!  If you're the author of the Common Lisp
-binding, this is a great way to get people to use 0MQ in Common Lisp.
+;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; -*-
+;;;
+;;;  Weather update server in Common Lisp
+;;;  Binds PUB socket to tcp://*:5556
+;;;  Publishes random weather updates
+;;;
+;;; Kamil Shakirov <kamils80@gmail.com>
+;;;
 
-To submit a new translation email it to zeromq-dev@lists.zeromq.org.  Please:
+(defpackage #:zguide.wuserver
+  (:nicknames #:wuserver)
+  (:use #:cl #:zhelpers)
+  (:export #:main))
 
-* Stick to identical functionality and naming used in examples so that readers
-  can easily compare languages.
-* You MUST place your name as author in the examples so readers can contact you.
-* You MUST state in the email that you license your code under the MIT/X11
-  license.
+(in-package :zguide.wuserver)
 
-Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev.
+(defun within (num)
+  (1+ (random num)))
+
+(defun main ()
+  ;; Prepare our context and socket
+  (zmq:with-context (context 1)
+    (zmq:with-socket (publisher context zmq:pub)
+      (zmq:bind publisher "tcp://*:5556")
+      (zmq:bind publisher "ipc://weather.ipc")
+
+      (loop
+        ;; Get values that will fool the boss
+        (let ((zipcode (within 100000))
+              (temperature (- (within 215) 80))
+              (relhumidity (+ (within 50) 10)))
+
+          ;; Send message to all subscribers
+          (let ((message
+                 (make-instance 'zmq:msg
+                                :data (format nil "~5,'0D ~D ~D"
+                                              zipcode
+                                              temperature
+                                              relhumidity))))
+
+            ;; Send message to all subscribers
+            (zmq:send publisher message))))))
+
+  (cleanup))
diff --git a/examples/Common Lisp/zhelpers.lisp b/examples/Common Lisp/zhelpers.lisp
new file mode 100644
index 0000000..61373ef
--- /dev/null
+++ b/examples/Common Lisp/zhelpers.lisp	
@@ -0,0 +1,40 @@
+;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; -*-
+;;;
+;;;  Helpers for example applications
+;;;
+;;; Kamil Shakirov <kamils80@gmail.com>
+;;;
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (require :split-sequence)
+  (require :zeromq))
+
+(defpackage #:zguide.zhelpers
+  (:nicknames #:zhelpers)
+  (:use #:cl)
+  (:export
+   #:version
+   #:cleanup
+   #:cmd-args))
+
+(in-package :zguide.zhelpers)
+
+(defun version ()
+  (format t "Current 0MQ version is ~A~%" (zmq:version)))
+
+(defun cleanup ()
+  (tg:gc)
+  #+sbcl (sb-ext:quit)
+  #+ccl (ccl:quit)
+  #+clisp (ext:quit)
+  #+lispworks (lispworks:quit)
+  #+ecl (ext:quit))
+
+(defun cmd-args ()
+  (or
+   #+sbcl sb-ext:*posix-argv*
+   #+ccl ccl:*command-line-argument-list*
+   #+clisp ext:*args*
+   #+lispworks system:*line-arguments-list*
+   #+ecl (ext:command-args)
+   nil))
diff --git a/examples/Common Lisp/zversion.lisp b/examples/Common Lisp/zversion.lisp
index fd8061a..b7be2b7 100644
--- a/examples/Common Lisp/zversion.lisp	
+++ b/examples/Common Lisp/zversion.lisp	
@@ -1,13 +1,18 @@
-No-one has translated the zversion example into Common Lisp yet.  Be the first to create
-zversion in Common Lisp and get one free Internet!  If you're the author of the Common Lisp
-binding, this is a great way to get people to use 0MQ in Common Lisp.
+;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; -*-
+;;;
+;;;  Hello World client in Common Lisp
+;;;  Connects REQ socket to tcp://localhost:5555
+;;;  Sends "Hello" to server, expects "World" back
+;;;
+;;; Kamil Shakirov <kamils80@gmail.com>
+;;;
 
-To submit a new translation email it to zeromq-dev@lists.zeromq.org.  Please:
+(defpackage #:zguide.zversion
+  (:nicknames #:zversion)
+  (:use #:cl #:zhelpers)
+  (:export #:main))
 
-* Stick to identical functionality and naming used in examples so that readers
-  can easily compare languages.
-* You MUST place your name as author in the examples so readers can contact you.
-* You MUST state in the email that you license your code under the MIT/X11
-  license.
+(in-package :zguide.zversion)
 
-Subscribe to the email list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev.
+(defun main ()
+  (version))
diff --git a/examples/build b/examples/build
index b3179df..4a0242e 100755
--- a/examples/build
+++ b/examples/build
@@ -3,18 +3,22 @@
 #   Examples build script
 #   Syntax: build all | clean
 #
-LANGUAGES="c c++"
+
 if [ /$1/ = /all/ ]; then
-    for LANGUAGE in $LANGUAGES; do
-        cd $LANGUAGE
-        ./build all
-        cd ..
+    find . -mindepth 1 -type d | while read LANGUAGE; do
+        if [ -f "$LANGUAGE"/build ]; then
+            cd "$LANGUAGE"
+            ./build all
+            cd ..
+        fi
     done
 elif [ /$1/ = /clean/ ]; then
-    for LANGUAGE in $LANGUAGES; do
-        cd $LANGUAGE
-        ./build clean
-        cd ..
+    find . -mindepth 1 -type d | while read LANGUAGE; do
+        if [ -f "$LANGUAGE"/build ]; then
+            cd "$LANGUAGE"
+            ./build clean
+            cd ..
+        fi
     done
 else
     echo "syntax: build all | clean"
-- 
1.7.0.4

