MNE-CPP  beta 1.0
fiffsimulatorproducer.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "fiffsimulatorproducer.h"
42 #include "fiffsimulator.h"
43 
44 
45 //*************************************************************************************************************
46 //=============================================================================================================
47 // QT INCLUDES
48 //=============================================================================================================
49 
50 #include <QMutexLocker>
51 
52 //*************************************************************************************************************
53 //=============================================================================================================
54 // USED NAMESPACES
55 //=============================================================================================================
56 
57 using namespace FiffSimulatorPlugin;
58 
59 
60 //*************************************************************************************************************
61 //=============================================================================================================
62 // DEFINE MEMBER METHODS
63 //=============================================================================================================
64 
66 : m_pFiffSimulator(p_pFiffSimulator)
67 , m_pRtDataClient(0)
68 , m_bDataClientIsConnected(false)
69 , m_iDataClientId(-1)
70 , m_bFlagInfoRequest(false)
71 , m_bFlagMeasuring(false)
72 , m_bIsRunning(false)
73 {
74 }
75 
76 
77 //*************************************************************************************************************
78 
80 {
81 
82 }
83 
84 
85 //*************************************************************************************************************
86 
87 void FiffSimulatorProducer::connectDataClient(QString p_sRtSeverIP)
88 {
89  if(m_pRtDataClient.isNull())
90  m_pRtDataClient = QSharedPointer<RtDataClient>(new RtDataClient);
91  else if(m_bDataClientIsConnected)
92  return;
93 
94  m_pRtDataClient->connectToHost(p_sRtSeverIP);
95  m_pRtDataClient->waitForConnected(1000);
96 
97  if(m_pRtDataClient->state() == QTcpSocket::ConnectedState)
98  {
99  producerMutex.lock();
100  if(!m_bDataClientIsConnected)
101  {
102  //
103  // get client ID
104  //
105  m_iDataClientId = m_pRtDataClient->getClientId();
106 
107  //
108  // set data client alias -> for convinience (optional)
109  //
110  m_pRtDataClient->setClientAlias(m_pFiffSimulator->m_sFiffSimulatorClientAlias); // used in option 2 later on
111 
112  //
113  // set new state
114  //
115  m_bDataClientIsConnected = true;
116  emit dataConnectionChanged(m_bDataClientIsConnected);
117  }
118  producerMutex.unlock();
119  }
120 }
121 
122 
123 //*************************************************************************************************************
124 
126 {
127  if(m_bDataClientIsConnected)
128  {
129  m_pRtDataClient->disconnectFromHost();
130  m_pRtDataClient->waitForDisconnected();
131  producerMutex.lock();
132  m_iDataClientId = -1;
133  m_bDataClientIsConnected = false;
134  producerMutex.unlock();
135  emit dataConnectionChanged(m_bDataClientIsConnected);
136  }
137 }
138 
139 
140 //*************************************************************************************************************
141 
143 {
144  //Wait until this thread (Producer) is stopped
145  m_bIsRunning = false;
146  m_bFlagMeasuring = false;
147 
148  if(m_pFiffSimulator->m_bCmdClientIsConnected) //ToDo replace this with is running
149  {
150  // Stop Measurement at rt_Server
151  (*m_pFiffSimulator->m_pRtCmdClient)["stop-all"].send();
152  }
153 
154  if(m_pFiffSimulator->m_pRawMatrixBuffer_In)
155  {
156  //In case the semaphore blocks the thread -> Release the QSemaphore and let it exit from the push function (acquire statement)
157  m_pFiffSimulator->m_pRawMatrixBuffer_In->releaseFromPush();
158  }
159 
160  this->disconnectDataClient();
161 
162  //Check if the thread is already or still running. This can happen if the start button is pressed immediately after the stop button was pressed. In this case the stopping process is not finished yet but the start process is initiated.
163  if(this->isRunning())
164  QThread::wait();
165 }
166 
167 
168 //*************************************************************************************************************
169 
171 {
172  m_bIsRunning = true;
173  //
174  // Connect data client
175  //
176  this->connectDataClient(m_pFiffSimulator->m_sFiffSimulatorIP);
177 
178  qint32 count = 0;
179  while(m_pRtDataClient->state() != QTcpSocket::ConnectedState)
180  {
181  msleep(100);
182  this->connectDataClient(m_pFiffSimulator->m_sFiffSimulatorIP);
183  ++count;
184  if(count > 10 || !m_bIsRunning)
185  return;
186  }
187 
188  msleep(1000);
189 
190  m_bFlagMeasuring = true;
191 
192  //
193  // Inits
194  //
195  MatrixXf t_matRawBuffer;
196 
197  fiff_int_t kind;
198 
199  qint32 from = 0;
200  qint32 to = -1;
201 
202  while(true)
203  {
204  {
205  QMutexLocker locker(&producerMutex);
206  if(!m_bIsRunning)
207  break;
208  }
209 
210  producerMutex.lock();
211  if(m_bFlagInfoRequest)
212  {
213  m_pFiffSimulator->m_qMutex.lock();
214  m_pFiffSimulator->m_pFiffInfo = m_pRtDataClient->readInfo();
215  emit m_pFiffSimulator->fiffInfoAvailable();
216  m_pFiffSimulator->m_qMutex.unlock();
217 
218  m_bFlagInfoRequest = false;
219  }
220  producerMutex.unlock();
221 
222  if(m_bFlagMeasuring)
223  {
224  m_pRtDataClient->readRawBuffer(m_pFiffSimulator->m_pFiffInfo->nchan, t_matRawBuffer, kind);
225 
226  if(kind == FIFF_DATA_BUFFER)
227  {
228  to += t_matRawBuffer.cols();
229  from += t_matRawBuffer.cols();
230  m_pFiffSimulator->m_pRawMatrixBuffer_In->push(&t_matRawBuffer);
231  }
232  else if(FIFF_DATA_BUFFER == FIFF_BLOCK_END)
233  m_bFlagMeasuring = false;
234  }
235  }
236 }
Contains the declaration of the FiffSimulatorProducer class.
Real-time data client.
Definition: rtdataclient.h:92
FiffSimulatorProducer(FiffSimulator *p_pFiffSimulator)
#define FIFF_DATA_BUFFER
The FiffSimulator class provides a Fiff data simulator.
Definition: fiffsimulator.h:97