MNE-CPP  beta 1.0
fiffstreamserver.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "fiffstreamserver.h"
42 #include "fiffstreamthread.h"
43 
44 #include "mne_rt_server.h"
45 
46 
47 //*************************************************************************************************************
48 //=============================================================================================================
49 // STL INCLUDES
50 //=============================================================================================================
51 
52 #include <stdlib.h>
53 
54 
55 //*************************************************************************************************************
56 //=============================================================================================================
57 // USED NAMESPACES
58 //=============================================================================================================
59 
60 using namespace RTSERVER;
61 using namespace FIFFLIB;
62 
63 
64 //*************************************************************************************************************
65 //=============================================================================================================
66 // DEFINE MEMBER METHODS
67 //=============================================================================================================
68 
69 FiffStreamServer::FiffStreamServer(QObject *parent)
70 : QTcpServer(parent)
71 , m_iNextClientId(0)
72 {
73 
74 }
75 
76 
77 //*************************************************************************************************************
78 
80 {
81  emit closeFiffStreamServer();
82 }
83 
84 
85 //*************************************************************************************************************
86 
87 void FiffStreamServer::comClist(Command p_command)
88 {
89  //ToDo JSON
90  QString t_sOutput("");
91  t_sOutput.append("\tID\tAlias\r\n");
92  QMap<qint32, FiffStreamThread*>::iterator i;
93  for (i = this->m_qClientList.begin(); i != this->m_qClientList.end(); ++i)
94  {
95  QString str = QString("\t%1\t%2\r\n").arg(i.key()).arg(i.value()->getAlias());
96  t_sOutput.append(str);
97  }
98  t_sOutput.append("\n");
99  qobject_cast<MNERTServer*>(this->parent())->getCommandManager()["clist"].reply(t_sOutput);
100 
101  Q_UNUSED(p_command);
102 }
103 
104 
105 //*************************************************************************************************************
106 
107 void FiffStreamServer::comMeasinfo(Command p_command)
108 {
109  qint32 t_id = -1;
110 // p_blockOutputInfo.append(parseToId(p_sListCommand[1],t_id));
111 
112  t_id = p_command.pValues()[0].toInt();
113 
114  if(t_id != -1)
115  {
116  emit requestMeasInfo(t_id);//requestMeasInfo(t_id);
117 
118  QString str = QString("\tsend measurement info to FiffStreamClient (ID: %1)\r\n\n").arg(t_id);
119  qobject_cast<MNERTServer*>(this->parent())->getCommandManager()["measinfo"].reply(str);
120  }
121 }
122 
123 
124 //*************************************************************************************************************
125 
126 void FiffStreamServer::comStart(Command p_command)
127 {
128  qint32 t_id = -1;
129  QString t_sOutput("");
130  QString t_sAlias(p_command.pValues()[0].toString());
131  t_sOutput.append(parseToId(t_sAlias,t_id));
132 
133  if(t_id != -1)
134  {
135  emit startMeasFiffStreamClient(t_id);
136 
137  QString str = QString("\tFiffStreamClient (ID: %1) is now set to accept raw buffers\r\n\n").arg(t_id);
138  t_sOutput.append(str);
139  }
140  qobject_cast<MNERTServer*>(this->parent())->getCommandManager()["start"].reply(t_sOutput);
141 }
142 
143 
144 //*************************************************************************************************************
145 
146 void FiffStreamServer::comStop(Command p_command)
147 {
148  qint32 t_id = -1;
149  QString t_sOutput("");
150  QString t_sAlias(p_command.pValues()[0].toString());
151  t_sOutput.append(parseToId(t_sAlias,t_id));
152 
153  if(t_id != -1)
154  {
155  emit stopMeasFiffStreamClient(t_id);//emit requestStopMeas(t_id);
156 
157  QString str = QString("\tstop FiffStreamClient (ID: %1) from receiving raw Buffers.\r\n\n").arg(t_id);
158  t_sOutput.append(str);
159  }
160  qobject_cast<MNERTServer*>(this->parent())->getCommandManager()["stop"].reply(t_sOutput);
161 }
162 
163 
164 //*************************************************************************************************************
165 
166 void FiffStreamServer::comStopAll(Command p_command)
167 {
168  emit stopMeasFiffStreamClient(-1);
169  QString str = QString("\tstop all FiffStreamClients from receiving raw buffers\r\n\n");
170  qobject_cast<MNERTServer*>(this->parent())->getCommandManager()["stop-all"].reply(str);
171 
172  Q_UNUSED(p_command);
173 }
174 
175 
176 //*************************************************************************************************************
177 
179 {
180  //Connect slots
181  MNERTServer* t_pMNERTServer = qobject_cast<MNERTServer*> (this->parent());
182 
183  QObject::connect(&t_pMNERTServer->getCommandManager()["clist"], &Command::executed, this, &FiffStreamServer::comClist);
184  QObject::connect(&t_pMNERTServer->getCommandManager()["measinfo"], &Command::executed, this, &FiffStreamServer::comMeasinfo);
185  QObject::connect(&t_pMNERTServer->getCommandManager()["start"], &Command::executed, this, &FiffStreamServer::comStart);
186  QObject::connect(&t_pMNERTServer->getCommandManager()["stop"], &Command::executed, this, &FiffStreamServer::comStop);
187  QObject::connect(&t_pMNERTServer->getCommandManager()["stop-all"], &Command::executed, this, &FiffStreamServer::comStopAll);
188 
189 // t_pMNERTServer->getCommandManager().connectSlot(QString("clist"), this, &FiffStreamServer::comClist);
190 // t_pMNERTServer->getCommandManager().connectSlot(QString("measinfo"), this, &FiffStreamServer::comMeasinfo);
191 // t_pMNERTServer->getCommandManager().connectSlot(QString("start"), this, &FiffStreamServer::comStart);
192 // t_pMNERTServer->getCommandManager().connectSlot(QString("stop"), this, &FiffStreamServer::comStop);
193 // t_pMNERTServer->getCommandManager().connectSlot(QString("stop-all"), this, &FiffStreamServer::comStopAll);
194 }
195 
196 
198 
199 //bool FiffStreamServer::parseCommand(QStringList& p_sListCommand, QByteArray& p_blockOutputInfo)
200 //{
201 // bool success = false;
202 
203 // if(p_sListCommand[0].compare("clist",Qt::CaseInsensitive) == 0)
204 // {
205 // //
206 // // client list
207 // //
208 // printf("clist\n");
209 
210 // p_blockOutputInfo.append("\tID\tAlias\r\n");
211 // QMap<qint32, FiffStreamThread*>::iterator i;
212 // for (i = this->m_qClientList.begin(); i != this->m_qClientList.end(); ++i)
213 // {
214 // QString str = QString("\t%1\t%2\r\n").arg(i.key()).arg(i.value()->getAlias());
215 // p_blockOutputInfo.append(str);
216 // }
217 // p_blockOutputInfo.append("\n");
218 // success = true;
219 // }
220 // else if(p_sListCommand[0].compare("measinfo",Qt::CaseInsensitive) == 0)
221 // {
222 // //
223 // // Measurement Info
224 // //
225 // if(p_sListCommand.size() > 1)
226 // {
227 // qint32 t_id = -1;
229 
230 // bool t_isInt;
231 // t_id = p_sListCommand[1].toInt(&t_isInt);
232 
233 // if(t_isInt)// ToDo Check whether ID is correct --> move this parsing to fiff stream server
234 // {
235 // printf("measinfo %d\r\n", t_id);
236 // }
237 // if(t_id != -1)
238 // {
239 // emit requestMeasInfo(t_id);//requestMeasInfo(t_id);
240 
241 // QString str = QString("\tsend measurement info to FiffStreamClient (ID: %1)\r\n\n").arg(t_id);
242 // p_blockOutputInfo.append(str);
243 // success = true;
244 // }
245 // }
246 // }
247 // else if(p_sListCommand[0].compare("meas",Qt::CaseInsensitive) == 0)
248 // {
249 // //
250 // // meas
251 // //
252 // if(p_sListCommand.size() > 1)
253 // {
254 // qint32 t_id = -1;
255 // p_blockOutputInfo.append(parseToId(p_sListCommand[1],t_id));
256 
257 // printf("meas %d\n", t_id);
258 // if(t_id != -1)
259 // {
260 // emit startMeasFiffStreamClient(t_id);
261 
262 // QString str = QString("\tFiffStreamClient (ID: %1) is now set to accept raw buffers\r\n\n").arg(t_id);
263 // p_blockOutputInfo.append(str);
264 // }
265 // }
266 // success = true;
267 // }
268 // else if(p_sListCommand[0].compare("stop",Qt::CaseInsensitive) == 0)
269 // {
270 // //
271 // // stop
272 // //
273 
274 // if(p_sListCommand.size() > 1)
275 // {
276 // qint32 t_id = -1;
277 // p_blockOutputInfo.append(parseToId(p_sListCommand[1],t_id));
278 
279 // printf("stop %d\n", t_id);
280 
281 // if(t_id != -1)
282 // {
283 // emit stopMeasFiffStreamClient(t_id);//emit requestStopMeas(t_id);
284 
285 // QString str = QString("\tstop FiffStreamClient (ID: %1) from receiving raw Buffers.\r\n\n").arg(t_id);
286 // p_blockOutputInfo.append(str);
287 // }
288 // }
289 // success = true;
290 // }
291 // else if(p_sListCommand[0].compare("stop-all",Qt::CaseInsensitive) == 0)
292 // {
293 // //
294 // // stop-all
295 // //
296 // emit stopMeasFiffStreamClient(-1);
297 
298 // QString str = QString("\tstop all FiffStreamClients from receiving raw buffers\r\n\n");
299 // p_blockOutputInfo.append(str);
300 
301 // success = true;
302 // }
303 
304 // return success;
305 //}
306 
307 
308 //*************************************************************************************************************
309 
310 QByteArray FiffStreamServer::parseToId(QString& p_sRawId, qint32& p_iParsedId)
311 {
312  p_iParsedId = -1;
313  QByteArray t_blockCmdIdInfo;
314  if(!p_sRawId.isEmpty())
315  {
316  bool t_isInt;
317  qint32 t_id = p_sRawId.toInt(&t_isInt);
318 
319  if(t_isInt && this->m_qClientList.contains(t_id))
320  {
321  p_iParsedId = t_id;
322  }
323  else
324  {
325  QMap<qint32, FiffStreamThread*>::iterator i;
326  for (i = this->m_qClientList.begin(); i != this->m_qClientList.end(); ++i)
327  {
328  if(i.value()->getAlias().compare(p_sRawId) == 0)
329  {
330  p_iParsedId = i.key();
331  QString str = QString("\tconvert alias '%1' => %2\r\n").arg(i.value()->getAlias()).arg(i.key());
332  t_blockCmdIdInfo.append(str);
333  break;
334  }
335  }
336  }
337  }
338 
339  if(p_iParsedId != -1)
340  {
341  QString str = QString("\tselect FiffStreamClient %1\r\n").arg(p_iParsedId);
342  t_blockCmdIdInfo.append(str);
343  }
344  else
345  {
346  t_blockCmdIdInfo.append("\twarning: requested FiffStreamClient not available\r\n\n");
347  }
348 
349  return t_blockCmdIdInfo;
350 }
351 
352 
353 //*************************************************************************************************************
354 
355 //void FiffStreamServer::clearClients()
356 //{
357 // QMap<qint32, FiffStreamThread*>::const_iterator i = m_qClientList.constBegin();
358 // while (i != m_qClientList.constEnd()) {
359 // if(i.value())
360 // delete i.value();
361 // ++i;
362 // }
363 // m_qClientList.clear();
364 //}
365 
366 
367 //*************************************************************************************************************
368 
369 void FiffStreamServer::forwardMeasInfo(qint32 ID, FiffInfo p_fiffInfo)
370 {
371  emit remitMeasInfo(ID, p_fiffInfo);
372 }
373 
374 
375 //*************************************************************************************************************
376 //ToDo increase preformance --> try inline
377 void FiffStreamServer::forwardRawBuffer(QSharedPointer<Eigen::MatrixXf> m_pMatRawData)
378 {
379  emit remitRawBuffer(m_pMatRawData);
380 }
381 
382 
383 //*************************************************************************************************************
384 
385 void FiffStreamServer::incomingConnection(qintptr socketDescriptor)
386 {
387  FiffStreamThread* t_pStreamThread = new FiffStreamThread(m_iNextClientId, socketDescriptor, this);
388 
389  m_qClientList.insert(m_iNextClientId, t_pStreamThread);
390  ++m_iNextClientId;
391 
392  //when thread has finished it gets deleted
393  connect(t_pStreamThread, SIGNAL(finished()), t_pStreamThread, SLOT(deleteLater()));
394  connect(this, SIGNAL(closeFiffStreamServer()), t_pStreamThread, SLOT(deleteLater()));
395 
396  t_pStreamThread->start();
397 }
FIFF measurement file information.
Definition: fiff_info.h:96
void forwardMeasInfo(qint32 ID, FiffInfo p_fiffInfo)
CommandManager & getCommandManager()
The MNERTServer class provides a Fiff data simulator.
Definition: mne_rt_server.h:92
implementation of the FiffStreamServer Class.
implementation of the MNERTServer Class.
implementation of the FiffStreamThread Class.
QList< QVariant > & pValues()
Definition: command.h:370
Definition: fiff.h:98
void executed(Command p_command)