#lang imperative-syndicate

(require/activate "udp-dataspace.rkt")
(require/activate imperative-syndicate/drivers/external-event)
(require (only-in racket/port read-bytes-line-evt))
(require racket/random file/sha1)

(message-struct speak (who what))
(assertion-struct present (who))

(spawn (define me (bytes->hex-string (crypto-random-bytes 8)))
       (define stdin-evt (read-bytes-line-evt (current-input-port) 'any))

       (assert (mcds-outbound (present me)))

       (on (message (inbound (external-event stdin-evt (list $line))))
           (if (eof-object? line)
               (stop-current-facet)
               (send! (mcds-outbound (speak me line)))))

       (during (mcds-inbound (present $user))
         (on-start (printf "~a arrived\n" user))
         (on-stop  (printf "~a left\n" user))
         (on (message (mcds-inbound (speak user $text)))
             (printf "~a says '~a'\n" user text))))
