echo server protocol along with io.event changes
[iolib.git] / io.event / io-protocol.lisp
blob5185660c1b91866e8b6b9b6fb5f4a16b84db7ed5
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; io-protocol.lisp --- Manage an I/O protocol.
4 ;;;
5 ;;; Copyright (C) 2007, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
13 ;;;
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :io.event)
26 ;;;; Base Protocol Class
28 (defclass io-protocol ()
29 ((transport :initarg :transport :accessor transport-of))
30 (:documentation ""))
32 (defgeneric on-protocol-start (protocol)
33 (:documentation ""))
35 (defgeneric on-protocol-stop (protocol)
36 (:documentation ""))
38 ;;;; Stream Protocol
40 (defclass stream-protocol (io-protocol)
42 (:documentation ""))
44 (defgeneric on-connection-made (protocol)
45 (:documentation "")
46 ;; default empty method
47 (:method ((sp stream-protocol))
48 (values)))
50 (defgeneric on-connection-lost (protocol reason)
51 (:documentation "")
52 ;; default empty method
53 (:method ((sp stream-protocol) reason)
54 (declare (ignore reason))
55 (values)))
57 (defgeneric on-connection-end (protocol)
58 (:documentation "")
59 ;; default empty method
60 (:method ((sp stream-protocol))
61 (values)))
63 (defgeneric on-data-received (protocol data)
64 (:documentation "")
65 ;; default empty method
66 (:method ((sp stream-protocol) data)
67 (declare (ignore data))
68 (values)))
70 ;;;; Datagram Protocol
72 (defclass datagram-protocol (io-protocol)
74 (:documentation ""))
76 (defgeneric on-datagram-received (protocol datagram address)
77 (:documentation ""))
79 ;;;; Debug Mixins
81 ;;; seemed like a good idea at first but so far it's less useful than
82 ;;; plain TRACE.
84 (defclass protocol-debug-mixin ()
86 (:documentation ""))
88 (defmethod on-connection-made :before ((p protocol-debug-mixin))
89 (format *debug-io* "~&Connection made: ~S~%" p))
91 (defmethod on-connection-end :after ((p protocol-debug-mixin))
92 (format *debug-io* "~&Connection end: ~S~%" p))
94 (defmethod on-data-received :before ((p protocol-debug-mixin) data)
95 (format *debug-io* "~&Data received: ~A bytes~%" (length data)))
97 (defun debug-protocols ()
98 (trace on-data-received
99 on-transport-writable
100 on-transport-readable
101 on-transport-error
102 on-connection-made
103 on-connection-end))