/******************************************************************************
 * Copyright (c) ETSI 2017.
 *
 * This file is subject to copyrights owned by ETSI. Non-exclusive permission
 * is hereby granted, free of charge, to copy, reproduce and amend this file
 * under the following conditions: It is provided "as is", without warranty of any
 * kind, expressed or implied.
 * 
 * ETSI shall never be liable for any claim, damages, or other liability arising
 * from its use or inability of use.This permission does not apply to any documentation
 * associated with this file for which ETSI keeps all rights reserved. The present
 * copyright notice shall be included in all copies of whole or part of this
 * file and shall not imply any sub-license right.
 *
 * Modified by: Adrien Kirjak
 *
 ** @version  0.0.1
 ** @purpose  1:22.3.3, Ensure that the IUT correctly handles reply to multiple clients on the same server 
 ** @verdict  pass accept, ttcn3verdict:pass
 *****************************************************************/

module Sem_220303_ReplyOperation_002 {

    type charstring address;
    const address c_client1Addr := "client1Addr";
    const address c_client2Addr := "client2Addr";

    /**
    * @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_220303_ReplyOperation_002(in integer p_par1, out integer p_par2, inout integer p_par3) return integer;


    template p_Sem_220303_ReplyOperation_002 s_returnTemplate := {
        p_par1 := 0, // p_par1 := - causes core dump
        p_par2 := 4,
        p_par3 := 5
    }

    template p_Sem_220303_ReplyOperation_002 s_wrongTemplate := {
        p_par1 := 0, // p_par1 := - causes core dump
        p_par2 := 2,
        p_par3 := 3
    }

    template p_Sem_220303_ReplyOperation_002 s_callTemplate := {
        p_par1 := 1,
        p_par2 := 0, // p_par2 := - causes core dump
        p_par3 := 3
    }

    type port remotePort procedure {
        inout p_Sem_220303_ReplyOperation_002;
    } with {extension "internal"}

    type component GeneralComp {
        port remotePort PCO;
        var address v_myAddress;
    }

    function f_ClientQuery(address p_myAddress) runs on GeneralComp {
        v_myAddress := p_myAddress;
	
        PCO.call(p_Sem_220303_ReplyOperation_002:s_callTemplate, 5.0) {
	
            [] PCO.getreply(p_Sem_220303_ReplyOperation_002:s_returnTemplate value 1) {
                setverdict(pass);
            }
            [] PCO.catch (timeout) {
                setverdict(fail, v_myAddress&" did not receive a response");
            }
        }
    }

    function f_Client2Query(address p_myAddress) runs on GeneralComp {
        timer t_timeout:=5.0;
        v_myAddress := p_myAddress;
        t_timeout.start;
	
        alt {
	
            [] PCO.getreply(p_Sem_220303_ReplyOperation_002:s_returnTemplate value 1) {
                setverdict(pass);
            }
            [] t_timeout.timeout {
                setverdict(fail, v_myAddress&" did not receive a response");
            }
        }
    }

    function f_ServerResponses() runs on GeneralComp {
        var address v_clientAddress;
        var integer v_par1;
        var integer v_par3;
        timer t_timeout:=30.0;
		
        template p_Sem_220303_ReplyOperation_002 s_acceptTemplate := {
            p_par1 := ?,
            p_par2 := ?,
            p_par3 := ?
        };

		
        t_timeout.start;
	
        alt {
            [] PCO.getcall(p_Sem_220303_ReplyOperation_002:s_acceptTemplate) -> param(v_par1, v_par3) sender v_clientAddress {
                PCO.reply(p_Sem_220303_ReplyOperation_002:{p_par1 := 0, p_par2 := v_par1+v_par3, p_par3 := v_par1+v_par3+1} value v_par1) to (c_client1Addr,c_client2Addr);  	//sent to both connected clients
				// p_par1 := - causes core dump
                repeat;
            }
            [] t_timeout.timeout {
                setverdict(fail);
            }
        }
	 
    }

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

        server.start(f_ServerResponses());
       
        client2.start(f_Client2Query(c_client2Addr));		//query only waiting for reply
        client.start(f_ClientQuery(c_client1Addr));

        interleave {
            [] client.done {}
            [] client2.done {}
        }
        server.stop;

        alt {
            [] all component.done {}
        }

        disconnect(server:PCO, client:PCO);
    }

    control{
        execute(TC_Sem_220303_ReplyOperation_002());
    }

}
