v0.1 of the sockets/cc code.
[iolib.git] / tests / net.sockets.cc / reverser.lisp
blob1f0cff19438929fb4467b8c5d1e0cce5caf9ba86
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; pkgdcl.lisp --- Package definition.
4 ;;;
5 ;;; Copyright (C) 2006-2008, Attila Lendvai <attila.lendvai@gmail.com>
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 :sockets/cc-tests)
26 (in-suite test)
28 (defparameter *external-format* (babel:ensure-external-format '(:utf-8 :eol-style :crlf)))
30 (defun start-reverser-server (&key (address +loopback+) (port 4242) (worker-count 4)
31 (external-format *external-format*))
32 (bind ((acceptor (make-connection-acceptor 'reverser-connection-handler
33 :worker-count worker-count
34 :external-format external-format)))
35 (finishes
36 (unwind-protect
37 (progn
38 (startup-acceptor acceptor :address address :port port)
39 ;; TODO until epoll based multiplexing is added
40 (break "Server started, continue to shut it down")
41 ;;(sockets/cc::busy-loop-hack acceptor)
43 (shutdown-acceptor acceptor))))
44 (values))
46 (defun/cc reverser-connection-handler (connection)
47 (loop
48 for line = (read-line/cc connection)
49 until (zerop (length line)) do
50 (progn
51 (write-string (nreverse line) connection)
52 (terpri connection)
53 (force-output connection))))
55 #+nil
56 (defun start-reverser-client (&key (address +loopback+) (port 4242) (worker-count 4)
57 (external-format *external-format*))
58 (bind ((multiplexer (make-instance 'connection-multiplexer)))
61 (bind ((acceptor (make-connection-acceptor 'reversering-connection-handler
62 :worker-count worker-count
63 :external-format '(:utf-8 :eol-style :crlf))))
64 (finishes
65 (unwind-protect
66 (progn
67 (startup-acceptor acceptor :address address :port port)
68 ;;(break "Acceptor running, continue this thread to shut it down")
70 (shutdown-acceptor acceptor))))
71 (values))
73 #+nil
74 (defun reverser-client-worker-loop (address port external-format)
75 (bind ((socket (make-socket :connect :active :external-format external-format))
76 (done nil))
77 (unwind-protect
78 (progn
79 (setf (fd-non-blocking socket) t)
80 (bind-address socket (ensure-hostname address)
81 :port port
82 :reuse-address t)
83 (apply 'listen-on socket (when backlog
84 (list :backlog backlog)))
85 (setf (socket-of acceptor) socket)
86 #+nil(loop repeat (worker-count-of acceptor) do
87 (spawn-worker-thread-for-acceptor acceptor))
88 (setf done t))
89 (unless done
90 (close socket)))))