Changeset 33 for branches


Ignore:
Timestamp:
01/12/07 06:44:39 (18 years ago)
Author:
psmith
Message:

yarpc progress...

Location:
branches/home/psmith/restructure
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified branches/home/psmith/restructure/run-yarpc.lisp

    r27 r33  
    22(require :asdf)
    33(require :nio-yarpc)
     4
    45(nio:start-server 'identity 'identity 'nio-yarpc:yarpc-state-machine :host "127.0.0.1")
  • TabularUnified branches/home/psmith/restructure/src/buffer/buffer.lisp

    r28 r33  
    119119    byte-buffer))
    120120
     121;reads bytes from byte-buffer and returns a vector (unsigned-byte 8)
     122(defmethod bytebuffer-read-vector((bb byte-buffer) &optional (num-bytes-to-read (remaining bb)))
     123  (let ((vec  (make-uint8-seq num-bytes-to-read)))
     124    (with-slots (buf) bb
     125      (inc-position bb (cffi:mem-read-vector vec (buffer-buf bb) :unsigned-char num-bytes-to-read)))
     126    vec))
    121127
    122 (defmethod get-string((byte-buffer byte-buffer))
    123   (flip byte-buffer)
    124   (with-slots (position limit buf) byte-buffer
    125     (let ((tmp  (make-uint8-seq (remaining byte-buffer))))
    126       (inc-position byte-buffer (cffi:mem-read-vector tmp buf :unsigned-char limit))
    127       (format t " read: ~A~%" (sb-ext:octets-to-string tmp :external-format :ascii))
    128       tmp)))
    129    
    130 ;;TODO
    131 ;;mem-write-vector (vector ptr type &optional (count (length vector)) (offset 0))
    132 (defmethod bytebuffer-write-string((byte-buffer byte-buffer) str &optional (index 0) (external-format :ascii))
     128; Read bytes from bytebuffer abd return a string using the supplied decoding
     129;TODO move octets-to-string into nio-compat
     130(defmethod bytebuffer-read-string((bb byte-buffer) &optional (num-bytes-to-read (remaining bb)) (external-format :ascii))
     131  (sb-ext:octets-to-string (bytebuffer-read-vector bb num-bytes-to-read) :external-format external-format))
     132
     133
     134;; Write bytes from vector vec to bytebuffer
     135(defmethod bytebuffer-write-vector((bb byte-buffer) vec)
    133136  :documentation "Returns number of bytes written to bytebuffer"
    134   (bytebuffer-write-vector byte-buffer (sb-ext:string-to-octets str :external-format external-format)))
    135 
    136 ;;TODO rename
    137 (defmethod bytebuffer-write-vector((byte-buffer byte-buffer) vec &optional (index 0))
    138   :documentation "Returns number of bytes written to bytebuffer"
    139   (if (> (remaining byte-buffer) 0)
     137  (if (> (remaining bb) 0)
    140138      0
    141139      (progn   
    142         (clear byte-buffer)
    143         (let ((bytes-written (cffi:mem-write-vector vec (buffer-buf byte-buffer) :unsigned-char)))
     140        (clear bb)
     141        (let ((bytes-written (cffi:mem-write-vector vec (buffer-buf bb) :unsigned-char)))
    144142          (format t "bytebuffer-write-vector -  byteswritten: ~A" bytes-written)
    145           (inc-position byte-buffer bytes-written)
     143          (inc-position bb bytes-written)
    146144          bytes-written))))
     145
     146;; Writes data from string str to bytebuffer using specified encoding
     147;TODO move string-to-octets into nio-compat
     148(defmethod bytebuffer-write-string((bb byte-buffer) str &optional (external-format :ascii))
     149  :documentation "Returns number of bytes written to bytebuffer"
     150  (bytebuffer-write-vector bb (sb-ext:string-to-octets str :external-format external-format)))
    147151
    148152
     
    169173    (format t "Remaining ~A~%" (remaining mybuf))
    170174
    171     (format t "mybuf string ~A~%" (get-string mybuf))
     175    (format t "mybuf string ~A~%" (bytebuffer-read-string mybuf))
    172176
    173177    (format t "Mybuf (after get-string): ~A~%" mybuf)
  • TabularUnified branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp

    r28 r33  
    2828           
    2929            (:export
    30              byte-buffer free-buffer remaining inc-position get-string buffer-buf bytebuffer-write-vector bytebuffer-write-string flip
     30             byte-buffer free-buffer remaining inc-position get-string buffer-buf bytebuffer-write-vector bytebuffer-write-string bytebuffer-read-vector bytebuffer-read-string flip
    3131             ))
  • TabularUnified branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp

    r31 r33  
    2525THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2626|#
    27 (defpackage :nio-yarpc (:use :cl :nio :nio-buffer)
     27(defpackage :nio-yarpc (:use :cl :nio :nio-sm :nio-buffer)
    2828           
    2929            (:export
    3030
    3131             ;; yarpc-state-machine
    32              yarpc-state-machine test-rpc test-rpc-list test-rpc-string
     32             yarpc-state-machine yarpc-state-machine-factory test-rpc test-rpc-list test-rpc-string get-packet-factory
    3333             ))
  • TabularUnified branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc.asd

    r27 r33  
    66
    77    :components ((:file "nio-yarpc-package")
    8                  (:file "yarpc-state-machine" :depends-on ("nio-yarpc-package"))
     8                 (:file "yarpc-packet-factory" :depends-on ("nio-yarpc-package"))
     9                 (:file "yarpc-state-machine" :depends-on ("yarpc-packet-factory"))
    910                 )
    1011
    11     :depends-on (:nio))
     12    :depends-on (:nio :nio-sm))
  • TabularUnified branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp

    r31 r33  
    4141;; response - who 2 'S
    4242;;
    43 (defclass yarpc-state-machine (async-fd)())
     43(defclass yarpc-state-machine (state-machine)())
    4444
     45(defun yarpc-state-machine ()
     46    (make-instance 'yarpc-state-machine))
     47
     48(defparameter yarpc-pf (yarpc-packet-factory))
     49
     50(defmethod get-packet-factory((sm yarpc-state-machine))
     51  yarpc-pf)
    4552
    4653;;TODO move somewhere suitable
     
    7582  (format stream "#<YARPC-STATE-MACHINE ~A >" (call-next-method sm nil)))
    7683
    77 (defmethod process-read((sm yarpc-state-machine))
    78   (with-slots (foreign-read-buffer foreign-write-buffer) sm
    79     (let ((fn-result (execute-call (sb-ext:octets-to-string (get-string foreign-read-buffer) :external-format :ascii))))
    80       (format t "process-read - function result: ~A~%" fn-result)
    81       (nio-buffer:bytebuffer-write-string foreign-write-buffer (write-to-string fn-result) 0 :utf-8)
    82       (close-sm sm))))
     84(defconstant STATE-INITIALISED 0)
     85(defconstant STATE-SEND-RESPONSE 1)
    8386
     87(defparameter state STATE-INITIALISED)
    8488
    8589(define-condition authorization-error (error) ())
    8690
     91;Process a call method packet, returns
     92(defmethod process-packet ((sm yarpc-state-machine) (call call-method-packet))
     93  ;todo change state, create method-response packet and return it
     94  ;(assert (eql state 0))
     95  (handler-case
     96    (let ((result (execute-call (get-call-string call))))
     97      (when result
     98        (let ((response-packet (progn
     99                                  (setf state STATE-SEND-RESPONSE)
     100                                  (method-response-packet result))))
     101          (values response-packet t))))
     102    (reader-error (re) (format t "No such function ~A~%" (get-call-string call)))
     103    (authorization-error (ae) (format t "Function not declared with defremote ~A~%" (get-call-string call)))))
     104
     105
    87106(defun execute-call (call-string)
    88   (handler-case
    89107      (let* ((rpc-call-list (read-from-string call-string ))
    90108             (fn (member (symbol-function (first rpc-call-list)) *remote-fns* )))
     
    92110        (if fn
    93111            (apply (first rpc-call-list) (rest rpc-call-list))
    94             (error 'authorization-error)))
    95     (reader-error (re) (format t "No such function ~A~%" call-string))))
     112            (error 'authorization-error))))
    96113
  • TabularUnified branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp

    r32 r33  
    3030
    3131             ;; state-machine
    32              state-machine
     32             state-machine packet-factory get-packet-factory get-packet
    3333             ))
  • TabularUnified branches/home/psmith/restructure/src/statemachine/state-machine.lisp

    r32 r33  
    4343  (format stream "#<STATE-MACHINE ~A >" (call-next-method sm nil)))
    4444
     45(defgeneric process-packet(state-machine packet))
     46
     47(defgeneric get-packet-factory(state-machine))
     48
    4549(defmethod process-read((sm state-machine))
    4650  (with-slots (foreign-read-buffer foreign-write-buffer) sm
    47     (let ((fn-result (execute-call (sb-ext:octets-to-string (get-string foreign-read-buffer) :external-format :ascii))))
    48       (format t "process-read - function result: ~A~%" fn-result)
    49       (nio-buffer:bytebuffer-write-string foreign-write-buffer (write-to-string fn-result) 0 :utf-8)
    50       (close-sm sm))))
     51    (let ((incomming-packet (get-packet (get-packet-factory sm) foreign-read-buffer)))
     52      (format t "state-machine::process-read - incomming packet: ~A~%" incomming-packet)
     53      (when incomming-packet
     54        (multiple-value-bind (ret-packet close) (process-packet sm incomming-packet)
     55          (format t "state-machine::process-read - return packet: ~A~%" ret-packet)
     56          (when ret-packet (put-packet ret-packet foreign-write-buffer))
     57          (if close
     58            (close-sm sm)
     59            ))))))
    5160
    5261
     62
     63(defclass packet-factory ()
     64  ())
     65 
     66; Get the packet in buf using the packet factory
     67(defgeneric get-packet (packet-factory buf))
     68
     69; Write the packet to the buffer
     70(defun put-packet (packet buf)
     71  (nio-buffer:bytebuffer-write-vector buf (get-bytes packet)))
Note: See TracChangeset for help on using the changeset viewer.