Created
May 24, 2019 10:15
-
-
Save phoe/e99b9d0b9df9968c564eadde56aeef00 to your computer and use it in GitHub Desktop.
ZeroMQ pzmq sample router/dealer in Common Lisp
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
;; modified from pzmq examples | |
(defun hwclient (&optional (server-address "tcp://localhost:5555")) | |
"Translation of http://zguide.zeromq.org/c:hwclient updated for ZMQ 3. | |
Includes some parameters in with-* macros to demonstrate syntax." | |
(pzmq:with-context (ctx :max-sockets 10) | |
(pzmq:with-socket (requester ctx) (:dealer :affinity 3 :linger 100) | |
;; linger is important in case of (keyboard) interrupt; | |
;; see http://api.zeromq.org/3-3:zmq-ctx-destroy | |
(pzmq:connect requester server-address) | |
(dotimes (i 10) | |
(format t "Sending Hello ~d...~%" i) | |
(pzmq:send requester "Hello!" :dontwait nil) | |
(write-string "Receiving... ") | |
(write-line (pzmq:recv-string requester)))))) | |
(defun hwserver (&optional (listen-address "tcp://*:5555")) | |
"Translation of http://zguide.zeromq.org/c:hwserver updated for ZMQ 3. " | |
(pzmq:with-context nil ; use *default-context* | |
(pzmq:with-socket responder :router | |
(pzmq:bind responder listen-address) | |
(dotimes (i 10) | |
(pzmq:with-message msg | |
(pzmq:msg-recv msg responder) | |
(let ((identity (cffi:foreign-array-to-lisp | |
(pzmq:msg-data msg) `(:array :unsigned-char 5) | |
:element-type '(unsigned-byte 8)))) | |
(print identity) | |
(pzmq:msg-recv msg responder) | |
(let ((hello (cffi:foreign-string-to-lisp | |
(pzmq:msg-data msg) | |
:count (pzmq:msg-size msg)))) | |
(write-line hello)) | |
(cffi:with-pointer-to-vector-data (pointer identity) | |
(pzmq:send responder pointer :len 5 :sndmore t)) | |
(pzmq:send responder "World!") | |
(write-line "Sent response..."))))))) | |
;; run (hwclient) and (hwserver) on two different Lisp threads/instances on the same machine |
Author
phoe
commented
May 28, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment