2016-04-01 23:53:46 +00:00
|
|
|
#lang syndicate
|
2015-03-06 13:29:59 +00:00
|
|
|
|
2016-07-30 17:02:07 +00:00
|
|
|
(require syndicate/protocol/advertise)
|
2015-03-06 13:29:59 +00:00
|
|
|
(require (only-in racket/string string-trim))
|
2016-07-12 19:05:56 +00:00
|
|
|
(require/activate "../drivers/tcp.rkt")
|
2015-03-06 13:29:59 +00:00
|
|
|
(require "../demand-matcher.rkt")
|
|
|
|
|
|
|
|
(define (spawn-session them us)
|
|
|
|
(define user (gensym 'user))
|
2016-07-30 17:02:07 +00:00
|
|
|
(define remote-detector (inbound (?!)))
|
2016-03-12 16:54:31 +00:00
|
|
|
(define peer-detector (advertise `(,(?!) says ,?)))
|
2015-03-06 13:29:59 +00:00
|
|
|
(define (send-to-remote fmt . vs)
|
2016-07-30 17:02:07 +00:00
|
|
|
(message (outbound (tcp-channel us them (string->bytes/utf-8 (apply format fmt vs))))))
|
2015-03-06 13:29:59 +00:00
|
|
|
(define (say who fmt . vs)
|
|
|
|
(unless (equal? who user) (send-to-remote "~a ~a\n" who (apply format fmt vs))))
|
|
|
|
(list (send-to-remote "Welcome, ~a.\n" user)
|
|
|
|
(spawn/stateless
|
|
|
|
(lambda (e)
|
|
|
|
(match e
|
2016-07-30 17:02:07 +00:00
|
|
|
[(message (inbound (tcp-channel _ _ bs)))
|
2015-10-23 23:50:08 +00:00
|
|
|
(define input-string (string-trim (bytes->string/utf-8 bs)))
|
2016-04-07 07:42:54 +00:00
|
|
|
(if (equal? input-string "quit-dataspace")
|
|
|
|
(quit-dataspace)
|
2015-10-23 23:50:08 +00:00
|
|
|
(message `(,user says ,input-string)))]
|
2015-03-06 13:29:59 +00:00
|
|
|
[(message `(,who says ,what))
|
|
|
|
(say who "says: ~a" what)]
|
|
|
|
[(? patch? p)
|
|
|
|
(if (patch/removed? (patch-project p remote-detector))
|
|
|
|
(quit (send-to-remote "Goodbye!\n"))
|
|
|
|
(let-values (((arrived departed) (patch-project/set/single p peer-detector)))
|
|
|
|
(list (for/list [(who arrived)] (say who "arrived."))
|
|
|
|
(for/list [(who departed)] (say who "departed.")))))]
|
|
|
|
[#f #f]))
|
2015-12-03 20:53:07 +00:00
|
|
|
(patch-seq
|
|
|
|
(sub `(,? says ,?)) ;; read actual chat messages
|
|
|
|
(sub (advertise `(,? says ,?))) ;; observe peer presence
|
|
|
|
(pub `(,user says ,?)) ;; advertise our presence
|
2016-07-30 17:02:07 +00:00
|
|
|
(sub (inbound (tcp-channel them us ?))) ;; read from remote client
|
|
|
|
(sub (inbound (advertise (tcp-channel them us ?)))) ;; monitor remote client
|
|
|
|
(pub (inbound (tcp-channel us them ?))) ;; we will write to remote client
|
2015-12-03 20:53:07 +00:00
|
|
|
))))
|
2015-03-06 13:29:59 +00:00
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
(spawn-dataspace
|
2016-07-30 17:02:07 +00:00
|
|
|
(spawn-demand-matcher (inbound (advertise (tcp-channel (?!) (?! (tcp-listener 5999)) ?)))
|
|
|
|
(inbound (observe (tcp-channel (?!) (?! (tcp-listener 5999)) ?)))
|
2015-03-06 13:29:59 +00:00
|
|
|
spawn-session))
|