123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149(************************************************************************)(* * The Rocq Prover / The Rocq Development Team *)(* v * Copyright INRIA, CNRS and contributors *)(* <O___,, * (see version control and CREDITS file for authors & dates) *)(* \VV/ **************************************************************)(* // * This file is distributed under the terms of the *)(* * GNU Lesser General Public License Version 2.1 *)(* * (see LICENSE file for the text of the license) *)(************************************************************************)letdebug=falsetypepriority=Low|High(* Default priority *)letdefault_async_proofs_worker_priority=Lowletstring_of_priority=functionLow->"low"|High->"high"letpriority_of_string=function|"low"->Low|"high"->High|_->raise(Invalid_argument"priority_of_string")typerequest=|Helloofpriority|Getofint|TryGetofint|GiveBackofint|Pingtyperesponse=|Tokensofint|Noluck|Pongofint*int*intexceptionParseError(* make it work with telnet: strip trailing \r *)letstrip_rs=letlen=String.lengthsinifs.[len-1]<>'\r'thenselseString.subs0(len-1)letpositive_int_of_stringn=tryletn=int_of_stringninifn<=0thenraiseParseErrorelsenwithInvalid_argument_|Failure_->raiseParseErrorletparse_requests=ifdebugthenPrintf.eprintf"parsing '%s'\n"s;matchStr.split(Str.regexp" ")(strip_rs)with|["HELLO";"LOW"]->HelloLow|["HELLO";"HIGH"]->HelloHigh|["GET";n]->Get(positive_int_of_stringn)|["TRYGET";n]->TryGet(positive_int_of_stringn)|["GIVEBACK";n]->GiveBack(positive_int_of_stringn)|["PING"]->Ping|_->raiseParseErrorletparse_responses=ifdebugthenPrintf.eprintf"parsing '%s'\n"s;matchStr.split(Str.regexp" ")(strip_rs)with|["TOKENS";n]->Tokens(positive_int_of_stringn)|["NOLUCK"]->Noluck|["PONG";n;m;p]->letn=tryint_of_stringnwithFailure_->raiseParseErrorinletm=tryint_of_stringmwithFailure_->raiseParseErrorinletp=tryint_of_stringpwithFailure_->raiseParseErrorinPong(n,m,p)|_->raiseParseErrorletprint_request=function|HelloLow->"HELLO LOW\n"|HelloHigh->"HELLO HIGH\n"|Getn->Printf.sprintf"GET %d\n"n|TryGetn->Printf.sprintf"TRYGET %d\n"n|GiveBackn->Printf.sprintf"GIVEBACK %d\n"n|Ping->"PING\n"letprint_response=function|Tokensn->Printf.sprintf"TOKENS %d\n"n|Noluck->"NOLUCK\n"|Pong(n,m,p)->Printf.sprintf"PONG %d %d %d\n"nmpletconnects=trymatchStr.split(Str.regexp":")swith|[h;p]->letopenUnixinlets=socketPF_INETSOCK_STREAM0inconnects(ADDR_INET(inet_addr_of_stringh,int_of_stringp));Somes|_->NonewithUnix.Unix_error_->Noneletmanager=refNoneletoption_mapf=functionNone->None|Somex->Some(fx)letinitp=tryletsock=trySys.getenv"ROCQWORKMGR_SOCK"withNot_found->Sys.getenv"COQWORKMGR_SOCK"inmanager:=option_map(funs->letcout=Unix.out_channel_of_descrsinset_binary_mode_outcouttrue;letcin=Unix.in_channel_of_descrsinset_binary_mode_incintrue;output_stringcout(print_request(Hellop));flushcout;cin,cout)(connectsock)withNot_found|End_of_file->()letwith_managerfg=trymatch!managerwith|None->f()|Some(cin,cout)->gcincoutwith|ParseError|End_of_file->manager:=None;f()letgetn=with_manager(fun()->n)(funcincout->output_stringcout(print_request(Getn));flushcout;letl=input_linecininmatchparse_responselwith|Tokensm->m|_->raise(Failure"coqworkmgr protocol error"))lettrygetn=with_manager(fun()->Somen)(funcincout->output_stringcout(print_request(TryGetn));flushcout;letl=input_linecininmatchparse_responselwith|Tokensm->Somem|Noluck->None|_->raise(Failure"coqworkmgr protocol error"))letgivebackn=with_manager(fun()->())(funcincout->output_stringcout(print_request(GiveBackn));flushcout)