#| 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 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 ; the SM can deal with protocol logic and state maintenance ; (defclass state-machine (async-fd) ((state :initform 0 :accessor state))) (defmethod print-object ((sm state-machine) stream) (format stream "#" (call-next-method sm nil))) (defgeneric process-incoming-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) sm (let ((incoming-packet (handler-case (get-packet (get-packet-factory sm) foreign-read-buffer) (buffer-too-small-error (read-err) (if (recommend-buffer-size sm :read (recommended-size read-err)) (progn #+nio-debug (format-log t "state-machine::process-read - resized incomming buffer ~A~%"foreign-read-buffer) nil) (error 'not-implemented-yet-read-resize-failure)))))) #+nio-debug (format-log t "state-machine::process-read - incoming packet: ~A~%" incoming-packet) (when incoming-packet (process-incoming-packet sm incoming-packet))))) ;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))) (when outgoing-packet #+nio-debug (format-log t "state-machine::process-write - outgoing packet: ~A~%" outgoing-packet) (handler-case (write-bytes outgoing-packet foreign-write-buffer) (buffer-too-small-error (write-error1) (let ((new-size (get-packet-size outgoing-packet))) (format-log t "state-machine::process-write - write-error1 trying resize to ~A" new-size) (if (recommend-buffer-size sm :write new-size) (handler-case (write-bytes outgoing-packet foreign-write-buffer) (buffer-too-small-error (write-error2) (format t "Failed to write packet after resize (something already in write buffer?, dropping packet ~A~% out buffer:~%~A~%" outgoing-packet foreign-write-buffer))) (format t "Failed to resize io buffer, dropping packet: ~A~%" outgoing-packet))))))))) (defclass packet-factory () ()) ; Get the packet in buf using the packet factory (defgeneric get-packet (packet-factory buf)) ; Get size of packet (defgeneric get-packet (packet-factory buf))