Add symbol macro +DEFAULT-INET-FAMILY+.
[iolib.git] / io.event / protocol.lisp
blob7fd55e1154089ea75eb7ed08d0ce19ab7d8033aa
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; protocol.lisp --- Protocol classes and generic functions.
4 ;;;
5 ;;; Copyright (C) 2007, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net>
7 ;;;
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
14 ;;;
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.
19 ;;;
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))
33 (:documentation ""))
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))
41 ;;;; Stream Protocol
43 (defclass stream-protocol (io-protocol)
45 (:documentation ""))
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)
52 (:documentation "")
53 ;; default empty method
54 (:method ((sp stream-protocol) transport)
55 (declare (ignore transport))
56 (values)))
58 (defgeneric on-connection-lost (protocol transport reason)
59 (:documentation "")
60 ;; default empty method
61 (:method ((sp stream-protocol) transport reason)
62 (declare (ignore transport reason))
63 (values)))
65 (defgeneric on-connection-end (protocol transport)
66 (:documentation "")
67 ;; default empty method
68 (:method ((sp stream-protocol) transport)
69 (declare (ignore transport))
70 (values)))
72 (defgeneric on-data-received (protocol transport data)
73 (:documentation "")
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))
79 (values)))
81 ;;;; Datagram Protocol
83 (defclass datagram-protocol (io-protocol)
85 (:documentation ""))
87 (defgeneric on-datagram-received (protocol transport datagram address port)
88 (:documentation "")
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))
92 (values)))
94 ;;;; Debug Mixins
96 ;;; This seemed like a good idea at first but so far it's less useful
97 ;;; than plain TRACE.
99 (defclass protocol-debug-mixin ()
101 (:documentation ""))
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)))
115 #-(and)
116 (defun debug-protocols ()
117 (trace on-data-received
118 on-transport-writable
119 on-transport-readable
120 on-transport-error
121 on-connection-made
122 on-connection-end))