-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat-server.rkt
47 lines (42 loc) · 1.93 KB
/
chat-server.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname chat-server) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp")) #f)))
; Bundle is
; (make-bundle UniverseState [Listof mail?] [Listof iworld?])
; UniverseState iworld? -> Bundle
; next list of worlds when world iw is joining
; the universe in state s
(define (add-client univ client)
(local ((define univ* (append univ (list client))))
(make-bundle univ*
(list (make-mail client "welcome to the chativerse"))
empty)))
; an obvious example for adding a world:
(check-expect
(add-client '() iworld1)
(make-bundle (list iworld1)
(list (make-mail iworld1 "welcome to the chativerse"))
'()))
; UniverseState iworld? W2U -> Bundle
; next list of worlds when world iw is sending message m to
; the universe in state s
(define (process univ client msg)
(local ((define other-worlds
(filter (λ (c) (not (string=? (iworld-name client)
(iworld-name c))))
univ))
(define msg* (string-append (iworld-name client) ": " msg)))
(make-bundle univ
(map (λ (w) (make-mail w msg*)) other-worlds)
empty)))
(check-expect
(process (list iworld1 iworld2 iworld3) iworld1 "hello")
(make-bundle (list iworld1 iworld2 iworld3)
(list (make-mail iworld2 "iworld1: hello")
(make-mail iworld3 "iworld1: hello"))
empty))
(define go!
(universe '()
[on-new add-client]
[on-msg process]
))