
%

%   Copyright 2012 The MathWorks, Inc.

classdef ParametersTCP < coder.rtiostreamtest.Parameters
    %PARAMETERSTCP TCP	parameters for use with
    %rtiostreamtest
    %   Sets the parameters required for testing a rtiostream connection
    %   over a TCP connection
    
    properties (GetAccess = 'public', SetAccess = 'private')
        host;
        port;
    end
    
    properties (GetAccess = 'private', SetAccess = 'private')
       SkipBufferDetection;
    end
    
    methods
        
        function this = ParametersTCP(host, port, skipBufferDetection)
            % Initialize the rtiostream tcpip implementation on a specified
            % host and port
            
            narginchk(2,3);
            if nargin == 3
               this.SkipBufferDetection = skipBufferDetection;
            else 
               this.SkipBufferDetection = true;
            end
            
            assert(ischar(host), coder.rtiostreamtest.Test.message('ErrorHostNameNotChar'));
            
            % If it is a string, covert so it could be checked
            if ischar(port)
                port = str2double(port);
                assert(~isnan(port), coder.rtiostreamtest.Test.message('ErrorPortString'));
            end
            
            % Convert the number back to string after checking
            if isnumeric(port)
                assert(port >= 0 && port <= 65535 && port == round(port), ...
                    coder.rtiostreamtest.Test.message('ErrorPortInvalid'));
                port = num2str(port);
            end
            
            % Make sure it is a string by the time we reach here
            assert(ischar(port), coder.rtiostreamtest.Test.message('ErrorPortInvalid'));
            
            % Assign for further usage
            this.host = host;
            this.port = port;
        end
        
        function [library, parameters] = getSharedLibrary(this)
            
            % Get the library
            library = ['libmwrtiostreamtcpip' ...
                system_dependent('GetSharedLibExt')];
            
            % Get the parameters
            %            
            % Run in 10ms blocking mode to avoid high CPU load without
            % affecting the buffer detection algorithm
            parameters =  {'-client', '1', '-blocking', '1', '-recv_timeout_secs', '-3', '-port', ...
                this.port, '-hostname', this.host};
        end
        
    end
    
    methods (Access = protected)
        function parameters = getSymmetricParameters(~)
            parameters = {'default_chunks', [1 4 128 512 2048 4096]};
        end
        
        function parameters = getBufferDetectionParameters(this)
            if this.SkipBufferDetection
                parameters = {'skip', true};
            else
                parameters = {};
            end
        end                
    end
    
end
