2017-08-10 19:17:28 +00:00
|
|
|
#lang syndicate
|
2016-01-13 16:35:03 +00:00
|
|
|
|
2016-07-30 17:02:07 +00:00
|
|
|
(require syndicate/protocol/advertise)
|
2016-07-12 19:05:56 +00:00
|
|
|
(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)
|
|
|
|
|
2017-03-14 22:29:45 +00:00
|
|
|
(define us (tcp-listener 5999))
|
|
|
|
(spawn (assert (advertise (observe (tcp-channel _ us _))))
|
|
|
|
(during/spawn (advertise (tcp-channel $them us _))
|
|
|
|
(assert (advertise (tcp-channel us them _)))
|
2016-01-13 16:35:03 +00:00
|
|
|
|
2017-03-14 22:29:45 +00:00
|
|
|
(define (send-to-remote fmt . vs)
|
|
|
|
(send! (tcp-channel us them (string->bytes/utf-8 (apply format fmt vs)))))
|
2016-01-13 16:35:03 +00:00
|
|
|
|
|
|
|
(define user (gensym 'user))
|
|
|
|
|
2017-03-14 22:29:45 +00:00
|
|
|
(on-start (send-to-remote "Welcome, ~a.\n" user))
|
2016-08-24 16:35:38 +00:00
|
|
|
(assert (present user))
|
|
|
|
(on (message (tcp-channel them us $bs))
|
2017-03-14 22:29:45 +00:00
|
|
|
(send! (says user (string-trim (bytes->string/utf-8 bs)))))
|
2016-01-13 16:35:03 +00:00
|
|
|
|
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))))))
|