2016-07-10 16:33:16 +00:00
|
|
|
#lang syndicate/actor
|
2015-12-11 02:23:32 +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)
|
2015-12-11 02:23:32 +00:00
|
|
|
(require (only-in racket/string string-trim))
|
|
|
|
|
|
|
|
(struct says (who what) #:prefab)
|
|
|
|
(struct present (who) #:prefab)
|
2015-12-11 17:45:15 +00:00
|
|
|
(struct shutdown () #:prefab)
|
2015-12-11 02:23:32 +00:00
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
(dataspace (define us (tcp-listener 5999))
|
|
|
|
(until (message (shutdown))
|
2017-03-14 22:29:45 +00:00
|
|
|
(assert (outbound (advertise (observe (tcp-channel _ us _)))))
|
|
|
|
(during/spawn (inbound (advertise (tcp-channel $them us _)))
|
|
|
|
(assert (outbound (advertise (tcp-channel us them _))))
|
|
|
|
|
|
|
|
(define (send-to-remote fmt . vs)
|
|
|
|
(send! (outbound (tcp-channel us them (string->bytes/utf-8 (apply format fmt vs))))))
|
|
|
|
|
|
|
|
(define user (gensym 'user))
|
|
|
|
|
|
|
|
(on-start (send-to-remote "Welcome, ~a.\n" user))
|
|
|
|
(assert (present user))
|
|
|
|
(on (message (inbound (tcp-channel them us $bs)))
|
|
|
|
(define input-string (string-trim (bytes->string/utf-8 bs)))
|
|
|
|
(if (equal? input-string "quit-dataspace")
|
|
|
|
(send! (shutdown))
|
|
|
|
(send! (says user input-string))))
|
|
|
|
|
|
|
|
(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)))))))
|