8#include <QHyodaMachine.h>
9#include <QHyodaToolMesh.h>
10#include <QHyodaToolMatrix.h>
11#include <QHyodaPapi.h>
19, tcpServerConnection(nullptr)
20, iceWdth((job->iceWidthHeight>>16)&0xFFFFul)
21, iceHght((job->iceWidthHeight)&0xFFFFul)
22, tcpImageBytes(iceWdth*iceHght*sizeof(uint32_t))
30 connect(&tcpServer, SIGNAL(newConnection()),
this, SLOT(acceptConnection()));
31 while (!tcpServer.isListening() &&
32 !tcpServer.listen(QHostAddress::Any,job->tcpPort));
33 qDebug() <<
"\33[37m[QHyodaTcp::QHyodaTcp] Listening @ port" << tcpServer.serverPort()<<
"\33[m";
39QHyodaTcp::~QHyodaTcp(){
40 qDebug() <<
"\33[37m[QHyodaTcp::~QHyodaTcp]\33[m";
41 if (tcpServerConnection){
42 tcpServerConnection->close();
43 delete tcpServerConnection;
53void QHyodaTcp::acceptConnection(){
55 qDebug() <<
"\33[37m[QHyodaTcp::acceptConnection] NEW Connection detected!\33[m";
56 tcpServerConnection = tcpServer.nextPendingConnection();
59 connect(tcpServerConnection, SIGNAL(readyRead()),
this,SLOT(tcpReadyRead()));
60 connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),
61 this, SLOT(displayError(QAbstractSocket::SocketError)));
68void QHyodaTcp::tcpSleeping(){
71 if (tcpServerConnection->bytesAvailable() < 8)
return;
74 if (tcpServerConnection->read(pktHeader,8)!=8)
75 qFatal(
"\33[37m[QHyodaTcp::tcpReadyRead] Could not fetch packet header!\33[m");
77 tcpPacketLength=*(
unsigned int*)&pktHeader[4];
80 if (*(
unsigned int*)&pktHeader[0]==0xe73b2e9cul) state=QHyodaTcp::HandShake;
81 if (*(
unsigned int*)&pktHeader[0]==0xca6cd6f0ul) state=QHyodaTcp::VariableName;
82 if (*(
unsigned int*)&pktHeader[0]==0xcbce69bcul) state=QHyodaTcp::MeshIceTHeader;
83 if (*(
unsigned int*)&pktHeader[0]==0x73491278ul) state=QHyodaTcp::MeshIceTImage;
84 if (*(
unsigned int*)&pktHeader[0]==0xb80dd1a3ul) state=QHyodaTcp::Papi;
85 if (*(
unsigned int*)&pktHeader[0]==0x78f78f67ul) state=QHyodaTcp::MatrixIceTHeader;
86 if (*(
unsigned int*)&pktHeader[0]==0x2cd5e780ul) state=QHyodaTcp::MatrixIceTImage;
94void QHyodaTcp::tcpHandShake(){
97 const int tcpPacketLengthLeft=tcpPacketLength-8;
98 if (tcpServerConnection->bytesAvailable() < tcpPacketLengthLeft)
return;
99 qDebug()<<
"\33[37m[QHyodaTcp::tcpHandShake] QHyodaTcp::HandShake\33[m";
101 qDebug()<<
"\33[37m[QHyodaTcp::tcpHandShake] @ HandShake: On recupere le pid\33[m";
102 if (tcpServerConnection->read(data,4)!=4)
103 qFatal(
"\33[37m[QHyodaTcp::tcpHandShake] Could not fetch pid!\33[m");
104 pid=*(
unsigned int*)&data[0];
105 qDebug()<<
"\33[37m[QHyodaTcp::tcpHandShake] \33[7m@ HandShake: pid="<<pid<<
"\33[m";
106 job->pid=QString(
"%1").arg(pid);
108 if (tcpServerConnection->read(data,4)!=4)
109 qFatal(
"\33[37m[QHyodaTcp::tcpHandShake] Could not fetch hostname len!\33[m");
110 unsigned int len=*(
unsigned int*)&data[0];
111 qDebug()<<
"\33[37m[QHyodaTcp::tcpHandShake] @ HandShake: hostname's len="<<
len<<
"\33[m";
112 if (tcpServerConnection->read(data,len)!=len)
113 qFatal(
"\33[37m[QHyodaTcp] Could not fetch hostname!\33[m");
114 qDebug()<<
"\33[37m[QHyodaTcp::tcpHandShake] \33[7m@ HandShake: rankZero is"<<data<<
"\33[m";
115 job->host=QString(data);
117 if (tcpServerConnection->read(data,4)!=4)
118 qFatal(
"\33[37m[QHyodaTcp::tcpHandShake] Could not fetch command line len!\33[m");
119 len=*(
unsigned int*)&data[0];
120 qDebug()<<
"\33[37m[QHyodaTcp::tcpHandShake] @ HandShake: COMMAND LINE's len="<<
len<<
"\33[m";
121 if (tcpServerConnection->read(data,len)!=len)
122 qFatal(
"\33[37m[QHyodaTcp] Could not fetch COMMAND LINE!\33[m");
123 qDebug()<<
"\33[37m[QHyodaTcp::tcpHandShake] \33[7m@ HandShake: COMMAND LINE is"<<data<<
"\33[m";
124 job->broadcasted_cmdline=QString(data);
125 job->has_been_broadcasted=
true;
127 if (tcpServerConnection->read(data,4)!=4)
128 qFatal(
"\33[37m[QHyodaTcp::tcpHandShake] Could not fetch SLURM_JOB_ID!\33[m");
129 job->id=*(
unsigned int*)&data[0];
130 qDebug()<<
"\33[37m[QHyodaTcp::tcpHandShake] \33[7m@ HandShake: SLURM_JOB_ID is"<<job->id<<
"\33[m";
132 sendAcknowledgePacket();
134 state=QHyodaTcp::Sleeping;
140void QHyodaTcp::tcpVariableName(){
141 char varName[4*1024];
142 const int tcpPacketLengthLeft=tcpPacketLength-8;
143 qDebug()<<
"\33[37m[QHyodaTcp::tcpVariableName] state @ QHyodaTcp::\33[7mVariableName\33[m";
145 if (tcpServerConnection->bytesAvailable() < tcpPacketLengthLeft)
return;
146 qDebug()<<
"\33[37m[QHyodaTcp::tcpVariableName] QHyodaTcp::VariableName\33[m";
148 if (tcpServerConnection->read(varName,tcpPacketLengthLeft)!=tcpPacketLengthLeft)
149 qFatal(
"\33[37m[QHyodaTcp::tcpVariableName] Could not fetch variable name!\33[m");
150 for(
int offset=0;offset<tcpPacketLengthLeft;){
152 unsigned int var_name_len=*(
unsigned int*)&varName[offset];
156 qDebug()<<
"\33[37m\t[QHyodaTcp::tcpVariableName] Variable \33[7m"<<&varName[offset]<<
"\33[m";
158 *job->arcane_variables_names<<&varName[offset];
161 offset+=var_name_len;
164 state=QHyodaTcp::Sleeping;
166 qDebug()<<
"\33[37m[QHyodaTcp::tcpVariableName] Maintenant qu'on a tout recupere, on accroche GDB\33[m";
168 job->gdbserver_hook();
171 sendAcknowledgePacket();
177void QHyodaTcp::tcpMeshIceTHeader(){
179 sendAcknowledgePacket();
180 state=QHyodaTcp::MeshIceTImage;
186void QHyodaTcp::tcpMeshIceTImage(){
188 if (tcpServerConnection->bytesAvailable() < tcpImageBytes)
return;
191 if (byteArray)
delete byteArray;
192 byteArray=
new QByteArray(tcpServerConnection->read(tcpImageBytes));
193 sendAcknowledgePacket();
196 double pov[6]={0.,0.,0.,0.,0.,0.};
197 if (job->topRightTools->mesh!=NULL){
198 job->topRightTools->mesh->ice->sxyz(&pov[0]);
199 pov[4]=job->topRightTools->mesh->variablesComboBox->currentIndex();
200 pov[5]=job->topRightTools->mesh->hPluginComboBox->currentIndex();
202 sendPacket((
char*)&pov[0],8*(4+1+1));
203 recvAcknowledgePacket();
205 state=QHyodaTcp::Sleeping;
207 if (job->topRightTools->mesh==NULL)
return;
209 if (job->meshButton->isEnabled())
return;
211 if (image!=NULL)
delete image;
212 image=
new QImage((uchar*)byteArray->data(), iceWdth, iceHght, QImage::Format_ARGB32);
213 job->topRightTools->mesh->ice->setImage(image);
221void QHyodaTcp::tcpPapi(){
222 const int bytesAvailable=tcpServerConnection->bytesAvailable();
223 if (bytesAvailable < 8)
return;
227 if (job->bottomRightTools->papi)
228 job->bottomRightTools->papi->update(
new QByteArray(tcpServerConnection->read(bytesAvailable)));
229 sendAcknowledgePacket();
230 state=QHyodaTcp::Sleeping;
236void QHyodaTcp::tcpMatrixIceTHeader(){
238 sendAcknowledgePacket();
239 state=QHyodaTcp::MatrixIceTImage;
245void QHyodaTcp::tcpMatrixIceTImage(){
247 if (tcpServerConnection->bytesAvailable() < tcpImageBytes)
return;
250 if (matrixArray)
delete matrixArray;
251 matrixArray=
new QByteArray(tcpServerConnection->read(tcpImageBytes));
253 sendAcknowledgePacket();
255 double pov[6]={0.,0.,0.,0.,0.,0.};
256 if (job->topLeftTools){
257 if (job->topLeftTools->matrix!=NULL){
258 job->topLeftTools->matrix->ice->sxyz(&pov[0]);
262 sendPacket((
char*)&pov[0],8*(4+1+1));
264 recvAcknowledgePacket();
266 state=QHyodaTcp::Sleeping;
268 job->matrixButton->setEnabled(
true);
270 if (job->topLeftTools->matrix==NULL)
return;
272 if (matrix!=NULL)
delete matrix;
273 matrix=
new QImage((uchar*)matrixArray->data(), iceWdth, iceHght, QImage::Format_ARGB32);
274 job->topLeftTools->matrix->ice->setImage(matrix);
282void QHyodaTcp::tcpReadyRead(){
285 case (QHyodaTcp::Sleeping):{tcpSleeping();
break;}
286 case (QHyodaTcp::HandShake):{tcpHandShake();
break;}
287 case (QHyodaTcp::VariableName):{tcpVariableName();
break;}
288 case (QHyodaTcp::MeshIceTHeader):{tcpMeshIceTHeader();
break;}
289 case (QHyodaTcp::MeshIceTImage):{tcpMeshIceTImage();
break;}
290 case (QHyodaTcp::Papi):{tcpPapi();
break;}
291 case (QHyodaTcp::MatrixIceTHeader):{tcpMatrixIceTHeader();
break;}
292 case (QHyodaTcp::MatrixIceTImage):{tcpMatrixIceTImage();
break;}
293 default: qFatal(
"\33[37m[QHyodaTcp::tcpReadyRead] Unknown state!\33[m");
300void QHyodaTcp::displayError(QAbstractSocket::SocketError socketError){
301 if (socketError == QTcpSocket::RemoteHostClosedError)
return;
302 qDebug() <<
"\33[37m[QHyodaTcp::displayError] \33[7mSocket error"<<socketError<<
"\33[m";
309qint64 QHyodaTcp::sendPacket(
const char *data, qint64 maxSize){
311 if (tcpServerConnection->write(data,maxSize)!=maxSize)
312 qFatal(
"\33[37m[QHyodaTcp::sendPacket] has not sent maxSize bytes!\33[m");
321void QHyodaTcp::sendAcknowledgePacket(
void){
323 *(
unsigned int*)&pkt[0]=0x3e9ff203ul;
325 sendPacket((
char*)&pkt[0],4);
332void QHyodaTcp::recvAcknowledgePacket(
void){
334 if (tcpServerConnection->bytesAvailable() < 4)
return;
336 if (tcpServerConnection->read(pktHeader,4)!=4)
337 qFatal(
"\33[37m[QHyodaTcp::recvAcknowledgePacket] Could not fetch packet header!\33[m");
339 if (*(
unsigned int*)&pktHeader[0]==0x3e9ff203ul)
return;
Integer len(const char *s)
Retourne la longueur de la chaîne s.