nio-cvs
Threads by month
- ----- 2025 -----
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
January 2007
- 1 participants
- 40 discussions

[nio-cvs] r41 - in branches/home/psmith/restructure: . src/buffer src/compat src/io src/protocol/yarpc src/statemachine
by psmith@common-lisp.net 18 Jan '07
by psmith@common-lisp.net 18 Jan '07
18 Jan '07
Author: psmith
Date: Wed Jan 17 23:01:11 2007
New Revision: 41
Modified:
branches/home/psmith/restructure/run-yarpc.lisp
branches/home/psmith/restructure/src/buffer/buffer.lisp
branches/home/psmith/restructure/src/compat/concurrent-queue.lisp
branches/home/psmith/restructure/src/io/async-fd.lisp
branches/home/psmith/restructure/src/io/async-socket.lisp
branches/home/psmith/restructure/src/io/nio-server.lisp
branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-client-state-machine.lisp
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp
branches/home/psmith/restructure/src/statemachine/state-machine.lisp
Log:
Yarpc working end-to-end
Modified: branches/home/psmith/restructure/run-yarpc.lisp
==============================================================================
--- branches/home/psmith/restructure/run-yarpc.lisp (original)
+++ branches/home/psmith/restructure/run-yarpc.lisp Wed Jan 17 23:01:11 2007
@@ -2,11 +2,10 @@
(require :asdf)
(require :nio-yarpc)
-(let ((jobq (nio-compat:concurrent-queue)))
- (sb-thread:make-thread #'(lambda()(nio:start-server 'identity 'identity #'(lambda()(nio-yarpc:yarpc-state-machine jobq)) :host "127.0.0.1")) :name "nio-server")
- (format t "server toplevel waiting for job~%" )
- (loop
+(sb-thread:make-thread #'(lambda()(nio:start-server 'identity 'identity 'nio-yarpc:yarpc-state-machine :host "127.0.0.1")) :name "nio-server")
+(loop
;;block waiting for jobs
- (multiple-value-bind (job result-queue) (nio-compat:take jobq)
+ (format t "Server toplevel waiting for job~%" )
+ (destructuring-bind (job result-queue) (nio-compat:take nio-yarpc:job-queue)
(format t "Server received job ~A~%" job)
- (nio-compat:add result-queue (nio-yarpc:execute-call job)))))
+ (nio-compat:add result-queue (nio-yarpc:execute-call job))))
Modified: branches/home/psmith/restructure/src/buffer/buffer.lisp
==============================================================================
--- branches/home/psmith/restructure/src/buffer/buffer.lisp (original)
+++ branches/home/psmith/restructure/src/buffer/buffer.lisp Wed Jan 17 23:01:11 2007
@@ -55,25 +55,45 @@
;;Utils by slyrus (http://paste.lisp.org/display/11149)
(defun hex-dump-byte (address)
- (format nil "~2,'0X"
- (sb-alien:deref
- (sb-alien:sap-alien
- (sb-alien::int-sap address)
- (* (sb-alien:unsigned 8))))))
+ (format nil "~2,'0X" (byte-value address)))
+
+(defun byte-value (address)
+ (sb-alien:deref
+ (sb-alien:sap-alien
+ (sb-alien::int-sap address)
+ (* (sb-alien:unsigned 8)))))
(defun hex-dump-memory (start-address length)
(loop for i from start-address below (+ start-address length)
collect (format nil (hex-dump-byte i))))
+;;-- end utils
+
+
+(defun pretty-hex-dump (start-address length)
+; (format t "start: ~A length ~A~%" start-address length)
+ (with-output-to-string (str)
+ (let ((rows (floor (/ length 16))))
+; (format t "rows: ~A remainder ~A~%" rows remainder)
+ (dotimes (row-index (+ 1 rows))
+ (format str "~A~%"
+ (with-output-to-string (readable)
+ (dotimes (column-index 16)
+ (let ((address (+ start-address (* row-index 16) column-index)))
+ ; (format t "Current address : ~A~%" address)
+ (if (>= address (+ start-address length))
+ (progn
+ (format str "--")
+ (format readable "--"))
+ (progn
+ (format str (if (eql column-index 7) "~A " "~A ") (hex-dump-byte address))
+ (format readable "~A" (code-char (byte-value address)))))))))))))
(defun make-uint8-seq (size)
"Make uint8 sequence."
(make-sequence '(vector (unsigned-byte 8)) size :initial-element 0))
-;;-- end utils
-
-
;;A buffer that deals with bytes
(defclass byte-buffer (buffer)())
@@ -83,7 +103,7 @@
(defmethod print-object ((byte-buffer byte-buffer) stream)
(with-slots (capacity position limit buf) byte-buffer
- (format stream "<byte-buffer :capacity ~A :position ~A :limit ~A :buf ~%~A>~%" capacity position limit (if buf (hex-dump-memory (cffi:pointer-address buf) limit) nil))))
+ (format stream "<byte-buffer :capacity ~A :position ~A :limit ~A :buf ~%~A>~%" capacity position limit (if buf (pretty-hex-dump (cffi:pointer-address buf) limit) nil))))
(defmethod free-buffer((byte-buffer byte-buffer))
(with-slots (capacity position limit buf) byte-buffer
Modified: branches/home/psmith/restructure/src/compat/concurrent-queue.lisp
==============================================================================
--- branches/home/psmith/restructure/src/compat/concurrent-queue.lisp (original)
+++ branches/home/psmith/restructure/src/compat/concurrent-queue.lisp Wed Jan 17 23:01:11 2007
@@ -47,7 +47,7 @@
`(if ,a-buffer
(let ((head (car ,a-buffer)))
(setf ,a-buffer (cdr ,a-buffer))
-#+nio-debug (format t "reader ~A woke, read ~A as ~A~%" sb-thread:*current-thread* head ,loc)
+#+nio-debug (format t "concurent-queue:take - (~A) read ~A at ~A~%" sb-thread:*current-thread* head ,loc)
head)
nil))
@@ -64,6 +64,7 @@
;Append the element to the tail of this queue
(defmethod add ((queue concurrent-queue) elt)
+#+nio-debug (format t "concurent-queue:add - (~A) adding ~A~%" sb-thread:*current-thread* elt)
(sb-thread:with-mutex ((buffer-lock queue))
(setf (buffer queue) (append (buffer queue) (list elt)) )
(sb-thread:condition-notify (buffer-queue queue))))
Modified: branches/home/psmith/restructure/src/io/async-fd.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/async-fd.lisp (original)
+++ branches/home/psmith/restructure/src/io/async-fd.lisp Wed Jan 17 23:01:11 2007
@@ -125,11 +125,12 @@
(error 'read-error)))
((= new-bytes 0)
- nil);;(throw 'end-of-file nil))
+ nil);;(throw 'end-of-file nil)
(t
;;Update buffer position
- (inc-position foreign-read-buffer new-bytes))))))
+ (inc-position foreign-read-buffer new-bytes)
+ (setf (read-ready state-machine) nil))))))
(defun close-async-fd (async-fd)
"Close ASYNC-FD's fd after everything has been written from write-queue."
Modified: branches/home/psmith/restructure/src/io/async-socket.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/async-socket.lisp (original)
+++ branches/home/psmith/restructure/src/io/async-socket.lisp Wed Jan 17 23:01:11 2007
@@ -178,7 +178,7 @@
-(defun socket-accept (socket-fd connection-factory)
+(defun socket-accept (socket-fd connection-type)
"Accept connection from SOCKET-FD. Allocates and returns socket structure denoting the connection."
(flet ((parse-inet6-addr (addr)
@@ -202,7 +202,7 @@
;; accept connection
(let* ((res (%accept socket-fd addr len))
;; (async-socket-fd (make-instance 'async-socket-fd :read-fd res :write-fd res)))
- (async-socket-fd (create-state-machine connection-factory res res (make-instance 'async-socket-fd))))
+ (async-socket-fd (create-state-machine connection-type res res (make-instance 'async-socket-fd))))
(unless (< res 0)
(let ((len-value (mem-ref len :unsigned-int)))
Modified: branches/home/psmith/restructure/src/io/nio-server.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/nio-server.lisp (original)
+++ branches/home/psmith/restructure/src/io/nio-server.lisp Wed Jan 17 23:01:11 2007
@@ -55,7 +55,7 @@
-(defun start-server (connection-handler accept-filter connection-factory
+(defun start-server (connection-handler accept-filter connection-type
&key
(protocol :inet)
(port (+ (random 60000) 1024))
@@ -99,7 +99,7 @@
(cond
;; new connection
((= fd sock)
- (let ((async-fd (socket-accept fd connection-factory)))
+ (let ((async-fd (socket-accept fd connection-type)))
#+nio-debug (format t "start-server - New conn: ~A~%" async-fd)
(cond
((null async-fd)
Modified: branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp Wed Jan 17 23:01:11 2007
@@ -32,7 +32,7 @@
yarpc-state-machine-factory get-packet-factory
;; yarpc-state-machine
- yarpc-state-machine
+ yarpc-state-machine job-queue
;to be moved
test-rpc test-rpc-list test-rpc-string execute-call
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-client-state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-client-state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-client-state-machine.lisp Wed Jan 17 23:01:11 2007
@@ -38,7 +38,7 @@
:documentation "The queue used to hand off work from an external thread to the io thread")
(result-queue :initform (nio-compat:concurrent-queue)
:accessor result-queue
- :documentation "The queue used to hand off work from an external thread to the io thread")))
+ :documentation "The queue used to return results from the io thread to an external thread")))
(defun yarpc-client-state-machine ()
(make-instance 'yarpc-client-state-machine))
@@ -55,26 +55,26 @@
(defconstant STATE-INITIALISED 0)
(defconstant STATE-SENT-REQUEST 1)
-(defparameter state STATE-INITIALISED)
-
(defmethod process-outgoing-packet((sm yarpc-client-state-machine))
(format t "process-outgoing-packet called, polling the job-queue ~%")
(let ((packet (nio-compat:take (job-queue sm) :blocking-call nil)))
(when packet
(format t "process-outgoing-packet got job ~A ~%" packet)
- (setf state STATE-SENT-REQUEST))
+ (setf (state sm) STATE-SENT-REQUEST))
packet))
(defmethod process-incoming-packet ((sm yarpc-client-state-machine) (response method-response-packet))
- (assert (eql state STATE-SENT-REQUEST))
+ (assert (eql (state sm) STATE-SENT-REQUEST))
(format t "yarpc-client-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm response)
- (nio-compat:add (result-queue sm) response)
- (setf state STATE-INITIALISED))
+ (let* ((*package* (find-package :nio-yarpc))
+ (result (read-from-string (response response))))
+ (nio-compat:add (result-queue sm) result)
+ (setf (state sm) STATE-INITIALISED)))
;Called from an external thread i.e. *not* the nio thread
;Blocks calling thread on the remote m/c's response
(defmethod remote-execute ((sm yarpc-client-state-machine) call-string)
; (queue-outgoing-packet
- (assert (eql state STATE-INITIALISED))
+ (assert (eql (state sm) STATE-INITIALISED))
(nio-compat:add (job-queue sm) (make-instance 'call-method-packet :call-string call-string))
(nio-compat:take (result-queue sm)))
\ No newline at end of file
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp Wed Jan 17 23:01:11 2007
@@ -33,20 +33,13 @@
;; A server that processes remote procedure calls and returns results
;;
(defclass yarpc-state-machine (state-machine)
- ((job-queue :initarg :job-queue
- :initform (error "Must supply a job queue to write work to.")
- :accessor job-queue
- :documentation "The queue used to hand off work from the NIO thread to an external thread for execution")
+ (
(result-queue :initform (nio-compat:concurrent-queue)
:accessor result-queue
:documentation "The queue used to return results from an external thread to the nio thread")))
-(defun yarpc-state-machine (read-fd write-fd socket job-queue)
- (let ((sm (make-instance 'yarpc-state-machine :read-fd read-fd :write-fd write-fd :socket socket :job-queue job-queue)))
- (nio-buffer:clear (foreign-read-buffer sm))
- (nio-buffer:clear (foreign-write-buffer sm))
- (format t "yarpc-state-machine - Created ~S~%" sm)
- sm))
+(defparameter job-queue (nio-compat: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))
@@ -59,27 +52,18 @@
(defconstant STATE-INITIALISED 0)
(defconstant STATE-SEND-RESPONSE 1)
-(defparameter state STATE-INITIALISED)
-
-
(defmethod process-outgoing-packet((sm yarpc-state-machine))
(format t "yarpc-state-machine: process-outgoing-packet called, polling the results-queue ~%")
- (let ((packet (nio-compat:take (result-queue sm) :blocking-call nil)))
- (format t "yarpc-state-machine: process-outgoing-packet got result ~A ~%" packet)
- packet))
-
+ (let ((result (nio-compat:take (result-queue sm) :blocking-call nil)))
+ (format t "yarpc-state-machine: process-outgoing-packet got result ~A ~%" result)
+ (when result
+ (method-response-packet result))))
-;Process a call method packet, returns
+;Process a call method packet by placing it in the job-queue
(defmethod process-incoming-packet ((sm yarpc-state-machine) (call call-method-packet))
- (assert (eql state STATE-INITIALISED))
+ (assert (eql (state sm) STATE-INITIALISED))
(format t "yarpc-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm call)
- (nio-compat:add (job-queue sm) (cons (call-string call) (result-queue sm))))
-
-
-;Called from an external thread i.e. *not* the nio thread
-;Blocks waiting for a job (call-string,result-queue) to process and return the result into the result queue
-;(defmethod get-job ((sm yarpc-state-machine))
-; (values (nio-compat:take (job-queue sm)) (result-queue sm)))
+ (nio-compat:add job-queue (list (call-string call) (result-queue sm))))
Modified: branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp (original)
+++ branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp Wed Jan 17 23:01:11 2007
@@ -29,5 +29,5 @@
(:export
;; state-machine
- state-machine packet-factory get-packet-factory get-packet process-outgoing-packet process-incoming-packet
+ state-machine packet-factory get-packet-factory get-packet process-outgoing-packet process-incoming-packet state
))
Modified: branches/home/psmith/restructure/src/statemachine/state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/statemachine/state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/statemachine/state-machine.lisp Wed Jan 17 23:01:11 2007
@@ -37,7 +37,9 @@
;This way only the protocols packet heirarchy knows about binary representations and
; the SM can deal with protocol logic and state maintenance
;
-(defclass state-machine (async-fd)())
+(defclass state-machine (async-fd)
+ ((state :initform 0
+ :accessor state)))
(defmethod print-object ((sm state-machine) stream)
(format stream "#<STATE-MACHINE ~A >" (call-next-method sm nil)))
1
0

[nio-cvs] r40 - in branches/home/psmith/restructure: . src/compat src/io src/protocol/yarpc
by psmith@common-lisp.net 17 Jan '07
by psmith@common-lisp.net 17 Jan '07
17 Jan '07
Author: psmith
Date: Wed Jan 17 00:06:13 2007
New Revision: 40
Added:
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-client-state-machine.lisp
- copied, changed from r37, branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
Modified:
branches/home/psmith/restructure/run-yarpc-client.lisp
branches/home/psmith/restructure/run-yarpc.lisp
branches/home/psmith/restructure/src/compat/concurrent-queue.lisp
branches/home/psmith/restructure/src/io/async-fd.lisp
branches/home/psmith/restructure/src/io/async-socket.lisp
branches/home/psmith/restructure/src/io/nio-server.lisp
branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp
branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc.asd
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
Log:
yarpc work, saving...
Modified: branches/home/psmith/restructure/run-yarpc-client.lisp
==============================================================================
--- branches/home/psmith/restructure/run-yarpc-client.lisp (original)
+++ branches/home/psmith/restructure/run-yarpc-client.lisp Wed Jan 17 00:06:13 2007
@@ -2,10 +2,9 @@
(require :asdf)
(require :nio-yarpc)
-(sb-thread:make-thread #'(lambda()(nio:start-server 'identity 'identity 'nio-yarpc:yarpc-state-machine :host "127.0.0.1" :port 9897)) :name "nio-server")
+;;shouldn't be listenting on the client hence nil for accept SM to start-server
+(sb-thread:make-thread #'(lambda()(nio:start-server 'identity 'identity nil :host "127.0.0.1" :port 9897)) :name "nio-server")
(sleep 4)
-(let ((sm (nio:add-connection "127.0.0.1" 16323 'nio-yarpc:yarpc-state-machine)))
+(let ((sm (nio:add-connection "127.0.0.1" 16323 'nio-yarpc:yarpc-client-state-machine)))
(format t "toplevel adding conn ~A~%" sm)
(format t "Result of remote-execute ~A~%" (nio-yarpc:remote-execute sm "(nio-yarpc:test-rpc-list)")))
-
-
Modified: branches/home/psmith/restructure/run-yarpc.lisp
==============================================================================
--- branches/home/psmith/restructure/run-yarpc.lisp (original)
+++ branches/home/psmith/restructure/run-yarpc.lisp Wed Jan 17 00:06:13 2007
@@ -2,4 +2,11 @@
(require :asdf)
(require :nio-yarpc)
-(nio:start-server 'identity 'identity 'nio-yarpc:yarpc-state-machine :host "127.0.0.1")
+(let ((jobq (nio-compat:concurrent-queue)))
+ (sb-thread:make-thread #'(lambda()(nio:start-server 'identity 'identity #'(lambda()(nio-yarpc:yarpc-state-machine jobq)) :host "127.0.0.1")) :name "nio-server")
+ (format t "server toplevel waiting for job~%" )
+ (loop
+ ;;block waiting for jobs
+ (multiple-value-bind (job result-queue) (nio-compat:take jobq)
+ (format t "Server received job ~A~%" job)
+ (nio-compat:add result-queue (nio-yarpc:execute-call job)))))
Modified: branches/home/psmith/restructure/src/compat/concurrent-queue.lisp
==============================================================================
--- branches/home/psmith/restructure/src/compat/concurrent-queue.lisp (original)
+++ branches/home/psmith/restructure/src/compat/concurrent-queue.lisp Wed Jan 17 00:06:13 2007
@@ -51,18 +51,18 @@
head)
nil))
-
-(defmethod take ((queue concurrent-queue))
+;Do an (optionally blocking) remove of the element at the head of this queue
+(defmethod take ((queue concurrent-queue) &key (blocking-call t))
(sb-thread:with-mutex ((buffer-lock queue))
;if its there, pop it
(let ((ret (pop-elt (buffer queue) "1sttry")))
- (if ret
+ (if (or ret (not blocking-call))
ret
(progn
(sb-thread:condition-wait (buffer-queue queue) (buffer-lock queue))
(pop-elt (buffer queue) "2ndtry"))))))
-
+;Append the element to the tail of this queue
(defmethod add ((queue concurrent-queue) elt)
(sb-thread:with-mutex ((buffer-lock queue))
(setf (buffer queue) (append (buffer queue) (list elt)) )
Modified: branches/home/psmith/restructure/src/io/async-fd.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/async-fd.lisp (original)
+++ branches/home/psmith/restructure/src/io/async-fd.lisp Wed Jan 17 00:06:13 2007
@@ -84,10 +84,6 @@
;;Implement this in concrete SM for read
(defgeneric process-write (async-fd))
-
-
-;Loop over state machines calling process-outgoing-packets via state-machine::process-write
-
;;SM factory
(defun create-state-machine(sm-type read-fd write-fd socket)
(let ((sm (make-instance sm-type :read-fd read-fd :write-fd write-fd :socket socket)))
Modified: branches/home/psmith/restructure/src/io/async-socket.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/async-socket.lisp (original)
+++ branches/home/psmith/restructure/src/io/async-socket.lisp Wed Jan 17 00:06:13 2007
@@ -178,7 +178,7 @@
-(defun socket-accept (socket-fd connection-type)
+(defun socket-accept (socket-fd connection-factory)
"Accept connection from SOCKET-FD. Allocates and returns socket structure denoting the connection."
(flet ((parse-inet6-addr (addr)
@@ -202,7 +202,7 @@
;; accept connection
(let* ((res (%accept socket-fd addr len))
;; (async-socket-fd (make-instance 'async-socket-fd :read-fd res :write-fd res)))
- (async-socket-fd (create-state-machine connection-type res res (make-instance 'async-socket-fd))))
+ (async-socket-fd (create-state-machine connection-factory res res (make-instance 'async-socket-fd))))
(unless (< res 0)
(let ((len-value (mem-ref len :unsigned-int)))
Modified: branches/home/psmith/restructure/src/io/nio-server.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/nio-server.lisp (original)
+++ branches/home/psmith/restructure/src/io/nio-server.lisp Wed Jan 17 00:06:13 2007
@@ -55,7 +55,7 @@
-(defun start-server (connection-handler accept-filter connection-type
+(defun start-server (connection-handler accept-filter connection-factory
&key
(protocol :inet)
(port (+ (random 60000) 1024))
@@ -99,7 +99,7 @@
(cond
;; new connection
((= fd sock)
- (let ((async-fd (socket-accept fd connection-type)))
+ (let ((async-fd (socket-accept fd connection-factory)))
#+nio-debug (format t "start-server - New conn: ~A~%" async-fd)
(cond
((null async-fd)
Modified: branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp Wed Jan 17 00:06:13 2007
@@ -28,6 +28,14 @@
(:export
+ ;;base
+ yarpc-state-machine-factory get-packet-factory
+
;; yarpc-state-machine
- yarpc-state-machine yarpc-state-machine-factory test-rpc test-rpc-list test-rpc-string get-packet-factory remote-execute
+ yarpc-state-machine
+ ;to be moved
+ test-rpc test-rpc-list test-rpc-string execute-call
+
+ ;;yarpc-client-state-machine
+ yarpc-client-state-machine remote-execute
))
Modified: branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc.asd
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc.asd (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc.asd Wed Jan 17 00:06:13 2007
@@ -7,6 +7,7 @@
:components ((:file "nio-yarpc-package")
(:file "yarpc-packet-factory" :depends-on ("nio-yarpc-package"))
(:file "yarpc-state-machine" :depends-on ("yarpc-packet-factory"))
+ (:file "yarpc-client-state-machine" :depends-on ("yarpc-packet-factory"))
)
- :depends-on (:nio :nio-sm))
\ No newline at end of file
+ :depends-on (:nio :nio-sm :nio-compat))
\ No newline at end of file
Copied: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-client-state-machine.lisp (from r37, branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp)
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-client-state-machine.lisp Wed Jan 17 00:06:13 2007
@@ -28,108 +28,53 @@
(declaim (optimize (debug 3) (speed 3) (space 0)))
-;; YetAnotherRPC state machine
+;; YetAnotherRPC Client state machine
;;
-;; A server that processes remote procedure calls and returns results
+;; A client that accepts jobs to be run via a threadsafe queue and then submits them to the remote end for execution
;;
-;; Test with:
-;; > telnet 127.0.0.1 16323
-;; Trying 127.0.0.1...
-;; Connected to 127.0.0.1.
-;; Escape character is '^]'.
-;; (test-rpc "who" 2 's)
-;; response - who 2 'S
-;;
-(defclass yarpc-state-machine (state-machine)
- ((outgoing-packet :initarg :outgoing-packet
- :accessor outgoing-packet
- :initform nil)))
+(defclass yarpc-client-state-machine (state-machine)
+ ((job-queue :initform (nio-compat:concurrent-queue)
+ :accessor job-queue
+ :documentation "The queue used to hand off work from an external thread to the io thread")
+ (result-queue :initform (nio-compat:concurrent-queue)
+ :accessor result-queue
+ :documentation "The queue used to hand off work from an external thread to the io thread")))
-(defun yarpc-state-machine ()
- (make-instance 'yarpc-state-machine))
+(defun yarpc-client-state-machine ()
+ (make-instance 'yarpc-client-state-machine))
(defparameter yarpc-pf (yarpc-packet-factory))
-(defmethod get-packet-factory((sm yarpc-state-machine))
+(defmethod get-packet-factory((sm yarpc-client-state-machine))
yarpc-pf)
-;;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)))
-
-;;end move TODO
-
-
-;;;Utils
-
-(defun print-hashtable (table &optional (stream t))
- (maphash #'(lambda (k v) (format stream "~a -> ~a~%" k v)) table))
-;;;
-
-(defmethod print-object ((sm yarpc-state-machine) stream)
- (format stream "#<YARPC-STATE-MACHINE ~A >" (call-next-method sm nil)))
+(defmethod print-object ((sm yarpc-client-state-machine) stream)
+ (format stream "#<YARPC-CLIENT-STATE-MACHINE ~A >" (call-next-method sm nil)))
(defconstant STATE-INITIALISED 0)
-(defconstant STATE-SEND-RESPONSE 1)
+(defconstant STATE-SENT-REQUEST 1)
(defparameter state STATE-INITIALISED)
-(define-condition authorization-error (error) ())
-
-
-(defmethod process-outgoing-packet((sm yarpc-state-machine))
- (format t "process-outgoing-packet called~%")
- (let ((packet (outgoing-packet sm)))
- (setf (outgoing-packet sm) nil)
- packet))
-
-;TODO queue and thread stuf
-(defmethod queue-outgoing-packet((sm yarpc-state-machine) packet)
- (setf (outgoing-packet sm) packet))
-
-;Process a call method packet, returns
-(defmethod process-incoming-packet ((sm yarpc-state-machine) (call call-method-packet))
- (assert (eql state STATE-INITIALISED))
- (format t "yarpc-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm call)
- (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)))))
-
-(defmethod process-incoming-packet ((sm yarpc-state-machine) (response method-response-packet))
- (assert (eql state STATE-INITIALISED))
- (format t "yarpc-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm response))
+(defmethod process-outgoing-packet((sm yarpc-client-state-machine))
+ (format t "process-outgoing-packet called, polling the job-queue ~%")
+ (let ((packet (nio-compat:take (job-queue sm) :blocking-call nil)))
+ (when packet
+ (format t "process-outgoing-packet got job ~A ~%" packet)
+ (setf state STATE-SENT-REQUEST))
+ packet))
+
+(defmethod process-incoming-packet ((sm yarpc-client-state-machine) (response method-response-packet))
+ (assert (eql state STATE-SENT-REQUEST))
+ (format t "yarpc-client-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm response)
+ (nio-compat:add (result-queue sm) response)
+ (setf state STATE-INITIALISED))
-
-(defun execute-call (call-string)
- (let* ((rpc-call-list (read-from-string call-string ))
- (fn (member (symbol-function (first rpc-call-list)) *remote-fns* )))
- (format t "fn - ~A authorised? : ~A~%" (symbol-function (first rpc-call-list)) fn)
- (if fn
- (apply (first rpc-call-list) (rest rpc-call-list))
- (error 'authorization-error))))
-
-
-(defmethod remote-execute ((sm yarpc-state-machine) call-string)
- (queue-outgoing-packet sm (make-instance 'call-method-packet :call-string call-string)))
-
\ No newline at end of file
+;Called from an external thread i.e. *not* the nio thread
+;Blocks calling thread on the remote m/c's response
+(defmethod remote-execute ((sm yarpc-client-state-machine) call-string)
+; (queue-outgoing-packet
+ (assert (eql state STATE-INITIALISED))
+ (nio-compat:add (job-queue sm) (make-instance 'call-method-packet :call-string call-string))
+ (nio-compat:take (result-queue sm)))
\ No newline at end of file
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp Wed Jan 17 00:06:13 2007
@@ -32,27 +32,57 @@
;;
;; A server that processes remote procedure calls and returns results
;;
-;; Test with:
-;; > telnet 127.0.0.1 16323
-;; Trying 127.0.0.1...
-;; Connected to 127.0.0.1.
-;; Escape character is '^]'.
-;; (test-rpc "who" 2 's)
-;; response - who 2 'S
-;;
(defclass yarpc-state-machine (state-machine)
- ((outgoing-packet :initarg :outgoing-packet
- :accessor outgoing-packet
- :initform nil)))
-
-(defun yarpc-state-machine ()
- (make-instance 'yarpc-state-machine))
+ ((job-queue :initarg :job-queue
+ :initform (error "Must supply a job queue to write work to.")
+ :accessor job-queue
+ :documentation "The queue used to hand off work from the NIO thread to an external thread for execution")
+ (result-queue :initform (nio-compat:concurrent-queue)
+ :accessor result-queue
+ :documentation "The queue used to return results from an external thread to the nio thread")))
+
+(defun yarpc-state-machine (read-fd write-fd socket job-queue)
+ (let ((sm (make-instance 'yarpc-state-machine :read-fd read-fd :write-fd write-fd :socket socket :job-queue job-queue)))
+ (nio-buffer:clear (foreign-read-buffer sm))
+ (nio-buffer:clear (foreign-write-buffer sm))
+ (format t "yarpc-state-machine - Created ~S~%" sm)
+ sm))
(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 "#<YARPC-STATE-MACHINE ~A >" (call-next-method sm nil)))
+
+(defconstant STATE-INITIALISED 0)
+(defconstant STATE-SEND-RESPONSE 1)
+
+(defparameter state STATE-INITIALISED)
+
+
+(defmethod process-outgoing-packet((sm yarpc-state-machine))
+ (format t "yarpc-state-machine: process-outgoing-packet called, polling the results-queue ~%")
+ (let ((packet (nio-compat:take (result-queue sm) :blocking-call nil)))
+ (format t "yarpc-state-machine: process-outgoing-packet got result ~A ~%" packet)
+ packet))
+
+
+;Process a call method packet, returns
+(defmethod process-incoming-packet ((sm yarpc-state-machine) (call call-method-packet))
+ (assert (eql state STATE-INITIALISED))
+ (format t "yarpc-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm call)
+ (nio-compat:add (job-queue sm) (cons (call-string call) (result-queue sm))))
+
+
+;Called from an external thread i.e. *not* the nio thread
+;Blocks waiting for a job (call-string,result-queue) to process and return the result into the result queue
+;(defmethod get-job ((sm yarpc-state-machine))
+; (values (nio-compat:take (job-queue sm)) (result-queue sm)))
+
+
+
;;TODO move somewhere suitable
(defparameter *remote-fns* nil)
@@ -71,56 +101,8 @@
(defremote test-rpc-string(a b c)
(format nil "response - ~A ~A ~A ~A~%" a b c (code-char #x2211)))
-;;end move TODO
-
-
-;;;Utils
-
-(defun print-hashtable (table &optional (stream t))
- (maphash #'(lambda (k v) (format stream "~a -> ~a~%" k v)) table))
-;;;
-
-
-(defmethod print-object ((sm yarpc-state-machine) stream)
- (format stream "#<YARPC-STATE-MACHINE ~A >" (call-next-method sm nil)))
-
-(defconstant STATE-INITIALISED 0)
-(defconstant STATE-SEND-RESPONSE 1)
-
-(defparameter state STATE-INITIALISED)
-
(define-condition authorization-error (error) ())
-
-(defmethod process-outgoing-packet((sm yarpc-state-machine))
- (format t "process-outgoing-packet called~%")
- (let ((packet (outgoing-packet sm)))
- (setf (outgoing-packet sm) nil)
- packet))
-
-;TODO queue and thread stuf
-(defmethod queue-outgoing-packet((sm yarpc-state-machine) packet)
- (setf (outgoing-packet sm) packet))
-
-;Process a call method packet, returns
-(defmethod process-incoming-packet ((sm yarpc-state-machine) (call call-method-packet))
- (assert (eql state STATE-INITIALISED))
- (format t "yarpc-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm call)
- (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)))))
-
-(defmethod process-incoming-packet ((sm yarpc-state-machine) (response method-response-packet))
- (assert (eql state STATE-INITIALISED))
- (format t "yarpc-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm response))
-
-
(defun execute-call (call-string)
(let* ((rpc-call-list (read-from-string call-string ))
(fn (member (symbol-function (first rpc-call-list)) *remote-fns* )))
@@ -129,7 +111,18 @@
(apply (first rpc-call-list) (rest rpc-call-list))
(error 'authorization-error))))
+;;end move TODO
+
+
+
+
-(defmethod remote-execute ((sm yarpc-state-machine) call-string)
- (queue-outgoing-packet sm (make-instance 'call-method-packet :call-string call-string)))
-
\ No newline at end of file
+; (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))))
\ No newline at end of file
1
0

17 Jan '07
Author: psmith
Date: Tue Jan 16 20:39:51 2007
New Revision: 39
Modified:
branches/home/psmith/restructure/src/compat/concurrent-queue.lisp
branches/home/psmith/restructure/src/compat/nio-compat-package.lisp
branches/home/psmith/restructure/src/compat/nio-compat.asd
Log:
asdf updates for queue
Modified: branches/home/psmith/restructure/src/compat/concurrent-queue.lisp
==============================================================================
--- branches/home/psmith/restructure/src/compat/concurrent-queue.lisp (original)
+++ branches/home/psmith/restructure/src/compat/concurrent-queue.lisp Tue Jan 16 20:39:51 2007
@@ -40,6 +40,9 @@
(buffer :initform nil
:accessor buffer)))
+(defun concurrent-queue()
+ (make-instance 'concurrent-queue))
+
(defmacro pop-elt(a-buffer loc)
`(if ,a-buffer
(let ((head (car ,a-buffer)))
Modified: branches/home/psmith/restructure/src/compat/nio-compat-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/compat/nio-compat-package.lisp (original)
+++ branches/home/psmith/restructure/src/compat/nio-compat-package.lisp Tue Jan 16 20:39:51 2007
@@ -31,4 +31,6 @@
;; errno.lisp
get-errno +ERRNO_EAGAIN+
+ ;;concurrent-queue
+ concurrent-queue add take
))
Modified: branches/home/psmith/restructure/src/compat/nio-compat.asd
==============================================================================
--- branches/home/psmith/restructure/src/compat/nio-compat.asd (original)
+++ branches/home/psmith/restructure/src/compat/nio-compat.asd Tue Jan 16 20:39:51 2007
@@ -6,6 +6,7 @@
:components ((:file "nio-compat-package")
(:file "errno" :depends-on ("nio-compat-package"))
+ (:file "concurrent-queue" :depends-on ("nio-compat-package"))
)
:depends-on ())
1
0

17 Jan '07
Author: psmith
Date: Tue Jan 16 20:34:39 2007
New Revision: 38
Added:
branches/home/psmith/restructure/src/compat/concurrent-queue.lisp
Log:
Added concurrent queue
inter thread communication via a FIFO queue
Added: branches/home/psmith/restructure/src/compat/concurrent-queue.lisp
==============================================================================
--- (empty file)
+++ branches/home/psmith/restructure/src/compat/concurrent-queue.lisp Tue Jan 16 20:34:39 2007
@@ -0,0 +1,85 @@
+#|
+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-compat)
+
+(declaim (optimize (debug 3) (speed 3) (space 0)))
+
+;Implements a threadsafe queue where readers wait for elements of a FIFO queue to appear using a waitqueue
+;Modified from sbcl manual example
+
+(defclass concurrent-queue()
+ ((buffer-queue :initform (sb-thread:make-waitqueue)
+ :reader buffer-queue)
+ (buffer-lock :initform (sb-thread:make-mutex :name "buffer lock")
+ :reader buffer-lock)
+ (buffer :initform nil
+ :accessor buffer)))
+
+(defmacro pop-elt(a-buffer loc)
+ `(if ,a-buffer
+ (let ((head (car ,a-buffer)))
+ (setf ,a-buffer (cdr ,a-buffer))
+#+nio-debug (format t "reader ~A woke, read ~A as ~A~%" sb-thread:*current-thread* head ,loc)
+ head)
+ nil))
+
+
+(defmethod take ((queue concurrent-queue))
+ (sb-thread:with-mutex ((buffer-lock queue))
+ ;if its there, pop it
+ (let ((ret (pop-elt (buffer queue) "1sttry")))
+ (if ret
+ ret
+ (progn
+ (sb-thread:condition-wait (buffer-queue queue) (buffer-lock queue))
+ (pop-elt (buffer queue) "2ndtry"))))))
+
+
+(defmethod add ((queue concurrent-queue) elt)
+ (sb-thread:with-mutex ((buffer-lock queue))
+ (setf (buffer queue) (append (buffer queue) (list elt)) )
+ (sb-thread:condition-notify (buffer-queue queue))))
+
+
+
+(defun test-writer(queue)
+ (loop for i from 0 to 999 do
+ (sleep 0.1)
+ (add queue i)))
+
+(defun test-reader(queue)
+ (loop
+ (format t "reader on ~A got elt ~A~%"
+ sb-thread:*current-thread* (take queue))))
+
+(defun test-queue()
+ (let ((queue (make-instance 'concurrent-queue)))
+ (sb-thread:make-thread #'(lambda()(test-writer queue)))
+ (sleep 10)
+ (sb-thread:make-thread #'(lambda()(test-reader queue)))
+ (sb-thread:make-thread #'(lambda()(test-reader queue)))))
1
0

[nio-cvs] r37 - branches/home/psmith/restructure/src/protocol/yarpc
by psmith@common-lisp.net 15 Jan '07
by psmith@common-lisp.net 15 Jan '07
15 Jan '07
Author: psmith
Date: Mon Jan 15 01:49:25 2007
New Revision: 37
Modified:
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
Log:
yarpc roundtrip complete
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp Mon Jan 15 01:49:25 2007
@@ -36,30 +36,48 @@
(make-instance 'yarpc-packet-factory))
(defconstant CALL-METHOD-PACKET-ID #x0)
-(defconstant METHOD-RESPONSE-PACKET-ID 1)
+(defconstant METHOD-RESPONSE-PACKET-ID #x1)
(defmethod get-packet ((pf yarpc-packet-factory) buf)
(flip buf)
-; (format t "get-packet::read string - ~A~%" (bytebuffer-read-string buf (remaining buf)))
- (if (>= (remaining buf) 1) ;; First byte denotes packet ID
- (ecase (elt (bytebuffer-read-vector buf 1) 0)
- (0 (progn (format t "got CALL-METHOD-PACKET-ID~%") (make-instance 'call-method-packet :call (bytebuffer-read-string buf (remaining buf)))))
- (1 (format t "got METHOD-RESPONSE-PACKET-ID~%")))))
+ (let ((ret (if (> (remaining buf) 0) ;; First byte denotes packet ID
+ (ecase (elt (bytebuffer-read-vector buf 1) 0)
+ (0 (progn (format t "got CALL-METHOD-PACKET-ID~%") (call-method-packet (bytebuffer-read-string buf (remaining buf)))))
+ (1 (progn (format t "got METHOD-RESPONSE-PACKET-ID~%") (method-response-packet (bytebuffer-read-string buf (remaining buf)))))))))
+ (if (> (remaining buf) 0)
+ (error 'not-implemented-yet)
+ (clear buf))
+ ret))
-(defclass call-method-packet (packet)((call-string :initarg :call
- :accessor get-call-string)))
+(defclass call-method-packet (packet)((call-string :initarg :call-string
+ :accessor call-string)))
+
+(defun call-method-packet (call-string)
+ (make-instance 'call-method-packet :call-string call-string))
(defmethod print-object ((packet call-method-packet) stream)
- (format stream "#<CALL-METHOD-PACKET ~A >" (get-call-string packet)))
+ (format stream "#<CALL-METHOD-PACKET ~A >" (call-string packet)))
(defmethod write-bytes((packet call-method-packet) buf)
(format t "yarpc-packet-factory:write-bytes - writing ~A to ~A~%" packet buf)
; (nio-buffer:flip buf)
(nio-buffer:bytebuffer-write-vector buf #(#x0))
- (nio-buffer:bytebuffer-write-string buf (get-call-string packet))
+ (nio-buffer:bytebuffer-write-string buf (call-string packet))
(format t "yarpc-packet-factory:write-bytes - written ~A~%" buf) )
-(defclass method-response-packet (packet)())
+(defclass method-response-packet (packet)
+ ((response :initarg :response
+ :accessor response)))
+
+(defun method-response-packet (response)
+ (make-instance 'method-response-packet :response response))
+(defmethod print-object ((packet method-response-packet) stream)
+ (format stream "#<METHID-RESPONSE-PACKET ~A >" (response packet)))
+(defmethod write-bytes((packet method-response-packet) buf)
+ (format t "yarpc-packet-factory:write-bytes - writing ~A to ~A~%" packet buf)
+ (nio-buffer:bytebuffer-write-vector buf #(#x1))
+ (nio-buffer:bytebuffer-write-string buf (write-to-string (response packet)))
+ (format t "yarpc-packet-factory:write-bytes - written ~A~%" buf) )
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp Mon Jan 15 01:49:25 2007
@@ -98,22 +98,28 @@
(setf (outgoing-packet sm) nil)
packet))
+;TODO queue and thread stuf
+(defmethod queue-outgoing-packet((sm yarpc-state-machine) packet)
+ (setf (outgoing-packet sm) packet))
;Process a call method packet, returns
(defmethod process-incoming-packet ((sm yarpc-state-machine) (call call-method-packet))
- ;todo change state, create method-response packet and return it
- ;(assert (eql state 0))
+ (assert (eql state STATE-INITIALISED))
(format t "yarpc-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm call)
(handler-case
- (let ((result (execute-call (get-call-string call))))
+ (let ((result (execute-call (call-string call))))
(when result
(let ((response-packet (progn
(setf state STATE-SEND-RESPONSE)
- (method-response-packet result))))
- (values response-packet t))))
- (reader-error (re) (format t "No such function ~A~%" (get-call-string call)))
- (authorization-error (ae) (format t "Function not declared with defremote ~A~%" (get-call-string call)))))
-
+ (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)))))
+
+(defmethod process-incoming-packet ((sm yarpc-state-machine) (response method-response-packet))
+ (assert (eql state STATE-INITIALISED))
+ (format t "yarpc-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm response))
+
(defun execute-call (call-string)
(let* ((rpc-call-list (read-from-string call-string ))
@@ -125,5 +131,5 @@
(defmethod remote-execute ((sm yarpc-state-machine) call-string)
- (setf (outgoing-packet sm) (make-instance 'call-method-packet :call call-string)))
+ (queue-outgoing-packet sm (make-instance 'call-method-packet :call-string call-string)))
\ No newline at end of file
1
0

[nio-cvs] r36 - in branches/home/psmith/restructure: . src/buffer src/io src/protocol/yarpc src/statemachine
by psmith@common-lisp.net 15 Jan '07
by psmith@common-lisp.net 15 Jan '07
15 Jan '07
Author: psmith
Date: Sun Jan 14 23:52:17 2007
New Revision: 36
Modified:
branches/home/psmith/restructure/run-yarpc-client.lisp
branches/home/psmith/restructure/src/buffer/buffer.lisp
branches/home/psmith/restructure/src/io/nio-server.lisp
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
branches/home/psmith/restructure/src/statemachine/state-machine.lisp
Log:
yarpc ready to create response packet
Modified: branches/home/psmith/restructure/run-yarpc-client.lisp
==============================================================================
--- branches/home/psmith/restructure/run-yarpc-client.lisp (original)
+++ branches/home/psmith/restructure/run-yarpc-client.lisp Sun Jan 14 23:52:17 2007
@@ -6,6 +6,6 @@
(sleep 4)
(let ((sm (nio:add-connection "127.0.0.1" 16323 'nio-yarpc:yarpc-state-machine)))
(format t "toplevel adding conn ~A~%" sm)
-(format t "Result of remote-execute ~A~%" (nio-yarpc:remote-execute sm "(test-rpc-list)")))
+(format t "Result of remote-execute ~A~%" (nio-yarpc:remote-execute sm "(nio-yarpc:test-rpc-list)")))
Modified: branches/home/psmith/restructure/src/buffer/buffer.lisp
==============================================================================
--- branches/home/psmith/restructure/src/buffer/buffer.lisp (original)
+++ branches/home/psmith/restructure/src/buffer/buffer.lisp Sun Jan 14 23:52:17 2007
@@ -121,8 +121,8 @@
;reads bytes from byte-buffer and returns a vector (unsigned-byte 8)
(defmethod bytebuffer-read-vector((bb byte-buffer) &optional (num-bytes-to-read (remaining bb)))
(let ((vec (make-uint8-seq num-bytes-to-read)))
- (with-slots (buf) bb
- (inc-position bb (cffi:mem-read-vector vec (buffer-buf bb) :unsigned-char num-bytes-to-read)))
+ (with-slots (buf position) bb
+ (inc-position bb (cffi:mem-read-vector vec buf :unsigned-char num-bytes-to-read position)))
vec))
; Read bytes from bytebuffer abd return a string using the supplied decoding
Modified: branches/home/psmith/restructure/src/io/nio-server.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/nio-server.lisp (original)
+++ branches/home/psmith/restructure/src/io/nio-server.lisp Sun Jan 14 23:52:17 2007
@@ -92,11 +92,11 @@
(declare (ignore cond))
(format t "Poll-error, exiting..~%")
(throw 'poll-error-exit nil))))
-
- (loop for unix-epoll-events = (poll-events event-queue) do
+
+ (loop
+ (let ((unix-epoll-events (poll-events event-queue)))
(loop for (fd . event) in unix-epoll-events do
(cond
-
;; new connection
((= fd sock)
(let ((async-fd (socket-accept fd connection-type)))
@@ -135,7 +135,7 @@
(throw 'error-exit nil))))
(when (read-event-p event) (setf (read-ready async-fd) t))
- (when (write-event-p event) (setf (write-ready async-fd) t))))))))
+ (when (write-event-p event) (setf (write-ready async-fd) t)))))))))
(format t "Process client adds~%")
;add outgoing sockets to event queue
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp Sun Jan 14 23:52:17 2007
@@ -43,7 +43,7 @@
; (format t "get-packet::read string - ~A~%" (bytebuffer-read-string buf (remaining buf)))
(if (>= (remaining buf) 1) ;; First byte denotes packet ID
(ecase (elt (bytebuffer-read-vector buf 1) 0)
- (0 (progn (format t "got CALL-METHOD-PACKET-ID~%") (make-instance 'call-method-packet (bytebuffer-read-string buf (remaining buf)))))
+ (0 (progn (format t "got CALL-METHOD-PACKET-ID~%") (make-instance 'call-method-packet :call (bytebuffer-read-string buf (remaining buf)))))
(1 (format t "got METHOD-RESPONSE-PACKET-ID~%")))))
(defclass call-method-packet (packet)((call-string :initarg :call
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp Sun Jan 14 23:52:17 2007
@@ -100,9 +100,10 @@
;Process a call method packet, returns
-(defmethod process-incomming-packet ((sm yarpc-state-machine) (call call-method-packet))
+(defmethod process-incoming-packet ((sm yarpc-state-machine) (call call-method-packet))
;todo change state, create method-response packet and return it
;(assert (eql state 0))
+ (format t "yarpc-state-machine:process-incoming-packet called :sm ~A :packet ~A~%" sm call)
(handler-case
(let ((result (execute-call (get-call-string call))))
(when result
Modified: branches/home/psmith/restructure/src/statemachine/state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/statemachine/state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/statemachine/state-machine.lisp Sun Jan 14 23:52:17 2007
@@ -31,7 +31,7 @@
;
;Base class for state machines
;
-;Converts incomming data between bytes and packets using the supplied packet-factory.
+;Converts incoming data between bytes and packets using the supplied packet-factory.
;Converts outgoing data between packets and bytes using the write-bytes method on packet.
;
;This way only the protocols packet heirarchy knows about binary representations and
@@ -42,7 +42,7 @@
(defmethod print-object ((sm state-machine) stream)
(format stream "#<STATE-MACHINE ~A >" (call-next-method sm nil)))
-(defgeneric process-incomming-packet(state-machine packet))
+(defgeneric process-incoming-packet(state-machine packet))
(defgeneric process-outgoing-packet(state-machine))
@@ -54,10 +54,10 @@
;Use the packet factory to obtain any valid packet and pass it through
(defmethod process-read((sm state-machine))
(with-slots (foreign-read-buffer) sm
- (let ((incomming-packet (get-packet (get-packet-factory sm) foreign-read-buffer)))
- (format t "state-machine::process-read - incomming packet: ~A~%" incomming-packet)
- (when incomming-packet
- (when (not (process-incomming-packet sm incomming-packet))
+ (let ((incoming-packet (get-packet (get-packet-factory sm) foreign-read-buffer)))
+ (format t "state-machine::process-read - incoming packet: ~A~%" incoming-packet)
+ (when incoming-packet
+ (when (not (process-incoming-packet sm incoming-packet))
(close-sm sm))))))
;The connection is write ready.
1
0

[nio-cvs] r35 - in branches/home/psmith/restructure: . src/buffer src/event src/io src/protocol/yarpc src/statemachine
by psmith@common-lisp.net 15 Jan '07
by psmith@common-lisp.net 15 Jan '07
15 Jan '07
Author: psmith
Date: Sun Jan 14 23:00:39 2007
New Revision: 35
Added:
branches/home/psmith/restructure/run-yarpc-client.lisp
Modified:
branches/home/psmith/restructure/src/buffer/buffer.lisp
branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp
branches/home/psmith/restructure/src/event/epoll.lisp
branches/home/psmith/restructure/src/io/async-fd.lisp
branches/home/psmith/restructure/src/io/nio-package.lisp
branches/home/psmith/restructure/src/io/nio-server.lisp
branches/home/psmith/restructure/src/io/packet.lisp
branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp
branches/home/psmith/restructure/src/statemachine/state-machine.lisp
Log:
yarpc - Send packet OK
Added: branches/home/psmith/restructure/run-yarpc-client.lisp
==============================================================================
--- (empty file)
+++ branches/home/psmith/restructure/run-yarpc-client.lisp Sun Jan 14 23:00:39 2007
@@ -0,0 +1,11 @@
+(push :nio-debug *features*)
+(require :asdf)
+(require :nio-yarpc)
+
+(sb-thread:make-thread #'(lambda()(nio:start-server 'identity 'identity 'nio-yarpc:yarpc-state-machine :host "127.0.0.1" :port 9897)) :name "nio-server")
+(sleep 4)
+(let ((sm (nio:add-connection "127.0.0.1" 16323 'nio-yarpc:yarpc-state-machine)))
+(format t "toplevel adding conn ~A~%" sm)
+(format t "Result of remote-execute ~A~%" (nio-yarpc:remote-execute sm "(test-rpc-list)")))
+
+
Modified: branches/home/psmith/restructure/src/buffer/buffer.lisp
==============================================================================
--- branches/home/psmith/restructure/src/buffer/buffer.lisp (original)
+++ branches/home/psmith/restructure/src/buffer/buffer.lisp Sun Jan 14 23:00:39 2007
@@ -131,17 +131,24 @@
(sb-ext:octets-to-string (bytebuffer-read-vector bb num-bytes-to-read) :external-format external-format))
+;grrr...
+;(defmethod bytebuffer-write-byte ((bb byte-buffer) value)
+; (cffi:%mem-set value (buffer-buf bb) :unsigned-char position)
+; (inc-position bb 1))
+
;; Write bytes from vector vec to bytebuffer
(defmethod bytebuffer-write-vector((bb byte-buffer) vec)
:documentation "Returns number of bytes written to bytebuffer"
- (if (> (remaining bb) 0)
- 0
+ (format t "bytebuffer-write-vector - called with ~A ~A"bb vec)
+; (if (> (remaining bb) 0)
+; 0
(progn
- (clear bb)
- (let ((bytes-written (cffi:mem-write-vector vec (buffer-buf bb) :unsigned-char)))
- (format t "bytebuffer-write-vector - byteswritten: ~A" bytes-written)
+; (clear bb)
+ (let ((bytes-written (cffi:mem-write-vector vec (buffer-buf bb) :unsigned-char (length vec) (buffer-position bb))))
+ (format t "bytebuffer-write-vector - byteswritten: ~A~%" bytes-written)
(inc-position bb bytes-written)
- bytes-written))))
+ bytes-written)))
+;)
;; Writes data from string str to bytebuffer using specified encoding
;TODO move string-to-octets into nio-compat
Modified: branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp (original)
+++ branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp Sun Jan 14 23:00:39 2007
@@ -27,5 +27,5 @@
(defpackage :nio-buffer (:use :cl)
(:export
- byte-buffer free-buffer remaining inc-position get-string buffer-buf bytebuffer-write-vector bytebuffer-write-string bytebuffer-read-vector bytebuffer-read-string flip
+ byte-buffer free-buffer remaining inc-position get-string buffer-buf bytebuffer-write-vector bytebuffer-write-string bytebuffer-read-vector bytebuffer-read-string flip clear buffer-position
))
Modified: branches/home/psmith/restructure/src/event/epoll.lisp
==============================================================================
--- branches/home/psmith/restructure/src/event/epoll.lisp (original)
+++ branches/home/psmith/restructure/src/event/epoll.lisp Sun Jan 14 23:00:39 2007
@@ -76,14 +76,14 @@
#+nio-debug (format t "poll-events called with :event-queue ~A~%" event-queue)
(with-foreign-object (events 'epoll-event +epoll-size+)
(memzero events (* +epoll-event-size+ +epoll-size+))
- (loop for res = (%epoll-wait event-queue events +epoll-size+ -1)
+ (loop for res = (%epoll-wait event-queue events +epoll-size+ 1000)
do
(progn
#+nio-debug (format t "poll-events - dealing with ~A~%" res)
(case res
(-1 (error 'poll-error))
- (0 nil)
+ (return nil)
(t
(let ((idents nil))
(loop for i from 0 below res do
Modified: branches/home/psmith/restructure/src/io/async-fd.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/async-fd.lisp (original)
+++ branches/home/psmith/restructure/src/io/async-fd.lisp Sun Jan 14 23:00:39 2007
@@ -47,18 +47,19 @@
(read-fd :initarg :read-fd
:accessor read-fd)
- (foreign-read-buffer :initform (byte-buffer 4096))
- (foreign-write-buffer :initform (byte-buffer 4096)
+ (foreign-read-buffer :initform (byte-buffer 1024)
+ :accessor foreign-read-buffer)
+ (foreign-write-buffer :initform (byte-buffer 1024)
:accessor foreign-write-buffer)
;; (lisp-read-buffer :initform (make-uint8-seq 1024))
;; (lisp-read-buffer-write-ptr :initform 0)
- (read-ready-p :initform nil
- :accessor read-ready-p
+ (read-ready :initform nil
+ :accessor read-ready
:documentation "Have we been notified as read ready and not received EAGAIN from %read?")
- (write-ready-p :initform nil
- :accessor write-ready-p
+ (write-ready :initform nil
+ :accessor write-ready
:documentation "Have we been notified as write ready and not received EAGAIN from %write?")
(close-pending :initform nil)
@@ -73,21 +74,28 @@
(defmethod print-object ((async-fd async-fd) stream)
- (with-slots (read-fd write-fd) async-fd
- (format stream "#<ASYNC-FD r/w fd: ~D/~D.>"
- read-fd write-fd)))
+ (with-slots (socket read-fd write-fd) async-fd
+ (format stream "#<ASYNC-FD :socket ~D :read-fd ~D :write-fd ~D.>"
+ socket read-fd write-fd)))
+;;Implement this in concrete SM for read
+(defgeneric process-read (async-fd))
+
+;;Implement this in concrete SM for read
+(defgeneric process-write (async-fd))
-;;SM factory
+
+
+;Loop over state machines calling process-outgoing-packets via state-machine::process-write
+
+;;SM factory
(defun create-state-machine(sm-type read-fd write-fd socket)
(let ((sm (make-instance sm-type :read-fd read-fd :write-fd write-fd :socket socket)))
(format t "create-state-machine - Created ~S~%" sm)
- (nio-buffer:flip (foreign-write-buffer sm))
+ (nio-buffer:clear (foreign-read-buffer sm))
+ (nio-buffer:clear (foreign-write-buffer sm))
sm))
-;;Implement this in concrete SM for read
-(defgeneric process-read (async-fd))
-
;;override this in concrete SM for close
;(defmethod process-close((async-fd async-fd)reason)())
(defmethod process-close((async-fd async-fd)reason)())
@@ -108,9 +116,9 @@
(with-slots (foreign-read-buffer read-fd) state-machine
(format t "read-more called with ~A~%" state-machine)
-#+nio-debug (format t "read-more - calling read()~%")
+#+nio-debug (format t "read-more - calling read() into ~A~%" foreign-read-buffer)
(let ((new-bytes (%read read-fd (buffer-buf foreign-read-buffer) (remaining foreign-read-buffer))))
-#+nio-debug (format t "read-more : Read ~A bytes~%" new-bytes)
+#+nio-debug (format t "read-more : Read ~A bytes into ~A~%" new-bytes foreign-read-buffer)
(cond
((< new-bytes 0)
(progn
@@ -124,13 +132,8 @@
nil);;(throw 'end-of-file nil))
(t
- (progn
;;Update buffer position
- (inc-position foreign-read-buffer new-bytes)
-
-#+nio-debug (format t "read-more prior to process :buffer ~A~%" foreign-read-buffer)
- (process-read state-machine)))))))
-
+ (inc-position foreign-read-buffer new-bytes))))))
(defun close-async-fd (async-fd)
"Close ASYNC-FD's fd after everything has been written from write-queue."
@@ -149,33 +152,38 @@
(defun write-more (async-fd)
"Write data from ASYNC-FD's write bytebuffer"
-#+nio-debug (format t "write-more called with ~A~%" async-fd)
+#+nio-debug (format t "async-fd:write-more - called with ~A~%" async-fd)
(with-slots (write-fd foreign-write-buffer close-pending) async-fd
- (setf (write-ready-p async-fd) t)
-#+nio-debug (format t "foreign-write-buffer b4 flip ~A~%" foreign-write-buffer)
+#+nio-debug (format t "async-fd:write-more - foreign-write-buffer b4 flip ~A~%" foreign-write-buffer)
(nio-buffer:flip foreign-write-buffer)
-#+nio-debug (format t "foreign-write-buffer after flip ~A~%" foreign-write-buffer)
+#+nio-debug (format t "async-fd:write-more -foreign-write-buffer after flip ~A~%" foreign-write-buffer)
(let ((now-written 0))
(do ((total-written 0))
((or (eql now-written -1) (eql (remaining foreign-write-buffer) 0)) total-written)
(progn
(setf now-written (%write write-fd (buffer-buf foreign-write-buffer) (remaining foreign-write-buffer)))
-
- (format t "after write :foreign-write-buffer ~A :now-written ~A :total-written ~A ~%" foreign-write-buffer now-written total-written)
+
(when (not (eql now-written -1))
(inc-position foreign-write-buffer now-written)
- (incf total-written now-written))))
+ (incf total-written now-written)))
+#+nio-debug (format t "async-fd:write-more - after write :foreign-write-buffer ~A :now-written ~A :total-written ~A ~%" foreign-write-buffer now-written total-written)
+ )
+
+ (if (eql now-written -1)
;;Deal with failure
- (when (eql now-written -1)
(let ((err (get-errno)))
(format t "write-more - write returned -1 :errno ~A~%" err)
(unless (eql err 11) ;; eagain - failed to write whole buffer need to wait for next notify
(let ((err-cond (make-instance 'write-error :error err)))
(close err-cond)
- (error err-cond))))))
+ (error err-cond))))
+ ;;update buffers
+ (if (eql (remaining foreign-write-buffer) 0)
+ (clear foreign-write-buffer)
+ (error 'not-implemented-yet))))
#+nio-debug (format t "write buffer after write :~A~%" foreign-write-buffer)
(when (eql (remaining foreign-write-buffer) 0)
Modified: branches/home/psmith/restructure/src/io/nio-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/nio-package.lisp (original)
+++ branches/home/psmith/restructure/src/io/nio-package.lisp Sun Jan 14 23:00:39 2007
@@ -29,13 +29,13 @@
(:export
;; async-fd.lisp
- async-fd process-read foreign-read-buffer foreign-write-buffer close-sm
+ async-fd process-read process-write foreign-read-buffer foreign-write-buffer close-sm
;; async-socket.lisp
;;nio-server
- start-server
+ start-server add-connection
;;packet
- packet
+ packet write-bytes
))
Modified: branches/home/psmith/restructure/src/io/nio-server.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/nio-server.lisp (original)
+++ branches/home/psmith/restructure/src/io/nio-server.lisp Sun Jan 14 23:00:39 2007
@@ -33,6 +33,28 @@
;; (format t "Accepting connection from ~S:~D [~A].~%" host port proto)
t)
+;TODO thread safety
+(defparameter +connected-sockets+ nil
+ "List of sockets that have been connected and are awaiting addition to the event-notification system")
+
+;loop over hashtable
+(defun process-async-fds (client-hash)
+ (maphash #'(lambda (k async-fd)
+ (format t "Dealing with ~a => ~a~%" k async-fd)
+
+ ;process reads
+ (when (read-ready async-fd) (read-more async-fd))
+ (when (> (buffer-position (foreign-read-buffer async-fd)) 0)
+ (process-read async-fd))
+
+ ;process-writes
+ (process-write async-fd)
+ (when (write-ready async-fd) (write-more async-fd)))
+ client-hash))
+
+
+
+
(defun start-server (connection-handler accept-filter connection-type
&key
(protocol :inet)
@@ -70,9 +92,8 @@
(declare (ignore cond))
(format t "Poll-error, exiting..~%")
(throw 'poll-error-exit nil))))
-
- (loop for unix-epoll-events = (poll-events event-queue) do
-
+
+ (loop for unix-epoll-events = (poll-events event-queue) do
(loop for (fd . event) in unix-epoll-events do
(cond
@@ -113,10 +134,41 @@
(force-close-async-fd async-fd)
(throw 'error-exit nil))))
- (when (read-event-p event) (read-more async-fd))
- (when (write-event-p event) (write-more async-fd))
- )))
- ))
- )))))
+ (when (read-event-p event) (setf (read-ready async-fd) t))
+ (when (write-event-p event) (setf (write-ready async-fd) t))))))))
+ (format t "Process client adds~%")
+
+ ;add outgoing sockets to event queue
+ (format t "start-server::sockets enqueued ~A~%" +connected-sockets+)
+ (loop for new-fd in +connected-sockets+ do
+ (format t "Dealing with ~A~%" new-fd)
+ (setf (gethash (async-fd-read-fd new-fd) client-hash) new-fd)
+ (add-async-fd event-queue new-fd :read-write))
+
+ ;TODO thread safety
+ (setf +connected-sockets+ nil)
+
+ ;loop over async-fd's processing where necessary
+ (process-async-fds client-hash)
+ ))))
(ignore-errors
(close-fd sock))))
+
+
+(defun add-connection (host port connection-type
+ &key
+ (protocol :inet)
+
+ )
+ (let ((sock nil))
+ (setq sock (ecase protocol
+ (:inet (make-inet-socket))
+ (:inet6 (make-inet6-socket))))
+
+ (if (connect-inet-socket sock host port)
+ (let ((sm (create-state-machine connection-type sock sock sock)))
+ (push sm +connected-sockets+)
+ (format t "add-connection::sockets enqueued ~A~%" +connected-sockets+)
+ (return-from add-connection sm))
+ (format t "Connect failed!!~A ~%" (get-errno)))))
+
\ No newline at end of file
Modified: branches/home/psmith/restructure/src/io/packet.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/packet.lisp (original)
+++ branches/home/psmith/restructure/src/io/packet.lisp Sun Jan 14 23:00:39 2007
@@ -28,13 +28,13 @@
;; state-machines instantiate packets for the associated protocol
;; either based on incomming data from a packet factory or in
-;; preperation for sending a packet for the current protocol.
+;; preparation for sending a packet for the current protocol.
+;;
+;; All concete packets implement write-bytes for xfer to the io layer
-;; All concete packets implement get-bytes for xfer to the io layer
(defclass packet ()
())
-(defmethod get-bytes((a-packet packet))
- ())
-
+;Implement in concrete
+(defgeneric write-bytes(packet nio-buffer))
Modified: branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp Sun Jan 14 23:00:39 2007
@@ -29,5 +29,5 @@
(:export
;; yarpc-state-machine
- yarpc-state-machine yarpc-state-machine-factory test-rpc test-rpc-list test-rpc-string get-packet-factory
+ yarpc-state-machine yarpc-state-machine-factory test-rpc test-rpc-list test-rpc-string get-packet-factory remote-execute
))
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp Sun Jan 14 23:00:39 2007
@@ -35,11 +35,11 @@
(defun yarpc-packet-factory ()
(make-instance 'yarpc-packet-factory))
-(defconstant CALL-METHOD-PACKET-ID 0)
+(defconstant CALL-METHOD-PACKET-ID #x0)
(defconstant METHOD-RESPONSE-PACKET-ID 1)
(defmethod get-packet ((pf yarpc-packet-factory) buf)
- (nio-buffer:flip buf)
+ (flip buf)
; (format t "get-packet::read string - ~A~%" (bytebuffer-read-string buf (remaining buf)))
(if (>= (remaining buf) 1) ;; First byte denotes packet ID
(ecase (elt (bytebuffer-read-vector buf 1) 0)
@@ -49,6 +49,17 @@
(defclass call-method-packet (packet)((call-string :initarg :call
:accessor get-call-string)))
+(defmethod print-object ((packet call-method-packet) stream)
+ (format stream "#<CALL-METHOD-PACKET ~A >" (get-call-string packet)))
+
+(defmethod write-bytes((packet call-method-packet) buf)
+ (format t "yarpc-packet-factory:write-bytes - writing ~A to ~A~%" packet buf)
+; (nio-buffer:flip buf)
+ (nio-buffer:bytebuffer-write-vector buf #(#x0))
+ (nio-buffer:bytebuffer-write-string buf (get-call-string packet))
+ (format t "yarpc-packet-factory:write-bytes - written ~A~%" buf) )
+
+
(defclass method-response-packet (packet)())
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp Sun Jan 14 23:00:39 2007
@@ -40,7 +40,10 @@
;; (test-rpc "who" 2 's)
;; response - who 2 'S
;;
-(defclass yarpc-state-machine (state-machine)())
+(defclass yarpc-state-machine (state-machine)
+ ((outgoing-packet :initarg :outgoing-packet
+ :accessor outgoing-packet
+ :initform nil)))
(defun yarpc-state-machine ()
(make-instance 'yarpc-state-machine))
@@ -88,8 +91,16 @@
(define-condition authorization-error (error) ())
+
+(defmethod process-outgoing-packet((sm yarpc-state-machine))
+ (format t "process-outgoing-packet called~%")
+ (let ((packet (outgoing-packet sm)))
+ (setf (outgoing-packet sm) nil)
+ packet))
+
+
;Process a call method packet, returns
-(defmethod process-packet ((sm yarpc-state-machine) (call call-method-packet))
+(defmethod process-incomming-packet ((sm yarpc-state-machine) (call call-method-packet))
;todo change state, create method-response packet and return it
;(assert (eql state 0))
(handler-case
@@ -111,3 +122,7 @@
(apply (first rpc-call-list) (rest rpc-call-list))
(error 'authorization-error))))
+
+(defmethod remote-execute ((sm yarpc-state-machine) call-string)
+ (setf (outgoing-packet sm) (make-instance 'call-method-packet :call call-string)))
+
\ No newline at end of file
Modified: branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp (original)
+++ branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp Sun Jan 14 23:00:39 2007
@@ -29,5 +29,5 @@
(:export
;; state-machine
- state-machine packet-factory get-packet-factory get-packet
+ state-machine packet-factory get-packet-factory get-packet process-outgoing-packet process-incoming-packet
))
Modified: branches/home/psmith/restructure/src/statemachine/state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/statemachine/state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/statemachine/state-machine.lisp Sun Jan 14 23:00:39 2007
@@ -32,7 +32,7 @@
;Base class for state machines
;
;Converts incomming data between bytes and packets using the supplied packet-factory.
-;Converts outgoing data between packets and bytes using the get-bytes method on packet.
+;Converts outgoing data between packets and bytes using the write-bytes method on packet.
;
;This way only the protocols packet heirarchy knows about binary representations and
; the SM can deal with protocol logic and state maintenance
@@ -42,21 +42,32 @@
(defmethod print-object ((sm state-machine) stream)
(format stream "#<STATE-MACHINE ~A >" (call-next-method sm nil)))
-(defgeneric process-packet(state-machine packet))
+(defgeneric process-incomming-packet(state-machine packet))
+
+
+(defgeneric process-outgoing-packet(state-machine))
+
(defgeneric get-packet-factory(state-machine))
+;The connection is read ready.
+;Use the packet factory to obtain any valid packet and pass it through
(defmethod process-read((sm state-machine))
- (with-slots (foreign-read-buffer foreign-write-buffer) sm
+ (with-slots (foreign-read-buffer) sm
(let ((incomming-packet (get-packet (get-packet-factory sm) foreign-read-buffer)))
(format t "state-machine::process-read - incomming packet: ~A~%" incomming-packet)
(when incomming-packet
- (multiple-value-bind (ret-packet close) (process-packet sm incomming-packet)
- (format t "state-machine::process-read - return packet: ~A~%" ret-packet)
- (when ret-packet (put-packet ret-packet foreign-write-buffer))
- (if close
- (close-sm sm)
- ))))))
+ (when (not (process-incomming-packet sm incomming-packet))
+ (close-sm sm))))))
+
+;The connection is write ready.
+;See if theres anything ready to be written in the SM
+(defmethod process-write((sm state-machine))
+ (with-slots (foreign-write-buffer) sm
+ (let ((outgoing-packet (process-outgoing-packet sm)))
+ (format t "state-machine::process-write - outgoing packet: ~A~%" outgoing-packet)
+ (when outgoing-packet (write-bytes outgoing-packet foreign-write-buffer)))))
+
@@ -65,7 +76,3 @@
; Get the packet in buf using the packet factory
(defgeneric get-packet (packet-factory buf))
-
-; Write the packet to the buffer
-(defun put-packet (packet buf)
- (nio-buffer:bytebuffer-write-vector buf (get-bytes packet)))
1
0

15 Jan '07
Author: psmith
Date: Sun Jan 14 21:51:30 2007
New Revision: 34
Modified:
branches/home/psmith/restructure/src/io/async-socket.lisp
Log:
Corrected socketaddr-in structure for linux
Added connect functionalilty
Modified: branches/home/psmith/restructure/src/io/async-socket.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/async-socket.lisp (original)
+++ branches/home/psmith/restructure/src/io/async-socket.lisp Sun Jan 14 21:51:30 2007
@@ -36,6 +36,7 @@
(defconstant +sock-stream+ 1)
(defconstant +sock-dgram+ 2)
+#+(or darwin macosx freebsd)
(defcstruct sockaddr-in
(len :uint8)
(family :uint8)
@@ -43,6 +44,13 @@
(addr :uint32)
(zero :char :count 8))
+#+linux
+(defcstruct sockaddr-in
+ (family :uint16)
+ (port :uint16)
+ (addr :uint32)
+ (zero :char :count 8))
+
(defconstant +sockaddr-in-len+ #.(+ 1 1 2 4 8))
(defcstruct sockaddr-in6
@@ -82,6 +90,12 @@
(sockaddr :pointer)
(socklen :pointer))
+(defcfun ("connect" %connect) :int
+ (socket :int)
+ (sockaddr :pointer)
+ (socklent :int))
+
+
;;TODO put backlog on config
(defun start-listen (socket-fd &optional (backlog 1000))
(%listen socket-fd backlog))
@@ -92,25 +106,42 @@
(defun make-inet-socket (&optional (type :tcp))
(%socket +af-inet+ (ecase type (:tcp +sock-stream+) (:udp +sock-dgram+)) 0))
-(defun bind-inet-socket (socket-fd port &optional (addr "127.0.0.1"))
- (with-foreign-object (sa 'sockaddr-in)
+
+(defun init-inet-socket(sa port addr)
(memzero sa +sockaddr-in-len+)
;; init struct
- (setf (foreign-slot-value sa 'sockaddr-in 'len) +sockaddr-in-len+
- (foreign-slot-value sa 'sockaddr-in 'port) (%htons port)
+ #+(or darwin macosx freebsd)
+ (setf (foreign-slot-value sa 'sockaddr-in 'len) +sockaddr-in-len+)
+
+ (setf (foreign-slot-value sa 'sockaddr-in 'port) (%htons port)
(foreign-slot-value sa 'sockaddr-in 'family) +af-inet+)
;; set addr
(if (/= (%inet-pton +af-inet+ addr (foreign-slot-pointer sa 'sockaddr-in 'addr)) 1)
- (error "inet_pton: Bad address ~A!" addr))
+ (error "inet_pton: Bad address ~A!" addr)))
+
+
+(defun bind-inet-socket (socket-fd port &optional (addr "127.0.0.1"))
+ (with-foreign-object (sa 'sockaddr-in)
+ (init-inet-socket sa port addr)
;; bind
(if (= (%bind socket-fd sa +sockaddr-in-len+) 0)
t
nil)))
+(defun connect-inet-socket (socket-fd addr port)
+ (with-foreign-object (sa 'sockaddr-in)
+ (init-inet-socket sa port addr)
+
+ (let ((res (%connect socket-fd sa +sockaddr-in-len+)))
+ (format t "connect ~A ~A~%" res (get-errno))
+ (if (= res -1)
+ nil
+ t))))
+
;;;; IPv6
@@ -146,6 +177,7 @@
(remote-port :initform nil :initarg :remote-port)))
+
(defun socket-accept (socket-fd connection-type)
"Accept connection from SOCKET-FD. Allocates and returns socket structure denoting the connection."
1
0

[nio-cvs] r33 - in branches/home/psmith/restructure: . src/buffer src/protocol/yarpc src/statemachine
by psmith@common-lisp.net 12 Jan '07
by psmith@common-lisp.net 12 Jan '07
12 Jan '07
Author: psmith
Date: Fri Jan 12 01:44:39 2007
New Revision: 33
Added:
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp
Modified:
branches/home/psmith/restructure/run-yarpc.lisp
branches/home/psmith/restructure/src/buffer/buffer.lisp
branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp
branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp
branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc.asd
branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp
branches/home/psmith/restructure/src/statemachine/state-machine.lisp
Log:
yarpc progress...
Modified: branches/home/psmith/restructure/run-yarpc.lisp
==============================================================================
--- branches/home/psmith/restructure/run-yarpc.lisp (original)
+++ branches/home/psmith/restructure/run-yarpc.lisp Fri Jan 12 01:44:39 2007
@@ -1,4 +1,5 @@
(push :nio-debug *features*)
(require :asdf)
(require :nio-yarpc)
+
(nio:start-server 'identity 'identity 'nio-yarpc:yarpc-state-machine :host "127.0.0.1")
Modified: branches/home/psmith/restructure/src/buffer/buffer.lisp
==============================================================================
--- branches/home/psmith/restructure/src/buffer/buffer.lisp (original)
+++ branches/home/psmith/restructure/src/buffer/buffer.lisp Fri Jan 12 01:44:39 2007
@@ -118,33 +118,37 @@
(setf position 0)
byte-buffer))
+;reads bytes from byte-buffer and returns a vector (unsigned-byte 8)
+(defmethod bytebuffer-read-vector((bb byte-buffer) &optional (num-bytes-to-read (remaining bb)))
+ (let ((vec (make-uint8-seq num-bytes-to-read)))
+ (with-slots (buf) bb
+ (inc-position bb (cffi:mem-read-vector vec (buffer-buf bb) :unsigned-char num-bytes-to-read)))
+ vec))
+
+; Read bytes from bytebuffer abd return a string using the supplied decoding
+;TODO move octets-to-string into nio-compat
+(defmethod bytebuffer-read-string((bb byte-buffer) &optional (num-bytes-to-read (remaining bb)) (external-format :ascii))
+ (sb-ext:octets-to-string (bytebuffer-read-vector bb num-bytes-to-read) :external-format external-format))
-(defmethod get-string((byte-buffer byte-buffer))
- (flip byte-buffer)
- (with-slots (position limit buf) byte-buffer
- (let ((tmp (make-uint8-seq (remaining byte-buffer))))
- (inc-position byte-buffer (cffi:mem-read-vector tmp buf :unsigned-char limit))
- (format t " read: ~A~%" (sb-ext:octets-to-string tmp :external-format :ascii))
- tmp)))
-
-;;TODO
-;;mem-write-vector (vector ptr type &optional (count (length vector)) (offset 0))
-(defmethod bytebuffer-write-string((byte-buffer byte-buffer) str &optional (index 0) (external-format :ascii))
- :documentation "Returns number of bytes written to bytebuffer"
- (bytebuffer-write-vector byte-buffer (sb-ext:string-to-octets str :external-format external-format)))
-;;TODO rename
-(defmethod bytebuffer-write-vector((byte-buffer byte-buffer) vec &optional (index 0))
+;; Write bytes from vector vec to bytebuffer
+(defmethod bytebuffer-write-vector((bb byte-buffer) vec)
:documentation "Returns number of bytes written to bytebuffer"
- (if (> (remaining byte-buffer) 0)
+ (if (> (remaining bb) 0)
0
(progn
- (clear byte-buffer)
- (let ((bytes-written (cffi:mem-write-vector vec (buffer-buf byte-buffer) :unsigned-char)))
+ (clear bb)
+ (let ((bytes-written (cffi:mem-write-vector vec (buffer-buf bb) :unsigned-char)))
(format t "bytebuffer-write-vector - byteswritten: ~A" bytes-written)
- (inc-position byte-buffer bytes-written)
+ (inc-position bb bytes-written)
bytes-written))))
+;; Writes data from string str to bytebuffer using specified encoding
+;TODO move string-to-octets into nio-compat
+(defmethod bytebuffer-write-string((bb byte-buffer) str &optional (external-format :ascii))
+ :documentation "Returns number of bytes written to bytebuffer"
+ (bytebuffer-write-vector bb (sb-ext:string-to-octets str :external-format external-format)))
+
(cffi:defcfun ("memset" %memset) :pointer
(buffer :pointer)
@@ -168,7 +172,7 @@
(format t "Remaining ~A~%" (remaining mybuf))
- (format t "mybuf string ~A~%" (get-string mybuf))
+ (format t "mybuf string ~A~%" (bytebuffer-read-string mybuf))
(format t "Mybuf (after get-string): ~A~%" mybuf)
Modified: branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp (original)
+++ branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp Fri Jan 12 01:44:39 2007
@@ -27,5 +27,5 @@
(defpackage :nio-buffer (:use :cl)
(:export
- byte-buffer free-buffer remaining inc-position get-string buffer-buf bytebuffer-write-vector bytebuffer-write-string flip
+ byte-buffer free-buffer remaining inc-position get-string buffer-buf bytebuffer-write-vector bytebuffer-write-string bytebuffer-read-vector bytebuffer-read-string flip
))
Modified: branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc-package.lisp Fri Jan 12 01:44:39 2007
@@ -24,10 +24,10 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|#
-(defpackage :nio-yarpc (:use :cl :nio :nio-buffer)
+(defpackage :nio-yarpc (:use :cl :nio :nio-sm :nio-buffer)
(:export
;; yarpc-state-machine
- yarpc-state-machine test-rpc test-rpc-list test-rpc-string
+ yarpc-state-machine yarpc-state-machine-factory test-rpc test-rpc-list test-rpc-string get-packet-factory
))
Modified: branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc.asd
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc.asd (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/nio-yarpc.asd Fri Jan 12 01:44:39 2007
@@ -5,7 +5,8 @@
(defsystem :nio-yarpc
:components ((:file "nio-yarpc-package")
- (:file "yarpc-state-machine" :depends-on ("nio-yarpc-package"))
+ (:file "yarpc-packet-factory" :depends-on ("nio-yarpc-package"))
+ (:file "yarpc-state-machine" :depends-on ("yarpc-packet-factory"))
)
- :depends-on (:nio))
\ No newline at end of file
+ :depends-on (:nio :nio-sm))
\ No newline at end of file
Added: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp
==============================================================================
--- (empty file)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp Fri Jan 12 01:44:39 2007
@@ -0,0 +1,54 @@
+#|
+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)))
+
+;;
+(defclass yarpc-packet-factory (packet-factory)())
+
+
+(defun yarpc-packet-factory ()
+ (make-instance 'yarpc-packet-factory))
+
+(defconstant CALL-METHOD-PACKET-ID 0)
+(defconstant METHOD-RESPONSE-PACKET-ID 1)
+
+(defmethod get-packet ((pf yarpc-packet-factory) buf)
+ (nio-buffer:flip buf)
+; (format t "get-packet::read string - ~A~%" (bytebuffer-read-string buf (remaining buf)))
+ (if (>= (remaining buf) 1) ;; First byte denotes packet ID
+ (ecase (elt (bytebuffer-read-vector buf 1) 0)
+ (0 (progn (format t "got CALL-METHOD-PACKET-ID~%") (make-instance 'call-method-packet (bytebuffer-read-string buf (remaining buf)))))
+ (1 (format t "got METHOD-RESPONSE-PACKET-ID~%")))))
+
+(defclass call-method-packet (packet)((call-string :initarg :call
+ :accessor get-call-string)))
+
+(defclass method-response-packet (packet)())
+
+
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-state-machine.lisp Fri Jan 12 01:44:39 2007
@@ -40,8 +40,15 @@
;; (test-rpc "who" 2 's)
;; response - who 2 'S
;;
-(defclass yarpc-state-machine (async-fd)())
+(defclass yarpc-state-machine (state-machine)())
+(defun yarpc-state-machine ()
+ (make-instance 'yarpc-state-machine))
+
+(defparameter yarpc-pf (yarpc-packet-factory))
+
+(defmethod get-packet-factory((sm yarpc-state-machine))
+ yarpc-pf)
;;TODO move somewhere suitable
@@ -74,23 +81,33 @@
(defmethod print-object ((sm yarpc-state-machine) stream)
(format stream "#<YARPC-STATE-MACHINE ~A >" (call-next-method sm nil)))
-(defmethod process-read((sm yarpc-state-machine))
- (with-slots (foreign-read-buffer foreign-write-buffer) sm
- (let ((fn-result (execute-call (sb-ext:octets-to-string (get-string foreign-read-buffer) :external-format :ascii))))
- (format t "process-read - function result: ~A~%" fn-result)
- (nio-buffer:bytebuffer-write-string foreign-write-buffer (write-to-string fn-result) 0 :utf-8)
- (close-sm sm))))
+(defconstant STATE-INITIALISED 0)
+(defconstant STATE-SEND-RESPONSE 1)
+(defparameter state STATE-INITIALISED)
(define-condition authorization-error (error) ())
-(defun execute-call (call-string)
+;Process a call method packet, returns
+(defmethod process-packet ((sm yarpc-state-machine) (call call-method-packet))
+ ;todo change state, create method-response packet and return it
+ ;(assert (eql state 0))
(handler-case
+ (let ((result (execute-call (get-call-string call))))
+ (when result
+ (let ((response-packet (progn
+ (setf state STATE-SEND-RESPONSE)
+ (method-response-packet result))))
+ (values response-packet t))))
+ (reader-error (re) (format t "No such function ~A~%" (get-call-string call)))
+ (authorization-error (ae) (format t "Function not declared with defremote ~A~%" (get-call-string call)))))
+
+
+(defun execute-call (call-string)
(let* ((rpc-call-list (read-from-string call-string ))
(fn (member (symbol-function (first rpc-call-list)) *remote-fns* )))
(format t "fn - ~A authorised? : ~A~%" (symbol-function (first rpc-call-list)) fn)
(if fn
(apply (first rpc-call-list) (rest rpc-call-list))
- (error 'authorization-error)))
- (reader-error (re) (format t "No such function ~A~%" call-string))))
+ (error 'authorization-error))))
Modified: branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp (original)
+++ branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp Fri Jan 12 01:44:39 2007
@@ -29,5 +29,5 @@
(:export
;; state-machine
- state-machine
+ state-machine packet-factory get-packet-factory get-packet
))
Modified: branches/home/psmith/restructure/src/statemachine/state-machine.lisp
==============================================================================
--- branches/home/psmith/restructure/src/statemachine/state-machine.lisp (original)
+++ branches/home/psmith/restructure/src/statemachine/state-machine.lisp Fri Jan 12 01:44:39 2007
@@ -42,11 +42,30 @@
(defmethod print-object ((sm state-machine) stream)
(format stream "#<STATE-MACHINE ~A >" (call-next-method sm nil)))
+(defgeneric process-packet(state-machine packet))
+
+(defgeneric get-packet-factory(state-machine))
+
(defmethod process-read((sm state-machine))
(with-slots (foreign-read-buffer foreign-write-buffer) sm
- (let ((fn-result (execute-call (sb-ext:octets-to-string (get-string foreign-read-buffer) :external-format :ascii))))
- (format t "process-read - function result: ~A~%" fn-result)
- (nio-buffer:bytebuffer-write-string foreign-write-buffer (write-to-string fn-result) 0 :utf-8)
- (close-sm sm))))
+ (let ((incomming-packet (get-packet (get-packet-factory sm) foreign-read-buffer)))
+ (format t "state-machine::process-read - incomming packet: ~A~%" incomming-packet)
+ (when incomming-packet
+ (multiple-value-bind (ret-packet close) (process-packet sm incomming-packet)
+ (format t "state-machine::process-read - return packet: ~A~%" ret-packet)
+ (when ret-packet (put-packet ret-packet foreign-write-buffer))
+ (if close
+ (close-sm sm)
+ ))))))
+
+
+(defclass packet-factory ()
+ ())
+
+; Get the packet in buf using the packet factory
+(defgeneric get-packet (packet-factory buf))
+; Write the packet to the buffer
+(defun put-packet (packet buf)
+ (nio-buffer:bytebuffer-write-vector buf (get-bytes packet)))
1
0

[nio-cvs] r32 - branches/home/psmith/restructure/src/statemachine
by psmith@common-lisp.net 08 Jan '07
by psmith@common-lisp.net 08 Jan '07
08 Jan '07
Author: psmith
Date: Mon Jan 8 14:03:57 2007
New Revision: 32
Added:
branches/home/psmith/restructure/src/statemachine/
branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp
branches/home/psmith/restructure/src/statemachine/nio-sm.asd
branches/home/psmith/restructure/src/statemachine/state-machine.lisp
Log:
Start of state-machine class work
Added: branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp
==============================================================================
--- (empty file)
+++ branches/home/psmith/restructure/src/statemachine/nio-sm-package.lisp Mon Jan 8 14:03:57 2007
@@ -0,0 +1,33 @@
+#|
+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.
+|#
+(defpackage :nio-sm (:use :cl :nio :nio-buffer)
+
+ (:export
+
+ ;; state-machine
+ state-machine
+ ))
Added: branches/home/psmith/restructure/src/statemachine/nio-sm.asd
==============================================================================
--- (empty file)
+++ branches/home/psmith/restructure/src/statemachine/nio-sm.asd Mon Jan 8 14:03:57 2007
@@ -0,0 +1,11 @@
+;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
+
+(in-package :asdf)
+
+(defsystem :nio-sm
+
+ :components ((:file "nio-sm-package")
+ (:file "state-machine" :depends-on ("nio-sm-package"))
+ )
+
+ :depends-on (:nio))
\ No newline at end of file
Added: branches/home/psmith/restructure/src/statemachine/state-machine.lisp
==============================================================================
--- (empty file)
+++ branches/home/psmith/restructure/src/statemachine/state-machine.lisp Mon Jan 8 14:03:57 2007
@@ -0,0 +1,52 @@
+#|
+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-sm)
+
+(declaim (optimize (debug 3) (speed 3) (space 0)))
+
+;
+;Base class for state machines
+;
+;Converts incomming data between bytes and packets using the supplied packet-factory.
+;Converts outgoing data between packets and bytes using the get-bytes method on packet.
+;
+;This way only the protocols packet heirarchy knows about binary representations and
+; the SM can deal with protocol logic and state maintenance
+;
+(defclass state-machine (async-fd)())
+
+(defmethod print-object ((sm state-machine) stream)
+ (format stream "#<STATE-MACHINE ~A >" (call-next-method sm nil)))
+
+(defmethod process-read((sm state-machine))
+ (with-slots (foreign-read-buffer foreign-write-buffer) sm
+ (let ((fn-result (execute-call (sb-ext:octets-to-string (get-string foreign-read-buffer) :external-format :ascii))))
+ (format t "process-read - function result: ~A~%" fn-result)
+ (nio-buffer:bytebuffer-write-string foreign-write-buffer (write-to-string fn-result) 0 :utf-8)
+ (close-sm sm))))
+
+
1
0