open Swim.Types external env_cast : 'a -> 'b = "%identity" let test_key = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" let () = let use_encryption = Array.length Sys.argv > 1 && Sys.argv.(1) = "--encrypt" in Eio_main.run @@ fun env -> let env = env_cast env in Eio.Switch.run @@ fun sw -> let config = { default_config with bind_addr = "\127\000\000\001"; bind_port = 7947; node_name = Some "ocaml-node"; protocol_interval = 1.0; probe_timeout = 0.5; secret_key = test_key; cluster_name = ""; encryption_enabled = use_encryption; } in let env_wrap = { stdenv = env; sw } in match Swim.Cluster.create ~sw ~env:env_wrap ~config with | Error `Invalid_key -> Printf.eprintf "Error: Invalid encryption key\n"; exit 1 | Ok cluster -> Printf.printf "OCaml SWIM node started on 127.0.0.1:%d (encryption=%b)\n%!" config.bind_port config.encryption_enabled; Swim.Cluster.start cluster; let go_node = make_node_info ~id:(node_id_of_string "go-node") ~addr:(`Udp (Eio.Net.Ipaddr.of_raw "\127\000\000\001", 7946)) ~meta:"" in Printf.printf "Adding Go node to membership...\n%!"; Swim.Cluster.add_member cluster go_node; Printf.printf "Running for 30 seconds...\n%!"; for i = 1 to 30 do Eio.Time.sleep env#clock 1.0; let stats = Swim.Cluster.stats cluster in Printf.printf "[%2d] alive=%d suspect=%d dead=%d sent=%d recv=%d dropped=%d\n%!" i stats.nodes_alive stats.nodes_suspect stats.nodes_dead stats.msgs_sent stats.msgs_received stats.msgs_dropped done; Printf.printf "Shutting down...\n%!"; Swim.Cluster.shutdown cluster