-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.ml
53 lines (45 loc) · 1.73 KB
/
main.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
open Eio.Std
open Capnp_rpc.Std
module Restorer = Capnp_rpc_net.Restorer
let () =
Logs.set_level (Some Logs.Warning);
Logs.set_reporter (Logs_fmt.reporter ())
let secret_key = `Ephemeral
let listen_address = `TCP ("127.0.0.1", 7000)
let or_fail = function
| Ok x -> x
| Error (`Msg m) -> failwith m
(* $MDX part-begin=main *)
let make_service ~config ~services name =
let service = Logger.local name in
let id = Capnp_rpc_unix.Vat_config.derived_id config name in
Restorer.Table.add services id service;
name, id
let start_server ~sw net =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key ~net listen_address in
let make_sturdy = Capnp_rpc_unix.Vat_config.sturdy_uri config in
let services = Restorer.Table.create ~sw make_sturdy in
let restore = Restorer.of_table services in
let services = List.map (make_service ~config ~services) ["alice"; "bob"] in
let vat = Capnp_rpc_unix.serve ~sw ~restore config in
services |> List.iter (fun (name, id) ->
let cap_file = name ^ ".cap" in
Capnp_rpc_unix.Cap_file.save_service vat id cap_file |> or_fail;
Printf.printf "[server] saved %S\n%!" cap_file
)
let run_client ~net cap_file msg =
Switch.run @@ fun sw ->
let vat = Capnp_rpc_unix.client_only_vat ~sw net in
let sr = Capnp_rpc_unix.Cap_file.load vat cap_file |> or_fail in
Printf.printf "[client] loaded %S\n%!" cap_file;
Sturdy_ref.with_cap_exn sr @@ fun cap ->
Logger.log cap msg
let () =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
Switch.run @@ fun sw ->
let net = env#net in
start_server ~sw net;
run_client ~net "./alice.cap" "Message from Alice";
run_client ~net "./bob.cap" "Message from Bob"
(* $MDX part-end *)