-
Notifications
You must be signed in to change notification settings - Fork 9
/
RtConductor.cpp
415 lines (337 loc) · 9.76 KB
/
RtConductor.cpp
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
/*=========================================================================
* Conductor.cpp defines a class that oversees and coordinates all
* operations during a single real-time fMRI session.
*
* Copyright 2007-2013, the MURFI dev team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include"RtConductor.h"
#include"RtExperiment.h"
#include"RtInputSynth.h"
#include"RtInfoClient.h"
#include<iostream>
// constructor with config
RtConductor::RtConductor(const RtConfigFmriRun &_config)
: ACE_Task_Base(),
configured(false),
running(false) {
ACE_TRACE(("RtConductor::RtConductor"));
configure(config);
infoClient = NULL;
}
// destructor
RtConductor::~RtConductor() {
ACE_TRACE(("RtConductor::~RtConductor"));
}
//*** initialization routines ***//
// configure the conductor
// in
// configuration
bool RtConductor::configure(const RtConfigFmriRun &_config) {
// copy the config
config = _config;
int curCodeNum;
// prepare inputs
// attach code numbers to inputs
curCodeNum = START_CODE_INPUTS;
for(vector<RtInput*>::iterator i = inputs.begin(); i != inputs.end();
curCodeNum++, i++) {
(*i)->setConductor(this);
(*i)->setCodeNum(curCodeNum);
}
// prepare outputs
if(config.get("study:log:disabled")==false) {
if(!outputLog.open(config)) {
cerr << "ERROR: could not open logfile \""
<< config.get("study:log:filename") << "\"" << endl;
}
}
// look for infoclient
if(config.isSet("infoclient:disabled")
&& config.get("infoclient:disabled") == false) {
cout << "configuring infoclient" << endl;
if(!config.isSet("infoclient:localPort")
||!config.isSet("infoclient:remoteHost")
|| !config.isSet("infoclient:remotePort")) {
cerr << "ERROR: localPort, remoteHost and remotePort are required options for infoclient"
<< endl;
return false;
}
infoClient
= new RtInfoClient((unsigned int) config.get("infoclient:localPort"),
config.get("infoclient:remoteHost").str(),
(unsigned int) config.get("infoclient:remotePort"));
addOutput(infoClient);
infoClient->activate();
cout << "infoclient configured" << endl;
}
// attach code numbers to outputs
curCodeNum = START_CODE_OUTPUTS;
for(vector<RtOutput*>::iterator i = outputs.begin(); i != outputs.end();
curCodeNum++, i++) {
(*i)->setConductor(this);
(*i)->setCodeNum(curCodeNum);
}
// build the stream and its components
if(!buildStream()) {
cerr << "ERROR: failed to build the processing stream for the conductor"
<< endl;
return false;
}
configured = true;
// write configuration to the log
outputLog.writeConfig(config);
outputLog << "initialization completed at ";
outputLog.printNow();
outputLog << "\n";
return true;
}
// deconfigure the conductor by cleaning up streams
void RtConductor::deconfigure() {
stream.close();
// tell everyone that we're done and delete them
for(vector<RtInput*>::iterator i=inputs.begin(); i != inputs.end(); i++) {
if((*i)->isDeleteable()) {
(*i)->close();
delete (*i);
}
}
for(vector<RtOutput*>::iterator o=outputs.begin(); o != outputs.end(); o++){
if((*o)->isDeleteable()) {
(*o)->close();
delete (*o);
}
}
configured = false;
}
// builds the processing stream
// in:
// config: configuration
// out:
// true (for success) or false
bool RtConductor::buildStream() {
ACE_TRACE(("RtConductor::buildStream"));
// set the conductor to us
stream.setStreamConductor(this);
// open the stream
if(!stream.configure(config)) {
return false;
}
return true;
}
// adds input mode
// in:
// in: input object
// out:
// true (for success) or false
bool RtConductor::addInput(RtInput *in) {
ACE_TRACE(("RtConductor::addInput"));
// open and configure
if(!in->open(config)) {
cerr << "WARNING: failed to add input: " << in->getID() << endl;
return false;
}
else {
in->setActivatable(true);
in->setDeleteable(true);
}
inputs.push_back(in);
cout << "added input: " << in->getID() << endl;
return true;
}
// adds input mode that has already been configured
// in:
// out: input object
// out:
// true (for success) or false
bool RtConductor::addExistingInput(RtInput *in) {
ACE_TRACE(("RtConductor::addInput"));
inputs.push_back(in);
cout << "added input: " << in->getID() << endl;
return true;
}
// adds inputs from a vector
// in:
// in: vector of input objects
// out:
// true (for success) or false
bool RtConductor::addVectorOfInputs(vector<RtInput*> &ins) {
bool ret = true;
for(vector<RtInput*>::iterator i = ins.begin(); i != ins.end(); i++) {
if(!addInput(*i)) {
ret = false;
}
}
return ret;
}
// adds output mode
// in:
// out: output object
// out:
// true (for success) or false
bool RtConductor::addOutput(RtOutput *out) {
ACE_TRACE(("RtConductor::addOutput"));
// open and configure
if(!out->open(config)) {
cerr << "WARNING: failed to add output: " << out->getID() << endl;
return false;
}
else {
out->setDeleteable(true);
}
outputs.push_back(out);
cout << "added output: " << out->getID() << endl;
return true;
}
// adds output mode that has already been configured
// in:
// out: output object
// out:
// true (for success) or false
bool RtConductor::addExistingOutput(RtOutput *out) {
ACE_TRACE(("RtConductor::addOutput"));
outputs.push_back(out);
cout << "added output: " << out->getID() << endl;
return true;
}
// adds outputs from a vector
// in:
// out: vector of output objects
// out:
// true (for success) or false
bool RtConductor::addVectorOfOutputs(vector<RtOutput*> &outs) {
bool ret = true;
for(vector<RtOutput*>::iterator i = outs.begin(); i != outs.end(); i++) {
if(!addOutput(*i)) {
ret = false;
}
}
return ret;
}
//*** operation routines ***//
// begins execution of a realtime fMRI session
// out:
// true (for success) or false
int RtConductor::svc() {
ACE_TRACE(("RtConductor::svc"));
if(!configured) {
cerr << "ERROR: the conductor for this run has not been configured"
<< endl;
return false;
}
// print start time to log file
outputLog << "began running at ";
outputLog.printNow();
outputLog << "\n";
stream.startProcessing();
running = true;
// start up the threads that listen for input
for(vector<RtInput*>::iterator i = inputs.begin(); i != inputs.end(); i++) {
if((*i)->isActivatable()) {
(*i)->activate();
}
}
while(running) {
usleep(1000);
}
cout << "conductor waiting for stream to complete processing...";
stream.waitForProcessingToComplete();
cout << "done" << endl;
// print end time to log file
outputLog << "done running at ";
outputLog.printNow();
outputLog << "\n";
deconfigure();
return true;
}
// receive a code signaling completetion of data input or processing
void RtConductor::receiveCode(unsigned int code, RtData *data) {
ACE_TRACE(("RtConductor::receiveCode"));
// handle based on the thrower
if(code == SHUTDOWN) {
cout << "shutdown signal " << code << " received" << endl;
running = false;
}
else if(code == START_CODE_STREAM) { // stream component has data
// make data available to all output processes
for(vector<RtOutput*>::iterator i = outputs.begin();
i != outputs.end(); i++) {
if(*i == infoClient) {
//cout << "setting data on info client" << endl;
}
(*i)->setData(data);
return;
}
}
else if(code < START_CODE_OUTPUTS) { // this is an input
// let the stream decide if it should spawn a new processing instance
stream.setInput(code,data);
cout << "sent ready signal" << endl;
}
else { // this is an output
cout << "caught a ready signal from an output" << endl;
// dont need to do much here
}
}
// write to the log file
void RtConductor::log(const string &s) {
outputLog.write(s);
}
// write to the log file
void RtConductor::log(stringstream &s) {
outputLog.write(s);
}
// get an input by its name
// in
// name: name of input to get
// out
// pointer to the input object
RtInput *RtConductor::getInputByName(const string &name) {
for(vector<RtInput*>::iterator i = inputs.begin(); i != inputs.end(); i++) {
if((*i)->getID() == name) {
return *i;
}
}
return NULL;
}
// get an output by its name
// in
// name: name of output to get
// out
// pointer to the output object (the first with the specified name)
RtOutput *RtConductor::getOutputByName(const string &name) {
for(vector<RtOutput*>::iterator i = outputs.begin(); i != outputs.end(); i++){
//cout << (*i)->getID() << "=" << name << endl;
if((*i)->getID() == name) {
return *i;
}
}
return NULL;
}
// get all the outputs with a name
// in
// name: name of output to get
// out
// vector of pointers to the output objects
vector<RtOutput*> RtConductor::getAllOutputsWithName(const string &name) {
vector<RtOutput*> outs;
for(vector<RtOutput*>::iterator i = outputs.begin(); i != outputs.end(); i++){
//cout << (*i)->getID() << "=" << name << endl;
if((*i)->getID() == name) {
outs.push_back(*i);
}
}
return outs;
}