0001 classdef mne_rt_client < handle
0002     
0003     
0004     
0005     
0006     
0007     
0008     
0009     
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 = []; 
0022         m_iNumOfRetries = -1;
0023     end
0024     
0025     methods
0026         
0027         
0028         function obj = mne_rt_client(host, port, number_of_retries)
0029             
0030             
0031             
0032             
0033             
0034             
0035             
0036             
0037             
0038 
0039             
0040             
0041             
0042             obj.init(host, port, number_of_retries);
0043         end 
0044         
0045         
0046         
0047         function result = init(obj, host, port, numOfRetries) 
0048             
0049             
0050             
0051             
0052             
0053             
0054             
0055             
0056             
0057 
0058             
0059             
0060             
0061             import java.net.Socket
0062             import java.io.*
0063 
0064             if (nargin < 3)
0065                 obj.m_iNumOfRetries = 20; 
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                     
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                     
0105                     pause(1);
0106                 end
0107             end
0108         end 
0109 
0110         
0111         
0112         function close(obj)
0113             
0114             
0115             
0116 
0117             
0118             
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 
0130     end
0131 end
0132 
0133