forked from fyrdahl/fire-matlab-ismrmrd-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.m
70 lines (62 loc) · 2.21 KB
/
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
classdef server < handle
% Created by Alexander Fyrdahl <[email protected]>
properties
port = [];
server_socket = [];
output_socket = [];
log = [];
end
methods
function obj = server(port,log)
log.info('Initializing server on port %d', port);
obj.port = port;
obj.log = log;
end
function serve(obj)
obj.log.info('Serving...');
while true
try
obj.server_socket = ServerSocket(obj.port);
obj.log.info('Waiting for client to connect to this host on port : %d', obj.port);
[obj.output_socket,remote_socket_address] = SocketAccept(obj.server_socket);
obj.log.info('Accepting connection from: %s', remote_socket_address);
handle(obj);
SocketClose(obj.server_socket);
SocketClose(obj.output_socket);
catch
if ~isempty(obj.output_socket)
SocketClose(obj.output_socket);
end
if ~isempty(obj.server_socket)
SocketClose(obj.server_socket);
end
pause(1);
end
end
end
function handle(obj)
try
conn = connection(obj.output_socket);
config = next(conn);
parameters = next(conn);
image = simplefft.process(conn,config,parameters,obj.log);
obj.log.info('Image done, sending!');
send_image(conn,image);
write_gadget_message_close(conn);
obj.log.info('Sending done!\n');
catch ME
if strcmp(ME.identifier,'Iterator:StopIteration')
SocketClose(obj.output_socket);
else
obj.log.error(ME);
rethrow(ME);
end
end
end
function delete(obj)
if ~isempty(obj.server_socket)
SocketClose(obj.server_socket);
end
end
end
end