MNE-CPP  beta 0.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mnertclientproducer.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "mnertclientproducer.h"
42 #include "mnertclient.h"
43 
44 
45 //*************************************************************************************************************
46 //=============================================================================================================
47 // USED NAMESPACES
48 //=============================================================================================================
49 
50 using namespace MneRtClientPlugin;
51 
52 
53 //*************************************************************************************************************
54 //=============================================================================================================
55 // QT INCLUDES
56 //=============================================================================================================
57 
59 : m_pMneRtClient(p_pMneRtClient)
60 , m_pRtDataClient(0)
61 , m_bDataClientIsConnected(false)
62 , m_iDataClientId(-1)
63 , m_bFlagInfoRequest(false)
64 , m_bFlagMeasuring(false)
65 {
66 }
67 
68 
69 //*************************************************************************************************************
70 
72 {
73 
74 }
75 
76 
77 //*************************************************************************************************************
78 
79 void MneRtClientProducer::connectDataClient(QString p_sRtSeverIP)
80 {
81  if(m_pRtDataClient.isNull())
82  m_pRtDataClient = QSharedPointer<RtDataClient>(new RtDataClient);
83  else if(m_bDataClientIsConnected)
84  this->disconnectDataClient();
85 
86  m_pRtDataClient->connectToHost(p_sRtSeverIP);
87  m_pRtDataClient->waitForConnected(1000);
88 
89  if(m_pRtDataClient->state() == QTcpSocket::ConnectedState)
90  {
91  producerMutex.lock();
92  if(!m_bDataClientIsConnected)
93  {
94  //
95  // get client ID
96  //
97  m_iDataClientId = m_pRtDataClient->getClientId();
98 
99  //
100  // set data client alias -> for convinience (optional)
101  //
102  m_pRtDataClient->setClientAlias(m_pMneRtClient->m_sMneRtClientClientAlias); // used in option 2 later on
103 
104  //
105  // set new state
106  //
107  m_bDataClientIsConnected = true;
108  emit dataConnectionChanged(m_bDataClientIsConnected);
109  }
110  producerMutex.unlock();
111  }
112 }
113 
114 
115 //*************************************************************************************************************
116 
118 {
119  if(m_bDataClientIsConnected)
120  {
121  m_pRtDataClient->disconnectFromHost();
122  m_pRtDataClient->waitForDisconnected();
123  producerMutex.lock();
124  m_iDataClientId = -1;
125  m_bDataClientIsConnected = false;
126  producerMutex.unlock();
127  emit dataConnectionChanged(m_bDataClientIsConnected);
128  }
129 }
130 
131 
132 //*************************************************************************************************************
133 
135 {
136  m_bIsRunning = false;
137  QThread::terminate();
138  QThread::wait();
139 }
140 
141 
142 //*************************************************************************************************************
143 
145 {
146  //
147  // Connect data client
148  //
149  this->connectDataClient(m_pMneRtClient->m_sMneRtClientIP);
150 
151  while(m_pRtDataClient->state() != QTcpSocket::ConnectedState)
152  {
153  msleep(100);
154  this->connectDataClient(m_pMneRtClient->m_sMneRtClientIP);
155  }
156 
157  msleep(1000);
158 
159  m_bIsRunning = true;
160 
161  //
162  // Inits
163  //
164  MatrixXf t_matRawBuffer;
165 
166  fiff_int_t kind;
167 
168  qint32 from = 0;
169  qint32 to = -1;
170 
171  while(m_bIsRunning)
172  {
173  if(m_bFlagInfoRequest)
174  {
175  m_pMneRtClient->rtServerMutex.lock();
176  m_pMneRtClient->m_pFiffInfo = m_pRtDataClient->readInfo();
177  emit m_pMneRtClient->fiffInfoAvailable();
178  m_pMneRtClient->rtServerMutex.unlock();
179 
180  producerMutex.lock();
181  m_bFlagInfoRequest = false;
182  producerMutex.unlock();
183  }
184 
185  if(m_bFlagMeasuring)
186  {
187 // QByteArray buffer;
188 // QByteArray dat;
189 
190 // int numBytes = m_pRtDataClient->bytesAvailable();
191 // qDebug() << "MNE_X. byte available: " << numBytes;
192 // if (numBytes > 0){
193 // dat = m_pRtDataClient->read(numBytes); // read all pending data
194 // qDebug()<<"[dat Size]"<<dat.size();
195 // if (!dat.isEmpty()){
196 // buffer.append(dat); // and append it to your own buffer
197 // qDebug()<<"[ReadToBuffer: Buffer Size]"<<buffer.size();
198 // }
199 // else
200 // {
201 // qDebug()<<"[Empty dat: error]";
202 // }
203 // }
204 
205  m_pRtDataClient->readRawBuffer(m_pMneRtClient->m_pFiffInfo->nchan, t_matRawBuffer, kind);
206 
207  if(kind == FIFF_DATA_BUFFER)
208  {
209  to += t_matRawBuffer.cols();
210 // printf("Reading %d ... %d = %9.3f ... %9.3f secs...", from, to, ((float)from)/m_pMneRtClient->m_pFiffInfo->sfreq, ((float)to)/m_pMneRtClient->m_pFiffInfo->sfreq);
211  from += t_matRawBuffer.cols();
212 
213  m_pMneRtClient->m_pRawMatrixBuffer_In->push(&t_matRawBuffer);
214  }
215  else if(FIFF_DATA_BUFFER == FIFF_BLOCK_END)
216  m_bFlagMeasuring = false;
217 
218 // printf("[done]\n");
219  }
220  }
221 }
Real-time data client.
Definition: rtdataclient.h:92
void connectDataClient(QString p_sRtSeverIP)
MneRtClientProducer(MneRtClient *p_pMneRtClient)
The MneRtClient class provides a RT server connection.
Definition: mnertclient.h:115
Contains the declaration of the RTServerProducer class.
Contains the declaration of the MneRtClient class.
void dataConnectionChanged(bool p_bStatus)
#define FIFF_DATA_BUFFER