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