diff --git a/chat-os2.rkt b/chat-os2.rkt index de1b64a..e94dd3c 100644 --- a/chat-os2.rkt +++ b/chat-os2.rkt @@ -20,25 +20,25 @@ (match-define (topic _ (tcp-channel connection-id _ _) _) t) (define-values (cin cout in-topic out-topic) (tcp-accept t)) (transition 'no-state - (at-meta-level (cout (term->bytes `(you-are ,connection-id)))) - (at-meta-level (cin (tcp-mode 'lines))) - (at-meta-level (cin (tcp-credit 1))) - (at-meta-level (role/anon out-topic)) - (at-meta-level (role/anon in-topic - #:on-absence (kill) - [(tcp-channel _ _ (? bytes? line)) - (list (at-meta-level (cin (tcp-credit 1))) - (send-message `(,connection-id says ,line)))])) (role/anon (topic-publisher `(,connection-id says ,(wild)))) (role/anon (topic-subscriber `(,(wild) says ,(wild))) #:topic t - #:on-presence (match t [(topic _ `(,who ,_ ,_) _) - (when (not (equal? who connection-id)) + #:on-presence (match t [(topic _ (list who _ _) _) + (unless (equal? who connection-id) (at-meta-level (cout (term->bytes `(,who arrived)))))]) - #:on-absence (match t [(topic _ `(,who ,_ ,_) _) - (when (not (equal? who connection-id)) - (at-meta-level (cout (term->bytes `(,who departed)))))]) - [message (at-meta-level (cout (term->bytes message)))]))) + #:on-absence (match t [(topic _ (list who _ _) _) + (unless (equal? who connection-id) + (at-meta-level (cout (term->bytes `(,who departed)))))]) + [message (at-meta-level (cout (term->bytes message)))]) + (at-meta-level (cout (term->bytes `(you-are ,connection-id))) + (cin (tcp-mode 'lines)) + (cin (tcp-credit 1)) + (role/anon out-topic) + (role/anon in-topic + #:on-absence (kill) + [(tcp-channel _ _ (? bytes? line)) + (list (at-meta-level (cin (tcp-credit 1))) + (send-message `(,connection-id says ,line)))])))) (define (term->bytes v) (with-output-to-bytes (lambda () (write v) (newline))))