File indexing completed on 2024-05-19 05:41:34
0001 #include "readersender.h" 0002 #include <QByteArray> 0003 #include <QCoreApplication> 0004 #include <QDebug> 0005 #include <QTcpSocket> 0006 #include <chrono> 0007 #include <thread> 0008 0009 ReaderSender::ReaderSender(QObject* parent) : QObject(parent) {} 0010 void ReaderSender::readFile() 0011 { 0012 QString path= QStringLiteral("/home/renaud/www/%1").arg("DataServer_2018-08-05T15:59:37.log"); 0013 m_file.setFileName(path); 0014 if(m_file.open(QIODevice::ReadOnly)) 0015 { 0016 m_reader.setDevice(&m_file); 0017 } 0018 0019 while(!m_reader.atEnd()) 0020 { 0021 m_data << m_reader.readLine(); 0022 } 0023 0024 sendData(); 0025 } 0026 void ReaderSender::sendData() 0027 { 0028 for(auto line : m_data) 0029 { 0030 auto array= line.split("_", QString::SkipEmptyParts); 0031 auto date= array[0]; 0032 auto id= array[1].toInt(); 0033 auto data= array[2]; 0034 0035 if(!m_hash.contains(id)) 0036 { 0037 QTcpSocket* tcp= new QTcpSocket(); 0038 0039 tcp->connectToHost(QStringLiteral("127.0.0.1"), 6660); 0040 if(tcp->waitForConnected()) 0041 { 0042 qDebug() << "Connected"; 0043 m_hash.insert(id, tcp); 0044 } 0045 else 0046 { 0047 qDebug() << "No server"; 0048 emit finish(); 0049 return; 0050 } 0051 } 0052 auto tcp= m_hash[id]; 0053 if(nullptr == tcp) 0054 return; 0055 0056 QByteArray bytearray; 0057 bytearray= QByteArray::fromBase64(data.toLatin1()); 0058 0059 tcp->write(bytearray); 0060 std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 0061 } 0062 0063 emit finish(); 0064 // qApp->quit(); 0065 }