import std/dist/remote.{type Remote} import std/io import std/list import std/process.{type Subject, type proc} import std/string import std/wire.{type Arg, UnknownEntry, WrongType} // `@remote` functions (ADR 0072): a reference is a key, the hash of the // function's qualified name. The dispatcher gives an entry's actor body // only for its key and a value of its argument's type, as a node will // for a spawn from another one. pub type Job { Job(id: Int, words: List, reply: Subject) } @remote fn worker :: job: Job -> () / { process.send job.reply (show job.id <> ": " <> string.join job.words " ") } // No parameter: its argument is `()`. @remote fn hello :: -> () / { io.println "hello from an entry" } @remote fn total :: numbers: List -> () / { io.println (show (list.sum numbers)) } // The descriptor of a type built from a given dictionary is the entry's. fn twice :: x: a -> Arg where Wire { wire.arg [x, x] } fn key_of :: entry: Remote -> Int { remote.key entry } fn run :: key: Int arg: Arg -> String / { match wire.dispatch key arg { Ok(body) -> { // Monitored, to wait for it. let (_, down) = process.spawn_monitor body let _ = process.receive down (-1) "ran" } , Error(UnknownEntry) -> "refused: unknown entry" , Error(WrongType) -> "refused: wrong type" } } pub fn main :: -> () / { io.println (show (key_of worker)) io.println (show (remote.key hello)) list.each (wire.entries ()) fn e { io.println (e.name <> " " <> e.descriptor <> " " <> e.hash) } let replies = process.subject () // Called, it is a function. worker Job(0, ["called"], replies) io.println (show (process.receive replies 0)) let job = Job(1, ["across", "hosts"], replies) io.println (run (key_of worker) (wire.arg job)) io.println (show (process.receive replies 0)) io.println (run (key_of hello) (wire.arg ())) io.println (run (key_of total) (twice 21)) io.println (run (key_of worker) (wire.arg ())) io.println (run (key_of worker) (wire.arg [job.id])) io.println (run 12345 (wire.arg job)) io.println (show (process.receive replies 0)) } //> 2424948851695069083 //> 4293403725595976477 //> remote_dispatch.total Nstd/prelude.List;1i 75e3aca9dd867f77 //> remote_dispatch.worker Nremote_dispatch.Job;0 20e527425fa9ee67 //> remote_dispatch.hello u 4433df13a5780ab1 //> Some(0: called) //> ran //> Some(1: across hosts) //> hello from an entry //> ran //> 42 //> ran //> refused: wrong type //> refused: wrong type //> refused: unknown entry //> None