/* * New client connection */void SenseSystem::TechServer::newclient(){ // If server started qDebug() << QString::fromUtf8("New connection!!!"); qintptr IDSocket = tcpServer->nextPendingConnection()->socketDescriptor(); QThread *thread = new QThread(); Client *client = new Client(IDSocket); client->moveToThread(thread); // Соединяем сигнал started потока, со слотом process "рабочего" класса, т.е. начинается выполнение нужной работы. connect(thread,SIGNAL(started()),client,SLOT(process())); connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater())); // По завершению выходим из потока, и удаляем рабочий класс connect(client,SIGNAL(finished()),thread,SLOT(quit())); connect(client,SIGNAL(finished()),client,SLOT(deleteLater())); // Удаляем поток, после выполнения операции if(config.typeView == CONSOLE) { connect(client,SIGNAL(println(QString)),(Console *)config.display,SLOT(outStream(QString))); connect(client,SIGNAL(addClientToList(int)),(Console *)config.display,SLOT(addClientToList(int))); } else if(config.typeView == WINDOW) { connect(client,SIGNAL(println(QString)),((MainWindow *)config.display),SLOT(outStream(QString))); connect(client,SIGNAL(addClientToList(int)),(MainWindow *)config.display,SLOT(addClientToList(int))); } connect(client,SIGNAL(removeClientFromList(int)),this,SLOT(removeMap(int))); SenseSystem::TechServer::client.insert(IDSocket,client); thread->start();}
/* * Run the thread */void SenseSystem::Client::process(void){ clientSocket = new QTcpSocket(); // Trying to connect to socket descriptor if (!clientSocket->setSocketDescriptor(socketDescriptor)) { // Can't connect to socket descriptor finished(); } else { sql = new MainSQL("localhost","postirayka", "root","root"); // Initiolization settings for system timer = new QTimer(this); // setup signal and slot connect(timer,SIGNAL(timeout()),this,SLOT(readCommand())); // Reading command from MySQL timer->start(2000); // Initiolization settings for system autoriz = new QTimer(this); // setup signal and slot connect(autoriz,SIGNAL(timeout()),this,SLOT(finishThread())); // Reading command from MySQL autoriz->start(20000); // Initiolization settings for system timAlive = new QTimer(this); // setup signal and slot connect(timAlive,SIGNAL(timeout()),this,SLOT(finishThread())); // Reading command from MySQL timAlive->start(600000); timModem = new QTimer(this); // setup signal and slot connect(timModem,SIGNAL(timeout()),this,SLOT(readModem())); // Reading command from MySQL timModem->start(50000); timBufClear = new QTimer(this); // setup signal and slot connect(timBufClear,SIGNAL(timeout()),this,SLOT(bufferClear())); // Starting the thread of socket to execute events connect(clientSocket,SIGNAL(readyRead()),this, SLOT(socketReadClient())); connect(clientSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this, SLOT(clientChangeState())); connect(clientSocket,SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(socketErrorClient())); connect(clientSocket,SIGNAL(disconnected()),this, SLOT(socketRemoveClient())); // String to window QString strSocket; strSocket += QString("[") + QString::number(socketDescriptor) + "] New connection!!!" + "\r\n\r\n"; // Set text to the text browser println(strSocket); }}
/* * Constructor console task */Console::Console(QStringList agruments,QObject *parent) : QObject(parent){ Console::agruments = agruments; // Function of fetch of user interface server = new SenseSystem::TechServer(config.port); connect(this,SIGNAL(println(QString)),this,SLOT(outStream(QString))); // Checking if server is started ? if(server != NULL) { // Set text to the text browser println("Server is working!\r\n"); println("UTC: "+QDateTime::currentDateTimeUtc().toString()+"\r\n"); } // If server isn't working else { // Set text to the text browser println("Server isn\'t working!!!\r\n"); println("Please check the internet connection\r\n"); }}
// Checking if we need console application if(config.typeView == SenseSystem::CONSOLE) { QCoreApplication app(argc, argv); config.display = new Console(app.arguments()); // Return end of application return app.exec(); } // Checking if we need window application else { QApplication app(argc, argv); config.display = new MainWindow(); ((MainWindow *) config.display)->show(); // Return end of application return app.exec(); }
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newclient()));
/* * Recive of the request to the server */void SenseSystem::Client::socketReadClient(void){ if(clientSocket->bytesAvailable() == 0) return; qDebug() << "Recieved some data";.......................................................................}