classdef (Hidden) Communicator < rtw.connectivity.Component
%COMMUNICATOR communicates with target application
%
%   This is an undocumented class. Its methods and properties are likely to
%   change without warning from one release to the next.

%   Copyright 2007-2021 The MathWorks, Inc.
%   

    properties (SetAccess = 'private', GetAccess = 'private')
        Launcher;        
        RtIOStream;
    end

    methods
        % constructor
        function this = Communicator(componentArgs, ...
                                     launcher, ...
                                     rtIOStream)
            narginchk(3, 3);
            % call super class constructor
            this@rtw.connectivity.Component(componentArgs);
            % validate arg
            rtw.connectivity.Utils.validateArg(launcher, ...
                                     'rtw.connectivity.Launcher');
            % validate arg
            rtw.connectivity.Utils.validateArg(rtIOStream, ...
                                     'rtw.connectivity.RtIOStream');
            this.Launcher = launcher;
            
            this.RtIOStream = rtIOStream;            
            this.RtIOStream.addPreOpenHook(@() this.initCommunications());
            this.RtIOStream.addPostOpenHook(@() this.startCommands());            
        end
    end

    % methods can't be overridden
    methods (Sealed)
        function launcher = getLauncher(this)
            narginchk(1, 1);
            launcher = this.Launcher;
        end                        
    end   
    
    methods (Sealed, Hidden)
        function rtIOStream = getRtIOStream(this)
            rtIOStream = this.RtIOStream;
        end
        
        function setVerbose(this, verbose)
            rtw.connectivity.Utils.validateArg(verbose, ...
                'logical');
            this.getComponentArgs().setVerboseCommunicationOutput(verbose);
        end
        
        function verbose = isVerbose(this)
            verbose = this.getComponentArgs().getVerboseCommunicationOutput();
        end
    end
    
    methods              
        % legacy hook point exposed by rtw.connectivity.HostTCPIPCommunicator
        %
        % called BEFORE RtIOStream.open
        function initCommunications(this)           %#ok<MANU>
        end        
        
        % legacy hook point exposed by arduino.Communicator
        %
        % called AFTER RtIOStream.open
        function startCommands(this)             %#ok<MANU>
        end
    end
end
