Guest User

ZeroMQ Common Lisp

a guest
Oct 1st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 5.65 KB | None | 0 0
  1. -- lisp-zmq on Clozure CL: all the functions from its "beginner's guide" fail like this
  2.  
  3. CL-USER> (ql:quickload "zmq")
  4. To load "zmq":
  5.   Load 1 ASDF system:
  6.     zmq
  7. ; Loading "zmq"
  8.  
  9. ("zmq")
  10. CL-USER> (defun publisher ()
  11.   (zmq:with-context (context 1)
  12.     (zmq:with-socket (socket context :pub)
  13.       (zmq:bind socket "tcp://*:8999")
  14.       (loop (let ((data-string (format nil "~d" (random 100))))
  15.               (zmq:with-msg-init-data (message data-string)
  16.                 (zmq:send socket message))
  17.               (format t "Publisher sent ~a~%" data-string))))))
  18. PUBLISHER
  19. CL-USER> (publisher)
  20. NIL is not of type (OR SYMBOL
  21.                        FUNCTION), and can't be FUNCALLed or APPLYed
  22.    [Condition of type TYPE-ERROR]
  23.  
  24. Restarts:
  25.  0: [RETRY] Retry SLIME REPL evaluation request.
  26.  1: [*ABORT] Return to SLIME's top level.
  27.  2: [ABORT-BREAK] Reset this thread
  28.  3: [ABORT] Kill this thread
  29.  
  30. Backtrace:
  31.   0: (ZMQ::CALL-FFI -1 NIL #<A Foreign Pointer #x7F2F98015550> #<A Foreign Pointer [stack-allocated] #x7F2FBCA19B60> 0)
  32.   1: (PUBLISHER)
  33.   2: (CCL::CALL-CHECK-REGS PUBLISHER)
  34.   3: (CCL::CHEAP-EVAL (PUBLISHER))
  35.  
  36. -- cl-zmq on sbcl: I try to compile the two first functions from the example at http://www.cliki.net/cl-zmq but they fail
  37.  
  38. CL-USER> (ql:quickload "zeromq")
  39. To load "zeromq":
  40.   Load 1 ASDF system:
  41.     zeromq
  42. ; Loading "zeromq"
  43.  
  44. ("zeromq")
  45. CL-USER> (defun client ()
  46.   "Connects to a socket and passes a message and waits to receive the
  47.  \"OK\" from the server."
  48.   (zmq:with-context (ctx)
  49.     (zmq:with-socket (socket ctx zmq:req)
  50.       (zmq:connect socket "tcp://localhost:5555")
  51.       (zmq:send socket (make-instance 'zmq:msg
  52.                                       :data "Hello" ))
  53.       (let ((result (make-instance 'zmq:msg)))
  54.         (zmq:recv socket result)
  55.         (format t "Received string: '~A'~%"
  56.                 (zmq:msg-data-as-string result) )))))
  57. ; in: DEFUN CLIENT
  58. ;     (ZEROMQ:WITH-SOCKET (SOCKET CTX ZEROMQ:REQ)
  59. ;       (ZEROMQ:CONNECT SOCKET "tcp://localhost:5555")
  60. ;       (ZEROMQ:SEND SOCKET (MAKE-INSTANCE 'ZEROMQ:MSG :DATA "Hello"))
  61. ;       (LET ((RESULT (MAKE-INSTANCE 'ZEROMQ:MSG)))
  62. ;         (ZEROMQ:RECV SOCKET RESULT)
  63. ;         (FORMAT T "Received string: '~A'~%" (ZEROMQ:MSG-DATA-AS-STRING RESULT))))
  64. ; --> LET
  65. ; ==>
  66. ;   (ZEROMQ:SOCKET CTX ZEROMQ:REQ)
  67. ;
  68. ; caught WARNING:
  69. ;   undefined variable: ZEROMQ:REQ
  70. ;
  71. ; compilation unit finished
  72. ;   Undefined variable:
  73. ;     ZEROMQ:REQ
  74. ;   caught 1 WARNING condition
  75. CLIENT
  76. CL-USER> (defun server ()
  77.   "Bind to socket and wait to receive a message.  After receipt,
  78.  return the message \"OK\"."
  79.   (zmq:with-context (ctx 1)
  80.     (zmq:with-socket (socket ctx zmq:rep)
  81.       (zmq:bind socket "tcp://lo:5555")
  82.       (loop
  83.        (let ((query (make-instance 'zmq:msg)))
  84.          (zmq:recv socket query)
  85.          (format t "Recieved query: '~A'~%"
  86.                  (zmq:msg-data-as-string query) ))
  87.        (zmq:send socket (make-instance 'zmq:msg :data "OK")) ))))
  88. ; in: DEFUN SERVER
  89. ;     (ZEROMQ:WITH-CONTEXT (CTX 1)
  90. ;       (ZEROMQ:WITH-SOCKET (SOCKET CTX ZEROMQ:REP)
  91. ;         (ZEROMQ:BIND SOCKET "tcp://lo:5555")
  92. ;         (LOOP
  93. ;          (LET (#)
  94. ;            (ZEROMQ:RECV SOCKET QUERY)
  95. ;            (FORMAT T "Recieved query: '~A'~%" #))
  96. ;          (ZEROMQ:SEND SOCKET (MAKE-INSTANCE 'ZEROMQ:MSG :DATA "OK")))))
  97. ;
  98. ; caught ERROR:
  99. ;   during macroexpansion of
  100. ;   (ZEROMQ:WITH-CONTEXT (CTX 1)
  101. ;     (ZEROMQ:WITH-SOCKET #
  102. ;       #
  103. ;       ...)).
  104. ;   Use *BREAK-ON-SIGNALS* to intercept.
  105. ;  
  106. ;    error while parsing arguments to DEFMACRO ZEROMQ:WITH-CONTEXT:
  107. ;      too many elements in
  108. ;        (CTX 1)
  109. ;      to satisfy lambda list
  110. ;        (ZEROMQ::CONTEXT):
  111. ;      exactly 1 expected, but got 2
  112. ;
  113. ; compilation unit finished
  114. ;   caught 1 ERROR condition
  115. SERVER
  116. CL-USER>
  117.  
  118.  
  119. -- pzmq on sbcl: it compiles, but I can't seem to get the client or the server to receive any message. The client does seem to send one message, because my server written in C receives one "Hello", but then everything blocks, this looks like a serious crash of SBCL (Control stack guard page temporarily disabled: proceed with caution) . my C server can communicate with my C client.
  120.  
  121. CL-USER> (defun zclient (&optional (server-address "tcp://localhost:5555"))
  122.   (pzmq:with-context (ctx)
  123.     (pzmq:with-socket (requester ctx) (:req)
  124.       (write-line "Connecting to server...")
  125.       (pzmq:connect requester server-address)
  126.       (dotimes (i 10)
  127.         (format t "Sending Hello ~d...~%" i)
  128.         (pzmq:send requester "Hello")
  129.         (write-string "Receiving... ")
  130.         (write-line (pzmq:recv-string requester))))))
  131. ZCLIENT
  132. CL-USER> (defun zserver (&optional (listen-address "tcp://*:5555"))
  133.   "Translation of http://zguide.zeromq.org/c:hwserver updated for ZMQ 3. "
  134.   (pzmq:with-context nil ; use *default-context*
  135.     (pzmq:with-socket responder :rep
  136.       (pzmq:bind responder listen-address)
  137.       (loop
  138.         (write-string "Waiting for a request... ")
  139.         (write-line (pzmq:recv-string responder))
  140.         (sleep 1)
  141.         (pzmq:send responder "World")))))
  142. ZSERVER
  143.  
  144. CL-USER> (zclient)
  145. Connecting to server...
  146. Sending Hello 0...
  147. Receiving... Control stack guard page temporarily disabled: proceed with caution
  148.  
  149.  
  150. /*hello world server written in C*/
  151. int zserver(void) {
  152.   void *context = zmq_ctx_new ();
  153.   void *responder = zmq_socket (context, ZMQ_REP);
  154.   int rc = zmq_bind (responder, "tcp://*:5555");
  155.   assert (rc == 0);
  156.  
  157.   while (1) {
  158.     char buffer [10];
  159.     zmq_recv (responder, buffer, 10, 0);
  160.     printf ("Received Hello\n");
  161.     sleep (1);          //  Do some 'work'
  162.     zmq_send (responder, "World", 5, 0);
  163.   }
  164.   return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment