1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
3 ;;; protocol.lisp --- Protocol classes and generic functions.
5 ;;; Copyright (C) 2007, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net>
8 ;;; This code is free software; you can redistribute it and/or
9 ;;; modify it under the terms of the version 2.1 of
10 ;;; the GNU Lesser General Public License as published by
11 ;;; the Free Software Foundation, as clarified by the
12 ;;; preamble found here:
13 ;;; http://opensource.franz.com/preamble.html
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU Lesser General
21 ;;; Public License along with this library; if not, write to the
22 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 ;;; Boston, MA 02110-1301, USA
25 (in-package :io.event
)
27 ;;;; Base Protocol Class
29 ;;; Calling it IO-PROTOCOL in order to avoid clashing with
30 ;;; the SOCKETS:PROTOCOL class.
31 (defclass io-protocol
()
32 ((transport :initarg
:transport
:accessor transport-of
))
35 ;;; What would the semantics for these GFs be? Namely, how would
36 ;;; ON-PROTOCOL-START differ from INITIALIZE-INSTANCE?
38 ;; (defgeneric on-protocol-start (protocol))
39 ;; (defgeneric on-protocol-stop (protocol))
43 (defclass stream-protocol
(io-protocol)
47 ;;; These generic functions all take a TRANSPORT argument because a
48 ;;; PROTOCOL may need to deal with multiple TRANSPORTS (per
49 ;;; connection). FTP would be an example of such a PROTOCOL.
51 (defgeneric on-connection-made
(protocol transport
)
53 ;; default empty method
54 (:method
((sp stream-protocol
) transport
)
55 (declare (ignore transport
))
58 (defgeneric on-connection-lost
(protocol transport reason
)
60 ;; default empty method
61 (:method
((sp stream-protocol
) transport reason
)
62 (declare (ignore transport reason
))
65 (defgeneric on-connection-end
(protocol transport
)
67 ;; default empty method
68 (:method
((sp stream-protocol
) transport
)
69 (declare (ignore transport
))
72 (defgeneric on-data-received
(protocol transport data
)
74 ;; default empty method, though perhaps we should make it issue a
75 ;; warning, since it's probably a good bet that we want to do
76 ;; something with the data we receive.
77 (:method
((sp stream-protocol
) transport data
)
78 (declare (ignore transport data
))
81 ;;;; Datagram Protocol
83 (defclass datagram-protocol
(io-protocol)
87 (defgeneric on-datagram-received
(protocol transport datagram address port
)
89 ;; default empty method. Again, same issue as with ON-DATA-RECEIVED.
90 (:method
((dp datagram-protocol
) transport datagram address port
)
91 (declare (ignore transport datagram address port
))
96 ;;; This seemed like a good idea at first but so far it's less useful
99 (defclass protocol-debug-mixin
()
103 (defmethod on-connection-made :before
((p protocol-debug-mixin
) protocol
)
104 (declare (ignore protocol
))
105 (format *debug-io
* "~&Connection made: ~S~%" p
))
107 (defmethod on-connection-end :after
((p protocol-debug-mixin
) protocol
)
108 (declare (ignore protocol
))
109 (format *debug-io
* "~&Connection end: ~S~%" p
))
111 (defmethod on-data-received :before
((p protocol-debug-mixin
) protocol data
)
112 (declare (ignore protocol
))
113 (format *debug-io
* "~&Data received: ~A bytes~%" (length data
)))
116 (defun debug-protocols ()
117 (trace on-data-received
118 on-transport-writable
119 on-transport-readable