classdef (Hidden = true) HostTCPIPCommunicator < rtw.connectivity.RtIOStreamHostCommunicator
%HOSTTCPIPCOMMUNICATOR implements a host-based TCP/IP communicator
%   HOSTTCPIPCOMMUNICATOR uses the TCP/IP implementation of the host-side
%   rtiostream API to provide a communications channel with the target
%
%   This is an undocumented class. Its methods and properties are likely to
%   change without warning from one release to the next.

%   Copyright 2007-2020 The MathWorks, Inc.

    methods
        % constructor
        function this = HostTCPIPCommunicator(componentArgs, ...
                launcher, rtiostreamLib)
            narginchk(3, 3);

            % call super class constructor
            this@rtw.connectivity.RtIOStreamHostCommunicator(componentArgs, ...
                                          launcher, ...
                                          rtiostreamLib);
        end
        
        % Open a TCPIP connection
        function initCommunications(this)
           % dynamically update the port number by processing the stdoutput
           % of the server          
           serverPort = rtw.connectivity.HostTCPIPCommunicator.parseOutputFileForServerPort(this.getLauncher);
           
           % Set TCP/IP port number arguments for opening the rtiostream
           argPair = {'-port', serverPort};
           this.setOpenRtIOStreamArgPair(argPair);                                            
        end        
    end
    
    methods (Static)
        % parse the output file for the server port number
        function serverPort = parseOutputFileForServerPort(launcher, ...
                                                           extendTimeout)
                                                       
            if nargin < 2
                extendTimeout = false;
            end
            
            [serverPort, outputFileContents] = coder.connectivity.PILCommunication.parseOutputFileForHostServerPort(...
                                                                launcher.getExePID, launcher.getOutputFile, extendTimeout);
            
            if isempty(serverPort)
                % display the content of stdOut and stdErr
                launcher.dispStdOutAndStdErr;
                
                rtw.connectivity.ProductInfo.error('target', 'UnknownServerPort', ...
                    outputFileContents, launcher.getCommandLine());
            end
        end
    end
end
