From b3c30b9f59b25ed14abec0b1569b62787cc448ba Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Sat, 10 Nov 2012 19:19:27 +0100 Subject: Add pd_client that works with SWI-Prolog Since SWI-Prolog provides a different socket interface. --- pd_client_swipl.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 pd_client_swipl.pl diff --git a/pd_client_swipl.pl b/pd_client_swipl.pl new file mode 100644 index 0000000..1739be7 --- /dev/null +++ b/pd_client_swipl.pl @@ -0,0 +1,28 @@ +start(Module):-start(Module, localhost). +start(Module, Host):-start(Module, Host, 8068). + +start(Module, Host, Port):- + tcp_socket(Socket), + tcp_connect(Socket, Host:Port, StreamIn, StreamOut), + launch(Module, StreamIn, StreamOut), + tcp_close_socket(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), + format('Own choose:\t~w\n', [ModuleDecision]), + loop(StreamIn, StreamOut, Module, NewModuleState, Hist). + +loop(StreamIn, StreamOut, Module, ModuleState, Hist) :- + get_code(StreamIn, ChoiceCode), byte_to_atom(ChoiceCode, Choice), + format('Opponent chose:\t~w\n', [Choice]), + do(StreamIn, StreamOut, Module, ModuleState, [Choice|Hist]). + +% -1 = EOF +byte_to_atom(-1, e):-!. +byte_to_atom(Byte, Atom):-atom_codes(Atom, [Byte]). -- cgit