MNE-CPP  beta 1.0
rtclient.cpp
Go to the documentation of this file.
1 //=============================================================================================================
38 //*************************************************************************************************************
39 //=============================================================================================================
40 // INCLUDES
41 //=============================================================================================================
42 
43 #include "rtclient.h"
44 #include "rtcmdclient.h"
45 #include "rtdataclient.h"
46 
47 
48 //*************************************************************************************************************
49 //=============================================================================================================
50 // USED NAMESPACES
51 //=============================================================================================================
52 
53 using namespace RTCLIENTLIB;
54 
55 
56 //*************************************************************************************************************
57 //=============================================================================================================
58 // DEFINE MEMBER METHODS
59 //=============================================================================================================
60 
61 RtClient::RtClient(QString p_sRtServerHostname, QString p_sClientAlias, QObject *parent)
62 : QThread(parent)
63 , m_bIsConnected(false)
64 , m_bIsMeasuring(false)
65 , m_bIsRunning(false)
66 , m_sClientAlias(p_sClientAlias)
67 , m_sRtServerHostName(p_sRtServerHostname)
68 {
69 }
70 
71 
72 //*************************************************************************************************************
73 
75 {
76  stop();
77 }
78 
79 
80 //*************************************************************************************************************
81 
83 {
84  return m_bIsConnected;
85 }
86 
87 
88 //*************************************************************************************************************
89 
91 {
92  m_bIsRunning = false;
93  QThread::wait();
94 
95  return true;
96 }
97 
98 
99 //*************************************************************************************************************
100 
102 {
103  m_bIsRunning = true;
104 
105  //
106  // Connect Clients
107  //
108  RtCmdClient t_cmdClient;
109  t_cmdClient.connectToHost(m_sRtServerHostName);
110  t_cmdClient.waitForConnected(1000);
111 
112  while(t_cmdClient.state() != QTcpSocket::ConnectedState)
113  {
114  msleep(100);
115  t_cmdClient.connectToHost(m_sRtServerHostName);
116  t_cmdClient.waitForConnected(1000);
117  }
118 
119  RtDataClient t_dataClient;
120  t_dataClient.connectToHost(m_sRtServerHostName);
121  t_dataClient.waitForConnected();
122 
123  mutex.lock();
124  m_bIsConnected = true;
125  mutex.unlock();
126 
127  emit connectionChanged(m_bIsConnected);
128 
129  msleep(1000);
130 
131  //
132  // get client ID
133  //
134  qint32 clientId = t_dataClient.getClientId();
135 
136  //
137  // request available commands
138  //
139  t_cmdClient.requestCommands();
140 
141  //
142  // Inits
143  //
144  MatrixXf t_matRawBuffer;
145 
146  fiff_int_t kind;
147 
148  qint32 from = 0;
149  qint32 to = -1;
150 
151  // set data client alias -> for convinience (optional)
152  t_dataClient.setClientAlias(m_sClientAlias); // used in option 2 later on
153 
154 // // example commands
155 // t_cmdClient["help"].send();
156 // t_cmdClient.waitForDataAvailable(1000);
157 // qDebug() << t_cmdClient.readAvailableData();
158 // t_cmdClient["clist"].send();
159 // t_cmdClient.waitForDataAvailable(1000);
160 // qDebug() << t_cmdClient.readAvailableData();
161 // t_cmdClient["conlist"].send();
162 // t_cmdClient.waitForDataAvailable(1000);
163 // qDebug() << t_cmdClient.readAvailableData();
164 
165 
166  // read meas info
167  t_cmdClient["measinfo"].pValues()[0].setValue(clientId);
168  t_cmdClient["measinfo"].send();
169 
170  m_pFiffInfo = t_dataClient.readInfo();
171 
172  // start measurement
173  t_cmdClient["start"].pValues()[0].setValue(clientId);
174  t_cmdClient["start"].send();
175 
176  while(m_bIsRunning)
177  {
178 
179 // while(m_bIsMeasuring)
180 
181 
182  t_dataClient.readRawBuffer(m_pFiffInfo->nchan, t_matRawBuffer, kind);
183 
184  if(kind == FIFF_DATA_BUFFER)
185  {
186  to += t_matRawBuffer.cols();
187  printf("Reading %d ... %d = %9.3f ... %9.3f secs...", from, to, ((float)from)/m_pFiffInfo->sfreq, ((float)to)/m_pFiffInfo->sfreq);
188  from += t_matRawBuffer.cols();
189 
190  emit rawBufferReceived(t_matRawBuffer);
191  }
192  else if(FIFF_DATA_BUFFER == FIFF_BLOCK_END)
193  m_bIsRunning = false;
194 
195  printf("[done]\n");
196  }
197 
198  //
199  // Disconnect Stuff
200  //
201  t_cmdClient.disconnectFromHost();
202  t_dataClient.disconnectFromHost();
203 
204  mutex.lock();
205  m_bIsConnected = false;
206  mutex.unlock();
207 
208  emit connectionChanged(m_bIsConnected);
209 }
void readRawBuffer(qint32 p_nChannels, MatrixXf &data, fiff_int_t &kind)
Real-time data client.
Definition: rtdataclient.h:92
void connectToHost(QString &p_sRtServerHostName)
Definition: rtcmdclient.cpp:79
void setClientAlias(const QString &p_sAlias)
Real-time command client.
Definition: rtcmdclient.h:86
bool getConnectionStatus()
Definition: rtclient.cpp:82
void rawBufferReceived(Eigen::MatrixXf p_rawBuffer)
virtual bool stop()
Definition: rtclient.cpp:90
virtual void run()
Definition: rtclient.cpp:101
FiffInfo::SPtr readInfo()
declaration of the RtCmdClient Class.
#define FIFF_DATA_BUFFER
RtClient(QString p_sRtServerHostname, QString p_sClientAlias="rtclient", QObject *parent=0)
Definition: rtclient.cpp:61
void connectionChanged(bool p_bStatus)
declaration of the RtDataClient Class.
declaration of the RtClient Class.
void connectToHost(const QString &p_sRtServerHostName)
virtual void disconnectFromHost()