2016-07-10 16:33:16 +00:00
|
|
|
#lang syndicate/actor
|
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)
|
|
|
|
|
|
|
|
(define (spawn-session them us)
|
2017-02-15 23:18:19 +00:00
|
|
|
(spawn (define (send-to-remote fmt . vs)
|
2016-01-13 16:35:03 +00:00
|
|
|
(send! (tcp-channel us them (string->bytes/utf-8 (apply format fmt vs)))))
|
|
|
|
|
|
|
|
(define (say who fmt . vs)
|
|
|
|
(unless (equal? who user)
|
|
|
|
(send-to-remote "~a ~a\n" who (apply format fmt vs))))
|
|
|
|
|
|
|
|
(define user (gensym 'user))
|
2016-08-24 16:35:38 +00:00
|
|
|
(on-start (send-to-remote "Welcome, ~a.\n" user))
|
2016-01-13 16:35:03 +00:00
|
|
|
|
2016-08-24 16:35:38 +00:00
|
|
|
(stop-when (retracted (advertise (tcp-channel them us _))))
|
2016-01-13 16:35:03 +00:00
|
|
|
|
2016-08-24 16:35:38 +00:00
|
|
|
(assert (present user))
|
|
|
|
(on (asserted (present $who)) (say who "arrived."))
|
|
|
|
(on (retracted (present $who)) (say who "departed."))
|
2016-01-13 16:35:03 +00:00
|
|
|
|
2016-08-24 16:35:38 +00:00
|
|
|
(on (message (says $who $what)) (say who "says: ~a" what))
|
|
|
|
|
|
|
|
(assert (advertise (tcp-channel us them _)))
|
|
|
|
(on (message (tcp-channel them us $bs))
|
|
|
|
(send! (says user (string-trim (bytes->string/utf-8 bs)))))))
|
2016-01-13 16:35:03 +00:00
|
|
|
|
2016-02-05 23:03:40 +00:00
|
|
|
(define us (tcp-listener 5999))
|
2017-02-15 23:18:19 +00:00
|
|
|
(spawn (assert (advertise (observe (tcp-channel _ us _))))
|
2016-08-24 16:35:38 +00:00
|
|
|
(on (asserted (advertise (tcp-channel $them us _)))
|
|
|
|
(spawn-session them us)))
|