Use new <repl-manager>
[mudsync.git] / mudsync / networking.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 (define-module (mudsync networking)
20   #:use-module (8sync actors)
21   #:use-module (8sync agenda)
22   #:use-module (ice-9 format)
23   #:use-module (ice-9 match)
24   #:use-module (ice-9 rdelim)
25   #:use-module (oop goops)
26
27   #:export (;; Should we be exporting these?
28             %default-server
29             %default-port
30
31             <network-manager>
32             nm-close-everything))
33
34 ;;; Networking
35 ;;; ==========
36
37 (define %default-server #f)
38 (define %default-port 8889)
39
40 (define-class <network-manager> (<actor>)
41   (server-socket #:getter nm-server-socket)
42   ;; mapping of client -> client-id
43   (clients #:getter nm-clients
44            #:init-thunk make-hash-table)
45   ;; send input to this actor
46   (send-input-to #:getter nm-send-input-to
47                  #:init-keyword #:send-input-to)
48
49   (actions
50    #:allocation #:each-subclass
51    #:init-value
52    (build-actions
53     (start-listening
54      (lambda* (actor message
55                      #:key (server %default-server)
56                      (port %default-port))
57        (nm-install-socket actor server port)))
58     (send-to-client
59      (lambda* (actor message #:key client data)
60        (nm-send-to-client-id actor client data))))))
61
62 ;;; TODO: We should provide something like this, but this isn't used currently,
63 ;;;    and uses old deprecated code (the 8sync-port-remove stuff).
64 ;; (define-method (nm-close-everything (nm <network-manager>) remove-from-agenda)
65 ;;   "Shut it down!"
66 ;;   ;; close all clients
67 ;;   (hash-for-each
68 ;;    (lambda (_ client)
69 ;;      (close client)
70 ;;      (if remove-from-agenda
71 ;;          (8sync-port-remove client)))
72 ;;    (nm-clients nm))
73 ;;   ;; reset the clients list
74 ;;   (set! (nm-clients) (make-hash-table))
75 ;;   ;; close the server
76 ;;   (close (nm-server-socket nm))
77 ;;   (if remove-from-agenda
78 ;;       (8sync-port-remove (nm-server-socket nm))))
79
80 ;; Maximum number of backlogged connections when we listen
81 (define %maximum-backlog-conns 128)     ; same as SOMAXCONN on Linux 2.X,
82                                         ; says the intarwebs
83
84 (define (nm-install-socket nm server port)
85   "Install socket on SERVER with PORT"
86   (define s
87     (socket PF_INET  ; ipv4
88             SOCK_STREAM  ; two-way connection-based byte stream
89             0))
90   (define addr
91     (if server
92         (inet-pton AF_INET server)
93         INADDR_LOOPBACK))
94
95   ;; Totally mimed from the Guile manual.  Not sure if we need this, but:
96   ;; http://www.unixguide.net/network/socketfaq/4.5.shtml
97   (setsockopt s SOL_SOCKET SO_REUSEADDR 1) ; reuse port even if port is busy
98   ;; Connecting to a non-specific address:
99   ;;   (bind s AF_INET INADDR_ANY port)
100   ;; Should this be an option?  Guess I don't know why we'd need it
101   ;; @@: If we wanted to support listening on a particular hostname,
102   ;;   could see 8sync's irc.scm...
103   (bind s AF_INET addr port)
104   ;; Listen to connections
105   (listen s %maximum-backlog-conns)
106
107   ;; Make port non-blocking
108   (fcntl s F_SETFL (logior O_NONBLOCK (fcntl s F_GETFL)))
109
110   ;; @@: This is used in Guile's http server under the commit:
111   ;;       * module/web/server/http.scm (http-open): Ignore SIGPIPE. Keeps the
112   ;;         server from dying in some circumstances.
113   ;;   (sigaction SIGPIPE SIG_IGN)
114   ;; Will this break other things that use pipes for us though?
115
116   (slot-set! nm 'server-socket s)
117
118   (format #t "Listening for clients in pid: ~s\n" (getpid))
119
120   ;; TODO: set up periodic close of idle connections?
121   (let loop ()
122     ;; (yield)  ;; @@: Do we need this?
123     (define client-connection (accept s))
124     (8sync (nm-new-client nm s client-connection))
125     (loop)))
126
127 (define (nm-new-client nm s client-connection)
128   "Handle new client coming in to socket S"
129   (define client-details (cdr client-connection))
130   (define client (car client-connection))
131   (define client-id (big-random-number))
132   (format #t "New client: ~s\n" client-details)
133   (format #t "Client address: ~s\n"
134           (gethostbyaddr
135            (sockaddr:addr client-details)))
136   (fcntl client F_SETFL (logior O_NONBLOCK (fcntl client F_GETFL)))
137   (hash-set! (nm-clients nm) client-id client)
138   (<- nm (nm-send-input-to nm) 'new-client #:client client-id)
139   (nm-client-receive-loop nm client client-id))
140
141 (define (nm-client-receive-loop nm client client-id)
142   "Make a method to receive client data"
143   (define (loop)
144     (define line (read-line client))
145     (if (eof-object? line)
146         (nm-handle-port-eof nm client client-id)
147         (begin
148           (nm-handle-line nm client client-id
149                           (string-trim-right line #\return))
150           (loop))))
151   (loop))
152
153 (define (nm-handle-port-closed nm client client-id)
154   "Handle a closed port"
155   (format #t "DEBUG: handled closed port ~x\n" client-id)
156   (hash-remove! (nm-clients nm) client-id)
157   (<- nm (nm-send-input-to nm) 'client-closed #:client client-id))
158
159 (define-method (nm-handle-port-eof nm client client-id)
160   "Handle seeing an EOF on port"
161   (format #t "DEBUG: handled eof-object on port ~x\n" client-id)
162       (close client)
163   (hash-remove! (nm-clients nm) client-id)
164   (<- nm (nm-send-input-to nm) 'client-closed #:client client-id))
165
166 (define-method (nm-handle-line nm client client-id line)
167   "Handle an incoming line of input from a client"
168   (<- nm (nm-send-input-to nm) 'client-input
169       #:data line
170       #:client client-id))
171
172 (define-method (nm-send-to-client-id nm client-id data)
173   "Send DATA to TO-CLIENT id"
174   (define client-obj (hash-ref (nm-clients nm) client-id))
175   (if (not client-obj)
176       (throw 'no-such-client
177              "Asked to send data to client but that client doesn't exist"
178              #:client-id client-id
179              #:data data))
180   (display data client-obj))