2021-06-04 13:56:03 +00:00
|
|
|
;;; SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
;;; SPDX-FileCopyrightText: Copyright © 2010-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
2021-06-01 15:19:24 +00:00
|
|
|
|
2020-04-27 18:27:48 +00:00
|
|
|
#lang syndicate
|
2018-05-01 19:58:43 +00:00
|
|
|
|
2020-04-27 18:27:48 +00:00
|
|
|
(require/activate syndicate/drivers/tcp)
|
2018-05-01 19:58:43 +00:00
|
|
|
(require racket/format)
|
|
|
|
|
|
|
|
(message-struct speak (who what))
|
|
|
|
(assertion-struct present (who))
|
|
|
|
|
|
|
|
(dataspace
|
|
|
|
(spawn #:name 'chat-server
|
|
|
|
(during/spawn (inbound (tcp-connection $id (tcp-listener 5999)))
|
|
|
|
#:name (list 'chat-connection id)
|
|
|
|
(assert (outbound (tcp-accepted id)))
|
2019-05-12 12:07:38 +00:00
|
|
|
(on-start (send! (outbound (credit (tcp-listener 5999) 1)))
|
|
|
|
(send! (outbound (credit tcp-in id 1))))
|
2018-05-01 19:58:43 +00:00
|
|
|
(let ((me (gensym 'user)))
|
|
|
|
(assert (present me))
|
|
|
|
(on (message (inbound (tcp-in-line id $bs)))
|
|
|
|
(match bs
|
|
|
|
[#"/quit" (stop-current-facet)]
|
|
|
|
[#"/stop-server" (quit-dataspace!)]
|
2019-05-12 12:07:38 +00:00
|
|
|
[_ (send! (speak me (bytes->string/utf-8 bs)))
|
|
|
|
(send! (outbound (credit tcp-in id 1)))])))
|
2018-05-01 19:58:43 +00:00
|
|
|
(during (present $user)
|
|
|
|
(on-start (send! (outbound (tcp-out id (string->bytes/utf-8 (~a user " arrived\n"))))))
|
|
|
|
(on-stop (send! (outbound (tcp-out id (string->bytes/utf-8 (~a user " left\n"))))))
|
|
|
|
(on (message (speak user $text))
|
|
|
|
(send!
|
|
|
|
(outbound (tcp-out id (string->bytes/utf-8 (~a user " says '" text "'\n"))))))))))
|