%% Acknowledging Receipt of Messages
% This example illustrates  a common design pattern using message 
% semantics: receiving acknowledgements from one or more charts to which
% you have sent a message.
% 

mdlName = 'sf_msg_acknowledge';
open_system(mdlName);

%% 
% *Receiving acknowledgement from a single chart*
% 
% The Stateflow(R) charts "Sender1" and "Receiver" demonstrate acknowledgement
% from a single recipient.
%

% Copyright 2006-2015 The MathWorks, Inc.
open_system([mdlName '/Sender1'], 'window');

%% 
% The "Sender1" chart sends a message to "Receiver" chart and waits for an
% acknowledgement message. If this acknowledgement is received within 10
% seconds, then "Sender" moves to "Done" state. Otherwise, "Sender1" moves
% to "TimedOut" state.

open_system([mdlName '/Receiver'], 'window');

%% 
% The "Receiver" chart receives a message from "Sender" and stays in a
% "Processing" state for 5 seconds and sends an acknowledgement message
% back to "Sender". It has an input data "fault_present" which is checked
% to decide whether to skip sending an acknowledgement.
% 

%% 
% 
% *Receiving acknowledgement from multiple charts*
% 
% The charts "Sender2", "Receiver1" and "Receiver2" demonstrate
% acknowledgement from multiple recipients.
% 

open_system([mdlName '/Sender2'], 'window');

%%
% "Sender2" is a Stateflow(R) chart that sends messages to "Receiver1"
% and "Receiver2" charts and waits for acknowledgement messages from both
% charts by moving into the "WaitForAcknowledgements" state. While in this
% state, it counts the number of acknowledgements it receives by using the
% "on MESSAGE" syntax in the state action. If both acknowledgements are
% received within 15 seconds, then "Sender2" moves to "Done" state.
% Otherwise, "Sender2" moves to "TimedOut" state.
% 

open_system([mdlName '/Receiver1'], 'window');

%%
% "Receiver1" and "Receiver2" are Stateflow(R) charts that receive a message
% from "Sender2" and stay in a "Processing" state for 5 seconds and sends an
% acknowledgement message back to "Sender2". It has an input data
% "fault_present" which is checked to decide whether to skip sending an
% acknowledgement.

% This can be used to simulate failures in a  system of charts
% communicating using messages.
% 

close_system(mdlName);