Home > vbmeg > external > mne > mne_rt_client.m

mne_rt_client

PURPOSE ^

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 classdef mne_rt_client < handle
0002     %MNE_RT_CLIENT is a base class for a mne_rt_server real-time connection
0003     %
0004     %   This class provides methods for establishing a mne_rt_server
0005     %   connection. It stores the established connection until connection
0006     %   is closed.
0007     %
0008     %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0009     %   License : BSD 3-clause
0010     %
0011     
0012     properties (Access = public)
0013         m_DataInputStream = [];
0014         m_DataOutputStream = [];
0015     end
0016     
0017     properties (Access = private)
0018         m_TcpSocket = [];
0019         m_InputStream = [];
0020         m_OutputStream = [];
0021         m_BufferedInputStream = []; %to increase performance use a buffer in between
0022         m_iNumOfRetries = -1;
0023     end
0024     
0025     methods
0026         % =================================================================
0027         %% mne_rt_client
0028         function obj = mne_rt_client(host, port, number_of_retries)
0029             %
0030             % obj = mne_rt_client(host, port, numOfRetries)
0031             %
0032             % Constructor
0033             %
0034             % host          - ip adress of the mne_rt_Server
0035             % port          - comand port of the mne_rt_Server
0036             % numOfRetries  - number of connection retries, when a
0037             %                 connection fails
0038 
0039             %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0040             %   License : BSD 3-clause
0041             %
0042             obj.init(host, port, number_of_retries);
0043         end % mne_rt_client
0044         
0045         % =================================================================
0046         %% init
0047         function result = init(obj, host, port, numOfRetries) 
0048             %
0049             % result = init(obj, host, port, numOfRetries)
0050             %
0051             % Init the connection
0052             %
0053             % host          - ip adress of the mne_rt_Server
0054             % port          - comand port of the mne_rt_Server
0055             % numOfRetries  - number of connection retries, when a
0056             %                 connection fails
0057 
0058             %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0059             %   License : BSD 3-clause
0060             %
0061             import java.net.Socket
0062             import java.io.*
0063 
0064             if (nargin < 3)
0065                 obj.m_iNumOfRetries = 20; % set to -1 for infinite
0066             end
0067 
0068             retry = 0;
0069             obj.close();
0070             
0071             result      = false;
0072 
0073             while true
0074 
0075                 retry = retry + 1;
0076                 if ((obj.m_iNumOfRetries > 0) && (retry > obj.m_iNumOfRetries))
0077                     fprintf(1, 'Too many retries\n');
0078                     break;
0079                 end
0080 
0081                 try
0082                     fprintf(1, 'Retry %d connecting to %s:%d\n', ...
0083                             retry, host, port);
0084 
0085                     % throws if unable to connect
0086                     obj.m_TcpSocket = Socket(host, port);
0087 
0088                     obj.m_InputStream = obj.m_TcpSocket.getInputStream;
0089                     obj.m_BufferedInputStream = BufferedInputStream(obj.m_InputStream);
0090                     obj.m_DataInputStream = DataInputStream(obj.m_BufferedInputStream);
0091 
0092                     obj.m_OutputStream = obj.m_TcpSocket.getOutputStream;
0093                     obj.m_DataOutputStream = DataOutputStream(obj.m_OutputStream);
0094                     
0095                     fprintf(1, 'Connected to server\n');
0096                     
0097                     result = true;
0098                     
0099                     break;
0100 
0101                 catch
0102                     obj.close();
0103 
0104                     % pause before retrying
0105                     pause(1);
0106                 end
0107             end
0108         end % init
0109 
0110         % =================================================================
0111         %% close
0112         function close(obj)
0113             %
0114             % Close the connection
0115             %
0116 
0117             %   Author : Christoph Dinh, Matti Hamalainen, MGH Martinos Center
0118             %   License : BSD 3-clause
0119             %
0120             if ~isempty(obj.m_TcpSocket)
0121                 obj.m_TcpSocket.close();
0122             end
0123             obj.m_TcpSocket = [];
0124             obj.m_InputStream = [];
0125             obj.m_OutputStream = [];
0126             obj.m_BufferedInputStream = []; 
0127             obj.m_DataInputStream = [];
0128             obj.m_DataOutputStream = [];
0129         end % close
0130     end
0131 end
0132 
0133

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005