classdef ServerFunctionInfo < rtw.pil.SimulinkCSFunctionInfo
    %SERVERFUNCTIONINFO: representation for server function (in
    % Client/Server semantics)
    
    %   Copyright 2014-2019 The MathWorks, Inc.
    
    properties(GetAccess = public, SetAccess = private)
        IsInvokedInModelTaskOnly;
    end
        
    methods (Access = {?rtw.pil.CSFunctionInfoFactory})
        % Only allow construction through the factory class
        function this = ServerFunctionInfo(codeInfoData, operationName, rtmTypeIdentifier, slFunctionInfo, tid, isAUTOSAR, isLegacyAutosarOperation)
            
            % validate inputs
            assert(rtw.connectivity.CodeInfoUtils.isa(codeInfoData, 'FunctionInterface'), ...
                'codeInfoData is not the expected type');
            % need to check coder.descriptor.FunctionInterface
            % call superclass constructor
            this@rtw.pil.SimulinkCSFunctionInfo(codeInfoData, operationName, rtmTypeIdentifier, slFunctionInfo, tid, isAUTOSAR, isLegacyAutosarOperation);
            
            % Initialize TargetFunctionName
            this.TargetFunctionName = this.getCodeInfoData.Prototype.Name;
            
            % Simulink Functions that are periodic don't count as
            % separate periodic tasks. They can't be invoked
            % outside of the model task i.e. they are simply part
            % of the implementation. Also skip any Simulink Functions with
            % no timing information.
            this.IsInvokedInModelTaskOnly = ...
                isempty(this.getCodeInfoData.Timing) || ...
                strcmp(this.getCodeInfoData.Timing.TimingMode, 'PERIODIC');
        end
    end
    
    methods
        function hasChild = hasChildCaller(this)
            % return true if the server function has a child caller
            hasChild = numel(this.ChildCallers) > 0;
        end
        
        function uniqueCallers = getChildCallersHierarchy(this, allServers)           
            % return an array containing all the child callers below this
            % Simulink function
            otherServers = setdiff(allServers, this);
            calledFcns = rtw.pil.ServerFunctionInfo.getChildCallersFromServer(this, otherServers);
            % remove duplicates
            [~, uniqueIdx] = unique(calledFcns, 'stable');
            uniqueCallers = calledFcns(uniqueIdx);
        end
        
        function [sid, argTypePrefix, argNamePrefix, argConstructor] = getArgDataInfo(this) %#ok<MANU>
            sid = '';
            argTypePrefix = 'server';
            argNamePrefix = 'xsd_svr';
            argConstructor = @rtw.pil.ServerFunctionArgData;
        end
    end
    
    methods (Static, Access = 'private')
        function calledFcns = getChildCallersFromServer(parent, allServers)
            % return an array containing all the child callers below a
            % parent Simulink function 
            
            % get the child callers in the parent
            calledFcns = parent.ChildCallers;
            
            % recursively search for child callers inside each parent's
            % child caller
            for idx = 1:length(calledFcns)
                currChild = calledFcns{idx};
                currParent = [];
                for fcnIdx = 1:length(allServers)
                    currFcn = allServers(fcnIdx);
                    if strcmp(currChild, currFcn.getSlFunctionName)
                        currParent = currFcn;
                        break;
                    end
                end
                if ~isempty(currParent)
                    otherServers = setdiff(allServers, currParent);
                    calledFcns = [calledFcns ,...
                        rtw.pil.ServerFunctionInfo.getChildCallersFromServer(currParent, otherServers)]; %#ok<AGROW>
                end
            end
        end
    end
end

