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
|
|
|
|
|
|
|
(define (spawn-session them us)
|
2017-02-15 23:18:19 +00:00
|
|
|
(spawn (define (send-to-remote fmt . vs)
|
2016-07-30 17:02:07 +00:00
|
|
|
(send! (outbound (tcp-channel us them (string->bytes/utf-8 (apply format fmt vs))))))
|
2015-12-11 02:23:32 +00:00
|
|
|
|
|
|
|
(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))
|
2015-12-11 02:23:32 +00:00
|
|
|
|
2016-08-24 16:35:38 +00:00
|
|
|
(stop-when (retracted (inbound (advertise (tcp-channel them us _)))))
|
2015-12-11 02:23:32 +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."))
|
2015-12-11 02:23:32 +00:00
|
|
|
|
2016-08-24 16:35:38 +00:00
|
|
|
(on (message (says $who $what)) (say who "says: ~a" what))
|
|
|
|
|
|
|
|
(assert (outbound (advertise (tcp-channel us them _))))
|
|
|
|
(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))))))
|
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))
|
2016-07-30 17:02:07 +00:00
|
|
|
(assert (outbound (advertise (observe (tcp-channel _ us _)))))
|
|
|
|
(on (asserted (inbound (advertise (tcp-channel $them us _))))
|
2016-04-07 07:42:54 +00:00
|
|
|
(spawn-session them us))))
|