1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of Mudsync.
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.
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.
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/>.
19 (use-modules (8sync systems actors)
20 (8sync systems actors debug)
31 (define %default-server #f)
32 (define %default-port 8889)
34 (define-class <network-manager> (<actor>)
35 (server-socket #:accessor nm-server-socket)
36 ;; mapping of client -> client-id
37 (clients #:accessor nm-clients
38 #:init-thunk make-hash-table)
39 ;; send input to this actor
40 (send-input-to #:getter nm-send-input-to
41 #:init-keyword #:send-input-to)
45 ((start-listening actor message)
46 (nm-install-socket actor (message-ref message 'server %default-server)
47 (message-ref message 'port %default-port)))
48 ((send-to-client actor message client data)
49 (nm-send-to-client-id actor client data)))))
51 (define-method (nm-close-everything (nm <network-manager>) remove-from-agenda)
57 (if remove-from-agenda
58 (8sync-port-remove client)))
60 ;; reset the clients list
61 (set! (nm-clients) (make-hash-table))
63 (close (nm-server-socket nm))
64 (if remove-from-agenda
65 (8sync-port-remove (nm-server-socket nm))))
67 ;; Maximum number of backlogged connections when we listen
68 (define %maximum-backlog-conns 128) ; same as SOMAXCONN on Linux 2.X,
71 (define (nm-install-socket nm server port)
72 "Install socket on SERVER with PORT"
73 (let ((s (socket PF_INET ; ipv4
74 SOCK_STREAM ; two-way connection-based byte stream
77 (inet-pton AF_INET server)
79 ;; Totally mimed from the Guile manual. Not sure if we need this, but:
80 ;; http://www.unixguide.net/network/socketfaq/4.5.shtml
81 (setsockopt s SOL_SOCKET SO_REUSEADDR 1) ; reuse port even if port is busy
82 ;; Connecting to a non-specific address:
83 ;; (bind s AF_INET INADDR_ANY port)
84 ;; Should this be an option? Guess I don't know why we'd need it
85 ;; @@: If we wanted to support listening on a particular hostname,
86 ;; could see 8sync's irc.scm...
87 (bind s AF_INET addr port)
88 ;; Listen to connections
89 (listen s %maximum-backlog-conns)
91 ;; Throw a system-error rather than block on an (accept)
92 ;; that has nothing to do
97 ;; @@: This is used in Guile's http server under the commit:
98 ;; * module/web/server/http.scm (http-open): Ignore SIGPIPE. Keeps the
99 ;; server from dying in some circumstances.
100 ;; (sigaction SIGPIPE SIG_IGN)
101 ;; Will this break other things that use pipes for us though?
103 (set! (nm-server-socket nm) s)
105 (format #t "Listening for clients in pid: ~s\n" (getpid))
106 (8sync-port s #:read (lambda (s) (nm-new-client nm s)))
107 ;; TODO: set up periodic close of idle connections?
110 (define (nm-new-client nm s)
111 "Handle new client coming in to socket S"
112 (let* ((client-connection (accept s))
113 (client-details (cdr client-connection))
114 (client (car client-connection)))
115 (format #t "New client: ~s\n" client-details)
116 (format #t "Client address: ~s\n"
118 (sockaddr:addr client-details)))
120 (let ((client-id (big-random-number)))
121 (hash-set! (nm-clients nm) client-id client)
122 (8sync-port client #:read (nm-make-client-receive nm client-id)))))
124 (define (nm-make-client-receive nm client-id)
125 "Make a method to receive client data"
127 (define (reset-buffer)
129 (define (should-read-char client)
130 (and (not (port-closed? client))
132 (not (eof-object? (peek-char client)))))
133 (define (receive-handler client)
134 (while (should-read-char client)
135 (set! buffer (cons (read-char client) buffer))
137 (;; @@: Do we need the "char?"
138 (#\newline #\return (? char? line-chars) ...)
139 (let ((ready-line (list->string (reverse line-chars))))
143 (nm-handle-line nm client client-id ready-line)))
145 ;; Shut things down on closed port or EOF object
147 ((port-closed? client)
148 (nm-handle-port-closed nm client client-id))
149 ((and (char-ready? client)
150 (eof-object? (peek-char client)))
151 (nm-handle-port-eof nm client client-id))))
154 (define (nm-handle-port-closed nm client client-id)
155 "Handle a closed port"
156 (format #t "DEBUG: handled closed port ~x\n" client-id)
157 (8sync-port-remove client)
158 (hash-remove! (nm-clients nm) client-id))
160 (define-method (nm-handle-port-eof nm client client-id)
161 "Handle seeing an EOF on port"
162 (format #t "DEBUG: handled eof-object on port ~x\n" client-id)
164 (8sync-port-remove client)
165 (hash-remove! (nm-clients nm) client-id))
167 (define-method (nm-handle-line nm client client-id line)
168 "Handle an incoming line of input from a client"
169 (<- nm (nm-send-input-to nm) 'client-input
173 (define-method (nm-send-to-client-id nm client-id data)
174 "Send DATA to TO-CLIENT id"
176 (hash-ref (nm-clients nm) client-id)))
178 ; (ez-run-hive hive (list (bootstrap-message hive (actor-id nm) 'start-listening)))
181 ;; (define-method (nm-close-port (nm <network-manager>)))
184 ;;; The game master! Runs the world.
185 ;;; =================================
187 ;; @@: We could call this a "world builder" instead...
188 ;; I kinda like calling it a GM though.
190 (define (gm-init-world gm message)
194 ;; Init basic rooms / structure
197 ;; Restore database-based actors
200 ;; Set up the network
201 (gm-setup-network gm))
203 (define (gm-setup-network gm)
204 ;; Create a default network manager if none available
205 (set! (gm-network-manager gm)
206 (create-actor* gm <network-manager> "netman"
207 #:send-input-to (actor-id gm)))
209 ;; TODO: Add host and port options
210 (<-wait gm (gm-network-manager gm) 'start-listening))
212 (define (gm-handle-client-input actor message)
213 "Handle input from a client."
214 (define client-id (message-ref message 'client))
215 (define input (message-ref message 'data))
216 (format #t "From ~s: ~s\n" client-id input)
217 (<- actor (gm-network-manager actor) 'send-to-client
219 #:data "Thanks, we got it!\n"))
221 (define-class <game-master> (<actor>)
222 ;; The directory is a "namespaced" directory of all "special" content
223 ;; in the game, identifiable by some special key.
224 ;; (The namespace is simply a cons of (namespace . special-symbol))
225 (directory #:init-thunk make-hash-table)
226 ;; A mapping of client ids to in-game actors
227 (client-to-actor #:init-thunk make-hash-table)
229 (network-manager #:accessor gm-network-manager
234 (make-action-dispatch
235 (init-world gm-init-world)
236 (client-input gm-handle-client-input))))
239 (define-method (gm-setup-database (gm <game-master>))
242 (define-method (gm-setup-rooms-etc (gm <game-master>))
249 (define (run-demo . args)
250 (define hive (make-hive))
252 (hive-create-actor-gimmie* hive <game-master> "gm"))
254 ;; @@: Boy, wouldn't it be nice if the agenda could do things
257 (list (bootstrap-message hive (actor-id gm) 'init-world))))