-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.m
executable file
·164 lines (144 loc) · 7.14 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
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
classdef server < handle
properties
port = [];
tcpHandle = [];
log = [];
savedata = false;
savedataFolder = '';
end
methods
function obj = server(port, log, savedata, savedataFolder)
log.info('Starting server and listening for data at %s:%d', '0.0.0.0', port);
if (savedata)
log.info("Saving incoming data is enabled.")
end
obj.port = port;
obj.log = log;
obj.savedata = savedata;
obj.savedataFolder = savedataFolder;
end
function serve(obj)
while true
try
obj.tcpHandle = tcpip('0.0.0.0', obj.port, ...
'NetworkRole', 'server', ...
'InputBufferSize', 32 * 2^20 , ...
'OutputBufferSize', 32 * 2^20, ...
'Timeout', 3000); %#ok<TNMLP> Consider moving toolbox function TCPIP out of the loop for better performance
obj.log.info('Waiting for client to connect to this host on port : %d', obj.port);
fopen(obj.tcpHandle);
obj.log.info('Accepting connection from: %s:%d', obj.tcpHandle.RemoteHost, obj.tcpHandle.RemotePort);
handle(obj);
flushoutput(obj.tcpHandle);
fclose(obj.tcpHandle);
delete(obj.tcpHandle);
obj.tcpHandle = [];
catch ME
if ~isempty(obj.tcpHandle)
fclose(obj.tcpHandle);
delete(obj.tcpHandle);
obj.tcpHandle = [];
end
obj.log.error(sprintf('%s\nError in %s (%s) (line %d)', ME.message, ME.stack(1).('name'), ME.stack(1).('file'), ME.stack(1).('line')));
end
pause(1)
end
end
function handle(obj)
try
conn = connection(obj.tcpHandle, obj.log, obj.savedata, '', obj.savedataFolder);
config = next(conn);
metadata = next(conn);
try
metadata = ismrmrd.xml.deserialize(metadata);
if ~isempty(metadata.acquisitionSystemInformation.systemFieldStrength_T)
obj.log.info("Data is from a %s %s at %1.1fT", metadata.acquisitionSystemInformation.systemVendor, metadata.acquisitionSystemInformation.systemModel, metadata.acquisitionSystemInformation.systemFieldStrength_T)
end
catch
obj.log.info("Metadata is not a valid MRD XML structure. Passing on metadata as text")
end
% Decide what program to use based on config
% As a shortcut, we accept the file name as text too.
if strcmpi(config, "simplefft")
obj.log.info("Starting simplefft processing based on config")
recon = simplefft;
elseif strcmpi(config, "invertcontrast")
obj.log.info("Starting invertcontrast processing based on config")
recon = invertcontrast;
elseif strcmpi(config, "mapvbvd")
obj.log.info("Starting mapvbvd processing based on config")
recon = fire_mapVBVD;
elseif strcmpi(config, "msasha") || strcmpi(config, "jointt1t2_4p")
obj.log.info("Starting mapvbvd processing based on config")
recon = mSASHA;
elseif strcmpi(config, "savedataonly")
% Dummy loop with no processing
try
while true
item = next(conn);
if isempty(item)
break;
end
end
conn.send_close();
catch
conn.send_close();
end
% Dummy function for below as we already processed the data
recon = @(conn, config, meta, log) (true);
else
if exist(config, 'class')
obj.log.info("Starting %s processing based on config", config)
eval(['recon = ' config ';'])
else
obj.log.info("Unknown config '%s'. Falling back to 'invertcontrast'", config)
recon = invertcontrast;
end
end
recon.process(conn, config, metadata, obj.log);
catch ME
obj.log.error('[%s:%d] %s', ME.stack(2).name, ME.stack(2).line, ME.message);
rethrow(ME);
end
if (conn.savedata)
% Dataset may not be closed properly if a close message is not received
if (~isempty(conn.dset) && H5I.is_valid(conn.dset.fid))
conn.dset.close()
end
if (isempty(conn.savedataFile) && exist(conn.mrdFilePath, 'file'))
try
% Rename the saved file to use the protocol name
info = h5info(conn.mrdFilePath);
% Check if the group exists
indGroup = find(strcmp(arrayfun(@(x) x.Name, info.Groups, 'UniformOutput', false), strcat('/', conn.savedataGroup)), 1);
% Check if xml exists
xmlExists = any(strcmp(arrayfun(@(x) x.Name, info.Groups(indGroup).Datasets, 'UniformOutput', false), 'xml'));
if (xmlExists)
dset = ismrmrd.Dataset(conn.mrdFilePath, conn.savedataGroup);
xml = dset.readxml();
dset.close();
mrdHead = ismrmrd.xml.deserialize(xml);
if ~isempty(mrdHead.measurementInformation.protocolName)
newFilePath = strrep(conn.mrdFilePath, 'MRD_input_', strcat(mrdHead.measurementInformation.protocolName, '_'));
movefile(conn.mrdFilePath, newFilePath);
conn.mrdFilePath = newFilePath;
end
end
catch
obj.log.error('Failed to rename saved file %s', conn.mrdFilePath);
end
end
if ~isempty(conn.mrdFilePath)
obj.log.info("Incoming data was saved at %s", conn.mrdFilePath)
end
end
end
function delete(obj)
if ~isempty(obj.tcpHandle)
fclose(obj.tcpHandle);
delete(obj.tcpHandle);
obj.tcpHandle = [];
end
end
end
end