2015-05-11 22:25:38 +00:00
|
|
|
#lang racket/base
|
|
|
|
;; Marketplace-style endpoints (analogous to threads)
|
|
|
|
|
|
|
|
(provide (struct-out endpoint-group)
|
|
|
|
(struct-out add-endpoint)
|
|
|
|
(struct-out delete-endpoint)
|
2015-12-01 22:22:08 +00:00
|
|
|
(struct-out as-endpoint)
|
2015-05-11 22:25:38 +00:00
|
|
|
make-endpoint-group
|
|
|
|
spawn-endpoint-group
|
2015-12-03 20:53:07 +00:00
|
|
|
boot-endpoint-group
|
2015-05-11 22:25:38 +00:00
|
|
|
endpoint-action?
|
|
|
|
endpoint-group-handle-event
|
|
|
|
pretty-print-endpoint-group)
|
|
|
|
|
|
|
|
(require racket/set)
|
|
|
|
(require racket/match)
|
|
|
|
(require (only-in racket/list flatten))
|
|
|
|
(require "route.rkt")
|
|
|
|
(require "patch.rkt")
|
|
|
|
(require "core.rkt")
|
|
|
|
(require "mux.rkt")
|
|
|
|
(require "pretty.rkt")
|
2015-08-25 19:54:23 +00:00
|
|
|
(require "tset.rkt")
|
2015-05-11 22:25:38 +00:00
|
|
|
|
|
|
|
;; An EID is a Nat.
|
|
|
|
|
|
|
|
;; Endpoint-group private states
|
2015-12-04 17:23:28 +00:00
|
|
|
(struct endpoint-group (mux ;; Mux
|
2015-05-11 22:25:38 +00:00
|
|
|
endpoints ;; (HashTable EID Endpoint)
|
|
|
|
state ;; Any
|
|
|
|
)
|
|
|
|
#:transparent
|
|
|
|
#:methods gen:prospect-pretty-printable
|
|
|
|
[(define (prospect-pretty-print g [p (current-output-port)])
|
|
|
|
(pretty-print-endpoint-group g p))])
|
|
|
|
|
2015-11-25 16:27:58 +00:00
|
|
|
;; A Endpoint is a (Event State -> Transition)
|
2015-05-11 22:25:38 +00:00
|
|
|
;; A Transition reuses the struct from core, but with EndpointActions instead of plain Actions.
|
|
|
|
;; An EndpointAction is either an Action, or a
|
|
|
|
;; (add-endpoint (EID State -> (Values Endpoint Transition))), or a
|
2015-12-01 22:22:08 +00:00
|
|
|
;; (delete-endpoint)
|
|
|
|
;; (as-endpoint EID EndpointAction)
|
2015-05-11 22:25:38 +00:00
|
|
|
(struct add-endpoint (function) #:prefab)
|
2015-12-01 22:22:08 +00:00
|
|
|
(struct delete-endpoint () #:prefab)
|
|
|
|
(struct as-endpoint (eid action) #:prefab)
|
2015-05-11 22:25:38 +00:00
|
|
|
|
|
|
|
(define (make-endpoint-group initial-state)
|
2015-12-04 17:23:28 +00:00
|
|
|
(endpoint-group (mux)
|
2015-05-11 22:25:38 +00:00
|
|
|
(hash)
|
|
|
|
initial-state))
|
|
|
|
|
2015-12-03 20:53:07 +00:00
|
|
|
(define-syntax-rule (spawn-endpoint-group initial-state action-constree ...)
|
|
|
|
(<spawn> (lambda () (boot-endpoint-group initial-state (list action-constree ...)))))
|
|
|
|
|
|
|
|
(define (boot-endpoint-group initial-state initial-actions)
|
|
|
|
(define-values (final-cumulative-patch final-actions final-g)
|
|
|
|
(interpret-endpoint-actions empty-patch
|
|
|
|
'()
|
|
|
|
(make-endpoint-group initial-state)
|
|
|
|
-1
|
|
|
|
initial-actions))
|
|
|
|
(list endpoint-group-handle-event
|
|
|
|
(transition final-g (incorporate-cumulative-patch final-actions
|
|
|
|
final-cumulative-patch))))
|
2015-05-11 22:25:38 +00:00
|
|
|
|
|
|
|
(define (endpoint-action? a)
|
|
|
|
(or (action? a)
|
|
|
|
(add-endpoint? a)
|
2015-12-01 22:22:08 +00:00
|
|
|
(delete-endpoint? a)
|
|
|
|
(and (as-endpoint? a) (endpoint-action? (as-endpoint-action a)))))
|
2015-05-11 22:25:38 +00:00
|
|
|
|
2015-11-25 16:27:58 +00:00
|
|
|
(define (inert-endpoint e state) #f)
|
2015-05-11 22:25:38 +00:00
|
|
|
|
|
|
|
(define (endpoint-group-handle-event e g)
|
2015-12-04 17:23:28 +00:00
|
|
|
(match-define (endpoint-group m endpoints state) g)
|
2015-05-11 22:25:38 +00:00
|
|
|
(define affected-eids
|
|
|
|
(match e
|
|
|
|
[#f (hash-keys endpoints)]
|
2015-12-04 17:23:28 +00:00
|
|
|
[(? patch?) (compute-affected-pids (mux-routing-table m) e)]
|
|
|
|
[(message body) (mux-route-message m body)]))
|
2015-12-03 20:55:40 +00:00
|
|
|
(sequence-handlers g (for/list [(eid (sort affected-eids <))]
|
2015-11-25 16:27:58 +00:00
|
|
|
(list (if (patch? e)
|
2015-12-04 17:23:28 +00:00
|
|
|
(view-patch e (mux-interests-of m eid))
|
2015-11-25 16:27:58 +00:00
|
|
|
e)
|
|
|
|
eid
|
|
|
|
(hash-ref endpoints eid (lambda () inert-endpoint))))))
|
2015-05-11 22:25:38 +00:00
|
|
|
|
2015-11-25 16:27:58 +00:00
|
|
|
(define (sequence-handlers g tasks)
|
2015-05-11 22:25:38 +00:00
|
|
|
(let/ec return
|
|
|
|
(define-values (final-cumulative-patch final-actions final-g idle?)
|
|
|
|
(for/fold ([cumulative-patch empty-patch]
|
|
|
|
[actions '()]
|
|
|
|
[g g]
|
|
|
|
[idle? #t])
|
|
|
|
([task tasks])
|
|
|
|
(match-define (list e eid ep) task)
|
2015-11-25 16:27:58 +00:00
|
|
|
(match (ep e (endpoint-group-state g))
|
2015-05-11 22:25:38 +00:00
|
|
|
[#f (values cumulative-patch actions g idle?)]
|
2015-11-18 19:32:43 +00:00
|
|
|
[(<quit> exn ep-acs) (return (<quit> exn (filter action? (flatten ep-acs))))]
|
2015-05-11 22:25:38 +00:00
|
|
|
[(transition new-state ep-acs)
|
|
|
|
(define-values (cp acs next-g)
|
|
|
|
(interpret-endpoint-actions cumulative-patch
|
|
|
|
actions
|
|
|
|
(struct-copy endpoint-group g [state new-state])
|
|
|
|
eid
|
|
|
|
ep-acs))
|
|
|
|
(values cp acs next-g #f)])))
|
|
|
|
(if idle?
|
|
|
|
#f
|
|
|
|
(transition final-g (incorporate-cumulative-patch final-actions final-cumulative-patch)))))
|
|
|
|
|
|
|
|
(define (incorporate-cumulative-patch actions cumulative-patch)
|
|
|
|
(if (patch-empty? cumulative-patch)
|
|
|
|
actions
|
|
|
|
(cons actions cumulative-patch)))
|
|
|
|
|
|
|
|
(define (interpret-endpoint-patch cumulative-patch actions g eid p0)
|
2015-12-04 17:23:28 +00:00
|
|
|
(define-values (new-mux _eid p p-aggregate)
|
|
|
|
(mux-update-stream (endpoint-group-mux g) eid p0))
|
2015-05-11 22:25:38 +00:00
|
|
|
(values (patch-seq cumulative-patch p-aggregate)
|
|
|
|
actions
|
2015-12-04 17:23:28 +00:00
|
|
|
(struct-copy endpoint-group g [mux new-mux])))
|
2015-05-11 22:25:38 +00:00
|
|
|
|
|
|
|
(define (interpret-endpoint-action cumulative-patch actions g eid endpoint-action)
|
|
|
|
(match endpoint-action
|
|
|
|
[(or (? message?)
|
|
|
|
(? spawn?))
|
|
|
|
(values empty-patch
|
|
|
|
(cons (incorporate-cumulative-patch actions cumulative-patch) endpoint-action)
|
|
|
|
g)]
|
|
|
|
[(? patch? p0)
|
|
|
|
(interpret-endpoint-patch cumulative-patch actions g eid p0)]
|
|
|
|
[(add-endpoint function)
|
2015-12-04 17:23:28 +00:00
|
|
|
(define-values (new-mux new-eid _p _p-aggregate)
|
|
|
|
(mux-add-stream (endpoint-group-mux g) empty-patch))
|
2015-05-11 22:25:38 +00:00
|
|
|
(define-values (new-ep initial-transition) (function new-eid (endpoint-group-state g)))
|
|
|
|
(interpret-endpoint-actions cumulative-patch
|
|
|
|
actions
|
|
|
|
(struct-copy endpoint-group g
|
2015-12-04 17:23:28 +00:00
|
|
|
[mux new-mux]
|
2015-05-11 22:25:38 +00:00
|
|
|
[endpoints
|
|
|
|
(hash-set (endpoint-group-endpoints g)
|
|
|
|
new-eid
|
|
|
|
new-ep)]
|
|
|
|
[state (transition-state initial-transition)])
|
|
|
|
new-eid
|
|
|
|
(transition-actions initial-transition))]
|
2015-12-01 22:22:08 +00:00
|
|
|
[(delete-endpoint)
|
2015-05-11 22:25:38 +00:00
|
|
|
(interpret-endpoint-patch cumulative-patch
|
|
|
|
actions
|
|
|
|
(struct-copy endpoint-group g
|
|
|
|
[endpoints
|
|
|
|
(hash-remove (endpoint-group-endpoints g) eid)])
|
|
|
|
eid
|
2016-01-22 02:55:41 +00:00
|
|
|
(patch (trie-empty) (pattern->trie #t ?)))]
|
2015-12-01 22:22:08 +00:00
|
|
|
[(as-endpoint other-eid inner-endpoint-action)
|
|
|
|
(interpret-endpoint-actions cumulative-patch actions g other-eid inner-endpoint-action)]))
|
2015-05-11 22:25:38 +00:00
|
|
|
|
|
|
|
(define (interpret-endpoint-actions cumulative-patch actions g eid unflattened-endpoint-actions)
|
|
|
|
(define endpoint-actions (filter endpoint-action? (flatten unflattened-endpoint-actions)))
|
|
|
|
(for/fold ([cumulative-patch cumulative-patch]
|
|
|
|
[actions actions]
|
|
|
|
[g g])
|
|
|
|
([endpoint-action endpoint-actions])
|
|
|
|
(interpret-endpoint-action cumulative-patch
|
|
|
|
actions
|
|
|
|
g
|
|
|
|
eid
|
|
|
|
endpoint-action)))
|
|
|
|
|
|
|
|
(define (pretty-print-endpoint-group g [p (current-output-port)])
|
2015-12-04 17:23:28 +00:00
|
|
|
(match-define (endpoint-group m endpoints state) g)
|
2015-05-11 22:25:38 +00:00
|
|
|
(fprintf p "ENDPOINT GROUP:\n")
|
|
|
|
(fprintf p " ---- STATE:\n")
|
|
|
|
(display (indented-port-output 6 (lambda (p) (prospect-pretty-print state p))) p)
|
|
|
|
(newline p)
|
|
|
|
(fprintf p " - ~a endpoints\n" (hash-count endpoints))
|
2015-12-04 17:23:28 +00:00
|
|
|
(fprintf p " - next eid: ~a\n" (mux-next-pid mux))
|
2015-05-11 22:25:38 +00:00
|
|
|
(fprintf p " - routing table:\n")
|
2016-01-22 02:55:41 +00:00
|
|
|
(pretty-print-trie (mux-routing-table mux) p))
|