let () = let cli_args = Base.Sys.get_argv () in let port = int_of_string cli_args.(1) in let discovery_port = if Array.length cli_args = 3 then int_of_string_opt cli_args.(2) else None in let config = default_p2p_config port discovery_port in Lwt_main.run ( Internal_event_unix.init () >>= fun () -> Node_Event.(emit p2p_event) "bootstrapping" >>= fun () -> P2p.create ~config ~limits:default_p2p_limits peer_metadata_cfg (connection_metadata_cfg (init_connection_metadata (Some config) true)) msg_config >>= function | Ok p2p -> Lwt.return (P2p.activate p2p) >>= fun () -> let peer_id = P2p.peer_id p2p in let process_msg = function | conn, msg -> ( match msg with | Ping -> Node_Event.(emit received_ping) () >>= fun () -> P2p.send p2p conn (Pong peer_id) >>= fun _ -> Lwt.return_unit | Pong id -> Node_Event.(emit received_pong) id) in Node_Event.(emit show_peer_id) peer_id >>= fun () -> P2p.on_new_connection p2p (fun _ conn -> dont_wait (* not blocking *) (fun _ -> ()) (fun _ -> ()) (fun () -> P2p.send p2p conn Ping >>= function | Ok () -> ( P2p.recv p2p conn >>= function | Ok msg -> process_msg (conn, msg) >>= Lwt_result.return | Error _ -> Lwt_result.return ()) | Error _ -> Lwt_result.return ())); Format.eprintf "Waiting for recv\n@."; P2p.recv_any p2p >>= process_msg >>= fun () -> P2p.shutdown p2p >>= fun _ -> Format.eprintf "bye bye@."; Node_Event.(emit bye) () >>= fun () -> Internal_event_unix.close () | Error _ -> Format.printf "error@."; Node_Event.(emit error) () >>= fun () -> Internal_event_unix.close () )