syndicate-rkt/syndicate/broker/server.rkt

53 lines
2.0 KiB
Racket

#lang imperative-syndicate
(provide (struct-out server-connection)
(struct-out server-inbound)
(struct-out server-outbound)
(struct-out server-envelope))
(require "wire-protocol.rkt")
(require imperative-syndicate/term)
(require racket/set)
;; Internal connection protocol
(assertion-struct server-connection (connection-id scope))
(assertion-struct server-inbound (connection-id body))
(assertion-struct server-outbound (connection-id body))
;; Internal isolation
(assertion-struct server-envelope (scope body))
(spawn #:name 'server-connection-factory
(during/spawn (server-connection $id $scope)
(define endpoints (set))
(on (message (server-inbound id (Assert $ep $a)))
(when (not (set-member? endpoints ep))
(set! endpoints (set-add endpoints ep))
(react
(on-stop (set! endpoints (set-remove endpoints ep)))
(field [assertion a])
(assert (server-envelope scope (assertion)))
(let ((! (lambda (ctor) (lambda (cs) (send! (server-outbound id (ctor ep cs)))))))
(add-observer-endpoint! (lambda ()
(let ((a (assertion)))
(when (observe? a)
(server-envelope scope (observe-specification a)))))
#:on-add (! Add)
#:on-remove (! Del)
#:on-message (! Msg)))
(on (message (server-inbound id (Assert ep $new-a)))
(assertion new-a))
(stop-when (message (server-inbound id (Clear ep)))))))
(on (message (server-inbound id (Message $body)))
(send! (server-envelope scope body)))
(on (message (server-inbound id (Ping)))
(send! (server-outbound id (Pong))))))