syndicate-2017/racket/syndicate/examples/actor/chat-no-quit-world.rkt

30 lines
1.3 KiB
Racket
Raw Permalink Normal View History

#lang syndicate
2016-01-13 16:35:03 +00:00
(require syndicate/protocol/advertise)
(require/activate syndicate/drivers/tcp)
2016-01-13 16:35:03 +00:00
(require (only-in racket/string string-trim))
(struct says (who what) #:prefab)
(struct present (who) #:prefab)
(define us (tcp-listener 5999))
(dataspace (spawn (assert (outbound (advertise (observe (tcp-channel _ us _)))))
(during/spawn (inbound (advertise (tcp-channel $them us _)))
(assert (outbound (advertise (tcp-channel us them _))))
2017-03-14 22:29:45 +00:00
(define (send-to-remote fmt . vs)
(send! (outbound (tcp-channel us them (string->bytes/utf-8 (apply format fmt vs))))))
2017-03-14 22:29:45 +00:00
(define user (gensym 'user))
2017-03-14 22:29:45 +00:00
(on-start (send-to-remote "Welcome, ~a.\n" user))
(assert (present user))
(on (message (inbound (tcp-channel them us $bs)))
(send! (says user (string-trim (bytes->string/utf-8 bs)))))
2017-03-14 22:29:45 +00:00
(during (present $who)
(unless (equal? who user)
(on-start (send-to-remote "~a arrived.\n" who))
(on-stop (send-to-remote "~a departed.\n" who))
(on (message (says who $what)) (send-to-remote "~a says: ~a\n" who what)))))))