Add GPL
[mudsync.git] / mudsync.scm
1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of Mudsync.
5 ;;;
6 ;;; Mudsync is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or
9 ;;; (at your option) any later version.
10 ;;;
11 ;;; Mudsync is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;;; General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with Mudsync.  If not, see <http://www.gnu.org/licenses/>.
18
19 (use-modules (8sync systems actors)
20              (8sync agenda)
21              (ice-9 format)
22              (ice-9 match)
23              (oop goops))
24
25
26 (define %default-server #f)
27 (define %default-port 8889)
28
29 (define-class <network-manager> (<actor>)
30   (server-socket #:accessor nm-server-socket)
31   ;; mapping of client -> client-id
32   (clients #:accessor nm-clients
33            #:init-thunk make-hash-table)
34   ;; send input to this actor
35   (send-input-to #:getter nm-send-input-to
36                  #:init-keyword #:send-input-to)
37   (message-handler
38    #:init-value
39    (make-action-dispatch
40     ((start-listening actor message)
41      (nm-install-socket actor (message-ref message 'server %default-server)
42                         (message-ref message 'port %default-port)))
43     ((send-to-client actor message to-client data)
44      (nm-send-to-client-id actor to-client data)))))
45
46 (define-method (nm-close-everything (nm <network-manager>) remove-from-agenda)
47   "Shut it down!"
48   ;; close all clients
49   (hash-for-each
50    (lambda (_ client)
51      (close client)
52      (if remove-from-agenda
53          (8sync-port-remove client)))
54    (nm-clients nm))
55   ;; reset the clients list
56   (set! (nm-clients) (make-hash-table))
57   ;; close the server
58   (close (nm-server-socket nm))
59   (if remove-from-agenda
60       (8sync-port-remove (nm-server-socket nm))))
61
62 ;; Maximum number of backlogged connections when we listen
63 (define %maximum-backlog-conns 128)     ; same as SOMAXCONN on Linux 2.X,
64                                         ; says the intarwebs
65
66 (define-method (nm-install-socket (nm <network-manager>) server port)
67   "Install socket on SERVER with PORT"
68   (let ((s (socket PF_INET  ; ipv4
69                    SOCK_STREAM  ; two-way connection-based byte stream
70                    0))
71         (addr (if server
72                   (inet-pton AF_INET server)
73                   INADDR_LOOPBACK)))
74     ;; Totally mimed from the Guile manual.  Not sure if we need this, but:
75     ;; http://www.unixguide.net/network/socketfaq/4.5.shtml
76     (setsockopt s SOL_SOCKET SO_REUSEADDR 1) ; reuse port even if port is busy
77     ;; Connecting to a non-specific address:
78     ;;   (bind s AF_INET INADDR_ANY port)
79     ;; Should this be an option?  Guess I don't know why we'd need it
80     ;; @@: If we wanted to support listening on a particular hostname,
81     ;;   could see 8sync's irc.scm...
82     (bind s AF_INET addr port)
83     ;; Listen to connections
84     (listen s %maximum-backlog-conns)
85
86     ;; Throw a system-error rather than block on an (accept)
87     ;; that has nothing to do
88     (fcntl s F_SETFL
89            (logior O_NONBLOCK
90                    (fcntl s F_GETFL)))
91
92     ;; @@: This is used in Guile's http server under the commit:
93     ;;       * module/web/server/http.scm (http-open): Ignore SIGPIPE. Keeps the
94     ;;         server from dying in some circumstances.
95     ;;   (sigaction SIGPIPE SIG_IGN)
96     ;; Will this break other things that use pipes for us though?
97
98     (set! (nm-server-socket nm) s)
99
100     (format #t "Listening for clients in pid: ~s\n" (getpid))
101     (8sync-port s #:read (lambda (s) (nm-new-client nm s)))
102     ;; TODO: set up periodic close of idle connections?
103     ))
104
105 (define-method (nm-new-client (nm <network-manager>) s)
106   "Handle new client coming in to socket S"
107   (let* ((client-connection (accept s))
108          (client-details (cdr client-connection))
109          (client (car client-connection)))
110     (format #t "New client: ~s\n" client-details)
111     (format #t "Client address: ~s\n"
112             (gethostbyaddr
113              (sockaddr:addr client-details)))
114
115     (let ((client-id (big-random-number)))
116       (hash-set! (nm-clients nm) client-id client)
117       (8sync-port client #:read (nm-make-client-receive nm client-id)))))
118
119 (define-method (nm-make-client-receive (nm <network-manager>) client-id)
120   "Make a method to receive client data"
121   (let ((buffer '()))
122     (define (reset-buffer)
123       (set! buffer '()))
124     (define (should-read-char client)
125       (and (not (port-closed? client))
126            (char-ready? client)
127            (not (eof-object? (peek-char client)))))
128     (define (receive-handler client)
129       (while (should-read-char client)
130         (set! buffer (cons (read-char client) buffer))
131         (match buffer
132           (;; @@: Do we need the "char?"
133            (#\newline #\return (? char? line-chars) ...)
134            (let ((ready-line (list->string (reverse line-chars))))
135              ;; reset buffer
136              (set! buffer '())
137              ;; run it
138              (nm-handle-line nm client client-id ready-line)))
139           (_ #f)))
140       ;; Shut things down on closed port or EOF object
141       (cond
142        ((port-closed? client)
143         (nm-handle-port-closed nm client client-id))
144        ((and (char-ready? client)
145              (eof-object? (peek-char client)))
146         (nm-handle-port-eof nm client client-id))))
147     receive-handler))
148
149 (define-method (nm-handle-port-closed (nm <network-manager>) client client-id)
150   "Handle a closed port"
151   (format #t "DEBUG: handled closed port ~x\n" client-id)
152   (8sync-port-remove client)
153   (hash-remove! (nm-clients nm) client-id))
154
155 (define-method (nm-handle-port-eof (nm <network-manager>) client client-id)
156   "Handle seeing an EOF on port"
157   (format #t "DEBUG: handled eof-object on port ~x\n" client-id)
158   (close client)
159   (8sync-port-remove client)
160   (hash-remove! (nm-clients nm) client-id))
161
162 (define-method (nm-handle-line (nm <network-manager>) client client-id line)
163   "Handle an incoming line of input from a client"
164   (format #t "~x got line: ~s\n" client-id line)
165   (nm-send-to-client-id nm client-id "Thank, you, processed.\n" ))
166
167 (define-method (nm-send-to-client-id (nm <network-manager>) client-id data)
168   "Send DATA to TO-CLIENT id"
169   (display data
170            (hash-ref (nm-clients nm) client-id)))
171
172 ; (ez-run-hive hive (list (bootstrap-message hive (actor-id nm) 'start-listening)))
173
174
175 ;; (define-method (nm-close-port (nm <network-manager>)))
176
177
178 (define-generic gm-init-world)
179 (define-class <game-master> (<actor>)
180   (message-handler
181    #:init-value
182    (make-action-dispatch
183     ;; init-world
184     )))
185
186