MNE-CPP  beta 1.0
collectorsocket.cpp
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "collectorsocket.h"
42 
43 
44 //*************************************************************************************************************
45 //=============================================================================================================
46 // Qt INCLUDES
47 //=============================================================================================================
48 
49 #include <QtNetwork>
50 
51 
52 //*************************************************************************************************************
53 //=============================================================================================================
54 // USED NAMESPACES
55 //=============================================================================================================
56 
57 using namespace NeuromagPlugin;
58 
59 
60 //*************************************************************************************************************
61 //=============================================================================================================
62 // DEFINE MEMBER METHODS
63 //=============================================================================================================
64 
66 : QTcpSocket(parent)
67 , m_sCollectorHost(QHostAddress(QHostAddress::LocalHost).toString())
68 , m_bIsMeasuring(false)
69 {
70 
71 }
72 
73 //*************************************************************************************************************
74 
76 {
77  printf("About to connect to collector... ");
78  if(this->state() == QAbstractSocket::ConnectedState)
79  {
80  printf("Note: Tried to re-open an open connection... [done]\r\n");//dacq_log("Note: Tried to re-open an open connection\n");
81  return true;
82  }
83  this->abort();
84  this->connectToHost(m_sCollectorHost, COLLECTOR_PORT);
85  this->waitForConnected( -1 );//qDebug() << "Socket Stat: " << this->state();
86  printf("[done]\r\n");
87 
88  if (!this->server_login(QString(COLLECTOR_PASS), QString("mne_rt_server"))) {
89  printf("Neuromag collector connection: Error\r\n");//dacq_log("Neuromag collector connection: %s\n", err_get_error());
90  return false;
91  }
92 
93  return true;
94 }
95 
96 
97 //*************************************************************************************************************
98 
99 //int CollectorSocket::close()
100 //{
101 
102 // qDebug() << "DacqServer::collector_close()";
103 
104 // if (this == NULL)
105 // return(0);
110 
111 // this->waitForReadyRead(100);
112 
113 // this->close();
114 
116 // return(0);
117 //}
118 
119 
120 //*************************************************************************************************************
121 // ToDo doesn't work without setting first buffersize first
123 {
124 
125  int maxbuflen = -1;
126 
127  QString t_sSend = QString("%1\r\n").arg(COLLECTOR_GETVARS);
128 
129  QByteArray t_buf;
130  if (!this->server_send(t_sSend, t_buf, DACQ_KEEP_INPUT)) {
131  printf("Neuromag collector connection: Error\r\n");//dacq_log("Neuromag collector connection: %s\n", err_get_error());
132  return -1;
133  }
134 
135  QList<QByteArray> t_lLinesBuffer = t_buf.split('\n');
136 
137  QByteArray bufVar1(COLLECTOR_BUFVAR);
138  QByteArray bufVar2("Vectors in a buffer");
139  for(qint32 i = 0; i < t_lLinesBuffer.size(); ++i)
140  {
141  if(t_lLinesBuffer[i].contains(bufVar1)) //option 1
142  {
143  char var_name[1024];
144  char var_value[1024];
145  char var_type[1024];
146  sscanf(t_lLinesBuffer[i].data()+4, "%s %s %s", var_name, var_value, var_type);
147  maxbuflen = QString(var_value).toInt();
148  return maxbuflen;
149  }
150  else if(t_lLinesBuffer[i].contains(bufVar2)) //option 2
151  {
152  QList<QByteArray> t_lMaxBuflen = t_lLinesBuffer[i].split(':');
153  maxbuflen = t_lMaxBuflen[t_lMaxBuflen.size()-1].trimmed().toInt();
154  return maxbuflen;
155  }
156  }
157 
158  return -1;
159 }
160 
161 
162 //*************************************************************************************************************
163 
165 {
166  if (maxbuflen < 1)
167  return(0);
168 
169  if (!this->server_command(QString("%1 %2 %3\n").arg(COLLECTOR_SETVARS).arg(COLLECTOR_BUFVAR).arg(maxbuflen)) ||
170  !this->server_command(QString("%1\n").arg(COLLECTOR_DOSETUP)))
171  {
172  printf("Neuromag collector connection: Error\n");//dacq_log("Neuromag collector connection: %s\n", err_get_error());
173  return(-1);
174  }
175 
176  return(0);
177 }
178 
179 
180 //*************************************************************************************************************
181 
182 bool CollectorSocket::server_command(const QString& p_sCommand)
183 {
184 
185  if (this->state() != QAbstractSocket::ConnectedState)
186  return false;
187 
188  //ToDo Command Check
189  QByteArray t_arrCommand = p_sCommand.toLocal8Bit();
190 
191  if(t_arrCommand.size() > 0)
192  {
193  this->write(t_arrCommand);
194  this->flush();
195  }
196 
197  //ToDo check if command was succefull processed by the collector
198  this->waitForReadyRead(-1);
199  this->readAll();//readAll that QTcpSocket is empty again
200 
201  return true;
202 }
203 
204 
205 //*************************************************************************************************************
206 
207 bool CollectorSocket::server_login(const QString& p_sCollectorPass, const QString& p_sMyName)
208 {
209  printf("Login... ");
210 
211  QString t_sCommand = QString("%1 %2\r\n").arg(DACQ_CMD_PASSWORD).arg(p_sCollectorPass);
212  this->server_command(t_sCommand);
213 
214  t_sCommand = QString("%1 %2\r\n").arg(DACQ_CMD_NAME).arg(p_sMyName);
215  this->server_command(t_sCommand);
216 
217  printf("[done]\r\n");
218 
219  return true;
220 }
221 
222 
223 //*************************************************************************************************************
224 
225 bool CollectorSocket::server_send(QString& p_sDataSend, QByteArray& p_dataOut, int p_iInputFlag)
226 {
227  if (this->state() != QAbstractSocket::ConnectedState)
228  return false;
229 
230  QByteArray t_arrSend = p_sDataSend.toLocal8Bit();
231 
232  if(t_arrSend.size() > 0)
233  {
234  this->write(t_arrSend);
235  this->flush();
236  }
237 
238  this->waitForReadyRead(-1);
239  if ( p_iInputFlag == DACQ_DRAIN_INPUT )
240  {
241  this->readAll();//readAll that QTcpSocket is empty again -> prevent overflow
242  }
243  else if( p_iInputFlag == DACQ_KEEP_INPUT )
244  {
245  p_dataOut = this->readAll();//readAll that QTcpSocket is empty again -> prevent overflow
246  }
247 
248  return true;
249 }
250 
251 
252 //*************************************************************************************************************
253 
255 {
256  if(!m_bIsMeasuring)
257  {
258  printf("Start measurement... ");
259 
260  QString t_sCommand = QString("%1\r\n").arg("meas");
261  this->server_command(t_sCommand);
262 
263  m_bIsMeasuring = true;
264  }
265 
266  return true;
267 }
268 
269 
270 //*************************************************************************************************************
271 
273 {
274  if(m_bIsMeasuring)
275  {
276  printf("Stop measurement... ");
277 
278  QString t_sCommand = QString("%1\r\n").arg("stop");
279  this->server_command(t_sCommand);
280 
281  m_bIsMeasuring = false;
282  printf("[done]\r\n");
283  }
284 
285  return true;
286 }
287 
bool server_send(QString &p_sDataSend, QByteArray &p_dataOut, int p_iInputFlag=DACQ_DRAIN_INPUT)
bool server_command(const QString &p_sCommand)
bool server_login(const QString &p_sCollectorPass, const QString &p_sMyName)
implementation of the CollectorSocket Class.