-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_server.m
444 lines (360 loc) · 11.2 KB
/
tcp_server.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
function [ out ] = tcp_server( action, varargin )
%TCP_SERVER TCP Server control function.
%
% [ OUT ] = TCP_SERVER( ACTION, ARG ) Sets up and controls
% a TCP server in Matlab and Octave. ACTION specifies one
% of INITIALIZE, ACCEPT, RECEIVE, SEND, or CLOSE operations.
% The optional output OUT argument is indicates success 0,
% failure -1, or the read DATA for the RECEIVE action. The
% TCP data struct is returned when TCP_SERVER is called
% without any input arguments.
%
% The INITIALIZE action accepts the following ARG
% property/value pairs.
%
% Property Value/{Default} Description
% -----------------------------------------------------------------------------------
% port scalar (2)/{[4000 inf]} Port or range to establish server connection.
% timeout scalar/{1000} Accept/read socket [ms] timeout limit.
% fid scalar/{1} Output log file identifstat.
% debug scalar/{0} Debug message level (0/1/2).
%
% The SEND action accepts the DATA to send as second
% input argument.
% Copyright 2013-2017 Precise Simulation, Ltd.
mlock
if( ~(nargin || nargout) ),help tcp_server, return, end
if( nargout && ~nargin )
out = tcp_get_set();
return
end
if( ischar(action) )
action = find(strncmpi( action, ...
{'initialize','accept','receive','send','close','read','write'}, 4 ));
end
out = -1;
switch( action )
case 1 % Initialize.
out = tcp_initialize( varargin{:} );
case 2 % Accept.
out = tcp_accept();
case {3,6} % Receive/read.
out = tcp_receive();
case {4,7} % Send/write.
out = tcp_send( varargin{:} );
case 5 % Close.
out = tcp_close();
otherwise
error( 'Unknown TCP action/operation.' )
end
if( ~nargout )
clear out;
end
%------------------------------------------------------------------------------%
function [ stat ] = tcp_initialize( varargin )
% Initialize and open TCP socket.
try
tmp = javaObject('java.lang.String');
assert( isa(tmp,'java.lang.String') )
catch
error( 'tcp_server:initialize', ...
'Java not found. Please install and enable Java. ' )
end
% Set up initial TCP parameters.
tcp = tcp_configure( varargin{:} );
% Attempt to open socket.
stat = -1;
while( tcp.port(1)<=tcp.port(2) )
try
server_socket = javaObject( 'java.net.ServerSocket', tcp.port(1) );
stat = 0;
break;
catch
% Try another port in valid range.
tcp.port(1) = tcp.port(1) + 1;
end
end
if( stat~=0 )
error( 'tcp_server:initialize', ...
sprintf('Failed to open server on port %i.', tcp.port(1) ) )
% Possible reasons: "port is still open by Octave/Matlab for
% instance due to an previous crash", "Blocked by Firewall
% application", "Port open in another Application", "No rights to
% open the port"
end
assert( ~server_socket.isClosed() )
% Sucessfully opened socket and established connection.
server_socket.setSoTimeout( tcp.timeout );
tcp.port = tcp.port(1);
tcp.server_socket = server_socket;
if( tcp.debug )
fprintf( tcp.fid, 'Server sucessfully openened on port %i.\n', tcp.port );
end
tcp_get_set( tcp );
%------------------------------------------------------------------------------%
function [ tcp ] = tcp_configure( varargin )
% Setup and parse input parameters.
NRANGE = 20;
% Try to close socket if it exists and is open on port.
tcp = tcp_get_set();
if( ~isempty(tcp) && isfield(tcp,'server_socket') && ...
isa(tcp.server_socket,'java.net.ServerSocket') && ...
tcp.server_socket.getLocalPort()==tcp.port(1) && ...
~tcp.server_socket.isClosed() )
tcp.server_socket.close();
end
tcp = [];
% Default parameters.
tcp.port = [4000 inf];
tcp.timeout = 1000;
tcp.fid = 1;
tcp.debug = 0;
% Parse configuration.
config = varargin;
if( ~isempty(config) )
if( iscell(config) )
config = cell2struct( config(2:2:end), config(1:2:end-1), 2 );
end
for s_field=fieldnames(config)'
tcp.(s_field{1}) = config.(s_field{1});
end
end
if( ~length(tcp.port~=2) )
tcp.port = tcp.port([1 1]);
end
%------------------------------------------------------------------------------%
function [ stat ] = tcp_accept()
% Callback function to accept socket connection.
tcp = tcp_get_set();
stat = -1;
if( tcp.debug>1 )
fprintf( tcp.fid, 'Running tcp_accept callback.\n' );
end
try
socket = tcp.server_socket.accept();
if( isempty(socket) || ~isa(socket,'java.net.Socket') )
return
end
catch
return
end
stat = 0;
% Store accepted socket data.
tcp.socket = socket;
tcp.remoteHost = char(socket.getInetAddress());
tcp.outputStream = socket.getOutputStream();
tcp.inputStream = socket.getInputStream();
tcp_get_set( tcp );
if( tcp.debug )
fprintf( tcp.fid, 'Connection established on socket.\n' );
end
%------------------------------------------------------------------------------%
function [ data ] = tcp_receive()
% Receive/read message.
tcp = tcp_get_set();
NDATA0 = 10000; % Initial data array size.
T_MIN = 0.01; % Minimum time to receive any data [s].
T_MAX = 5.0; % Maximum time to wait for and receive data [s].
data = zeros( 1, NDATA0, 'int8' );
n_data_bytes = 0; % Total number of received data bytes.
n_data_length = NDATA0; % Current total length of data array.
t_start = tic();
while( true )
% Check number of bytes in partial stream and exit if done.
n_partial_bytes = tcp.inputStream.available();
t = toc( t_start );
if( n_partial_bytes<=0 && ...
( (n_data_bytes>0 && t>T_MIN) || (t>T_MAX) ) )
break;
end
% Increase data array length if required.
if( n_data_bytes+n_partial_bytes>n_data_length )
data = [ data zeros( 1, NDATA0, 'int8' ) ];
n_data_length = n_data_bytes + NDATA0;
end
% Recieve partial data stream.
try
data_i = toByteArray( org.apache.commons.io.IOUtils, tcp.inputStream, n_partial_bytes );
data(n_data_bytes+1:n_data_bytes+length(data_i)) = data_i;
catch
for i=1:n_partial_bytes
data(n_data_bytes+i) = tcp.inputStream.read();
end
end
n_data_bytes = n_data_bytes + n_partial_bytes;
end
if( n_data_bytes<=0 || n_partial_bytes<0 )
error( 'Could not recieve data.' )
end
data = data(1:n_data_bytes);
if( tcp.debug )
fprintf( tcp.fid, 'Data sucessfully received.\n' );
if( tcp.debug>1 )
fprintf( tcp.fid, '\n%s\n', char(data) );
end
end
tcp.data = data;
tcp_get_set( tcp );
%------------------------------------------------------------------------------%
function [ stat ] = tcp_send( varargin )
% Send/write message.
stat = 0;
if( isnumeric(varargin{1}) )
data = varargin{1};
elseif( any(strcmpi(varargin,'data')) )
data = varargin{find(strcmpi(varargin,'data'),1,'first')+1};
else
error( 'Error in input data.' );
end
if( ischar(data) )
data = int8(data);
end
if( ~isa(varargin{1},'int8') )
error( 'Input data must be of type int8.' );
end
tcp = tcp_get_set();
tcp.outputStream.write( data, 0, length(data) );
tcp.outputStream.flush();
%------------------------------------------------------------------------------%
function [ stat ] = tcp_close()
% Disconnect and close TCP server.
stat = 0;
tcp = tcp_get_set();
tcp.server_socket.close();
assert( tcp.server_socket.isClosed() )
if( tcp.debug )
fprintf( tcp.fid, 'Server on port %i sucessfully closed.\n', tcp.port );
end
if( isfield(tcp,'shandle') )
delete( tcp.shandle )
end
tcp = [];
tcp_get_set( tcp );
%------------------------------------------------------------------------------%
function [ tcp ] = tcp_get_set( tcp )
% Persistently get and set TCP data struct.
persistent stored_tcp
if( nargin ) % Set.
stored_tcp = tcp;
end
if( nargout ) % Get.
tcp = stored_tcp;
end
%%-----------------------------------------------------------------------------%
%% TESTS
%%-----------------------------------------------------------------------------%
%!shared args
%! args = { 'debug', 0 };
%!test % Test 1. Initialize and close.
%!
%! tcp = tcp_server( 'initialize', args{:} );
%!
%! tcp = tcp_server( 'close' );
%! assert( tcp==0 )
%!
%! tcp = tcp_server();
%! assert( isempty(tcp) )
%!test % Test 2. Initialize, re-initalize, and close.
%!
%! tcp = tcp_server( 'initialize', args{:} );
%! tcp = tcp_server();
%! port0 = tcp.port;
%!
%! tcp = tcp_server( 'initialize', args{:} );
%! tcp = tcp_server();
%! port1 = tcp.port;
%!
%! tcp = tcp_server( 'close' );
%! assert( tcp==0 & port0==port1 )
%!
%! tcp = tcp_server();
%! assert( isempty(tcp) )
%!test % Test 3. Initialize, (clear), re-initalize, and close.
%!
%! tcp = tcp_server( 'initialize', args{:} );
%! tcp = tcp_server();
%! port0 = tcp.port;
%! s = tcp.server_socket;
%!
%! clear tcp_server
%! tcp = tcp_server( 'initialize', args{:} );
%! tcp = tcp_server();
%! port1 = tcp.port;
%!
%! tcp = tcp_server( 'close' );
%! assert( tcp==0 & port0<port1 )
%!
%! tcp = tcp_server();
%! assert( isempty(tcp) )
%! s.close();
%!test % Test 4. Initialize, accept connection, and close.
%!
%! tcp = tcp_server( 'initialize', args{:} );
%! tcp = tcp_server();
%! s = tcp.server_socket;
%! c = javaObject( 'java.net.Socket', s.getInetAddress(), s.getLocalPort() );
%!
%! tcp = tcp_server( 'accept' );
%! c.close();
%!
%! tcp = tcp_server();
%! assert( isfield(tcp,'socket') )
%!
%! tcp = tcp_server( 'close' );
%! assert( tcp==0 )
%!test % Test 5. Initialize, accept connection, receive message, and close.
%!
%! tcp = tcp_server( 'initialize', args{:} );
%! tcp = tcp_server();
%! s = tcp.server_socket;
%! c = javaObject( 'java.net.Socket', s.getInetAddress(), s.getLocalPort() );
%!
%! tcp = tcp_server( 'accept' );
%!
%! msg = int8('socket-test-message');
%! c.getOutputStream().write( msg, 0, length(msg) );
%! c.getOutputStream().flush();
%! data = tcp_server( 'receive' );
%! c.close();
%!
%! assert( isequal(msg,data) )
%! tcp = tcp_server( 'close' );
%! assert( tcp==0 )
%!test % Test 6. Initialize, accept connection, send message, and close.
%!
%! tcp = tcp_server( 'initialize', args{:} );
%! tcp = tcp_server();
%! s = tcp.server_socket;
%! c = javaObject( 'java.net.Socket', s.getInetAddress(), s.getLocalPort() );
%!
%! tcp = tcp_server( 'accept' );
%!
%! msg = int8('socket-test-message');
%! tcp = tcp_server( 'send', msg );
%! inputStream = c.getInputStream();
%! n = inputStream.available();
%! for i=1:n
%! data(i) = inputStream.read();
%! end
%! c.close();
%!
%! assert( isequal(msg,data) )
%! tcp = tcp_server( 'close' );
%! assert( tcp==0 )
%!test % Test 7. Accept timeout.
%!
%! tcp = tcp_server( 'initialize', args{:}, 'timeout', 500 );
%!
%! tcp = tcp_server( 'accept' );
%! assert( tcp==-1 )
%!
%! tcp = tcp_server();
%! s = tcp.server_socket;
%! c = javaObject( 'java.net.Socket', s.getInetAddress(), s.getLocalPort() );
%!
%! tcp = tcp_server( 'accept' );
%! c.close();
%! assert( tcp==0 )
%!
%! tcp = tcp_server( 'close' );
%! assert( tcp==0 )