123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109(* This file is free software. See file "license" for more details. *)(** {1 Main for Client} *)openLwt.InfixmoduleC=ClientmoduleProto_j=Protocol_j(*******************************************************************************)(* command line *)letconnection_file_name=ref""letcompletion=reffalseletobject_info=reffalseletinit_file=ref""letci_stdin=ref50000letci_shell=ref50001letci_iopub=ref50002letci_heartbeat=ref50003letci_control=ref50004letci_transport=ref"tcp"letci_ip_addr=ref""letmk_args~additional_args()=Arg.(align(additional_args@["--log",String(Log.open_log_file)," <file> open log file";"--connection-file",Set_string(connection_file_name)," <filename> connection file name";"--init",Set_string(init_file)," <file> load <file> instead of default init file";"--completion",Set(completion)," enable tab completion";"--object-info",Set(object_info)," enable introspection";(* pass connection info through command line *)"--ci-stdin",Set_int(ci_stdin)," (connection info) stdin zmq port";"--ci-iopub",Set_int(ci_iopub)," (connection info) iopub zmq port";"--ci-shell",Set_int(ci_shell)," (connection info) shell zmq port";"--ci-control",Set_int(ci_control)," (connection info) control zmq port";"--ci-heartbeat",Set_int(ci_heartbeat)," (connection info) heartbeat zmq port";"--ci-transport",Set_string(ci_transport)," (connection info) transport";"--ci-ip",Set_string(ci_ip_addr)," (connection info) ip address"]))letmk_connection_info():Proto_j.connection_info=if!ci_ip_addr<>""then(* get configuration parameters from command line *){Proto_j.stdin_port=!ci_stdin;ip=!ci_ip_addr;control_port=!ci_control;hb_port=!ci_heartbeat;signature_scheme="hmac-sha256";key="";shell_port=!ci_shell;transport=!ci_transport;iopub_port=!ci_iopub;}else((* read from configuration files *)letf_conn_info=tryopen_in!connection_file_namewith_->failwith("Failed to open connection file: '"^!connection_file_name^"'")inletstate=Yojson.init_lexer()inletlex=Lexing.from_channelf_conn_infoinletconn=Proto_j.read_connection_infostatelexinlet()=close_inf_conn_infoinconn)letmain_loopconnection_infokernel=Lwt.catch(fun()->letsockets=Sockets.open_socketsconnection_infoinletkey=connection_info.Proto_j.keyinletkey=ifkey=""thenNoneelseSomekeyinletsh=C.make?keysocketskernelinC.runsh;>>=function|C.Run_stop->Log.info(funk->k"Done.");Sockets.close_socketssockets|C.Run_restart->Log.info(funk->k"Done (restart).");Sockets.close_socketssockets)(fune->Log.err(funk->k"Exception: %s"(Printexc.to_stringe));Log.err(funk->k"Dying.");Lwt.failExit)typeconfig={connection_info:Proto_j.connection_info}letmk_config?(additional_args=[])~usage():config=letargs=mk_args~additional_args()inArg.parseargs(funs->failwith("invalid anonymous argument: "^s))usage;{connection_info=mk_connection_info()}letmain~(config:config)~kernel=Lwt_io.printf"Starting kernel for `%s`\n"kernel.C.Kernel.language>>=fun()->Log.info(funk->k"start main...");main_loopconfig.connection_infokernel>|=fun()->Log.info(funk->k"client_main: exiting")