2013-03-29 03:00:29 +00:00
|
|
|
#lang marketplace
|
|
|
|
|
2013-06-03 18:57:42 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-06-07 22:03:43 +00:00
|
|
|
(spawn-vm
|
2013-03-29 03:00:29 +00:00
|
|
|
(at-meta-level
|
2013-06-03 18:57:42 +00:00
|
|
|
(observe-publishers (tcp-channel ? (tcp-listener 5999) ?)
|
|
|
|
(match-conversation (tcp-channel them us _)
|
|
|
|
(on-presence (spawn (chat-session them us)))))))
|
2013-03-29 03:00:29 +00:00
|
|
|
|
2013-06-03 18:57:42 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-03-29 03:00:29 +00:00
|
|
|
(define (chat-session them us)
|
|
|
|
(define user (gensym 'user))
|
|
|
|
(transition stateless
|
|
|
|
(listen-to-user user them us)
|
|
|
|
(speak-to-user user them us)))
|
|
|
|
|
2013-06-03 18:57:42 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-03-29 03:00:29 +00:00
|
|
|
(define (listen-to-user user them us)
|
|
|
|
(list
|
|
|
|
(at-meta-level
|
2013-06-07 21:46:20 +00:00
|
|
|
(subscriber (tcp-channel them us ?)
|
2013-06-03 18:57:42 +00:00
|
|
|
(on-absence (quit))
|
|
|
|
(on-message
|
|
|
|
[(tcp-channel _ _ (? bytes? text))
|
2013-06-07 19:50:21 +00:00
|
|
|
(send-message `(,user says ,text))])))
|
2013-06-07 21:46:20 +00:00
|
|
|
(publisher `(,user says ,?))))
|
2013-03-29 03:00:29 +00:00
|
|
|
|
2013-06-03 18:57:42 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-03-29 03:00:29 +00:00
|
|
|
(define (speak-to-user user them us)
|
|
|
|
(define (say fmt . args)
|
|
|
|
(at-meta-level
|
|
|
|
(send-message
|
|
|
|
(tcp-channel us them (apply format fmt args)))))
|
|
|
|
(define (announce who did-what)
|
|
|
|
(unless (equal? who user)
|
|
|
|
(say "~s ~s.~n" who did-what)))
|
|
|
|
(list
|
|
|
|
(say "You are ~s.~n" user)
|
|
|
|
(at-meta-level
|
2013-06-07 21:46:20 +00:00
|
|
|
(publisher (tcp-channel us them ?)))
|
|
|
|
(subscriber `(,? says ,?)
|
2013-06-03 18:57:42 +00:00
|
|
|
(match-conversation `(,who says ,_)
|
|
|
|
(on-presence (announce who 'arrived))
|
|
|
|
(on-absence (announce who 'departed))
|
|
|
|
(on-message [`(,who says ,what)
|
|
|
|
(say "~a: ~a" who what)])))))
|