summaryrefslogtreecommitdiff
path: root/pd_client.pl
blob: 7b32e3af2b7c4f947ce9bdbd3a787b124e15e490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
start(Module):-start(Module, localhost).
start(Module, Host):-start(Module, Host, 8068).

start(Module, Host, Port):-
	socket('AF_INET', Socket),
	hostname_address(Host, HostAddress),
	socket_connect(Socket, 'AF_INET'(HostAddress, Port),
	               StreamIn, StreamOut),
	launch(Module, StreamIn, StreamOut),
	socket_close(Socket).

launch(Module, StreamIn, StreamOut) :-
	atom_concat('rules/', Module, Path), consult(Path),
	do(StreamIn, StreamOut, Module, [], []).

do(_,  _, _, _, [e|_]) :- !.
do(StreamIn, StreamOut, Module, ModuleState, Hist) :-
	call(Module, Hist, ModuleDecision, ModuleState, NewModuleState),
	write(StreamOut, ModuleDecision), flush_output(StreamOut),
	loop(StreamIn, StreamOut, Module, NewModuleState, Hist).

loop(StreamIn, StreamOut, Module, ModuleState, Hist) :-
	get_code(StreamIn, ChoiceCode), byte_to_atom(ChoiceCode, Choice),
	write(Choice),
	do(StreamIn, StreamOut, Module, ModuleState, [Choice|Hist]).

% -1 = EOF
byte_to_atom(-1, e):-!.
byte_to_atom(Byte, Atom):-name(Atom, [Byte]).