#include "client.h" Client::Client( QString address, QWidget* parent ) { clientSocket = new QTcpSocket(); serverAddress = address; rawData = NULL; closing = false; connect( clientSocket, SIGNAL( readyRead() ), this, SLOT( readData() ) ); connect( this, SIGNAL( disableChannel( int ) ), parent, SLOT( disableChan( int ) ) ); connect( this, SIGNAL( sendDataDouble( float*, uint32_t*, int, int ) ), parent, SLOT( copyData( float*, uint32_t*, int, int ) ) ); connect( this, SIGNAL( writeMsg( QString, int ) ), parent, SLOT( writeToLog( QString, int ) ) ); connect( this, SIGNAL( closeError() ), parent, SLOT( closeQL() ) ); clientSocket->abort(); clientSocket->connectToHost( serverAddress, PORT ); } Client::~Client() { clientSocket->close(); delete clientSocket; clientSocket = NULL; } void Client::getData( int chan ) { if( !closing ) { w.start(); blockSize = 0; blockNum = 0; channel = chan; if( clientSocket->state() == QTcpSocket::ConnectedState ) { clientSocket->flush(); clientSocket->readAll(); if( clientSocket->write( "D", sizeof( char ) ) == -1 ) { emit sendDataDouble( NULL, NULL, 0, channel ); emit disableChannel( channel ); clientSocket->flush(); } } else { emit sendDataDouble( NULL, NULL, 0, channel ); emit disableChannel( channel ); clientSocket->flush(); } } } void Client::readData() { if( !closing ) { QDataStream in(clientSocket); in.setByteOrder( QDataStream::BigEndian ); if( blockSize == 0 ) { if( clientSocket->bytesAvailable() < ( int )sizeof( uint32_t ) ) { return; } in >> blockSize; blockSize = ntohl( blockSize ); blockNum = blockSize / sizeof( uint32_t ); //std::cout << "Block Size: " << blockSize << " " << channel+1 << std::endl; QString text( "Block Size: " ); QString num; num.setNum( blockSize ); text.append( num ); num.clear(); num.setNum( channel+1 ); text.append( " Channel: " + num ); if( blockSize < HEADERLENGTH*sizeof( uint32_t ) ) { // Not enough header information, which means the DAQ slices and/or radar was not setup to run properly. // Close Quick-Look and send message back about the error. emit writeMsg( "ERROR: Not enough data for header information. Closing Quick-Look.", CLIENT ); emit writeMsg( "ERROR: Not enough data for header information. Closing Quick-Look.", NORMAL ); closing = true; emit closeError(); return; } //std:: cout << "Time to read in size expected: " << w.elapsed() << std::endl; } if ( clientSocket->bytesAvailable() < blockSize ) { return; } rawData = new float[ blockNum-HEADERLENGTH ]; headerData = new uint32_t[HEADERLENGTH]; unsigned int counter = 0; uint32_t test; while( ( clientSocket->bytesAvailable() > 0 ) && ( counter < HEADERLENGTH ) ) { in >> test; headerData[counter] = test; counter++; } counter = 0; while( ( clientSocket->bytesAvailable() > 0 ) && ( counter < blockNum-HEADERLENGTH ) ) { in >> test; rawData[counter] = test; counter++; } // Send the data back to be processed. emit sendDataDouble( rawData, headerData, blockNum-HEADERLENGTH, channel ); rawData = NULL; } } void Client::connServer() { std::cout << "We are connected to the server." << std::endl; }