/* Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "webserverprocess.h" #include "moc_webserverprocess.cpp" #include "sitestingconfig.h" #include "webserverrunner.h" #include #include #include #include #include WebserverProcess::WebserverProcess(QObject* parent) : QThread(parent) { _runner = NULL; _finishedStartup = false; connect(this, SIGNAL(started()), this, SLOT(setStarted())); } WebserverProcess::~WebserverProcess() { if(_runner) delete _runner; } QString WebserverProcess::webserviceBaseUrl() { return appendSlashToUrl(QString(WEBSERVICE_BASE_URL)); } QString WebserverProcess::webserviceApiVersion() { return QString(WEBSERVICE_API_VERSION); } bool WebserverProcess::isRunning() const { QRegExp regex("http:\\/\\/([^:]+)+:([0-9]+)"); if(regex.indexIn(webserviceBaseUrl()) < 0){ qDebug() << QString("Regex %1 to find host and port not matching in %2").arg(regex.pattern()).arg(webserviceBaseUrl()); return false; } QStringList capturedTexts = regex.capturedTexts(); if(capturedTexts.size() != 3){ qDebug() << QString("Regex %1 does not matches enough items in %2").arg(regex.pattern()).arg(webserviceBaseUrl()); return false; } QTcpSocket *socket = new QTcpSocket(); socket->connectToHost(capturedTexts[1], capturedTexts[2].toInt(), QIODevice::ReadOnly); if (!socket->waitForConnected(2000)){ qDebug() << QString("Webservice at %1:%2 not reachable").arg(capturedTexts[1]).arg(capturedTexts[2]); delete socket; return false; } socket->close(); delete socket; return true; } void WebserverProcess::stopThread() { // stopping thread // cancel query first // stop eventloop quit(); // wait 2000 seconds to stop the eventloop wait(2000); } void WebserverProcess::run() { _runner = new WebserverRunner(); QTimer::singleShot(1, _runner, SLOT(start())); exec(); } bool WebserverProcess::isStarted() { return _finishedStartup; } bool WebserverProcess::waitForStart() { if(isStarted()) return true; // wait for thread to be started QTime dieTime = QTime::currentTime().addSecs(2); // Userevents should be excluded see http://lists.trolltech.com/qt-interest/2007-01/thread00133-0.html while( QTime::currentTime() < dieTime ){ QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 100); if(isStarted()) return true; } if(isStarted()) return true; return false; } void WebserverProcess::setStarted() { // Give the php webserver 5 seconds to start QTime dieTime = QTime::currentTime().addSecs(5); // Userevents should be excluded see http://lists.trolltech.com/qt-interest/2007-01/thread00133-0.html while( QTime::currentTime() < dieTime ){ QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 100); } _finishedStartup = true; } QString WebserverProcess::appendSlashToUrl(QString url) { if(url.right(1) != "/") url += "/"; return url; }