2017-08-10 19:04:45 +00:00
|
|
|
#lang syndicate/core
|
2015-03-06 13:29:59 +00:00
|
|
|
|
2016-07-30 17:02:07 +00:00
|
|
|
(require syndicate/protocol/advertise)
|
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 server-id (tcp-listener 5999))
|
|
|
|
|
|
|
|
(define (spawn-connection-handler c)
|
|
|
|
(log-info "spawn-connection-handler ~v" c)
|
|
|
|
(define (connection-handler e n)
|
|
|
|
(when e (log-info "connection-handler ~v: ~v /// ~v" c e n))
|
|
|
|
(match e
|
|
|
|
[(? patch/removed?) (quit)]
|
|
|
|
[(message (tcp-channel src dst #"quit\n"))
|
|
|
|
(quit (message (tcp-channel dst src #"OK, then.\n")))]
|
|
|
|
[(message (tcp-channel src dst bs))
|
|
|
|
(transition n (message (tcp-channel dst src (string->bytes/utf-8
|
|
|
|
(format "You said: ~a" bs)))))]
|
|
|
|
[_
|
|
|
|
(and (< n 5)
|
|
|
|
(transition (+ n 1) (message (tcp-channel server-id c (string->bytes/utf-8
|
|
|
|
(format "msg ~v\n" n))))))]))
|
2017-02-15 23:18:19 +00:00
|
|
|
(actor connection-handler
|
2015-03-06 13:29:59 +00:00
|
|
|
0
|
2015-12-03 20:53:07 +00:00
|
|
|
(patch-seq (sub (advertise (tcp-channel c server-id ?)))
|
|
|
|
(sub (tcp-channel c server-id ?))
|
|
|
|
(pub (tcp-channel server-id c ?)))))
|
2015-03-06 13:29:59 +00:00
|
|
|
|
|
|
|
(spawn-demand-matcher (advertise (tcp-channel (?!) server-id ?))
|
|
|
|
(observe (tcp-channel (?!) server-id ?))
|
|
|
|
spawn-connection-handler
|
|
|
|
(lambda (c)
|
|
|
|
(log-info "Connection handler ~v decided to exit" c)
|
|
|
|
'()))
|