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

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

Fixed large packet problems: calculated header size correctly; use buffer-pointer to take into account buffer-position on write and moved external format to UTF-8

File size: 4.1 KB
Line 
1#|
2Copyright (c) 2007
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
81. Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright
11   notice, this list of conditions and the following disclaimer in the
12   documentation and/or other materials provided with the distribution.
133. The name of the author may not be used to endorse or promote products
14   derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26|#
27(in-package :nio-sm)
28
29(declaim (optimize (debug 3) (speed 3) (space 0)))
30
31;
32;Base class for state machines
33;
34;Converts incoming data between bytes and packets using the supplied packet-factory.
35;Converts outgoing data between packets and bytes using the write-bytes method on packet.
36;
37;This way only the protocols packet heirarchy knows about binary representations and
38;  the SM can deal with protocol logic and state maintenance
39;
40(defclass state-machine (async-fd)
41 ((state :initform 0
42         :accessor state)))
43
44(defmethod print-object ((sm state-machine) stream)
45  (format stream "#<STATE-MACHINE ~A >" (call-next-method sm nil)))
46
47(defgeneric process-incoming-packet(state-machine packet))
48
49
50(defgeneric process-outgoing-packet(state-machine))
51
52
53(defgeneric get-packet-factory(state-machine))
54
55;The connection is read ready.
56;Use the packet factory to obtain any valid packet and pass it through
57(defmethod process-read((sm state-machine))
58  (with-slots (foreign-read-buffer) sm
59    (let ((incoming-packet 
60           (handler-case
61               (get-packet (get-packet-factory sm) foreign-read-buffer)
62             (buffer-too-small-error (read-err)
63               (if (recommend-buffer-size sm :read (recommended-size read-err))
64                   (progn
65#+nio-debug          (format-log t "state-machine::process-read - resized incomming buffer ~A~%"foreign-read-buffer)
66                     nil)
67                   (error 'not-implemented-yet-read-resize-failure))))))
68#+nio-debug (format-log t "state-machine::process-read - incoming packet: ~A~%" incoming-packet)
69      (when incoming-packet 
70          (process-incoming-packet sm incoming-packet)))))
71
72;The connection is write ready.
73;See if theres anything ready to be written in the SM
74(defmethod process-write((sm state-machine))
75  (with-slots (foreign-write-buffer) sm
76  (let ((outgoing-packet (process-outgoing-packet sm)))
77    (when outgoing-packet 
78#+nio-debug    (format-log t "state-machine::process-write - outgoing packet: ~A~%" outgoing-packet)
79      (handler-case
80          (write-bytes outgoing-packet foreign-write-buffer)
81        (buffer-too-small-error (write-error1) 
82          (let ((new-size (get-packet-size outgoing-packet)))
83            (format-log t "state-machine::process-write - write-error1 trying resize to ~A" new-size)
84            (if (recommend-buffer-size sm :write new-size)
85              (handler-case
86                  (write-bytes outgoing-packet foreign-write-buffer)
87                (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)))
88              (format t "Failed to resize io buffer, dropping packet: ~A~%" outgoing-packet)))))))))
89
90
91
92
93(defclass packet-factory ()
94  ())
95 
96; Get the packet in buf using the packet factory
97(defgeneric get-packet (packet-factory buf))
98
99; Get size of packet
100(defgeneric get-packet (packet-factory buf))
101
102
Note: See TracBrowser for help on using the repository browser.