Skip to content

Commit cba1f15

Browse files
committed
common-lisp: tutorial six code
1 parent f742e60 commit cba1f15

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

common-lisp/rpc-client.lisp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
3+
sbcl --noinform --noprint <<EOF
4+
5+
(ql:quickload :cl-bunny.examples)
6+
(ql:quickload :nibbles)
7+
8+
(in-package :cl-bunny.examples)
9+
10+
(defun int64-to-octets(val)
11+
(let ((obuffer (fast-io:make-output-buffer)))
12+
(fast-io:write64-be val obuffer)
13+
(fast-io:finish-output-buffer obuffer)))
14+
15+
(defun start-client (n)
16+
(with-connection ("amqp://")
17+
(with-channel ()
18+
(let ((x (exchange.default))
19+
(server-queue "rpc_queue")
20+
(reply-queue (queue.declare :auto-delete t))
21+
(lock (bt:make-lock))
22+
(condition (bt:make-condition-variable))
23+
(result nil))
24+
(format t " [x] Requesting fib(~a)~%" n)
25+
(bt:with-lock-held (lock)
26+
(subscribe reply-queue (lambda (message)
27+
(bt:with-lock-held (lock)
28+
(setf result (nibbles:sb64ref/be (message-body message) 0))
29+
(bt:condition-notify condition))))
30+
(publish x
31+
(int64-to-octets n)
32+
:routing-key server-queue
33+
:properties (list :correlation-id (format nil "~a~a~a" (random 100) (random 100) (random 100))
34+
:reply-to reply-queue))
35+
(bt:condition-wait condition lock)
36+
(format t " [.] Got ~a~%" result)
37+
result)))))
38+
39+
(start-client 0)
40+
(start-client 1)
41+
(start-client 22)
42+
(start-client 33)
43+
(start-client 44)
44+
(start-client 55)
45+
46+
EOF

common-lisp/rpc-server.lisp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sh
2+
3+
sbcl --noinform --noprint <<EOF
4+
5+
(ql:quickload :cl-bunny.examples)
6+
(ql:quickload :nibbles)
7+
8+
(in-package :cl-bunny.examples)
9+
10+
(defun int64-to-octets(val)
11+
(let ((obuffer (fast-io:make-output-buffer)))
12+
(fast-io:write64-be val obuffer)
13+
(fast-io:finish-output-buffer obuffer)))
14+
15+
;; http://www.cliki.net/fibonacci
16+
(defun fibonacci (n)
17+
"Successive squaring method from SICP"
18+
(check-type n (integer 0 *))
19+
(labels ((fib-aux (a b p q count)
20+
(cond ((= count 0) b)
21+
((evenp count)
22+
(fib-aux a
23+
b
24+
(+ (* p p) (* q q))
25+
(+ (* q q) (* 2 p q))
26+
(/ count 2)))
27+
(t (fib-aux (+ (* b q) (* a q) (* a p))
28+
(+ (* b p) (* a q))
29+
p
30+
q
31+
(- count 1))))))
32+
(fib-aux 1 0 0 1 n)))
33+
34+
(with-connection ()
35+
(with-channel ()
36+
(let ((x (exchange.default))
37+
(q (queue.declare :name "rpc_queue" :auto-delete t)))
38+
(format t " [x] Awaiting RPC requests~%")
39+
(handler-case
40+
(progn
41+
(subscribe q (lambda (message)
42+
(let* ((n (nibbles:sb64ref/be (message-body message) 0))
43+
(r (fibonacci n)))
44+
(format t " [.] fib(~a)~%" r)
45+
(publish x
46+
(int64-to-octets r)
47+
:routing-key (message-reply-to message)
48+
:properties (list :correlation-id (message-correlation-id message)))))
49+
:type :sync)
50+
(consume))
51+
(sb-sys:interactive-interrupt ()
52+
(sb-ext:exit))))))
53+
54+
EOF

0 commit comments

Comments
 (0)