1 | #| |
---|
2 | Copyright (c) 2007 |
---|
3 | All rights reserved. |
---|
4 | |
---|
5 | Redistribution and use in source and binary forms, with or without |
---|
6 | modification, are permitted provided that the following conditions |
---|
7 | are met: |
---|
8 | 1. Redistributions of source code must retain the above copyright |
---|
9 | notice, this list of conditions and the following disclaimer. |
---|
10 | 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | notice, this list of conditions and the following disclaimer in the |
---|
12 | documentation and/or other materials provided with the distribution. |
---|
13 | 3. The name of the author may not be used to endorse or promote products |
---|
14 | derived from this software without specific prior written permission. |
---|
15 | |
---|
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
17 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
18 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
19 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
25 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
26 | |# |
---|
27 | (in-package :nio-yarpc) |
---|
28 | |
---|
29 | (declaim (optimize (debug 3) (speed 3) (space 0))) |
---|
30 | |
---|
31 | ;; YetAnotherRPC state machine |
---|
32 | ;; |
---|
33 | ;; A server that processes remote procedure calls and returns results |
---|
34 | ;; |
---|
35 | (defclass yarpc-state-machine (state-machine) |
---|
36 | ( |
---|
37 | (result-queue :initform (nio-utils:concurrent-queue) |
---|
38 | :accessor result-queue |
---|
39 | :documentation "The queue used to return results from an external thread to the nio thread"))) |
---|
40 | |
---|
41 | (defparameter job-queue (nio-utils:concurrent-queue) |
---|
42 | "The queue used to hand off work from the NIO thread to an external thread for execution") |
---|
43 | |
---|
44 | (defparameter yarpc-pf (yarpc-packet-factory)) |
---|
45 | |
---|
46 | (defmethod get-packet-factory((sm yarpc-state-machine)) |
---|
47 | yarpc-pf) |
---|
48 | |
---|
49 | (defmethod print-object ((sm yarpc-state-machine) stream) |
---|
50 | (format stream "#<YARPC-STATE-MACHINE ~A >" (call-next-method sm nil))) |
---|
51 | |
---|
52 | (defconstant STATE-INITIALISED 0) |
---|
53 | (defconstant STATE-SEND-RESPONSE 1) |
---|
54 | |
---|
55 | (defun run-job(&key (blocking t)) |
---|
56 | #+nio-debug (format-log t "yarpc-state-machine:run-job - Server toplevel waiting for job~%") |
---|
57 | (let ((server-job (nio-utils:take nio-yarpc:job-queue :blocking-call blocking))) |
---|
58 | (when server-job |
---|
59 | (destructuring-bind (job request-id result-queue) server-job |
---|
60 | #+nio-debug (format-log t "yarpc-state-machine:run-job - Server received job ~A~%" job) |
---|
61 | (nio-utils:add result-queue (list request-id (nio-yarpc:execute-call job))))))) |
---|
62 | |
---|
63 | (defmethod process-timeout((sm yarpc-state-machine))) |
---|
64 | |
---|
65 | (defmethod process-outgoing-packet((sm yarpc-state-machine)) |
---|
66 | #+nio-debug2 (format-log t "yarpc-state-machine:process-outgoing-packet - called, polling the results-queue ~%" ) |
---|
67 | (let ((server-job (nio-utils:take (result-queue sm) :blocking-call nil))) |
---|
68 | (when server-job |
---|
69 | (destructuring-bind (request-id result) server-job |
---|
70 | #+nio-debug (format-log t "yarpc-state-machine:process-outgoing-packet - got :request-id ~A result ~A ~%" request-id result) |
---|
71 | (method-response-packet result :request-id request-id))))) |
---|
72 | |
---|
73 | ;Process a call method packet by placing it in the job-queue |
---|
74 | (defmethod process-incoming-packet ((sm yarpc-state-machine) (call call-method-packet)) |
---|
75 | #+nio-debug (format-log t "yarpc-state-machine:process-incoming-packet - called :sm ~A :packet ~A~%" sm call) |
---|
76 | (nio-utils:add job-queue (list (call-string call) (request-id call) (result-queue sm)))) |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | ;;TODO move somewhere suitable |
---|
81 | |
---|
82 | (defparameter *remote-fns* nil) |
---|
83 | |
---|
84 | (defun register-remote-fn(name) |
---|
85 | (push name *remote-fns*)) |
---|
86 | |
---|
87 | (defmacro defremote (name args &rest body) |
---|
88 | `(progn |
---|
89 | (defun ,name (,@args) ,@body) |
---|
90 | (register-remote-fn #',name))) |
---|
91 | |
---|
92 | (defremote test-rpc-list() |
---|
93 | (list 3 "as" 's (code-char #x2211))) |
---|
94 | |
---|
95 | (defremote test-rpc-string(a b c) |
---|
96 | (format nil "response - ~A ~A ~A ~A~%" a b c (code-char #x2211))) |
---|
97 | |
---|
98 | (define-condition authorization-error (error) ()) |
---|
99 | |
---|
100 | (defun execute-call (call-string) |
---|
101 | (handler-case |
---|
102 | (let* ((rpc-call-list (read-from-string call-string )) |
---|
103 | (fn (member (symbol-function (first rpc-call-list)) *remote-fns* ))) |
---|
104 | #+nio-debug (format-log t "yarpc-state-machine:execute-call - fn ~A authorised? : ~A~%" (symbol-function (first rpc-call-list)) fn) |
---|
105 | (if fn |
---|
106 | (eval rpc-call-list) |
---|
107 | (error 'authorization-error))) |
---|
108 | (reader-error (re) (format-log t "yarpc-state-machine:execute-call - reader error on call-string ~A ~%" re)))) |
---|
109 | |
---|
110 | ;;end move TODO |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | ; (handler-case |
---|
117 | ; (let ((result (execute-call (call-string call)))) |
---|
118 | ; (when result |
---|
119 | ; (let ((response-packet (progn |
---|
120 | ; (setf state STATE-SEND-RESPONSE) |
---|
121 | ; (queue-outgoing-packet sm (method-response-packet result))))) |
---|
122 | ; t))) |
---|
123 | ; (reader-error (re) (format t "No such function ~A~%" (call-string call))) |
---|
124 | ; (authorization-error (ae) (format t "Function not declared with defremote ~A~%" (call-string call)))) |
---|