Skip to content

Commit

Permalink
Move net to Vat_config.t
Browse files Browse the repository at this point in the history
It's more logical to keep the network next to the addresses on it.
Also, this will allow using Eio paths more easily in future.
  • Loading branch information
talex5 committed Nov 28, 2024
1 parent bda4ef2 commit 0b7a3ee
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 109 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ clean:

test:
rm -rf _build/_tests
dune build test test-bin @install
dune build test test-bin examples @install
dune build @runtest --no-buffer -j 1
50 changes: 26 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,10 @@ let secret_key = `Ephemeral
let listen_address = `TCP ("127.0.0.1", 7000)
let start_server ~sw ~delay net =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key listen_address in
let config = Capnp_rpc_unix.Vat_config.create ~secret_key ~net listen_address in
let service_id = Capnp_rpc_unix.Vat_config.derived_id config "main" in
let restore = Capnp_rpc_net.Restorer.single service_id (Echo.local ~delay) in
let vat = Capnp_rpc_unix.serve ~sw ~net ~restore config in
let vat = Capnp_rpc_unix.serve ~sw ~restore config in
Capnp_rpc_unix.Vat.sturdy_uri vat service_id
let () =
Expand Down Expand Up @@ -531,7 +531,7 @@ let listen_address = `TCP ("0.0.0.0", 7000) (* Listen on all interfaces *)
let public_address = `TCP ("192.168.1.3", 7000) (* Tell clients to connect here *)
let start_server () =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key ~public_address listen_address in
let config = Capnp_rpc_unix.Vat_config.create ~secret_key ~net ~public_address listen_address in
```
In `start_server`:
Expand Down Expand Up @@ -592,14 +592,11 @@ let () =
let cap_file = "echo.cap"
let serve config =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
let delay = Eio.Time.Timeout.seconds env#mono_clock delay in
let serve ~delay config =
Switch.run @@ fun sw ->
let service_id = Capnp_rpc_unix.Vat_config.derived_id config "main" in
let restore = Restorer.single service_id (Echo.local ~delay) in
let vat = Capnp_rpc_unix.serve ~sw ~net:env#net ~restore config in
let vat = Capnp_rpc_unix.serve ~sw ~restore config in
match Capnp_rpc_unix.Cap_file.save_service vat service_id cap_file with
| Error `Msg m -> failwith m
| Ok () ->
Expand All @@ -608,13 +605,18 @@ let serve config =
open Cmdliner
let serve_cmd =
let ( $$ ) f x = Term.(const f $ x)
let serve_cmd env =
let doc = "run the server" in
let info = Cmd.info "serve" ~doc in
Cmd.v info Term.(const serve $ Capnp_rpc_unix.Vat_config.cmd)
let delay = Eio.Time.Timeout.seconds env#mono_clock delay in
Cmd.v info (serve ~delay $$ Capnp_rpc_unix.Vat_config.cmd env)
let () =
exit (Cmd.eval serve_cmd)
exit @@ Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
Cmd.eval (serve_cmd env)
```
The cmdliner term `Capnp_rpc_unix.Vat_config.cmd` provides an easy way to get a suitable `Vat_config`
Expand All @@ -638,27 +640,29 @@ let run_client service =
Capability.with_ref (Echo.Callback.local callback_fn) @@ fun callback ->
Echo.heartbeat service "foo" callback
let connect uri =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
let connect net uri =
Switch.run @@ fun sw ->
let client_vat = Capnp_rpc_unix.client_only_vat ~sw env#net in
let client_vat = Capnp_rpc_unix.client_only_vat ~sw net in
let sr = Capnp_rpc_unix.Vat.import_exn client_vat uri in
Capnp_rpc_unix.with_cap_exn sr run_client
open Cmdliner
let ( $$ ) f x = Term.(const f $ x)
let connect_addr =
let i = Arg.info [] ~docv:"ADDR" ~doc:"Address of server (capnp://...)" in
Arg.(required @@ pos 0 (some Capnp_rpc_unix.sturdy_uri) None i)
let connect_cmd =
let connect_cmd env =
let doc = "run the client" in
let info = Cmd.info "connect" ~doc in
Cmd.v info Term.(const connect $ connect_addr)
Cmd.v info (connect env#net $$ connect_addr)
let () =
exit (Cmd.eval connect_cmd)
exit @@ Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
Cmd.eval (connect_cmd env)
```
To test, start the server running:
Expand Down Expand Up @@ -840,12 +844,12 @@ let make_service ~config ~services name =
name, id
let start_server ~sw net =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key listen_address in
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 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 ~net ~restore config 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;
Expand Down Expand Up @@ -1102,9 +1106,7 @@ The main `start_server` function then uses `Db` to create the table:
<!-- $MDX include,file=examples/sturdy-refs-4/main.ml,part=server -->
```ocaml
let serve config =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
let serve env config =
Switch.run @@ fun sw ->
(* Create the on-disk store *)
let make_sturdy = Capnp_rpc_unix.Vat_config.sturdy_uri config in
Expand All @@ -1127,7 +1129,7 @@ let serve config =
(* Tell the database how to restore saved loggers *)
Promise.resolve set_loader (fun sr ~label -> Restorer.grant @@ Logger.local ~persist_new sr label);
(* Run the server *)
let _vat = Capnp_rpc_unix.serve ~sw ~net:env#net ~restore config in
let _vat = Capnp_rpc_unix.serve ~sw ~restore config in
let uri = Capnp_rpc_unix.Vat_config.sturdy_uri config root_id in
Capnp_rpc_unix.Cap_file.save_uri uri "admin.cap" |> or_fail;
print_endline "Wrote admin.cap";
Expand Down
4 changes: 2 additions & 2 deletions examples/pipelining/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ let secret_key = `Ephemeral
let listen_address = `TCP ("127.0.0.1", 7000)

let start_server ~sw ~delay net =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key listen_address in
let config = Capnp_rpc_unix.Vat_config.create ~secret_key ~net listen_address in
let service_id = Capnp_rpc_unix.Vat_config.derived_id config "main" in
let service = Echo.local ~delay in
Switch.on_release sw (fun () -> Capability.dec_ref service);
let restore = Capnp_rpc_net.Restorer.single service_id service in
let vat = Capnp_rpc_unix.serve ~sw ~net ~restore config in
let vat = Capnp_rpc_unix.serve ~sw ~restore config in
Capnp_rpc_unix.Vat.sturdy_uri vat service_id

let () =
Expand Down
4 changes: 2 additions & 2 deletions examples/sturdy-refs-2/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ let or_fail = function
| Error (`Msg m) -> failwith m

let start_server ~sw net =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key listen_address in
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 make_sturdy in
let restore = Restorer.of_table services in
let root_id = Capnp_rpc_unix.Vat_config.derived_id config "root" in
let root = Logger.local "root" in
Restorer.Table.add services root_id root;
let _vat = Capnp_rpc_unix.serve ~sw ~net ~restore config in
let _vat = Capnp_rpc_unix.serve ~sw ~restore config in
Capnp_rpc_unix.Vat_config.sturdy_uri config root_id

(* $MDX part-begin=main *)
Expand Down
4 changes: 2 additions & 2 deletions examples/sturdy-refs-3/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let or_fail = function
| Error (`Msg m) -> failwith m

let start_server ~sw net =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key listen_address in
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 make_sturdy in
Switch.on_release sw (fun () -> Restorer.Table.clear services);
Expand All @@ -28,7 +28,7 @@ let start_server ~sw net =
in
(* $MDX part-end *)
Restorer.Table.add services root_id root;
let _vat = Capnp_rpc_unix.serve ~sw ~net ~restore config in
let _vat = Capnp_rpc_unix.serve ~sw ~restore config in
Capnp_rpc_unix.Vat_config.sturdy_uri config root_id

let run_client ~net cap_file =
Expand Down
39 changes: 19 additions & 20 deletions examples/sturdy-refs-4/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ let or_fail = function
| Error (`Msg m) -> failwith m

(* $MDX part-begin=server *)
let serve config =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
let serve env config =
Switch.run @@ fun sw ->
(* Create the on-disk store *)
let make_sturdy = Capnp_rpc_unix.Vat_config.sturdy_uri config in
Expand All @@ -39,41 +37,40 @@ let serve config =
(* Tell the database how to restore saved loggers *)
Promise.resolve set_loader (fun sr ~label -> Restorer.grant @@ Logger.local ~persist_new sr label);
(* Run the server *)
let _vat = Capnp_rpc_unix.serve ~sw ~net:env#net ~restore config in
let _vat = Capnp_rpc_unix.serve ~sw ~restore config in
let uri = Capnp_rpc_unix.Vat_config.sturdy_uri config root_id in
Capnp_rpc_unix.Cap_file.save_uri uri "admin.cap" |> or_fail;
print_endline "Wrote admin.cap";
Fiber.await_cancel ()
(* $MDX part-end *)

let log cap_file msg =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
let log net cap_file msg =
Switch.run @@ fun sw ->
let vat = Capnp_rpc_unix.client_only_vat ~sw env#net in
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
Sturdy_ref.with_cap_exn sr @@ fun logger ->
Logger.log logger msg

let sub cap_file label =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
let sub env cap_file label =
Switch.run @@ fun sw ->
let sub_file = label ^ ".cap" in
if Sys.file_exists sub_file then Fmt.failwith "%S already exists!" sub_file;
let vat = Capnp_rpc_unix.client_only_vat ~sw env#net in
let sr = Capnp_rpc_unix.Cap_file.load vat cap_file |> or_fail in
Sturdy_ref.with_cap_exn sr @@ fun logger ->
Sturdy_ref.with_cap_exn sr @@ fun root ->
let uri = Capability.with_ref (Logger.sub root "alice") Capnp_rpc.Persistence.save_exn in
Capnp_rpc_unix.Cap_file.save_uri uri sub_file |> or_fail;
Printf.printf "Wrote %S\n%!" sub_file;

open Cmdliner

let serve_cmd =
let ( $ ) = Term.( $ )
let ( $$ ) f x = Term.const f $ x

let serve_cmd env =
let doc = "run the server" in
let info = Cmd.info "serve" ~doc in
Cmd.v info Term.(const serve $ Capnp_rpc_unix.Vat_config.cmd)
Cmd.v info (serve env $$ Capnp_rpc_unix.Vat_config.cmd env)

let cap_file =
let i = Arg.info [] ~docv:"PATH" ~doc:"logger.cap file" in
Expand All @@ -87,19 +84,21 @@ let label =
let i = Arg.info [] ~docv:"LABEL" ~doc:"Tag for new logger" in
Arg.(required @@ pos 1 (some string) None i)

let log_cmd =
let log_cmd env =
let doc = "log a message" in
let info = Cmd.info "log" ~doc in
Cmd.v info Term.(const log $ cap_file $ msg)
Cmd.v info Term.(log env#net $$ cap_file $ msg)

let sub_cmd =
let sub_cmd env =
let doc = "create a sub-logger" in
let info = Cmd.info "sub" ~doc in
Cmd.v info Term.(const sub $ cap_file $ label)
Cmd.v info Term.(sub env $$ cap_file $ label)

let cmds = [serve_cmd; sub_cmd; log_cmd]
let cmds env = [serve_cmd env; sub_cmd env; log_cmd env]

let () =
let doc = "a command-line interface for logger service" in
let info = Cmd.info ~doc "logger-client" in
exit (Cmd.eval @@ Cmd.group info cmds)
exit @@ Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
Cmd.eval @@ Cmd.group info (cmds env)
4 changes: 2 additions & 2 deletions examples/sturdy-refs/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ let make_service ~config ~services name =
name, id

let start_server ~sw net =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key listen_address in
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 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 ~net ~restore config 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;
Expand Down
4 changes: 2 additions & 2 deletions examples/v3/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ let secret_key = `Ephemeral
let listen_address = `TCP ("127.0.0.1", 7000)

let start_server ~sw ~delay net =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key listen_address in
let config = Capnp_rpc_unix.Vat_config.create ~secret_key ~net listen_address in
let service_id = Capnp_rpc_unix.Vat_config.derived_id config "main" in
let restore = Capnp_rpc_net.Restorer.single service_id (Echo.local ~delay) in
let vat = Capnp_rpc_unix.serve ~sw ~net ~restore config in
let vat = Capnp_rpc_unix.serve ~sw ~restore config in
Capnp_rpc_unix.Vat.sturdy_uri vat service_id

let () =
Expand Down
16 changes: 9 additions & 7 deletions examples/v4/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ let run_client service =
Capability.with_ref (Echo.Callback.local callback_fn) @@ fun callback ->
Echo.heartbeat service "foo" callback

let connect uri =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
let connect net uri =
Switch.run @@ fun sw ->
let client_vat = Capnp_rpc_unix.client_only_vat ~sw env#net in
let client_vat = Capnp_rpc_unix.client_only_vat ~sw net in
let sr = Capnp_rpc_unix.Vat.import_exn client_vat uri in
Capnp_rpc_unix.with_cap_exn sr run_client

open Cmdliner

let ( $$ ) f x = Term.(const f $ x)

let connect_addr =
let i = Arg.info [] ~docv:"ADDR" ~doc:"Address of server (capnp://...)" in
Arg.(required @@ pos 0 (some Capnp_rpc_unix.sturdy_uri) None i)

let connect_cmd =
let connect_cmd env =
let doc = "run the client" in
let info = Cmd.info "connect" ~doc in
Cmd.v info Term.(const connect $ connect_addr)
Cmd.v info (connect env#net $$ connect_addr)

let () =
exit (Cmd.eval connect_cmd)
exit @@ Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
Cmd.eval (connect_cmd env)
18 changes: 10 additions & 8 deletions examples/v4/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ let () =

let cap_file = "echo.cap"

let serve config =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
let delay = Eio.Time.Timeout.seconds env#mono_clock delay in
let serve ~delay config =
Switch.run @@ fun sw ->
let service_id = Capnp_rpc_unix.Vat_config.derived_id config "main" in
let restore = Restorer.single service_id (Echo.local ~delay) in
let vat = Capnp_rpc_unix.serve ~sw ~net:env#net ~restore config in
let vat = Capnp_rpc_unix.serve ~sw ~restore config in
match Capnp_rpc_unix.Cap_file.save_service vat service_id cap_file with
| Error `Msg m -> failwith m
| Ok () ->
Expand All @@ -25,10 +22,15 @@ let serve config =

open Cmdliner

let serve_cmd =
let ( $$ ) f x = Term.(const f $ x)

let serve_cmd env =
let doc = "run the server" in
let info = Cmd.info "serve" ~doc in
Cmd.v info Term.(const serve $ Capnp_rpc_unix.Vat_config.cmd)
let delay = Eio.Time.Timeout.seconds env#mono_clock delay in
Cmd.v info (serve ~delay $$ Capnp_rpc_unix.Vat_config.cmd env)

let () =
exit (Cmd.eval serve_cmd)
exit @@ Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
Cmd.eval (serve_cmd env)
Loading

0 comments on commit 0b7a3ee

Please sign in to comment.