syndicate-rkt/syndicate-examples/chat.rkt

47 lines
1.5 KiB
Racket

#lang syndicate
;;; SPDX-License-Identifier: LGPL-3.0-or-later
;;; SPDX-FileCopyrightText: Copyright © 2023-2024 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
(module+ main
(require syndicate/distributed/gatekeeper)
(require syndicate/distributed/tcp)
(require syndicate/driver-support)
(require syndicate/gensym)
(require "schemas/simpleChatProtocol.rkt")
(require syndicate/sturdy)
(require (only-in file/sha1 hex-string->bytes))
(define me (symbol->string (strong-gensym 'user)))
(define ref (SturdyRef (Parameters "syndicate"
(hex-string->bytes "69ca300c1dbfa08fba692102dd82311a")
(CaveatsField-absent))))
(standard-actor-system (ds)
(define conn-facet this-facet)
(define (on-connected remote-ds)
(on-stop (stop-facet conn-facet))
(linked-thread
#:name (list 'read-stdin)
(lambda (facet)
(let loop ()
(match (read-line)
[(? eof-object?) (log-info "EOF on stdin.")]
[line (turn! facet (lambda () (send! remote-ds (Says me line))))
(loop)]))))
(at remote-ds
(assert (Present me))
(during (Present $who)
(on-start (log-info "~a arrived" who))
(on-stop (log-info "~a departed" who)))
(on (message (Says $who $what)) (log-info "~a says: ~v" who what))))
(run-tcp-client-relay
ds
#:hostname "localhost"
#:port 9001
#:import (lambda (v) (gatekeeper-resolve (embedded-value v) ref on-connected)))))