Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Common Lisp translations

  • Loading branch information...
commit 15c9936b40fc2be09be0de5eb5fbb3aef5624ce1 1 parent 5cba7c8
@ska80 ska80 authored hintjens committed
View
5 CONTRIBUTORS
@@ -1,5 +1,5 @@
-Authors of Examples
--------------------
+Example Contributors
+--------------------
Pieter Hintjens <ph@imatix.com> (C, Python)
Martin Sustrik <sutrik@imatix.com> (C++)
@@ -11,6 +11,7 @@ Mike Castleman <m@mlcastle.net> (Ruby)
Brian Dorsey <briandorsey@gmail.com> (Python)
Randy Dryburgh <me@rwd.im> (C#)
Eric Desgranges <eric@vcardprocessor.com> (C#)
+Kamil Shakirov <kamils80@gmail.com> (Common Lisp)
Errata and Suggestions
----------------------
View
9 examples/Common Lisp/.gitignore
@@ -0,0 +1,9 @@
+# ignore file
+
+/hwclient
+/hwserver
+/wuclient
+/wuserver
+/zversion
+
+*.fas*
View
21 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
View
18 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))
View
25 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)"
View
43 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))
View
44 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))
View
53 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))
View
51 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))
View
40 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))
View
25 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))
View
22 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"
Please sign in to comment.
Something went wrong with that request. Please try again.