2014-06-02 18:13:12 +00:00
|
|
|
#lang minimart
|
|
|
|
|
|
|
|
(require racket/set)
|
|
|
|
(require (only-in racket/string string-trim))
|
|
|
|
(require "../drivers/tcp.rkt")
|
|
|
|
(require "../demand-matcher.rkt")
|
|
|
|
|
|
|
|
(define (spawn-session them us)
|
|
|
|
(define user (gensym 'user))
|
2014-06-18 20:05:46 +00:00
|
|
|
(define remote-detector (project-pubs #:meta-level 1 (?!)))
|
|
|
|
(define peer-detector (project-pubs `(,(?!) says ,?)))
|
2014-06-02 18:13:12 +00:00
|
|
|
(define (send-to-remote fmt . vs)
|
|
|
|
(send #:meta-level 1 (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))))
|
|
|
|
(list (send-to-remote "Welcome, ~a.\n" user)
|
2014-06-06 22:07:58 +00:00
|
|
|
(spawn (lambda (e old-peers)
|
|
|
|
(log-info "~a: ~v --> ~v" user e old-peers)
|
2014-06-02 18:13:12 +00:00
|
|
|
(match e
|
|
|
|
[(message (tcp-channel _ _ bs) 1 #f)
|
2014-06-06 22:07:58 +00:00
|
|
|
(transition old-peers
|
|
|
|
(send `(,user says ,(string-trim (bytes->string/utf-8 bs)))))]
|
2014-06-02 18:13:12 +00:00
|
|
|
[(message `(,who says ,what) 0 #f)
|
2014-06-06 22:07:58 +00:00
|
|
|
(transition old-peers (say who "says: ~a" what))]
|
2014-06-02 18:13:12 +00:00
|
|
|
[(routing-update g)
|
2014-06-18 20:05:46 +00:00
|
|
|
(define new-peers (gestalt-project/single g peer-detector))
|
2014-06-02 18:13:12 +00:00
|
|
|
(transition
|
2014-06-06 22:07:58 +00:00
|
|
|
new-peers
|
2014-06-18 20:05:46 +00:00
|
|
|
(list (when (matcher-empty? (gestalt-project g remote-detector)) (quit))
|
2014-06-06 22:07:58 +00:00
|
|
|
(for/list [(who (set-subtract new-peers old-peers))]
|
|
|
|
(say who "arrived."))
|
|
|
|
(for/list [(who (set-subtract old-peers new-peers))]
|
|
|
|
(say who "departed."))))]
|
2014-06-02 18:13:12 +00:00
|
|
|
[#f #f]))
|
2014-06-06 22:07:58 +00:00
|
|
|
(set)
|
2014-06-02 18:13:12 +00:00
|
|
|
(gestalt-union (sub `(,? says ,?))
|
|
|
|
(sub `(,? says ,?) #:level 1)
|
|
|
|
(pub `(,user says ,?))
|
|
|
|
(sub (tcp-channel them us ?) #:meta-level 1)
|
2014-06-06 22:07:58 +00:00
|
|
|
(sub (tcp-channel them us ?) #:meta-level 1 #:level 1)
|
|
|
|
(pub (tcp-channel us them ?) #:meta-level 1)))))
|
2014-06-02 18:13:12 +00:00
|
|
|
|
|
|
|
(spawn-tcp-driver)
|
|
|
|
(spawn-world
|
|
|
|
(spawn-demand-matcher (tcp-channel (?! (tcp-address ? ?)) (?! (tcp-listener 5999)) ?)
|
|
|
|
#:meta-level 1
|
|
|
|
spawn-session))
|