iolib-server.lisp 18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
;;;; -*- mode:lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE:               iolib-server.lisp
;;;;LANGUAGE:           Common-Lisp
;;;;SYSTEM:             Common-Lisp
;;;;USER-INTERFACE:     NONE
;;;;DESCRIPTION
;;;;    
;;;;    This file define a generic server using IOLIB.
;;;;    
;;;;AUTHORS
;;;;    <PJB> Pascal J. Bourguignon <pjb@informatimago.com>
;;;;MODIFICATIONS
;;;;    2012-02-01 <PJB> Created.
;;;;BUGS
;;;;LEGAL
17
;;;;    AGPL3
18
;;;;    
19
;;;;    Copyright Pascal J. Bourguignon 2012 - 2014
20
;;;;    
21 22 23 24
;;;;    This program is free software: you can redistribute it and/or modify
;;;;    it under the terms of the GNU Affero General Public License as published by
;;;;    the Free Software Foundation, either version 3 of the License, or
;;;;    (at your option) any later version.
25
;;;;    
26 27 28 29
;;;;    This program is distributed in the hope that it will be useful,
;;;;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;;    GNU Affero General Public License for more details.
30
;;;;    
31
;;;;    You should have received a copy of the GNU Affero General Public License
32
;;;;    along with this program.  If not, see http://www.gnu.org/licenses/
33
;;;;**************************************************************************
34 35
(eval-when (:compile-toplevel :load-toplevel :execute)
  (setf *readtable* (copy-readtable nil)))
36 37 38 39 40
(defpackage "COM.INFORMATIMAGO.IOLIB.SERVER"
  (:use "COMMON-LISP"
        "SPLIT-SEQUENCE"
        "ALEXANDRIA"
        "IOLIB"
41
        "COM.INFORMATIMAGO.COMMON-LISP.CESARUM.STRING"
42 43 44 45 46
        "COM.INFORMATIMAGO.COMMON-LISP.CESARUM.QUEUE"
        "COM.INFORMATIMAGO.LOGGER"
        "COM.INFORMATIMAGO.IOLIB.MESSAGE"
        "COM.INFORMATIMAGO.IOLIB.END-POINT"
        "COM.INFORMATIMAGO.IOLIB.UTILS")
47 48
  (:shadowing-import-from "COM.INFORMATIMAGO.COMMON-LISP.CESARUM.STRING"
                          "STRING-DESIGNATOR")
49 50
  (:export "ENQUEUE-MESSAGE"
           "+CLIENT-INPUT-BUFFER-SIZE+"
51 52
           "CLIENT" "CLIENT-SERVER" "CLIENT-STATE" "CLIENT-SOCKET" "CLIENT-LOCAL-END-POINT"
           "CLIENT-REMOTE-END-POINT" 
53 54 55 56 57 58 59 60
           "CLIENT-RESPONSE-QUEUE-LENGTH"
           "CLIENT-RECEIVE-MESSAGE"
           "RECEIVE-BYTES"
           "CLIENT-SEND-RESPONSE"
           "SEND-BYTES"
           "CLIENT-HELLO-MESSAGE"
           "CLIENT-CLOSE"
           "SERVER"
61 62
           "SERVER-NAME" "SERVER-STATE" "SERVER-SOCKET" "SERVER-CLIENTS"
           "SERVER-DATA-INPUT-QUEUE" "SERVER-DATA-RECEIVED-CALLBACK"
63 64 65 66 67
           "SERVER-ADD-CLIENT"
           "SERVER-REMOVE-CLIENT"
           "START-SERVER"
           "CLOSE-SERVER"
           "SERVER-STATE"
68 69
           ;; "RECEIVE-DATA"
           ;; "RECEIVE-COMMAND"
70
           "SERVER-RECEIVE-MESSAGE"
71 72
           ;; "SERVER-SEND-RESPONSE"
           )
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  (:documentation "

"))
(in-package "COM.INFORMATIMAGO.IOLIB.SERVER")

(defgeneric enqueue-message (sender message &key front))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; CLIENT
;;;
;;; Representation of the client in the server.

(defconstant +client-input-buffer-size+ 4096)


(defclass client ()
  ((server
    :initarg :server
    :reader client-server
    :documentation "The server of this client.")
   
   (state
    :initform :open
    :reader client-state
    :type (member :open :closed)
    :documentation "Indicates whether the client is closed.")
   
   (socket
    :initarg :socket
    :accessor client-socket
    :documentation "The IOLib active socket of the client.")
   (local-end-point
    :reader client-local-end-point
    :documentation "The remote end-point of the client socket.")
   (remote-end-point
    :reader client-remote-end-point
    :documentation "The remote end-point of the client socket.")

   ;; (acknowledge-sender)
   (response-sender)

   (last-index
    :initform 0
    :type (unsigned-byte 16)
    :documentation "The index of the last message sent.")
   
   (output-buffer
    :initform nil
    :documentation "The packet currently being sent.")
   (output-index
    :initform 0
    :documentation "The index of the next byte in the packet to send.")
   
   (receptor)

   (input-buffer
    :initform (make-array +client-input-buffer-size+
                          :element-type '(unsigned-byte 8)
                          :initial-element 0)
    :documentation "The socket input buffer."))
  
  (:documentation "Represents a client in the server."))


(defmethod print-object ((client client) stream)
  (print-unreadable-object (client stream :identity *print-escape* :type t)
    (ignore-errors
      (format stream
              (if *print-escape*
                  ":REMOTE-END-POINT ~S"
                  "~A")
              (client-remote-end-point client))))
  client)


(defmethod initialize-instance :after ((client client) &key)
  (with-slots (remote-end-point local-end-point ;; acknowledge-sender
                                response-sender receptor) client
    (setf remote-end-point (socket-remote-end-point (client-socket client))
          local-end-point  (socket-local-end-point  (client-socket client))
          response-sender (make-instance 'sender
                              :data-ready    (lambda ()
                                               (with-slots (socket) client
                                                 (when socket
                                                   (send-to socket #() :dont-wait t))))
                              :process-error (lambda (condition)
                                               (logger :com.informatimago.iolib.server :error "Error while sending ~A to ~A:~% ~A~%"
161
                                                       (ignore-errors (message-data (slot-value (slot-value client 'response-sender) 'message)))
162 163 164 165 166 167 168
                                                       client
                                                       condition)))
          receptor (make-instance 'receptor
                       :receive-message (lambda (message)
                                          (client-receive-message client message))
                       :process-error (lambda (condition)
                                        (logger :com.informatimago.iolib.server :error "Error while receiving ~A from ~A:~% ~A~%"
169
                                                (ignore-errors (message-data (slot-value (slot-value client 'response-sender) 'message)))
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
                                                client
                                                condition))))))


(defmethod client-response-queue-length ((client client))
  (with-slots (response-sender) client
    (with-slots (queue) response-sender
      (queue-length queue))))


(defmethod client-receive-message ((client client) message)
  (logger :com.informatimago.iolib.server :info "Client ~A received message ~A~%"
          client message)
  ;; (send-acknowledge client (message-index message) "Got it.")
  (server-receive-message (client-server client) client message))


(defmethod receive-bytes ((client client))
  (with-slots (socket input-buffer receptor) client
    (multiple-value-bind (buffer bytes-read)
        (receive-from socket :buffer input-buffer)
      (declare (ignore buffer))
      (if (zerop bytes-read)
          (error 'end-of-file)
          (receptor-receive-bytes receptor input-buffer bytes-read)))))


(defmethod client-send-response ((client client) (message message) &rest arguments)
  (declare (ignore arguments))
  (with-slots (response-sender last-index) client
    (enqueue-message response-sender message)))


203

204
(defmethod client-send-response ((client client) (control-string string) &rest arguments)
205 206 207 208 209 210 211 212 213 214 215 216
  (with-slots (last-index) client
    (client-send-response
     client
     (make-instance 'line-message
         :text (UNSPLIT-STRING
                (split-sequence #\newline (apply (function format) nil control-string arguments))
                #.(coerce (vector (code-char 13) (code-char 10)) 'string))
         :index (incf last-index)
         :message-sent (lambda (message)
                         (logger :com.informatimago.iolib.server :info
                                 "Sent RESPONSE ~A to ~A~%"
                                 (message-data message) client))))))
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255


(defmethod send-bytes ((client client))
  (with-slots (socket output-buffer output-index response-sender) client
    (when socket
      (when (null output-buffer)
        (setf output-buffer (get-next-packet response-sender)
              output-index 0))
      (when output-buffer
        (if (< output-index (length output-buffer))
            (let ((written-bytes (send-to socket output-buffer
                                          :start output-index
                                          :end (length output-buffer))))
              (logger :com.informatimago.iolib.server :debug "Wrote ~A bytes to ~A~%" written-bytes
                      (client-remote-end-point client))
              (incf output-index written-bytes))
            (progn
              (setf output-buffer nil)
              (send-bytes client)))))))



;; (defmethod send-eof ((client client))
;;   "
;; Add an EOF marker to the output buffers of the CLIENT.
;; When the send-byte method reaches this marker, the client will be closed.
;; "
;;   (queue-enqueue (client-output-queue client) :eof))


(defmethod client-hello-message ((client client) name)
  (format nil "~A ~A ~S"
          (server-name (client-server client))
          (ensure-hostname (local-host (client-socket client)))
          name))


(defmethod client-close ((client client))
  (setf (slot-value client 'state) :closed)
256 257 258 259 260 261
  (when (socket-os-fd (client-socket client))
   (ignore-errors (remove-fd-handlers *event-base*
                                      (socket-os-fd (client-socket client))
                                      :read t :write t :error t))
   (shutdown (client-socket client) :read t :write t)
   (close (client-socket client)))
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  (values))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; SERVER
;;;

(defclass server ()
  ((name
    :initarg :name
    :accessor server-name
    :initform "Generic Server"
    :documentation "A user label for identification and display purposes.")

   (state
    :initform :listening
    :reader server-state
    :type (member :listening :closed)
    :documentation "A keyword indicating the state of the server.")
   
   (socket
    :accessor server-socket 
    :documentation "The IOLib passive (listening) socket of the server.")

   (clients
    :accessor server-clients
    :initform '()
    :documentation "A list of clients.")

   (data-input-queue
    :accessor server-data-input-queue
    :initform (make-queue)
    :documentation "A FIFO holding lists of (client data-message).")

   (data-received-callback
    :accessor server-data-received-callback
    :initform nil
    :initarg :data-received-callback
    :documentation "A function called when data is received."))
  
  (:documentation "An server listens for incoming connections
and then receive data messages from clients, and sends answers to them."))


(defmethod print-object ((server server) stream)
  (print-unreadable-object (server stream :identity *print-escape* :type t)
309 310 311 312 313
    (format stream (if *print-escape*
                       ":name ~S :state ~S"
                       "~A [~A]")
            (server-name server)
            (server-state server)))
314 315 316 317 318 319 320 321
  server)


(defmethod server-add-client ((server server) (client client))
  (pushnew client (server-clients server)))

(defmethod server-remove-client ((server server) (client client))
  (client-close client)
322 323
  (deletef (server-clients server) client)
  (logger :com.informatimago.iolib.server :notice "Client disconnected ~A" client))
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423



(defmethod start-server (local-end-point
                         &key name tls
                         client-connected
                         data-received-callback
                         (client-class 'client))
  "
DO:                     Creates a new server (running in a separate thread if possible).

RETURN:                 The server object.


LOCAL-END-POINT:        The local address:port wher ethe server will listen from.

NAME:                   A string naming the server.

TLS:                    -- Not implemented yet.

CLIENT-CONNECTED:       A (function server client) called once the client is connected.

CLIENT-CLASS:           The class of client to instanciate.  Should be subclass of CLIENT.

DATA-RECEIVED-CALLBACK: A (function data server client) called everytime data is received.


NOTE:                   The CLIENT-RECEIVE-MESSAGE method is called
                        first, and the method on CLIENT calls
                        SERVER-RECEIVE-MESSAGE which calls the
                        DATA-RECEIVED-CALLBACK.  If
                        CLIENT-RECEIVE-MESSAGE is overriden by a
                        subclass, it may not CALL-NEXT-METHOD, and
                        therefore DATA-RECEIVED-CALLBACK may not be
                        called.
"
  (declare (ignorable tls)) ; tls is not implemented yet
  (let ((server (make-instance 'server
                    :name name
                    :data-received-callback data-received-callback)))
    (with-slots (socket) server
      (setf socket (listen-to-local-end-point local-end-point))

      (set-io-handler *event-base*
                      (socket-os-fd socket)
                      :read  
                      (make-listener-handler
                       socket
                       
                       (lambda (client-socket)      ; acceptor ; must return the client, or nil.
                         (let ((client (make-instance client-class
                                           :server server
                                           :socket client-socket)))
                           (server-add-client server client)
                           (logger :com.informatimago.iolib.server :notice "Accepted a client from ~A~%"
                                   (client-remote-end-point client))
                           client))

                       (make-disconnector-maker
                        (lambda (client client-socket)
                          (declare (ignore client-socket))
                          (logger :com.informatimago.iolib.server :notice "Closing connection to ~A~%"
                                  (client-remote-end-point client))
                          (server-remove-client server client)))
                       
                       (make-reader-maker
                        (lambda (client client-socket disconnector)
                          (declare (ignore client-socket disconnector))
                          ;; Read however much we are able.
                          (receive-bytes client)))

                       (make-writer-maker
                        (lambda (client client-socket disconnector)
                          (declare (ignore client-socket disconnector))
                          (send-bytes client)
                          ;; If we see we're out of data to write
                          ;; then close the connection, we're done. 
                          ;; (when (eql :eof (queue-first-element (client-output-queue client)))
                          ;;   (funcall disconnector
                          ;;            (remote-host client-socket)
                          ;;            (remote-port client-socket)
                          ;;            :close))
                          ))

                       (lambda (client client-socket) ; conclusion
                         (logger :com.informatimago.iolib.server :info "Conclusion for client ~A" client)
                         (when client-connected (funcall client-connected server client client-socket))))))
    server))


(defmethod close-server ((server server))
  (setf (slot-value server 'state) :closed)
  (dolist (client (copy-list (server-clients server)))
    (server-remove-client server client))
  (shutdown (server-socket server) :read t :write t)
  (close (server-socket server))
  (values))


(defmethod server-state ((server server))
424
  (with-slots (data-input-queue clients state) server
425
    (list state
426
          (queue-length data-input-queue)
427 428 429 430 431 432 433
          (mapcar (lambda (client)
                    (list
                     (client-remote-end-point client)
                     (client-response-queue-length client)))
                  clients))))


434 435 436 437 438 439 440 441 442 443 444 445 446 447
;; (defmethod receive-data ((server server))
;;   (with-slots (data-input-queue) server
;;     (if (queue-empty-p data-input-queue)
;;         (values nil nil nil)
;;         (destructuring-bind (client data) (queue-dequeue data-input-queue)
;;           (values data client t)))))
;; 
;; 
;; (defmethod receive-command ((server server))
;;   (with-slots (command-input-queue) server
;;     (if (queue-empty-p command-input-queue)
;;         (values nil nil nil)
;;         (destructuring-bind (client command) (queue-dequeue command-input-queue)
;;           (values command client t)))))
448 449 450 451 452 453 454 455 456 457


(defmethod server-receive-message ((server server) (client client) message)
  (flet ((receive (callback queue data)
           (unless (and callback (funcall callback data server client))
             (queue-enqueue queue (list client data)))))
    (with-slots (data-received-callback data-input-queue) server
      (receive data-received-callback data-input-queue (message-data message)))))


458 459 460 461 462 463 464 465 466 467 468 469 470
;; (defmethod server-send-response ((server server) (client client) response &key sent-callback)
;;   (assert (member client (server-clients server)))
;;   (client-send-response client "~A" (with-standard-io-syntax (prin1-to-string response)))
;;   ;; (with-slots (response-sender last-index) client
;;   ;;   (enqueue-message response-sender
;;   ;;                    (make-instance 'line-message
;;   ;;                        :text 
;;   ;;                        :index (incf last-index)
;;   ;;                        :message-sent (lambda (message)
;;   ;;                                        (declare (ignore message))
;;   ;;                                        (when sent-callback
;;   ;;                                          (funcall sent-callback response server client))))))
;;   )
471 472 473 474 475




;;;; THE END ;;;;