
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | import std/dist/remote.{type Remote} |
| 2 | import std/io |
| 3 | import std/list |
| 4 | import std/process.{type Subject, type proc} |
| 5 | import std/string |
| 6 | import std/wire.{type Arg, UnknownEntry, WrongType} |
| 7 | |
| 8 | // `@remote` functions (ADR 0072): a reference is a key, the hash of the |
| 9 | // function's qualified name. The dispatcher gives an entry's actor body |
| 10 | // only for its key and a value of its argument's type, as a node will |
| 11 | // for a spawn from another one. |
| 12 | pub type Job { Job(id: Int, words: List<String>, reply: Subject<String>) } |
| 13 | |
| 14 | @remote |
| 15 | fn worker :: job: Job -> () / <proc> |
| 16 | { |
| 17 | process.send job.reply (show job.id <> ": " <> string.join job.words " ") |
| 18 | } |
| 19 | |
| 20 | // No parameter: its argument is `()`. |
| 21 | @remote |
| 22 | fn hello :: -> () / <io> |
| 23 | { |
| 24 | io.println "hello from an entry" |
| 25 | } |
| 26 | |
| 27 | @remote |
| 28 | fn total :: numbers: List<Int> -> () / <io> |
| 29 | { |
| 30 | io.println (show (list.sum numbers)) |
| 31 | } |
| 32 | |
| 33 | // The descriptor of a type built from a given dictionary is the entry's. |
| 34 | fn twice :: x: a -> Arg where Wire<a> |
| 35 | { |
| 36 | wire.arg [x, x] |
| 37 | } |
| 38 | |
| 39 | fn key_of :: entry: Remote<a> -> Int |
| 40 | { |
| 41 | remote.key entry |
| 42 | } |
| 43 | |
| 44 | fn run :: key: Int arg: Arg -> String / <proc> |
| 45 | { |
| 46 | match wire.dispatch key arg |
| 47 | { Ok(body) -> |
| 48 | { |
| 49 | // Monitored, to wait for it. |
| 50 | let (_, down) = process.spawn_monitor body |
| 51 | let _ = process.receive down (-1) |
| 52 | "ran" |
| 53 | } |
| 54 | , Error(UnknownEntry) -> "refused: unknown entry" |
| 55 | , Error(WrongType) -> "refused: wrong type" |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub fn main :: -> () / <io, proc> |
| 60 | { |
| 61 | io.println (show (key_of worker)) |
| 62 | io.println (show (remote.key hello)) |
| 63 | list.each |
| 64 | (wire.entries ()) |
| 65 | fn e { io.println (e.name <> " " <> e.descriptor <> " " <> e.hash) } |
| 66 | let replies = process.subject () |
| 67 | // Called, it is a function. |
| 68 | worker Job(0, ["called"], replies) |
| 69 | io.println (show (process.receive replies 0)) |
| 70 | let job = Job(1, ["across", "hosts"], replies) |
| 71 | io.println (run (key_of worker) (wire.arg job)) |
| 72 | io.println (show (process.receive replies 0)) |
| 73 | io.println (run (key_of hello) (wire.arg ())) |
| 74 | io.println (run (key_of total) (twice 21)) |
| 75 | io.println (run (key_of worker) (wire.arg ())) |
| 76 | io.println (run (key_of worker) (wire.arg [job.id])) |
| 77 | io.println (run 12345 (wire.arg job)) |
| 78 | io.println (show (process.receive replies 0)) |
| 79 | } |
| 80 | //> 2424948851695069083 |
| 81 | //> 4293403725595976477 |
| 82 | //> remote_dispatch.total Nstd/prelude.List;1i 75e3aca9dd867f77 |
| 83 | //> remote_dispatch.worker Nremote_dispatch.Job;0 20e527425fa9ee67 |
| 84 | //> remote_dispatch.hello u 4433df13a5780ab1 |
| 85 | //> Some(0: called) |
| 86 | //> ran |
| 87 | //> Some(1: across hosts) |
| 88 | //> hello from an entry |
| 89 | //> ran |
| 90 | //> 42 |
| 91 | //> ran |
| 92 | //> refused: wrong type |
| 93 | //> refused: wrong type |
| 94 | //> refused: unknown entry |
| 95 | //> None |