#| Copyright (c) 2007 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |# (in-package :nio-yarpc) (declaim (optimize (debug 3) (speed 3) (space 0))) ;; YetAnotherRPC state machine ;; ;; A server that processes remote procedure calls and returns results ;; (defclass yarpc-state-machine (state-machine) ( (result-queue :initform (nio-utils:concurrent-queue) :accessor result-queue :documentation "The queue used to return results from an external thread to the nio thread"))) (defparameter job-queue (nio-utils:concurrent-queue) "The queue used to hand off work from the NIO thread to an external thread for execution") (defparameter yarpc-pf (yarpc-packet-factory)) (defmethod get-packet-factory((sm yarpc-state-machine)) yarpc-pf) (defmethod print-object ((sm yarpc-state-machine) stream) (format stream "#" (call-next-method sm nil))) (defconstant STATE-INITIALISED 0) (defconstant STATE-SEND-RESPONSE 1) (defun run-job(&key (blocking t)) #+nio-debug (format-log t "yarpc-state-machine:run-job - Server toplevel waiting for job~%") (let ((server-job (nio-utils:take nio-yarpc:job-queue :blocking-call blocking))) (when server-job (destructuring-bind (job request-id result-queue) server-job #+nio-debug (format-log t "yarpc-state-machine:run-job - Server received job ~A~%" job) (nio-utils:add result-queue (list request-id (nio-yarpc:execute-call job))))))) (defmethod process-timeout((sm yarpc-state-machine))) (defmethod process-outgoing-packet((sm yarpc-state-machine)) #+nio-debug2 (format-log t "yarpc-state-machine:process-outgoing-packet - called, polling the results-queue ~%" ) (let ((server-job (nio-utils:take (result-queue sm) :blocking-call nil))) (when server-job (destructuring-bind (request-id result) server-job #+nio-debug (format-log t "yarpc-state-machine:process-outgoing-packet - got :request-id ~A result ~A ~%" request-id result) (method-response-packet result :request-id request-id))))) ;Process a call method packet by placing it in the job-queue (defmethod process-incoming-packet ((sm yarpc-state-machine) (call call-method-packet)) #+nio-debug (format-log t "yarpc-state-machine:process-incoming-packet - called :sm ~A :packet ~A~%" sm call) (nio-utils:add job-queue (list (call-string call) (request-id call) (result-queue sm)))) ;;TODO move somewhere suitable (defparameter *remote-fns* nil) (defun register-remote-fn(name) (push name *remote-fns*)) (defmacro defremote (name args &rest body) `(progn (defun ,name (,@args) ,@body) (register-remote-fn #',name))) (defremote test-rpc-list() (list 3 "as" 's (code-char #x2211))) (defremote test-rpc-string(a b c) (format nil "response - ~A ~A ~A ~A~%" a b c (code-char #x2211))) (define-condition authorization-error (error) ()) (defun execute-call (call-string) (handler-case (let* ((rpc-call-list (read-from-string call-string )) (fn (member (symbol-function (first rpc-call-list)) *remote-fns* ))) #+nio-debug (format-log t "yarpc-state-machine:execute-call - fn ~A authorised? : ~A~%" (symbol-function (first rpc-call-list)) fn) (if fn (eval rpc-call-list) (error 'authorization-error))) (reader-error (re) (format-log t "yarpc-state-machine:execute-call - reader error on call-string ~A ~%" re)))) ;;end move TODO ; (handler-case ; (let ((result (execute-call (call-string call)))) ; (when result ; (let ((response-packet (progn ; (setf state STATE-SEND-RESPONSE) ; (queue-outgoing-packet sm (method-response-packet result))))) ; t))) ; (reader-error (re) (format t "No such function ~A~%" (call-string call))) ; (authorization-error (ae) (format t "Function not declared with defremote ~A~%" (call-string call))))