0001 classdef mne_rt_data_client < mne_rt_client
0002
0003
0004
0005
0006
0007
0008
0009
0010 properties
0011 m_clientID = -1;
0012 end
0013
0014 methods
0015
0016
0017 function obj = mne_rt_data_client(host, port, numOfRetries)
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 if (nargin < 3)
0032 numOfRetries = 20;
0033 end
0034 obj = obj@mne_rt_client(host, port, numOfRetries);
0035 obj.getClientId();
0036 end
0037
0038
0039
0040 function [info] = readInfo(obj)
0041
0042 import java.net.Socket
0043 import java.io.*
0044
0045 global FIFF;
0046 if isempty(FIFF)
0047 FIFF = fiff_define_constants();
0048 end
0049
0050 if ~isempty(obj.m_DataInputStream)
0051
0052 t_bReadMeasBlockStart = false;
0053 t_bReadMeasBlockEnd = false;
0054
0055
0056
0057 while(~t_bReadMeasBlockStart)
0058 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0059
0060 if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_MEAS_INFO)
0061 disp('FIFF_BLOCK_START FIFFB_MEAS_INFO');
0062 t_bReadMeasBlockStart = true;
0063 end
0064 end
0065
0066
0067
0068
0069
0070 info.dev_head_t = [];
0071 info.ctf_head_t = [];
0072 info.dev_ctf_t = [];
0073 info.acq_pars = [];
0074 info.acq_stim = [];
0075 info.chs = [];
0076
0077 dev_head_t_read = false;
0078 ctf_head_t_read = false;
0079
0080 while(~t_bReadMeasBlockEnd)
0081 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0082
0083
0084
0085 if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_DACQ_PARS)
0086 while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_DACQ_PARS)
0087 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0088 if(tag.kind == FIFF.FIFF_DACQ_PARS)
0089 info.acq_pars = tag.data;
0090 elseif(tag.kind == FIFF.FIFF_DACQ_STIM)
0091 info.acq_stim = tag.data;
0092 end
0093 end
0094 end
0095
0096
0097
0098 if (tag.kind == FIFF.FIFF_COORD_TRANS)
0099 if (~dev_head_t_read)
0100 info.dev_head_t = tag.data;
0101 dev_head_t_read = true;
0102 elseif (~ctf_head_t_read)
0103 info.ctf_head_t = tag.data;
0104 ctf_head_t_read = true;
0105 end
0106 end
0107
0108
0109
0110 if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_ISOTRAK)
0111 info.dig = [];
0112 while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_ISOTRAK)
0113 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0114 if(tag.kind == FIFF.FIFF_DIG_POINT)
0115 info.dig = [info.dig tag.data];
0116 end
0117 end
0118 end
0119
0120
0121
0122 if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_PROJ)
0123 info.projs = [];
0124 while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_PROJ)
0125 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0126 if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_PROJ_ITEM)
0127 proj = [];
0128 while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_PROJ_ITEM)
0129 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0130 switch tag.kind
0131 case FIFF.FIFF_NAME
0132 proj.desc = tag.data;
0133 case FIFF.FIFF_PROJ_ITEM_KIND
0134 proj.kind = tag.data;
0135 case FIFF.FIFF_NCHAN
0136 proj.data.ncol = tag.data;
0137 case FIFF.FIFF_PROJ_ITEM_NVEC
0138 proj.data.nrow = tag.data;
0139 case FIFF.FIFF_MNE_PROJ_ITEM_ACTIVE
0140 proj.active = tag.data;
0141 case FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST
0142 proj.data.col_names = fiff_split_name_list(tag.data);
0143 case FIFF.FIFF_PROJ_ITEM_VECTORS
0144 proj.data.data = tag.data;
0145 end
0146 end
0147 end
0148
0149 if ~isempty(proj)
0150 info.projs = [info.projs proj];
0151 end
0152 end
0153 end
0154
0155
0156
0157 if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_MNE_CTF_COMP)
0158 info.comps = [];
0159 while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_MNE_CTF_COMP)
0160 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0161 if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_MNE_CTF_COMP_DATA)
0162 comp = [];
0163 while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_MNE_CTF_COMP_DATA)
0164 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0165 switch tag.kind
0166 case FIFF.FIFF_MNE_CTF_COMP_KIND
0167 comp.ctfkind = tag.data;
0168 case FIFF.FIFF_MNE_CTF_COMP_CALIBRATED
0169 comp.save_calibrated = tag.data;
0170 case FIFF.FIFF_MNE_CTF_COMP_DATA
0171 comp.data = tag.data;
0172 end
0173 end
0174 end
0175
0176 if ~isempty(comp)
0177 info.comps = [info.comps comp];
0178 end
0179 end
0180 end
0181
0182
0183
0184 if(tag.kind == FIFF.FIFF_BLOCK_START && tag.data == FIFF.FIFFB_MNE_BAD_CHANNELS)
0185 info.bads = [];
0186 while(tag.kind ~= FIFF.FIFF_BLOCK_END || tag.data ~= FIFF.FIFFB_MNE_BAD_CHANNELS)
0187 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0188 if(tag.kind == FIFF.FIFF_MNE_CH_NAME_LIST)
0189 info.bads = fiff_split_name_list(tag.data);
0190 end
0191 end
0192 end
0193
0194
0195
0196 if (tag.kind == FIFF.FIFF_SFREQ)
0197 info.sfreq = tag.data;
0198 elseif (tag.kind == FIFF.FIFF_HIGHPASS)
0199 info.highpass = tag.data;
0200 elseif (tag.kind == FIFF.FIFF_LOWPASS)
0201 info.lowpass = tag.data;
0202 elseif (tag.kind == FIFF.FIFF_NCHAN)
0203 info.nchan = tag.data;
0204 elseif (tag.kind == FIFF.FIFF_MEAS_DATE)
0205 info.highpass = tag.data;
0206 end
0207
0208
0209 if (tag.kind == FIFF.FIFF_CH_INFO)
0210 info.chs = [info.chs tag.data];
0211 end
0212
0213
0214 if(tag.kind == FIFF.FIFF_BLOCK_END && tag.data == FIFF.FIFFB_MEAS_INFO)
0215 disp('FIFF_BLOCK_END FIFFB_MEAS_INFO');
0216 t_bReadMeasBlockEnd = true;
0217 end
0218 end
0219 else
0220 error('mne_rt_data_client: no available TcpSocket, call init to establish a connection.');
0221 end
0222 end
0223
0224
0225
0226 function [kind, data] = readRawBuffer(obj, p_nChannels)
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245 import java.net.Socket
0246 import java.io.*
0247
0248 global FIFF;
0249 if isempty(FIFF)
0250 FIFF = fiff_define_constants();
0251 end
0252
0253 data = [];
0254 kind = [];
0255
0256 if ~isempty(obj.m_DataInputStream)
0257
0258 bytes_available = obj.m_DataInputStream.available;
0259
0260 while(bytes_available == 0)
0261 bytes_available = obj.m_DataInputStream.available;
0262 end
0263
0264 tag = mne_rt_data_client.read_tag(obj.m_DataInputStream);
0265
0266 kind = tag.kind;
0267
0268 if(tag.kind == FIFF.FIFF_DATA_BUFFER)
0269 nSamples = length(tag.data)/p_nChannels;
0270 data = reshape(tag.data, p_nChannels, nSamples);
0271 else
0272 data = tag.data;
0273 end
0274 else
0275 error('mne_rt_data_client: no available TcpSocket, call init to establish a connection.');
0276 end
0277 end
0278
0279
0280
0281 function setClientAlias(obj, alias)
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 import java.net.Socket
0297 import java.io.*
0298
0299 global MNE_RT;
0300 if isempty(MNE_RT)
0301 MNE_RT = mne_rt_define_commands();
0302 end
0303
0304 if ~isempty(obj.m_DataOutputStream)
0305 mne_rt_data_client.sendFiffCommand(obj.m_DataOutputStream, MNE_RT.MNE_RT_SET_CLIENT_ALIAS, alias)
0306 end
0307 end
0308
0309
0310
0311 function [id] = getClientId(obj)
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323 import java.net.Socket
0324 import java.io.*
0325 if(obj.m_clientID == -1)
0326 global FIFF;
0327 if isempty(FIFF)
0328 FIFF = fiff_define_constants();
0329 end
0330 global MNE_RT;
0331 if isempty(MNE_RT)
0332 MNE_RT = mne_rt_define_commands();
0333 end
0334
0335 if ~isempty(obj.m_DataOutputStream)
0336
0337 mne_rt_data_client.sendFiffCommand(obj.m_DataOutputStream, MNE_RT.MNE_RT_GET_CLIENT_ID)
0338
0339
0340 tag = obj.read_tag(obj.m_DataInputStream);
0341 if (tag.kind == FIFF.FIFF_MNE_RT_CLIENT_ID)
0342 obj.m_clientID = tag.data;
0343 end
0344 end
0345 end
0346 id = obj.m_clientID;
0347 end
0348 end
0349
0350 methods(Static)
0351
0352
0353 function sendFiffCommand(p_DataOutputStream, p_Cmd, p_data)
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371 import java.net.Socket
0372 import java.io.*
0373
0374 global FIFF;
0375 if isempty(FIFF)
0376 FIFF = fiff_define_constants();
0377 end
0378
0379 if (nargin == 3)
0380 data = char(p_data);
0381 elseif(nargin == 2)
0382 data = [];
0383 else
0384 error('Wrong number of arguments.');
0385 end
0386
0387 kind = FIFF.FIFF_MNE_RT_COMMAND;
0388 type = FIFF.FIFFT_VOID;
0389 size = 4+length(data);
0390 next = 0;
0391
0392 p_DataOutputStream.writeInt(kind);
0393 p_DataOutputStream.writeInt(type);
0394 p_DataOutputStream.writeInt(size);
0395 p_DataOutputStream.writeInt(next);
0396 p_DataOutputStream.writeInt(p_Cmd);
0397 if(~isempty(data))
0398 p_DataOutputStream.writeBytes(data);
0399 end
0400 p_DataOutputStream.flush;
0401 end
0402
0403
0404
0405 function [tag] = read_tag(p_DataInputStream)
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418 import java.net.Socket
0419 import java.io.*
0420
0421 me='MNE_RT_DATA_CLIENT:read_tag';
0422
0423
0424
0425
0426 tagInfo = mne_rt_data_client.read_tag_info(p_DataInputStream);
0427
0428
0429
0430
0431 tag = mne_rt_data_client.read_tag_data(p_DataInputStream, tagInfo);
0432 end
0433
0434
0435
0436 function [tag] = read_tag_data(p_DataInputStream, p_tagInfo, pos)
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452 import java.net.Socket
0453 import java.io.*
0454
0455 global FIFF;
0456 if isempty(FIFF)
0457 FIFF = fiff_define_constants();
0458 end
0459
0460 me='MNE_RT_DATA_CLIENT:read_tag_data';
0461
0462 if nargin == 3
0463 p_DataInputStream.skipBytes(pos);
0464 elseif nargin ~= 2
0465 error(me,'Incorrect number of arguments');
0466 end
0467
0468 tag = p_tagInfo;
0469
0470
0471
0472
0473 is_matrix = 4294901760;
0474 matrix_coding_dense = 16384;
0475 matrix_coding_CCS = 16400;
0476 matrix_coding_RCS = 16416;
0477 data_type = 65535;
0478
0479 if tag.size > 0
0480 matrix_coding = bitand(is_matrix,tag.type);
0481 if matrix_coding ~= 0
0482 matrix_coding = bitshift(matrix_coding,-16);
0483
0484
0485
0486 if matrix_coding == matrix_coding_dense
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502 matrix_type = bitand(data_type,tag.type);
0503
0504 el_size = tag.size - 3*4;
0505
0506 switch matrix_type
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517 case FIFF.FIFFT_FLOAT
0518 t_MNERTBufferReader = mne_rt_buffer_reader(p_DataInputStream);
0519 tmp = t_MNERTBufferReader.readBuffer(el_size);
0520 tag.data = typecast(tmp, 'single');
0521 tag.data = swapbytes(tag.data);
0522 otherwise
0523 error(me,'Cannot handle a matrix of type %d yet',matrix_type)
0524 end
0525
0526
0527 dims = zeros(1, 2);
0528
0529 dims(1) = p_DataInputStream.readInt;
0530 dims(2) = p_DataInputStream.readInt;
0531
0532 ndim = p_DataInputStream.readInt;
0533
0534 tag.data = reshape(tag.data,dims(1),dims(2))';
0535 else
0536 error(me,'Cannot handle other than dense or sparse matrices yet')
0537 end
0538 else
0539
0540
0541
0542 switch tag.type
0543
0544
0545
0546 case FIFF.FIFFT_INT
0547
0548
0549
0550
0551 t_MNERTBufferReader = mne_rt_buffer_reader(p_DataInputStream);
0552 tmp = t_MNERTBufferReader.readBuffer(tag.size);
0553 tag.data = typecast(tmp, 'int32');
0554 tag.data = swapbytes(tag.data);
0555 case FIFF.FIFFT_FLOAT
0556 t_MNERTBufferReader = mne_rt_buffer_reader(p_DataInputStream);
0557 tmp = t_MNERTBufferReader.readBuffer(tag.size);
0558 tag.data = typecast(tmp, 'single');
0559 tag.data = swapbytes(tag.data);
0560 case FIFF.FIFFT_STRING
0561 t_MNERTBufferReader = mne_rt_buffer_reader(p_DataInputStream);
0562 tag.data = t_MNERTBufferReader.readBuffer(tag.size);
0563 tag.data = char(tag.data);
0564 case FIFF.FIFFT_ID_STRUCT
0565 tag.data.version = p_DataInputStream.readInt;
0566 tag.data.machid = zeros(2,1);
0567 tag.data.machid(1) = p_DataInputStream.readInt;
0568 tag.data.machid(2) = p_DataInputStream.readInt;
0569 tag.data.secs = p_DataInputStream.readInt;
0570 tag.data.usecs = p_DataInputStream.readInt;
0571 case FIFF.FIFFT_DIG_POINT_STRUCT
0572 tag.data.kind = p_DataInputStream.readInt;
0573 tag.data.ident = p_DataInputStream.readInt;
0574 tag.data.r = zeros(3,1);
0575 for i = 1:3
0576 tag.data.r(i) = p_DataInputStream.readFloat;
0577 end
0578 tag.data.coord_frame = 0;
0579 case FIFF.FIFFT_COORD_TRANS_STRUCT
0580 tag.data.from = p_DataInputStream.readInt;
0581 tag.data.to = p_DataInputStream.readInt;
0582 rot = zeros(9,1);
0583 for i = 1:9
0584 rot(i) = p_DataInputStream.readFloat;
0585 end
0586 rot = reshape(rot,3,3)';
0587 move = zeros(3,1);
0588 for i = 1:3
0589 move(i) = p_DataInputStream.readFloat;
0590 end
0591 tag.data.trans = [ rot move ; [ 0 0 0 1 ]];
0592
0593
0594
0595
0596 for i = 1:12
0597 p_DataInputStream.readFloat;
0598 end
0599 case FIFF.FIFFT_CH_INFO_STRUCT
0600 tag.data.scanno = p_DataInputStream.readInt;
0601 tag.data.logno = p_DataInputStream.readInt;
0602 tag.data.kind = p_DataInputStream.readInt;
0603 tag.data.range = p_DataInputStream.readFloat;
0604 tag.data.cal = p_DataInputStream.readFloat;
0605 tag.data.coil_type = p_DataInputStream.readInt;
0606
0607
0608
0609 tag.data.loc = zeros(12,1);
0610 for i = 1:12
0611 tag.data.loc(i) = p_DataInputStream.readFloat;
0612 end
0613 tag.data.coil_trans = [];
0614 tag.data.eeg_loc = [];
0615 tag.data.coord_frame = FIFF.FIFFV_COORD_UNKNOWN;
0616
0617
0618
0619 loc = tag.data.loc;
0620 if tag.data.kind == FIFF.FIFFV_MEG_CH || tag.data.kind == FIFF.FIFFV_REF_MEG_CH
0621 tag.data.coil_trans = [ [ loc(4:6) loc(7:9) loc(10:12) loc(1:3) ] ; [ 0 0 0 1 ] ];
0622 tag.data.coord_frame = FIFF.FIFFV_COORD_DEVICE;
0623 elseif tag.data.kind == FIFF.FIFFV_EEG_CH
0624 if norm(loc(4:6)) > 0
0625 tag.data.eeg_loc = [ loc(1:3) loc(4:6) ];
0626 else
0627 tag.data.eeg_loc = [ loc(1:3) ];
0628 end
0629 tag.data.coord_frame = FIFF.FIFFV_COORD_HEAD;
0630 end
0631
0632
0633
0634 tag.data.unit = p_DataInputStream.readInt;
0635 tag.data.unit_mul = p_DataInputStream.readInt;
0636
0637
0638
0639 ch_name = zeros(1, 16, 'uint8');
0640 for i = 1:16
0641 ch_name(i) = p_DataInputStream.readByte;
0642 end
0643 ch_name = char(ch_name);
0644
0645
0646
0647 len = 16;
0648 for k = 1:16
0649 if ch_name(k) == 0
0650 len = k-1;
0651 break
0652 end
0653 end
0654 tag.data.ch_name = ch_name(1:len);
0655 otherwise
0656 error(me,'Unimplemented tag data type %d',tag.type);
0657
0658 end
0659 end
0660 end
0661
0662
0663
0664
0665
0666 return;
0667 end
0668
0669
0670
0671 function [tag] = read_tag_info(p_DataInputStream, pos)
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686 import java.net.Socket
0687 import java.io.*
0688
0689 global FIFF;
0690 if isempty(FIFF)
0691 FIFF = fiff_define_constants();
0692 end
0693
0694 me='MNE_RT_DATA_CLIENT:read_tag_info';
0695
0696 if nargin == 2
0697 p_DataInputStream.skipBytes(pos);
0698 elseif nargin ~= 1
0699 error(me,'Incorrect number of arguments');
0700 end
0701
0702
0703
0704
0705 tag.kind = p_DataInputStream.readInt;
0706 tag.type = p_DataInputStream.readInt;
0707 tag.size = p_DataInputStream.readInt;
0708 tag.next = p_DataInputStream.readInt;
0709
0710
0711
0712
0713
0714
0715 return;
0716 end
0717 end
0718 end
0719