% Since version 2.0 RC2
% Demonstrate rcvMsg in branches after another rcvMsg.
% rcvMsg in the any branch after the first did not have any effect. 

:- eval(consult("utils.prova")).

:- eval(process_join_2()).

process_join_2() :-
	println(["==========process_join_2=========="]),
	iam(Me),
	% A more or less useless message to signal the start of the conversation. It may do something useful in a real application. 
%	sendMsg(XID,self,Me,start,[]),
	% The result of the above is that we now have the conversation-id XID initialised.

	sendMsg(XID,self,Me,reply,a()),
	rcvMsg(XID,self,Me,reply,a()),

	sendMsg(XID,self,Me,reply,a()),
	rcvMsg(XID,self,Me,reply,a()),

	% This will create two parallel processing streams.
	% Note that Prova does not run them in separate threads but they are independent and can communicate
	%    with other agents asynchronously effectively allowing for parallel processing.
	fork_b_e(Me,XID).

fork_b_e(Me,XID) :-
	sendMsg(XID,self,Me,reply,b()),
	rcvMsg(XID,self,Me,reply,b()),
	println([1]).
fork_b_e(Me,XID) :-
	sendMsg(XID,self,Me,reply,c()),
	rcvMsg(XID,self,Me,reply,c()),
	println([2]).

% A testing harness for printing all incoming 'reply' messages.
rcvMsg(XID,Protocol,From,reply,[X|Xs]|Extra) :-
	println(["reply for conversation-id ",XID,": ",[X|Xs]]).

% Prints:
% ==========process_join_2==========
% reply for conversation-id prova@mymachine3: ["a"]
% reply for conversation-id prova@mymachine3: ["a"]
% 1
% reply for conversation-id prova@mymachine3: ["b"]
% 2
% reply for conversation-id prova@mymachine3: ["c"]