source: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp @ 74

Last change on this file since 74 was 74, checked in by psmith, 18 years ago

Changes for server close

File size: 5.0 KB
Line 
1#|
2Copyright (c) 2007
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
81. Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
102. 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.
133. 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
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25THIS 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-compat: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-compat: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
56(defparameter +process-jobs-inline+ t
57  "Set this to make the NIO thread process the RPC calls - warning the procedure should not block!")
58
59
60
61(defun run-job(&key (wait-on-job-pdw t))
62  (format-log t "yarpc-state-machine:run-job - Server toplevel waiting for job~%")
63  (destructuring-bind (job result-queue) (nio-compat:take nio-yarpc:job-queue :blocking-call wait-on-job-pdw)
64    (format-log t "yarpc-state-machine:run-job - Server received job ~A~%" job)
65    (nio-compat:add result-queue (nio-yarpc:execute-call job))))
66
67
68(defmethod process-outgoing-packet((sm yarpc-state-machine))
69  (format-log t "yarpc-state-machine:process-outgoing-packet - called, polling the results-queue ~%" )
70  (let ((result (nio-compat:take (result-queue sm) :blocking-call nil)))
71    (format-log t "yarpc-state-machine:process-outgoing-packet - got result ~A ~%" result)
72     (when result
73        (method-response-packet result))))
74
75;Process a call method packet by placing it in the job-queue
76(defmethod process-incoming-packet ((sm yarpc-state-machine) (call call-method-packet))
77  (assert (eql (state sm) STATE-INITIALISED))
78  (format-log t "yarpc-state-machine:process-incoming-packet - called :sm ~A :packet ~A~%" sm call)
79  (nio-compat:add job-queue (list (call-string call) (result-queue sm)))
80  (when +process-jobs-inline+ (run-job :wait-on-job-pdw nil)))
81
82
83
84;;TODO move somewhere suitable
85
86(defparameter *remote-fns* nil)
87
88(defun register-remote-fn(name)
89    (push name *remote-fns*))
90
91(defmacro defremote (name args &rest body)
92  `(progn 
93     (defun ,name (,@args) ,@body)
94     (register-remote-fn #',name)))
95
96(defremote test-rpc-list()
97  (list 3 "as" 's (code-char #x2211)))
98
99(defremote test-rpc-string(a b c)
100  (format nil "response - ~A ~A ~A ~A~%" a b c (code-char #x2211)))
101
102(define-condition authorization-error (error) ())
103
104(defun execute-call (call-string)
105  (handler-case
106      (let* ((rpc-call-list (read-from-string call-string ))
107             (fn (member (symbol-function (first rpc-call-list)) *remote-fns* )))
108        (format-log t "yarpc-state-machine:execute-call - fn ~A authorised? : ~A~%" (symbol-function (first rpc-call-list)) fn)
109        (if fn
110            (apply (first rpc-call-list) (rest rpc-call-list))
111            (error 'authorization-error)))
112    (reader-error (re) (format-log t "yarpc-state-machine:execute-call - reader error on call-string ~A ~%" re))))
113
114;;end move TODO
115
116
117
118
119
120;  (handler-case
121;    (let ((result (execute-call (call-string call))))
122;      (when result
123;       (let ((response-packet (progn
124;                                  (setf state STATE-SEND-RESPONSE)
125;                                  (queue-outgoing-packet sm (method-response-packet result)))))
126;          t)))
127;    (reader-error (re) (format t "No such function ~A~%" (call-string call)))
128;    (authorization-error (ae) (format t "Function not declared with defremote ~A~%" (call-string call))))
Note: See TracBrowser for help on using the repository browser.