| 1 | /*
|
|---|
| 2 | Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 3 |
|
|---|
| 4 | This library is free software; you can redistribute it and/or
|
|---|
| 5 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 6 | License as published by the Free Software Foundation; either
|
|---|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This library is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 12 | Lesser General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 15 | License along with this library; if not, write to the Free Software
|
|---|
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|---|
| 17 | MA 02110-1301 USA.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | #include "webserverprocess.h"
|
|---|
| 21 | #include "moc_webserverprocess.cpp"
|
|---|
| 22 |
|
|---|
| 23 | #include "sitestingconfig.h"
|
|---|
| 24 |
|
|---|
| 25 | #include "webserverrunner.h"
|
|---|
| 26 |
|
|---|
| 27 | #include <QTimer>
|
|---|
| 28 | #include <QTime>
|
|---|
| 29 | #include <QCoreApplication>
|
|---|
| 30 | #include <QTcpSocket>
|
|---|
| 31 | #include <QRegExp>
|
|---|
| 32 |
|
|---|
| 33 | WebserverProcess::WebserverProcess(QObject* parent)
|
|---|
| 34 | : QThread(parent)
|
|---|
| 35 | {
|
|---|
| 36 | _runner = NULL;
|
|---|
| 37 | _finishedStartup = false;
|
|---|
| 38 | connect(this, SIGNAL(started()), this, SLOT(setStarted()));
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | WebserverProcess::~WebserverProcess()
|
|---|
| 42 | {
|
|---|
| 43 | if(_runner)
|
|---|
| 44 | delete _runner;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | QString WebserverProcess::webserviceBaseUrl()
|
|---|
| 48 | {
|
|---|
| 49 | return appendSlashToUrl(QString(WEBSERVICE_BASE_URL));
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | QString WebserverProcess::webserviceApiVersion()
|
|---|
| 53 | {
|
|---|
| 54 | return QString(WEBSERVICE_API_VERSION);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | bool WebserverProcess::isRunning() const
|
|---|
| 59 | {
|
|---|
| 60 | QRegExp regex("http:\\/\\/([^:]+)+:([0-9]+)");
|
|---|
| 61 | if(regex.indexIn(webserviceBaseUrl()) < 0){
|
|---|
| 62 | qDebug() << QString("Regex %1 to find host and port not matching in %2").arg(regex.pattern()).arg(webserviceBaseUrl());
|
|---|
| 63 | return false;
|
|---|
| 64 | }
|
|---|
| 65 | QStringList capturedTexts = regex.capturedTexts();
|
|---|
| 66 | if(capturedTexts.size() != 3){
|
|---|
| 67 | qDebug() << QString("Regex %1 does not matches enough items in %2").arg(regex.pattern()).arg(webserviceBaseUrl());
|
|---|
| 68 | return false;
|
|---|
| 69 | }
|
|---|
| 70 | QTcpSocket *socket = new QTcpSocket();
|
|---|
| 71 | socket->connectToHost(capturedTexts[1], capturedTexts[2].toInt(), QIODevice::ReadOnly);
|
|---|
| 72 | if (!socket->waitForConnected(2000)){
|
|---|
| 73 | qDebug() << QString("Webservice at %1:%2 not reachable").arg(capturedTexts[1]).arg(capturedTexts[2]);
|
|---|
| 74 | delete socket;
|
|---|
| 75 | return false;
|
|---|
| 76 | }
|
|---|
| 77 | socket->close();
|
|---|
| 78 | delete socket;
|
|---|
| 79 | return true;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void WebserverProcess::stopThread()
|
|---|
| 83 | {
|
|---|
| 84 | // stopping thread
|
|---|
| 85 | // cancel query first
|
|---|
| 86 | // stop eventloop
|
|---|
| 87 | quit();
|
|---|
| 88 | // wait 2000 seconds to stop the eventloop
|
|---|
| 89 | wait(2000);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | void WebserverProcess::run()
|
|---|
| 93 | {
|
|---|
| 94 | _runner = new WebserverRunner();
|
|---|
| 95 | QTimer::singleShot(1, _runner, SLOT(start()));
|
|---|
| 96 | exec();
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | bool WebserverProcess::isStarted()
|
|---|
| 100 | {
|
|---|
| 101 | return _finishedStartup;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | bool WebserverProcess::waitForStart()
|
|---|
| 105 | {
|
|---|
| 106 | if(isStarted())
|
|---|
| 107 | return true;
|
|---|
| 108 |
|
|---|
| 109 | // wait for thread to be started
|
|---|
| 110 | QTime dieTime = QTime::currentTime().addSecs(2);
|
|---|
| 111 | // Userevents should be excluded see http://lists.trolltech.com/qt-interest/2007-01/thread00133-0.html
|
|---|
| 112 | while( QTime::currentTime() < dieTime ){
|
|---|
| 113 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 100);
|
|---|
| 114 | if(isStarted())
|
|---|
| 115 | return true;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | if(isStarted())
|
|---|
| 119 | return true;
|
|---|
| 120 | return false;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void WebserverProcess::setStarted()
|
|---|
| 124 | {
|
|---|
| 125 | // Give the php webserver 5 seconds to start
|
|---|
| 126 | QTime dieTime = QTime::currentTime().addSecs(5);
|
|---|
| 127 | // Userevents should be excluded see http://lists.trolltech.com/qt-interest/2007-01/thread00133-0.html
|
|---|
| 128 | while( QTime::currentTime() < dieTime ){
|
|---|
| 129 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 100);
|
|---|
| 130 | }
|
|---|
| 131 | _finishedStartup = true;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | QString WebserverProcess::appendSlashToUrl(QString url)
|
|---|
| 135 | {
|
|---|
| 136 | if(url.right(1) != "/")
|
|---|
| 137 | url += "/";
|
|---|
| 138 | return url;
|
|---|
| 139 | }
|
|---|