1:- module(jsonrpc_connectors_stdio,[
2 create_stdio_server/2
3 ]). 4
5:- use_module('../server'). 6
10
11jsonrpc_connectors:connect_to_server(stdio(Program, Args), stdio_connection(ChildPID, StreamPair)) :-
12 process_create(
13 Program,
14 Args,
15 [
16 process(ChildPID),
17 stdin(pipe(ConnectionOut)),
18 stdout(pipe(ConnectionIn)),
19 stderr(std)
20 ]
21 ),
22 stream_pair(StreamPair, ConnectionIn, ConnectionOut).
23
24jsonrpc_connectors:connection_stream_pair(stdio_connection(_, StreamPair), StreamPair).
25
26jsonrpc_connectors:close_connection(stdio_connection(ChildPID, StreamPair)) :-
27 ignore(close(StreamPair)),
28 process_kill(ChildPID, quit),
29 process_wait(ChildPID, Status, [timeout(0)]),
30 Status = timeout
31 -> true
32 ; (
33 sleep(5),
34 process_wait(ChildPID, Status, [timeout(0)]),
35 ( Status = timeout -> process_kill(ChildPID, term) ; true )
36 ).
37
41
42create_stdio_server(ServerName, stdio_server(ServerName, StreamPair)) :-
43 current_input(In),
44 current_output(Out),
45 stream_pair(StreamPair, In, Out).
46
47jsonrpc_connectors:serve_messages(stdio_server(ServerName, StreamPair)) :-
48 handle_connection(ServerName, stdio, StreamPair)