This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: Scheme book recommendations
Mikael Djurfeldt <mdj@mdj.nada.kth.se> writes:
> "Peter C. Norton" <spacey@lenin.nu> writes:
>
> > I'm on it. Who wants to help me?
>
> I'll do the server script from ch6.
I see no reason that we have to translate the perl examples closely,
so here's an example which I think is more fun:
----------------------------------------------------------------------
#!/usr/local/bin/guile -s
!#
;;;; Copyright (C) 1997, 2000 Free Software Foundation, Inc.
;;;;
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2, or (at your option)
;;;; any later version.
;;;;
;;;; 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 General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this software; see the file COPYING. If not, write to
;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
;;;;
;;; Author: Mikael Djurfeldt, based upon code by Gary Houston
;;; Version: 1.1
;;; Works with Guile snapshots beginning with 971129
;;; This file implements a simple Guile server.
;;;
;;; It enables a Guile interpreter to work as a server, supplying
;;; multiple repl sessions, each running in its own thread.
;;;
;;; Load this file into a Guile running on machine x.y.z.w and type
;;;
;;; (server)
;;;
;;; Then type
;;;
;;; telnet x.y.z.w 2345
;;;
;;; at the shell prompt. ("x.y.z.w" can be replaced by "localhost" if
;;; telnet is running on the same machine as the interpreter.)
;;;
;;; Use `(quit)' at the telnet (Guile) prompt to close down a
;;; connection.
;;;
;;; BUGS: Currently, signals (like interrupt) don't work.
;;;
(use-modules (ice-9 threads))
(define server-port 2345)
(define (server-report action address p)
(display (strftime "%y%m%d %H:%M:%S: " (localtime (current-time))) p)
(display action p)
(display " connection from " p)
(display (inet-ntoa (sockaddr:addr address)) p)
(display " at port " p)
(display (sockaddr:port address) p)
(newline p))
(define (server)
(let ((o (current-output-port))
(port (socket AF_INET SOCK_STREAM 0)))
(bind port AF_INET INADDR_ANY server-port)
(listen port 10) ; Queue up to 10 requests.
(let loop ()
(select (list port) () ())
(let ((p (accept port)))
(begin-thread
(server-report "Opening" (cdr p) o)
(server-repl (car p))
(server-report "Closing" (cdr p) o)
(close-port (car p))))
(loop))))
(define (server-repl p)
(set-current-input-port p)
(set-current-output-port p)
(set-current-error-port p)
(top-repl))
(server)