From 5280f1eb5c5c4ac10443738100ede7a506b42cfb Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Fri, 9 Oct 2015 18:45:23 -0400 Subject: [PATCH] Toy file server example --- minimart/actor.rkt | 2 +- minimart/examples/toy-file-server.rkt | 35 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 minimart/examples/toy-file-server.rkt diff --git a/minimart/actor.rkt b/minimart/actor.rkt index 9a43d73..6d6bdbb 100644 --- a/minimart/actor.rkt +++ b/minimart/actor.rkt @@ -436,7 +436,7 @@ #`(#:when #,condition) #'()) (#,(if pub? #'pub #'sub) #,gestalt-stx - #:meta-level #,meta-level))))) + #:meta-level #,(or meta-level 0)))))) (define (push-action! action-stx) (define-temporaries [temp action-stx]) diff --git a/minimart/examples/toy-file-server.rkt b/minimart/examples/toy-file-server.rkt new file mode 100644 index 0000000..2050e29 --- /dev/null +++ b/minimart/examples/toy-file-server.rkt @@ -0,0 +1,35 @@ +#lang minimart + +(struct save (filename body) #:prefab) +(struct contents (filename body) #:prefab) + +(actor #:name file-server + #:state [files (hash)] + + (subscribe (save ($ filename) ($ body)) + #:update [files (hash-set files filename body)] + #:update-routes) + + (observe-subscribers (contents ($ filename) ?) + #:level 1 + #:name observed-filenames + #:set filename) + + (for/advertise [(filename observed-filenames) + #:when (hash-has-key? files filename)] + (contents filename (hash-ref files filename)))) + +(define (spawn-file-watcher filename) + (actor #:name observer-of-files + (observe-advertisers (contents filename ($ file-contents)) + #:name file-contents + #:set file-contents + (printf "Contents of ~a: ~v\n" filename file-contents)))) + +(spawn-file-watcher 'a) +(spawn-file-watcher 'b) +(spawn-file-watcher 'c) +(send (save 'a "first file")) +(send (save 'b "second file")) +(send (save 'c "third file")) +(send (save 'b "second file, second version"))