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

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

Remove +process-jobs-inline+ as can't work like this. Added timeout mechanism

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-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))))
Note: See TracBrowser for help on using the repository browser.