/***
@author  8boomgaa & 8njemz
@date	 2010/02/25 15:24
@descr	 the interface for communication between the logic running on the 
	 server and the gui running on clients
*/

:- consult('servers_little_helpers.pl').
:- consult('utils/pipe_through_command.pl').
:- consult('server.pl').
:- dynamic(response/2).

/**
@form broadcasting(+Predicate)
@constraints
@ground Predicate
@descr  sends given Predicate to all known clients
        (obviously this predicate should be named 'broadcast'
	 this name however is already taken by a prolog built-in)
*/
broadcasting(Pred) :-
	Pred =.. [N|_],
	wl(N),
	term_to_atom(Pred,Atom),
	% debug output
	concat('broadcasting: ', Atom, Txt),
	write_log(Txt),
	send_to_all(Atom).


/**
@form query(+ID, +Predicate) 
@constraints
@ground  ID
@ground  Predicate
@descr	 sends Predicate to a specific client identified by its ID
*/
query(Color, Pred):-
	Pred =.. [Name|_],
	wl(Name),!,
	color2id(Color,ID),
	term_to_atom(Pred,Atom),
	% debug output
	concat('query: ', Atom, Txt),
	write_log(Txt),
	id2socket(ID,Socket),
	set_player(ID, Socket),
	send(Socket, append_line, Atom).
	%send(Sock, append, Atom).
%	retractall(response(_,_)).

% Output from client
%wait_for_reply(ID,Output) :-
%	fork(child),
%	wait(child,exited(_)),
%	assert(pending(ID)),
%	repeat,
%	response(ID,Output).

%event(+ID, +PredName, +Output)
% Output is a List 








