/*****************************************************************
 ** @author   STF 433 (updated by STF 521)
 ** @version  0.0.2
 ** @purpose  1:22.3.1, blocking broadcast call with no response and exception handling part
 ** @verdict  pass accept, ttcn3verdict:pass
 *****************************************************************/

// The following requirements are tested:
// Like for the send operation, TTCN-3 also supports unicast, multicast and broadcast calls
// of procedures. This can be done in the same manner as described in clause 22.2.1, i.e.
// the argument of the to clause of a call operation is for unicast calls the address of
// one receiving entity (or can be omitted in case of one-to-one connections), for multicast
// calls a list of addresses of a set of receivers and for broadcast calls the all component
// keyword.

module Sem_220301_CallOperation_005 {

    /**
    * @desc testing of inline return template for remote procedure call
    * @param p_par1 only input parameter
    * @param p_par2 must have value 4 at return
    * @param p_par3 must have value 5 at return
    * @return must return value 1
    */
    signature p_Sem_220301_CallOperation_005(in integer p_par1, out integer p_par2, inout integer p_par3) return integer;

    template p_Sem_220301_CallOperation_005 s_returnTemplate := {
        p_par1 := -,
        p_par2 := 4,
        p_par3 := 5
    }

    template p_Sem_220301_CallOperation_005 s_wrongTemplate := {
        p_par1 := -,
        p_par2 := 2,
        p_par3 := 3
    }

    template p_Sem_220301_CallOperation_005 s_callTemplate := {
        p_par1 := 1,
        p_par2 := -,
        p_par3 := 3
    }

    type port remotePort procedure {
        inout p_Sem_220301_CallOperation_005;
    }

    type component GeneralComp {
        port remotePort PCO;
    }

    function f_ClientQuery() runs on GeneralComp {
        var integer v_counter := 0;

        //  validate broadcast calling
        PCO.call(p_Sem_220301_CallOperation_005:s_callTemplate, nowait) to all component;

        alt {
            // check that we get a reply from both servers
            [] PCO.getreply(p_Sem_220301_CallOperation_005:s_returnTemplate value 1) {
                v_counter := v_counter + 1;
                if (v_counter == 2) {
                    setverdict(pass, "Server1 reply received");
                } else {
                    repeat;
                }
            }
        }
    }

    template p_Sem_220301_CallOperation_005 s_acceptTemplate := {
        p_par1 := ?,
        p_par2 := ?,
        p_par3 := ?
    };

    function f_ServerResponses() runs on GeneralComp {
        var integer v_par1;
        var integer v_par3;

        alt {
            [] PCO.getcall(p_Sem_220301_CallOperation_005:s_acceptTemplate) -> param(v_par1, - , v_par3) {
                PCO.reply(p_Sem_220301_CallOperation_005:{p_par1 := -, p_par2 := v_par1+v_par3, p_par3 := v_par1+v_par3+1} value v_par1);
                repeat;
            }
        }
    }

    testcase TC_Sem_220301_CallOperation_005() runs on GeneralComp system GeneralComp {
        var GeneralComp server := GeneralComp.create("RemoteProcedure Service") alive;
        var GeneralComp server2 := GeneralComp.create("RemoteProcedure Service2") alive;
        var GeneralComp client := GeneralComp.create("RemoteProcedure Client") alive;
        // map the PTCs to the system port
        connect(server:PCO, client:PCO);
        connect(server2:PCO, client:PCO);

        // set server address and start operation
        server.start(f_ServerResponses());
        server2.start(f_ServerResponses());

        client.start(f_ClientQuery());

        alt {
            [] client.done {
                server.stop;
                server2.stop;
            }
        }

        alt {
            [] all component.done {}
        }

        disconnect(server:PCO);
        disconnect(server2:PCO);
    }

    control{
        execute(TC_Sem_220301_CallOperation_005(), 5.0);
    }

}