From f432ea8208ac157938f8f55550ffad1b48f7f254 Mon Sep 17 00:00:00 2001 From: Sam Anklesaria Date: Fri, 24 Apr 2009 23:26:10 -0500 Subject: [PATCH] distributed module system w/ qualified + from syntax --- extra/modules/authors.txt | 1 + .../remote-loading/remote-loading.factor | 3 ++ extra/modules/rpc-server/rpc-server.factor | 24 +++++++++++++ extra/modules/rpc/rpc-docs.factor | 9 +++++ extra/modules/rpc/rpc.factor | 26 ++++++++++++++ extra/modules/summary.txt | 1 + extra/modules/using/tests/test-server.factor | 3 ++ extra/modules/using/tests/tests.factor | 4 +++ extra/modules/using/using-docs.factor | 14 ++++++++ extra/modules/using/using.factor | 36 +++++++++++++++++++ extra/modules/util/util.factor | 4 +++ 11 files changed, 125 insertions(+) create mode 100644 extra/modules/authors.txt create mode 100644 extra/modules/remote-loading/remote-loading.factor create mode 100644 extra/modules/rpc-server/rpc-server.factor create mode 100644 extra/modules/rpc/rpc-docs.factor create mode 100644 extra/modules/rpc/rpc.factor create mode 100644 extra/modules/summary.txt create mode 100644 extra/modules/using/tests/test-server.factor create mode 100644 extra/modules/using/tests/tests.factor create mode 100644 extra/modules/using/using-docs.factor create mode 100644 extra/modules/using/using.factor create mode 100644 extra/modules/util/util.factor diff --git a/extra/modules/authors.txt b/extra/modules/authors.txt new file mode 100644 index 0000000000..2300f69f11 --- /dev/null +++ b/extra/modules/authors.txt @@ -0,0 +1 @@ +Sam Anklesaria diff --git a/extra/modules/remote-loading/remote-loading.factor b/extra/modules/remote-loading/remote-loading.factor new file mode 100644 index 0000000000..72557f42f3 --- /dev/null +++ b/extra/modules/remote-loading/remote-loading.factor @@ -0,0 +1,3 @@ +USING: modules.rpc-server vocabs ; +IN: modules.remote-loading service +: get-vocab ( vocabstr -- vocab ) vocab ; \ No newline at end of file diff --git a/extra/modules/rpc-server/rpc-server.factor b/extra/modules/rpc-server/rpc-server.factor new file mode 100644 index 0000000000..880af58581 --- /dev/null +++ b/extra/modules/rpc-server/rpc-server.factor @@ -0,0 +1,24 @@ +USING: accessors assocs effects generalizations io +io.encodings.binary io.servers.connection kernel modules.util +namespaces parser sets sequences serialize threads vocabs +vocabs.parser words tools.walker ; + +IN: modules.rpc-server + +SYMBOL: serving-vocabs V{ } clone serving-vocabs set-global + +: process ( vocabspec -- ) vocab-words [ deserialize-args ] dip deserialize + swap at [ execute ] keep stack-effect out>> length narray serialize flush ; + +: (serve) ( -- ) deserialize dup serving-vocabs get-global index + [ process ] [ f ] if ; + +: start-serving-vocabs ( -- ) [ + 5000 >>insecure binary >>encoding [ (serve) ] >>handler + start-server ] in-thread ; + +SYNTAX: service serving-vocabs get-global empty? [ start-serving-vocabs ] when + current-vocab serving-vocabs get-global adjoin + "get-words" create-in + in get [ vocab vocab-words [ stack-effect ] { } assoc-map-as ] curry + (( -- words )) define-inline ; \ No newline at end of file diff --git a/extra/modules/rpc/rpc-docs.factor b/extra/modules/rpc/rpc-docs.factor new file mode 100644 index 0000000000..af99d210ab --- /dev/null +++ b/extra/modules/rpc/rpc-docs.factor @@ -0,0 +1,9 @@ +USING: help.syntax help.markup ; +IN: modules.rpc +ARTICLE: { "modules" "protocol" } "RPC Protocol" +{ $list + "Send vocab as string" + "Send arglist" + "Send word as string" + "Receive result list" +} ; \ No newline at end of file diff --git a/extra/modules/rpc/rpc.factor b/extra/modules/rpc/rpc.factor new file mode 100644 index 0000000000..f574010aef --- /dev/null +++ b/extra/modules/rpc/rpc.factor @@ -0,0 +1,26 @@ +USING: accessors compiler.units combinators fry generalizations io +io.encodings.binary io.sockets kernel modules.util namespaces +parser sequences serialize vocabs vocabs.parser words ; +IN: modules.rpc + +DEFER: get-words + +: remote-quot ( addrspec vocabspec effect str -- quot ) + '[ _ 5000 binary + [ + _ serialize _ in>> length narray serialize _ serialize flush deserialize-args + ] with-client + ] ; + +: define-remote ( addrspec vocabspec effect str -- ) [ + [ remote-quot ] 2keep create-in -rot define-declared + ] with-compilation-unit ; + +: with-in ( vocab quot -- vocab ) over + [ '[ _ set-in @ ] in get swap dip set-in ] dip vocab ; inline + +: remote-vocab ( addrspec vocabspec -- vocab ) + dup "-remote" append [ + [ (( -- words )) [ "get-words" remote-quot ] keep call-effect ] 2keep + [ rot first2 swap define-remote ] 2curry each + ] with-in ; \ No newline at end of file diff --git a/extra/modules/summary.txt b/extra/modules/summary.txt new file mode 100644 index 0000000000..7ab3416915 --- /dev/null +++ b/extra/modules/summary.txt @@ -0,0 +1 @@ +Distributed module system + syntax diff --git a/extra/modules/using/tests/test-server.factor b/extra/modules/using/tests/test-server.factor new file mode 100644 index 0000000000..2fa813a382 --- /dev/null +++ b/extra/modules/using/tests/test-server.factor @@ -0,0 +1,3 @@ +USING: modules.rpc-server ; +IN: modules.test-server service +: rpc-hello ( -- str ) "hello world" ; \ No newline at end of file diff --git a/extra/modules/using/tests/tests.factor b/extra/modules/using/tests/tests.factor new file mode 100644 index 0000000000..c76feed2c0 --- /dev/null +++ b/extra/modules/using/tests/tests.factor @@ -0,0 +1,4 @@ +USING: modules.using ; +IN: modules.using.tests +USING: prettyprint localhost::modules.test-server ; +rpc-hello . \ No newline at end of file diff --git a/extra/modules/using/using-docs.factor b/extra/modules/using/using-docs.factor new file mode 100644 index 0000000000..c78e546525 --- /dev/null +++ b/extra/modules/using/using-docs.factor @@ -0,0 +1,14 @@ +USING: modules.using modules.rpc-server help.syntax help.markup strings ; +IN: modules + +HELP: service +{ $syntax "IN: module service" } +{ $description "Starts a server for requests for remote procedure calls." } ; + +ARTICLE: { "modules" "remote-loading" } "Using the remote-loading vocabulary" +"If loaded, starts serving vocabularies, accessable through a " { $link POSTPONE: USING: } " form" ; + +HELP: USING: +{ $syntax "USING: rpc-server::module fetch-sever::module { module qualified-name } { module => word ... } ... ;" } +{ $description "Adds vocabularies to the front of the search path. Vocabularies can be fetched remotely, if preceded by a valid hostname. Name pairs facilitate imports like in the " +{ $link POSTPONE: QUALIFIED: } " or " { $link POSTPONE: FROM: } " forms." } ; \ No newline at end of file diff --git a/extra/modules/using/using.factor b/extra/modules/using/using.factor new file mode 100644 index 0000000000..b0891aa391 --- /dev/null +++ b/extra/modules/using/using.factor @@ -0,0 +1,36 @@ +USING: assocs kernel modules.remote-loading modules.rpc +namespaces peg peg.ebnf peg-lexer sequences vocabs vocabs.parser +strings ; +IN: modules.using + +: >qualified ( vocab prefix -- assoc ) + [ vocab-words ] [ 58 suffix ] bi* [ swap [ prepend ] dip ] curry assoc-map ; + +: >partial-vocab ( words assoc -- assoc ) + [ dupd at [ no-word-error ] unless* ] curry { } map>assoc ; + +: remote-load ( addr vocabspec -- voab ) [ "modules.remote-loading" remote-vocab (use+) ] dip get-vocab ; + +: load'em ( vocab words/? -- ) [ swap >partial-vocab ] when* use get push ; + +EBNF: modulize +tokenpart = (!(':').)+ => [[ >string ]] +s = ':' => [[ drop ignore ]] +rpc = tokenpart s s tokenpart => [[ first2 remote-vocab ]] +remote = tokenpart s tokenpart => [[ first2 remote-load ]] +plain = tokenpart => [[ load-vocab ]] +module = rpc | remote | plain +;EBNF + +ON-BNF: USING: +tokenizer = +sym = !(";"|"}"|"=>"). +modspec = sym => [[ modulize ]] +qualified = modspec sym => [[ first2 >qualified ]] +unqualified = modspec => [[ vocab-words ]] +words = ("=>" sym+ )? => [[ [ f ] [ second ] if-empty ]] +long = "{" ( qualified | unqualified ) words "}" => [[ rest first2 load'em ignore ]] +short = modspec => [[ use+ ignore ]] +wordSpec = long | short +using = wordSpec+ ";" => [[ drop ignore ]] +;ON-BNF \ No newline at end of file diff --git a/extra/modules/util/util.factor b/extra/modules/util/util.factor new file mode 100644 index 0000000000..7171ecb991 --- /dev/null +++ b/extra/modules/util/util.factor @@ -0,0 +1,4 @@ +USING: generalizations kernel namespaces sequences serialize ; +IN: modules.util +: deserialize-args ( -- ) deserialize dup length firstn ; inline +: change-global ( var quot -- ) [ [ get-global ] keep ] dip dip set-global ; inline \ No newline at end of file