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